Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754272AbbHHIM7 (ORCPT ); Sat, 8 Aug 2015 04:12:59 -0400 Received: from terminus.zytor.com ([198.137.202.10]:53721 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754251AbbHHIMx (ORCPT ); Sat, 8 Aug 2015 04:12:53 -0400 Date: Sat, 8 Aug 2015 01:12:15 -0700 From: tip-bot for Wang Nan Message-ID: Cc: namhyung@kernel.org, lizefan@huawei.com, linux-kernel@vger.kernel.org, xiakaixu@huawei.com, hekuang@huawei.com, masami.hiramatsu.pt@hitachi.com, a.p.zijlstra@chello.nl, tglx@linutronix.de, daniel@iogearbox.net, wangnan0@huawei.com, acme@redhat.com, dsahern@gmail.com, jolsa@kernel.org, hpa@zytor.com, mingo@kernel.org, brendan.d.gregg@gmail.com, ast@plumgrid.com Reply-To: a.p.zijlstra@chello.nl, daniel@iogearbox.net, tglx@linutronix.de, linux-kernel@vger.kernel.org, xiakaixu@huawei.com, hekuang@huawei.com, masami.hiramatsu.pt@hitachi.com, namhyung@kernel.org, lizefan@huawei.com, hpa@zytor.com, mingo@kernel.org, brendan.d.gregg@gmail.com, ast@plumgrid.com, jolsa@kernel.org, acme@redhat.com, dsahern@gmail.com, wangnan0@huawei.com In-Reply-To: <1435716878-189507-12-git-send-email-wangnan0@huawei.com> References: <1435716878-189507-12-git-send-email-wangnan0@huawei.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] bpf tools: Collect symbol table from SHT_SYMTAB section Git-Commit-ID: bec7d68cb561e94f8a44c2b73c468b534c05f20d 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: 4099 Lines: 118 Commit-ID: bec7d68cb561e94f8a44c2b73c468b534c05f20d Gitweb: http://git.kernel.org/tip/bec7d68cb561e94f8a44c2b73c468b534c05f20d Author: Wang Nan AuthorDate: Wed, 1 Jul 2015 02:13:59 +0000 Committer: Arnaldo Carvalho de Melo CommitDate: Fri, 7 Aug 2015 10:16:57 -0300 bpf tools: Collect symbol table from SHT_SYMTAB section This patch collects symbols section. This section is useful when linking BPF maps. What 'bpf_map_xxx()' functions actually require are map's file descriptors (and the internal verifier converts fds into pointers to 'struct bpf_map'), which we don't know when compiling. Therefore, we should make compiler generate a 'ldr_64 r1, ' instruction, and fill the 'imm' field with the actual file descriptor when loading in libbpf. BPF programs should be written in this way: struct bpf_map_def SEC("maps") my_map = { .type = BPF_MAP_TYPE_HASH, .key_size = sizeof(unsigned long), .value_size = sizeof(unsigned long), .max_entries = 1000000, }; SEC("my_func=sys_write") int my_func(void *ctx) { ... bpf_map_update_elem(&my_map, &key, &value, BPF_ANY); ... } Compiler should convert '&my_map' into a 'ldr_64, r1, ' instruction, where imm should be the address of 'my_map'. According to the address, libbpf knows which map it actually referenced, and then fills the imm field with the 'fd' of that map created by it. However, since we never really 'link' the object file, the imm field is only a record in relocation section. Therefore libbpf should do the relocation: 1. In relocation section (type == SHT_REL), positions of each such 'ldr_64' instruction are recorded with a reference of an entry in symbol table (SHT_SYMTAB); 2. From records in symbol table we can find the indics of map variables. Libbpf first record SHT_SYMTAB and positions of each instruction which required bu such operation. Then create file descriptor. Finally, after map creation complete, replace the imm field. This is the first patch of BPF map related stuff. It records SHT_SYMTAB into object's efile field for further use. 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-12-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/lib/bpf/libbpf.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 87f5054..9b016c0 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -94,6 +94,7 @@ struct bpf_object { size_t obj_buf_sz; Elf *elf; GElf_Ehdr ehdr; + Elf_Data *symbols; } efile; char path[]; }; @@ -135,6 +136,7 @@ static void bpf_object__elf_finish(struct bpf_object *obj) elf_end(obj->efile.elf); obj->efile.elf = NULL; } + obj->efile.symbols = NULL; zclose(obj->efile.fd); obj->efile.obj_buf = NULL; obj->efile.obj_buf_sz = 0; @@ -333,6 +335,14 @@ static int bpf_object__elf_collect(struct bpf_object *obj) else if (strcmp(name, "maps") == 0) err = bpf_object__init_maps(obj, data->d_buf, data->d_size); + else if (sh.sh_type == SHT_SYMTAB) { + if (obj->efile.symbols) { + pr_warning("bpf: multiple SYMTAB in %s\n", + obj->path); + err = -EEXIST; + } else + obj->efile.symbols = data; + } if (err) goto out; } -- 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/