Skip to content

Commit d6b0049

Browse files
netoptimizergregkh
authored andcommitted
tools/libbpf: handle issues with bpf ELF objects containing .eh_frames
[ Upstream commit e3d91b0 ] 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 <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 327aac8 commit d6b0049

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,24 @@ bpf_object__init_maps(struct bpf_object *obj)
661661
return bpf_object__validate_maps(obj);
662662
}
663663

664+
static bool section_have_execinstr(struct bpf_object *obj, int idx)
665+
{
666+
Elf_Scn *scn;
667+
GElf_Shdr sh;
668+
669+
scn = elf_getscn(obj->efile.elf, idx);
670+
if (!scn)
671+
return false;
672+
673+
if (gelf_getshdr(scn, &sh) != &sh)
674+
return false;
675+
676+
if (sh.sh_flags & SHF_EXECINSTR)
677+
return true;
678+
679+
return false;
680+
}
681+
664682
static int bpf_object__elf_collect(struct bpf_object *obj)
665683
{
666684
Elf *elf = obj->efile.elf;
@@ -742,6 +760,14 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
742760
} else if (sh.sh_type == SHT_REL) {
743761
void *reloc = obj->efile.reloc;
744762
int nr_reloc = obj->efile.nr_reloc + 1;
763+
int sec = sh.sh_info; /* points to other section */
764+
765+
/* Only do relo for section with exec instructions */
766+
if (!section_have_execinstr(obj, sec)) {
767+
pr_debug("skip relo %s(%d) for section(%d)\n",
768+
name, idx, sec);
769+
continue;
770+
}
745771

746772
reloc = realloc(reloc,
747773
sizeof(*obj->efile.reloc) * nr_reloc);

0 commit comments

Comments
 (0)