bpf tools: Collect version and license from ELF sections

Expand bpf_obj_elf_collect() to collect license and kernel version
information in eBPF object file. eBPF object file should have a section
named 'license', which contains a string. It should also have a section
named 'version', contains a u32 LINUX_VERSION_CODE.

bpf_obj_validate() is introduced to validate object file after loaded.
Currently it only check existence of 'version' section.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1435716878-189507-10-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Wang Nan 2015-07-01 02:13:57 +00:00 committed by Arnaldo Carvalho de Melo
parent 296036653a
commit cb1e5e9619
1 changed files with 53 additions and 0 deletions

View File

@ -14,6 +14,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <asm/unistd.h> #include <asm/unistd.h>
#include <linux/kernel.h>
#include <linux/bpf.h> #include <linux/bpf.h>
#include <libelf.h> #include <libelf.h>
#include <gelf.h> #include <gelf.h>
@ -78,6 +79,8 @@ void libbpf_set_print(libbpf_print_fn_t warn,
#endif #endif
struct bpf_object { struct bpf_object {
char license[64];
u32 kern_version;
/* /*
* Information when doing elf related work. Only valid if fd * Information when doing elf related work. Only valid if fd
* is valid. * is valid.
@ -220,6 +223,33 @@ bpf_object__check_endianness(struct bpf_object *obj)
return -EINVAL; return -EINVAL;
} }
static int
bpf_object__init_license(struct bpf_object *obj,
void *data, size_t size)
{
memcpy(obj->license, data,
min(size, sizeof(obj->license) - 1));
pr_debug("license of %s is %s\n", obj->path, obj->license);
return 0;
}
static int
bpf_object__init_kversion(struct bpf_object *obj,
void *data, size_t size)
{
u32 kver;
if (size != sizeof(kver)) {
pr_warning("invalid kver section in %s\n", obj->path);
return -EINVAL;
}
memcpy(&kver, data, sizeof(kver));
obj->kern_version = kver;
pr_debug("kernel version of %s is %x\n", obj->path,
obj->kern_version);
return 0;
}
static int bpf_object__elf_collect(struct bpf_object *obj) static int bpf_object__elf_collect(struct bpf_object *obj)
{ {
Elf *elf = obj->efile.elf; Elf *elf = obj->efile.elf;
@ -266,11 +296,32 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
name, (unsigned long)data->d_size, name, (unsigned long)data->d_size,
(int)sh.sh_link, (unsigned long)sh.sh_flags, (int)sh.sh_link, (unsigned long)sh.sh_flags,
(int)sh.sh_type); (int)sh.sh_type);
if (strcmp(name, "license") == 0)
err = bpf_object__init_license(obj,
data->d_buf,
data->d_size);
else if (strcmp(name, "version") == 0)
err = bpf_object__init_kversion(obj,
data->d_buf,
data->d_size);
if (err)
goto out;
} }
out: out:
return err; return err;
} }
static int bpf_object__validate(struct bpf_object *obj)
{
if (obj->kern_version == 0) {
pr_warning("%s doesn't provide kernel version\n",
obj->path);
return -EINVAL;
}
return 0;
}
static struct bpf_object * static struct bpf_object *
__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz) __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
{ {
@ -291,6 +342,8 @@ __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
goto out; goto out;
if (bpf_object__elf_collect(obj)) if (bpf_object__elf_collect(obj))
goto out; goto out;
if (bpf_object__validate(obj))
goto out;
bpf_object__elf_finish(obj); bpf_object__elf_finish(obj);
return obj; return obj;