aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf
diff options
context:
space:
mode:
authorWang Nan <wangnan0@huawei.com>2015-06-30 22:14:01 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2015-08-07 09:16:57 -0400
commitb62f06e81bcf28d47fe736fe2beae40f15f496be (patch)
tree64d2cc088ee4c248d42b571cd37be9d622e100f2 /tools/lib/bpf
parenta5b8bd47dcc574cd3a71357b3a0f586969e4b887 (diff)
bpf tools: Collect relocation sections from SHT_REL sections
This patch collects relocation sections into 'struct object'. Such sections are used for connecting maps to bpf programs. 'reloc' field in 'struct bpf_object' is introduced for storing such information. This patch simply store the data into 'reloc' field. Following patch will parse them to know the exact instructions which are needed to be relocated. Note that the collected data will be invalid after ELF object file is closed. This is the second patch related to map relocation. The first one is 'bpf tools: Collect symbol table from SHT_SYMTAB section'. The principle of map relocation is described in its commit message. Signed-off-by: Wang Nan <wangnan0@huawei.com> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Cc: Brendan Gregg <brendan.d.gregg@gmail.com> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: David Ahern <dsahern@gmail.com> Cc: He Kuang <hekuang@huawei.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Kaixu Xia <xiakaixu@huawei.com> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Zefan Li <lizefan@huawei.com> Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1435716878-189507-14-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/lib/bpf')
-rw-r--r--tools/lib/bpf/libbpf.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 102156f322b6..e8088f8214d1 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -110,6 +110,11 @@ struct bpf_object {
110 Elf *elf; 110 Elf *elf;
111 GElf_Ehdr ehdr; 111 GElf_Ehdr ehdr;
112 Elf_Data *symbols; 112 Elf_Data *symbols;
113 struct {
114 GElf_Shdr shdr;
115 Elf_Data *data;
116 } *reloc;
117 int nr_reloc;
113 } efile; 118 } efile;
114 char path[]; 119 char path[];
115}; 120};
@@ -231,6 +236,9 @@ static void bpf_object__elf_finish(struct bpf_object *obj)
231 obj->efile.elf = NULL; 236 obj->efile.elf = NULL;
232 } 237 }
233 obj->efile.symbols = NULL; 238 obj->efile.symbols = NULL;
239
240 zfree(&obj->efile.reloc);
241 obj->efile.nr_reloc = 0;
234 zclose(obj->efile.fd); 242 zclose(obj->efile.fd);
235 obj->efile.obj_buf = NULL; 243 obj->efile.obj_buf = NULL;
236 obj->efile.obj_buf_sz = 0; 244 obj->efile.obj_buf_sz = 0;
@@ -447,6 +455,24 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
447 pr_warning("failed to alloc program %s (%s): %s", 455 pr_warning("failed to alloc program %s (%s): %s",
448 name, obj->path, errmsg); 456 name, obj->path, errmsg);
449 } 457 }
458 } else if (sh.sh_type == SHT_REL) {
459 void *reloc = obj->efile.reloc;
460 int nr_reloc = obj->efile.nr_reloc + 1;
461
462 reloc = realloc(reloc,
463 sizeof(*obj->efile.reloc) * nr_reloc);
464 if (!reloc) {
465 pr_warning("realloc failed\n");
466 err = -ENOMEM;
467 } else {
468 int n = nr_reloc - 1;
469
470 obj->efile.reloc = reloc;
471 obj->efile.nr_reloc = nr_reloc;
472
473 obj->efile.reloc[n].shdr = sh;
474 obj->efile.reloc[n].data = data;
475 }
450 } 476 }
451 if (err) 477 if (err)
452 goto out; 478 goto out;