Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1422821AbbEOHxz (ORCPT ); Fri, 15 May 2015 03:53:55 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:62680 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934161AbbEOHxt (ORCPT ); Fri, 15 May 2015 03:53:49 -0400 From: Wang Nan To: , , , , , , , , , , , CC: , , , Subject: [RFC PATCH v2 12/37] tools lib bpf: collect map definitions. Date: Fri, 15 May 2015 07:51:05 +0000 Message-ID: <1431676290-1230-13-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1431676290-1230-1-git-send-email-wangnan0@huawei.com> References: <1431676290-1230-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: 2136 Lines: 77 If maps are used by eBPF programs, corresponding object file(s) should contain a section named 'map'. Which contains map definitions. This patch copies the data of the whole section. Map data parsing should be acted just before map loading. Signed-off-by: Wang Nan --- tools/lib/bpf/libbpf.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index b26f1ee..6ee5f3c 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -82,6 +82,8 @@ struct bpf_object { bool needs_swap; char license[64]; u32 kern_version; + void *maps_buf; + size_t maps_buf_sz; /* * Information when doing elf related work. Only valid if fd @@ -244,6 +246,27 @@ static int bpf_obj_kver_init(struct bpf_object *obj, return 0; } +static int bpf_obj_maps_init(struct bpf_object *obj, void *data, + size_t size) +{ + if (size == 0) { + pr_debug("%s doesn't need map definition\n", + obj->path); + return 0; + } + + obj->maps_buf = malloc(size); + if (!obj->maps_buf) { + pr_warning("malloc maps failed: %s\n", obj->path); + return -ENOMEM; + } + + obj->maps_buf_sz = size; + memcpy(obj->maps_buf, data, size); + pr_debug("maps in %s: %ld bytes\n", obj->path, (long)size); + return 0; +} + static int bpf_obj_elf_collect(struct bpf_object *obj) { Elf *elf = obj->elf.elf; @@ -298,6 +321,9 @@ static int bpf_obj_elf_collect(struct bpf_object *obj) else if (strcmp(name, "version") == 0) err = bpf_obj_kver_init(obj, data->d_buf, data->d_size); + else if (strcmp(name, "maps") == 0) + err = bpf_obj_maps_init(obj, data->d_buf, + data->d_size); if (err) goto out; } @@ -359,5 +385,7 @@ void bpf_close_object(struct bpf_object *obj) if (obj->path) free(obj->path); + if (obj->maps_buf) + free(obj->maps_buf); free(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/