Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753639AbbEQLEK (ORCPT ); Sun, 17 May 2015 07:04:10 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:61095 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753231AbbEQK5q (ORCPT ); Sun, 17 May 2015 06:57:46 -0400 From: Wang Nan To: , , , , , , , , , , CC: , , Subject: [RFC PATCH v3 11/37] bpf tools: Iterate over ELF sections to collect information Date: Sun, 17 May 2015 10:56:36 +0000 Message-ID: <1431860222-61636-12-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1431860222-61636-1-git-send-email-wangnan0@huawei.com> References: <1431860222-61636-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.200] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2389 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/lib/bpf/libbpf.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index a4910a8..1af80c3 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -206,6 +206,58 @@ bpf_obj_swap_init(struct bpf_object *obj) } } +static int bpf_obj_elf_collect(struct bpf_object *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_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; +} + struct bpf_object *bpf_open_object(const char *path) { struct bpf_object *obj; @@ -229,6 +281,8 @@ struct bpf_object *bpf_open_object(const char *path) goto out; if (bpf_obj_swap_init(obj)) goto out; + if (bpf_obj_elf_collect(obj)) + goto out; return obj; -- 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/