Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750891AbbD3Kxo (ORCPT ); Thu, 30 Apr 2015 06:53:44 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:14393 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751316AbbD3Kxd (ORCPT ); Thu, 30 Apr 2015 06:53:33 -0400 From: Wang Nan To: , , , , , , CC: , , , Subject: [RFC PATCH 08/22] perf bpf: collect version and license from ELF. Date: Thu, 30 Apr 2015 10:52:31 +0000 Message-ID: <1430391165-30267-9-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1430391165-30267-1-git-send-email-wangnan0@huawei.com> References: <1430391165-30267-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.210] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020206.554209AB.0109,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: a1ffbd6fe849370e743492419f91b02b Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3140 Lines: 112 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 --- 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 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/