Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754209AbbHHIMI (ORCPT ); Sat, 8 Aug 2015 04:12:08 -0400 Received: from terminus.zytor.com ([198.137.202.10]:53689 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752066AbbHHIMC (ORCPT ); Sat, 8 Aug 2015 04:12:02 -0400 Date: Sat, 8 Aug 2015 01:11:14 -0700 From: tip-bot for Wang Nan Message-ID: Cc: wangnan0@huawei.com, brendan.d.gregg@gmail.com, hpa@zytor.com, jolsa@kernel.org, namhyung@kernel.org, lizefan@huawei.com, mingo@kernel.org, linux-kernel@vger.kernel.org, xiakaixu@huawei.com, hekuang@huawei.com, masami.hiramatsu.pt@hitachi.com, a.p.zijlstra@chello.nl, dsahern@gmail.com, ast@plumgrid.com, acme@redhat.com, tglx@linutronix.de, daniel@iogearbox.net Reply-To: mingo@kernel.org, lizefan@huawei.com, namhyung@kernel.org, jolsa@kernel.org, hpa@zytor.com, brendan.d.gregg@gmail.com, wangnan0@huawei.com, daniel@iogearbox.net, tglx@linutronix.de, acme@redhat.com, a.p.zijlstra@chello.nl, dsahern@gmail.com, ast@plumgrid.com, masami.hiramatsu.pt@hitachi.com, hekuang@huawei.com, xiakaixu@huawei.com, linux-kernel@vger.kernel.org In-Reply-To: <1435716878-189507-9-git-send-email-wangnan0@huawei.com> References: <1435716878-189507-9-git-send-email-wangnan0@huawei.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] bpf tools: Iterate over ELF sections to collect information Git-Commit-ID: 296036653ae8b1367ec9d06d65377c2e2371b153 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: 3421 Lines: 109 Commit-ID: 296036653ae8b1367ec9d06d65377c2e2371b153 Gitweb: http://git.kernel.org/tip/296036653ae8b1367ec9d06d65377c2e2371b153 Author: Wang Nan AuthorDate: Wed, 1 Jul 2015 02:13:56 +0000 Committer: Arnaldo Carvalho de Melo CommitDate: Fri, 7 Aug 2015 10:16:56 -0300 bpf tools: Iterate over ELF sections to collect information bpf_obj_elf_collect() is introduced to iterate over each elf sections to collection information in eBPF object files. This function will futher enhanced to collect license, kernel version, programs, configs and map information. 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-9-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 15b3e82..d8d6eb5 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -220,6 +220,57 @@ mismatch: return -EINVAL; } +static int bpf_object__elf_collect(struct bpf_object *obj) +{ + Elf *elf = obj->efile.elf; + GElf_Ehdr *ep = &obj->efile.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_warning("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_warning("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_warning("failed to get section name from %s\n", + obj->path); + err = -EINVAL; + goto out; + } + + data = elf_getdata(scn, 0); + if (!data) { + pr_warning("failed to get section data from %s(%s)\n", + name, obj->path); + err = -EINVAL; + goto out; + } + pr_debug("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; +} + static struct bpf_object * __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz) { @@ -238,6 +289,8 @@ __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz) goto out; if (bpf_object__check_endianness(obj)) goto out; + if (bpf_object__elf_collect(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/