diff options
author | Wang Nan <wangnan0@huawei.com> | 2015-06-30 22:13:56 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2015-08-07 09:16:56 -0400 |
commit | 296036653ae8b1367ec9d06d65377c2e2371b153 (patch) | |
tree | a6d53a2161dcd53d6c47df530732ea062c7d6ad7 /tools/lib/bpf | |
parent | cc4228d57c4c35ae0a29efa1e7687a817dc038d9 (diff) |
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 <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-9-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.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 15b3e82b22f6..d8d6eb580188 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c | |||
@@ -220,6 +220,57 @@ mismatch: | |||
220 | return -EINVAL; | 220 | return -EINVAL; |
221 | } | 221 | } |
222 | 222 | ||
223 | static int bpf_object__elf_collect(struct bpf_object *obj) | ||
224 | { | ||
225 | Elf *elf = obj->efile.elf; | ||
226 | GElf_Ehdr *ep = &obj->efile.ehdr; | ||
227 | Elf_Scn *scn = NULL; | ||
228 | int idx = 0, err = 0; | ||
229 | |||
230 | /* Elf is corrupted/truncated, avoid calling elf_strptr. */ | ||
231 | if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) { | ||
232 | pr_warning("failed to get e_shstrndx from %s\n", | ||
233 | obj->path); | ||
234 | return -EINVAL; | ||
235 | } | ||
236 | |||
237 | while ((scn = elf_nextscn(elf, scn)) != NULL) { | ||
238 | char *name; | ||
239 | GElf_Shdr sh; | ||
240 | Elf_Data *data; | ||
241 | |||
242 | idx++; | ||
243 | if (gelf_getshdr(scn, &sh) != &sh) { | ||
244 | pr_warning("failed to get section header from %s\n", | ||
245 | obj->path); | ||
246 | err = -EINVAL; | ||
247 | goto out; | ||
248 | } | ||
249 | |||
250 | name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name); | ||
251 | if (!name) { | ||
252 | pr_warning("failed to get section name from %s\n", | ||
253 | obj->path); | ||
254 | err = -EINVAL; | ||
255 | goto out; | ||
256 | } | ||
257 | |||
258 | data = elf_getdata(scn, 0); | ||
259 | if (!data) { | ||
260 | pr_warning("failed to get section data from %s(%s)\n", | ||
261 | name, obj->path); | ||
262 | err = -EINVAL; | ||
263 | goto out; | ||
264 | } | ||
265 | pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n", | ||
266 | name, (unsigned long)data->d_size, | ||
267 | (int)sh.sh_link, (unsigned long)sh.sh_flags, | ||
268 | (int)sh.sh_type); | ||
269 | } | ||
270 | out: | ||
271 | return err; | ||
272 | } | ||
273 | |||
223 | static struct bpf_object * | 274 | static struct bpf_object * |
224 | __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz) | 275 | __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz) |
225 | { | 276 | { |
@@ -238,6 +289,8 @@ __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz) | |||
238 | goto out; | 289 | goto out; |
239 | if (bpf_object__check_endianness(obj)) | 290 | if (bpf_object__check_endianness(obj)) |
240 | goto out; | 291 | goto out; |
292 | if (bpf_object__elf_collect(obj)) | ||
293 | goto out; | ||
241 | 294 | ||
242 | bpf_object__elf_finish(obj); | 295 | bpf_object__elf_finish(obj); |
243 | return obj; | 296 | return obj; |