aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib
diff options
context:
space:
mode:
authorJesper Dangaard Brouer <brouer@redhat.com>2018-02-08 06:48:32 -0500
committerDaniel Borkmann <daniel@iogearbox.net>2018-02-08 18:25:12 -0500
commite3d91b0ca523d53158f435a3e13df7f0cb360ea2 (patch)
tree0e3c09034cb8bc79fea586681d4de51689a23325 /tools/lib
parentf09b2e382e9a7053e3ae6f2fb6535efd5760cf5d (diff)
tools/libbpf: handle issues with bpf ELF objects containing .eh_frames
V3: More generic skipping of relo-section (suggested by Daniel) If clang >= 4.0.1 is missing the option '-target bpf', it will cause llc/llvm to create two ELF sections for "Exception Frames", with section names '.eh_frame' and '.rel.eh_frame'. The BPF ELF loader library libbpf fails when loading files with these sections. The other in-kernel BPF ELF loader in samples/bpf/bpf_load.c, handle this gracefully. And iproute2 loader also seems to work with these "eh" sections. The issue in libbpf is caused by bpf_object__elf_collect() skipping some sections, and later when performing relocation it will be pointing to a skipped section, as these sections cannot be found by bpf_object__find_prog_by_idx() in bpf_object__collect_reloc(). This is a general issue that also occurs for other sections, like debug sections which are also skipped and can have relo section. As suggested by Daniel. To avoid keeping state about all skipped sections, instead perform a direct qlookup in the ELF object. Lookup the section that the relo-section points to and check if it contains executable machine instructions (denoted by the sh_flags SHF_EXECINSTR). Use this check to also skip irrelevant relo-sections. Note, for samples/bpf/ the '-target bpf' parameter to clang cannot be used due to incompatibility with asm embedded headers, that some of the samples include. This is explained in more details by Yonghong Song in bpf_devel_QA. Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/lib')
-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 0c794f7fc050..97073d649c1a 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -742,6 +742,24 @@ bpf_object__init_maps(struct bpf_object *obj)
742 return 0; 742 return 0;
743} 743}
744 744
745static bool section_have_execinstr(struct bpf_object *obj, int idx)
746{
747 Elf_Scn *scn;
748 GElf_Shdr sh;
749
750 scn = elf_getscn(obj->efile.elf, idx);
751 if (!scn)
752 return false;
753
754 if (gelf_getshdr(scn, &sh) != &sh)
755 return false;
756
757 if (sh.sh_flags & SHF_EXECINSTR)
758 return true;
759
760 return false;
761}
762
745static int bpf_object__elf_collect(struct bpf_object *obj) 763static int bpf_object__elf_collect(struct bpf_object *obj)
746{ 764{
747 Elf *elf = obj->efile.elf; 765 Elf *elf = obj->efile.elf;
@@ -825,6 +843,14 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
825 } else if (sh.sh_type == SHT_REL) { 843 } else if (sh.sh_type == SHT_REL) {
826 void *reloc = obj->efile.reloc; 844 void *reloc = obj->efile.reloc;
827 int nr_reloc = obj->efile.nr_reloc + 1; 845 int nr_reloc = obj->efile.nr_reloc + 1;
846 int sec = sh.sh_info; /* points to other section */
847
848 /* Only do relo for section with exec instructions */
849 if (!section_have_execinstr(obj, sec)) {
850 pr_debug("skip relo %s(%d) for section(%d)\n",
851 name, idx, sec);
852 continue;
853 }
828 854
829 reloc = realloc(reloc, 855 reloc = realloc(reloc,
830 sizeof(*obj->efile.reloc) * nr_reloc); 856 sizeof(*obj->efile.reloc) * nr_reloc);