Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751225AbbD3LB7 (ORCPT ); Thu, 30 Apr 2015 07:01:59 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:14400 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751202AbbD3Kxd (ORCPT ); Thu, 30 Apr 2015 06:53:33 -0400 From: Wang Nan To: , , , , , , CC: , , , Subject: [RFC PATCH 07/22] perf bpf: iterater over elf sections to collect information. Date: Thu, 30 Apr 2015 10:52:30 +0000 Message-ID: <1430391165-30267-8-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.0A020202.554209AB.019A,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: 724430a8e2fbaa73c628104b50950be3 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2406 Lines: 90 bpf_obj_elf_collect() is introduced to iterate over each elf sections to collection informations in eBPF object files. This function will futher enhanced to collect license, kernel version, programs, configs and map information. Signed-off-by: Wang Nan --- tools/perf/util/bpf-loader.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index 14d76f6..9c077dd 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -157,6 +157,58 @@ bpf_obj_swap_init(struct bpf_obj *obj) } } +static int bpf_obj_elf_collect(struct bpf_obj *obj) +{ + Elf *elf = obj->elf.elf; + GElf_Ehdr *ep = &obj->elf.ehdr; + Elf_Scn *scn = NULL; + int idx = 0, err = 0; + + /* Elf is corrupted/truncated, avoid calling elf_strptr. */ + if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) { + pr_err("bpf: failed to get e_shstrndx from %s\n", + obj->path); + return -EINVAL; + } + + while ((scn = elf_nextscn(elf, scn)) != NULL) { + char *name; + GElf_Shdr sh; + Elf_Data *data; + + idx++; + if (gelf_getshdr(scn, &sh) != &sh) { + pr_err("bpf: failed to get section header" + " from %s\n", obj->path); + err = -EINVAL; + goto out; + } + + name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name); + if (!name) { + pr_err("bpf: failed to get section name " + "from %s\n", obj->path); + err = -EINVAL; + goto out; + } + + data = elf_getdata(scn, 0); + if (!data) { + pr_err("bpf: failed to get section data " + "from %s(%s)\n", name, obj->path); + err = -EINVAL; + goto out; + } + pr_debug("bpf: section %s, size %ld, link %d, flags %lx, type=%d\n", + name,(unsigned long)data->d_size, + (int)sh.sh_link, + (unsigned long)sh.sh_flags, + (int)sh.sh_type); + } +out: + return err; +} + int bpf__load(const char *path) { struct bpf_obj *obj; @@ -179,6 +231,8 @@ int bpf__load(const char *path) goto out; if ((err = bpf_obj_swap_init(obj))) goto out; + if ((err = bpf_obj_elf_collect(obj))) + goto out; list_add(&obj->list, &bpf_obj_list); return 0; -- 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/