Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754232AbbHHIMR (ORCPT ); Sat, 8 Aug 2015 04:12:17 -0400 Received: from terminus.zytor.com ([198.137.202.10]:53699 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752066AbbHHIMM (ORCPT ); Sat, 8 Aug 2015 04:12:12 -0400 Date: Sat, 8 Aug 2015 01:11:34 -0700 From: tip-bot for Wang Nan Message-ID: Cc: wangnan0@huawei.com, hekuang@huawei.com, daniel@iogearbox.net, jolsa@kernel.org, namhyung@kernel.org, acme@redhat.com, lizefan@huawei.com, hpa@zytor.com, dsahern@gmail.com, tglx@linutronix.de, ast@plumgrid.com, masami.hiramatsu.pt@hitachi.com, mingo@kernel.org, a.p.zijlstra@chello.nl, brendan.d.gregg@gmail.com, xiakaixu@huawei.com, linux-kernel@vger.kernel.org Reply-To: a.p.zijlstra@chello.nl, brendan.d.gregg@gmail.com, mingo@kernel.org, xiakaixu@huawei.com, linux-kernel@vger.kernel.org, ast@plumgrid.com, acme@redhat.com, lizefan@huawei.com, dsahern@gmail.com, tglx@linutronix.de, hpa@zytor.com, masami.hiramatsu.pt@hitachi.com, namhyung@kernel.org, jolsa@kernel.org, wangnan0@huawei.com, daniel@iogearbox.net, hekuang@huawei.com In-Reply-To: <1435716878-189507-10-git-send-email-wangnan0@huawei.com> References: <1435716878-189507-10-git-send-email-wangnan0@huawei.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] bpf tools: Collect version and license from ELF sections Git-Commit-ID: cb1e5e961991ee9b2cbfd3bf06ef490ea578cd2f X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4174 Lines: 138 Commit-ID: cb1e5e961991ee9b2cbfd3bf06ef490ea578cd2f Gitweb: http://git.kernel.org/tip/cb1e5e961991ee9b2cbfd3bf06ef490ea578cd2f Author: Wang Nan AuthorDate: Wed, 1 Jul 2015 02:13:57 +0000 Committer: Arnaldo Carvalho de Melo CommitDate: Fri, 7 Aug 2015 10:16:57 -0300 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 Acked-by: Alexei Starovoitov Cc: Brendan Gregg Cc: Daniel Borkmann Cc: David Ahern Cc: He Kuang Cc: Jiri Olsa Cc: Kaixu Xia Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Zefan Li 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 --- tools/lib/bpf/libbpf.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index d8d6eb5..95c8d8e 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -78,6 +79,8 @@ void libbpf_set_print(libbpf_print_fn_t warn, #endif struct bpf_object { + char license[64]; + u32 kern_version; /* * Information when doing elf related work. Only valid if fd * is valid. @@ -220,6 +223,33 @@ mismatch: 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) { 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, (int)sh.sh_link, (unsigned long)sh.sh_flags, (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: 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 * __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; if (bpf_object__elf_collect(obj)) goto out; + if (bpf_object__validate(obj)) + goto out; bpf_object__elf_finish(obj); return obj; -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/