Expand bpf_obj_elf_collect() to let it collect license and kernel
version information in eBPF object files. 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 existance of 'version' section.

Signed-off-by: Wang Nan <wangn...@huawei.com>
---
 tools/perf/util/bpf-loader.c | 47 ++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/bpf-loader.h |  2 ++
 2 files changed, 49 insertions(+)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index 9c077dd..296fb06 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -157,6 +157,32 @@ bpf_obj_swap_init(struct bpf_obj *obj)
        }
 }
 
+static int bpf_obj_license_init(struct bpf_obj *obj,
+                               void *data, size_t size)
+{
+       memcpy(obj->license, data,
+              min(size, sizeof(obj->license) - 1));
+       pr_debug("bpf: license of %s is %s\n", obj->path, obj->license);
+       return 0;
+}
+
+static int bpf_obj_kver_init(struct bpf_obj *obj,
+                            void *data, size_t size)
+{
+       u32 kver;
+       if (size < sizeof(kver)) {
+               pr_err("bpf: invalid kver section in %s\n", obj->path);
+               return -EINVAL;
+       }
+       memcpy(&kver, data, sizeof(kver));
+       if (obj->needs_swap)
+               kver = bswap_32(kver);
+       obj->kern_version = kver;
+       pr_debug("bpf: kernel version of %s is %x\n", obj->path,
+                       obj->kern_version);
+       return 0;
+}
+
 static int bpf_obj_elf_collect(struct bpf_obj *obj)
 {
        Elf *elf = obj->elf.elf;
@@ -204,11 +230,30 @@ static int bpf_obj_elf_collect(struct bpf_obj *obj)
                                (int)sh.sh_link,
                                (unsigned long)sh.sh_flags,
                                (int)sh.sh_type);
+
+               if (strcmp(name, "license") == 0)
+                       err = bpf_obj_license_init(obj, data->d_buf,
+                                                  data->d_size);
+               else if (strcmp(name, "version") == 0)
+                       err = bpf_obj_kver_init(obj, data->d_buf,
+                                               data->d_size);
+               if (err)
+                       goto out;
        }
 out:
        return err;
 }
 
+static int bpf_obj_validate(struct bpf_obj *obj)
+{
+       if (obj->kern_version == 0) {
+               pr_err("bpf: %s doesn't provide kernel version\n",
+                       obj->path);
+               return -EINVAL;
+       }
+       return 0;
+}
+
 int bpf__load(const char *path)
 {
        struct bpf_obj *obj;
@@ -233,6 +278,8 @@ int bpf__load(const char *path)
                goto out;
        if ((err = bpf_obj_elf_collect(obj)))
                goto out;
+       if ((err = bpf_obj_validate(obj)))
+               goto out;
 
        list_add(&obj->list, &bpf_obj_list);
        return 0;
diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h
index c27b0ac..e1d5c42 100644
--- a/tools/perf/util/bpf-loader.h
+++ b/tools/perf/util/bpf-loader.h
@@ -23,6 +23,8 @@ struct bpf_obj {
        struct list_head list;
        char *path;
        bool needs_swap;
+       char license[64];
+       u32 kern_version;
 
        /*
         * Information when doing elf related work. Only valid if fd
-- 
1.8.3.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to