Skip to content

Commit 50e0946

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Skip well-known ELF sections when iterating ELF
Skip and don't log ELF sections that libbpf knows about and ignores during ELF processing. This allows to not unnecessarily log details about those ELF sections and cleans up libbpf debug log. Ignored sections include DWARF data, string table, empty .text section and few special (e.g., .llvm_addrsig) useless sections. With such ELF sections out of the way, log unrecognized ELF sections at pr_info level to increase visibility. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200820231250.1293069-5-andriin@fb.com
1 parent 819c23a commit 50e0946

1 file changed

Lines changed: 49 additions & 6 deletions

File tree

‎tools/lib/bpf/libbpf.c‎

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,46 @@ static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn)
26722672
return data;
26732673
}
26742674

2675+
static bool is_sec_name_dwarf(const char *name)
2676+
{
2677+
/* approximation, but the actual list is too long */
2678+
return strncmp(name, ".debug_", sizeof(".debug_") - 1) == 0;
2679+
}
2680+
2681+
static bool ignore_elf_section(GElf_Shdr *hdr, const char *name)
2682+
{
2683+
/* no special handling of .strtab */
2684+
if (hdr->sh_type == SHT_STRTAB)
2685+
return true;
2686+
2687+
/* ignore .llvm_addrsig section as well */
2688+
if (hdr->sh_type == 0x6FFF4C03 /* SHT_LLVM_ADDRSIG */)
2689+
return true;
2690+
2691+
/* no subprograms will lead to an empty .text section, ignore it */
2692+
if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 &&
2693+
strcmp(name, ".text") == 0)
2694+
return true;
2695+
2696+
/* DWARF sections */
2697+
if (is_sec_name_dwarf(name))
2698+
return true;
2699+
2700+
if (strncmp(name, ".rel", sizeof(".rel") - 1) == 0) {
2701+
name += sizeof(".rel") - 1;
2702+
/* DWARF section relocations */
2703+
if (is_sec_name_dwarf(name))
2704+
return true;
2705+
2706+
/* .BTF and .BTF.ext don't need relocations */
2707+
if (strcmp(name, BTF_ELF_SEC) == 0 ||
2708+
strcmp(name, BTF_EXT_ELF_SEC) == 0)
2709+
return true;
2710+
}
2711+
2712+
return false;
2713+
}
2714+
26752715
static int bpf_object__elf_collect(struct bpf_object *obj)
26762716
{
26772717
Elf *elf = obj->efile.elf;
@@ -2694,6 +2734,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
26942734
if (!name)
26952735
return -LIBBPF_ERRNO__FORMAT;
26962736

2737+
if (ignore_elf_section(&sh, name))
2738+
continue;
2739+
26972740
data = elf_sec_data(obj, scn);
26982741
if (!data)
26992742
return -LIBBPF_ERRNO__FORMAT;
@@ -2746,8 +2789,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
27462789
obj->efile.st_ops_data = data;
27472790
obj->efile.st_ops_shndx = idx;
27482791
} else {
2749-
pr_debug("elf: skipping unrecognized data section(%d) %s\n",
2750-
idx, name);
2792+
pr_info("elf: skipping unrecognized data section(%d) %s\n",
2793+
idx, name);
27512794
}
27522795
} else if (sh.sh_type == SHT_REL) {
27532796
int nr_sects = obj->efile.nr_reloc_sects;
@@ -2758,9 +2801,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
27582801
if (!section_have_execinstr(obj, sec) &&
27592802
strcmp(name, ".rel" STRUCT_OPS_SEC) &&
27602803
strcmp(name, ".rel" MAPS_ELF_SEC)) {
2761-
pr_debug("elf: skipping relo section(%d) %s for section(%d) %s\n",
2762-
idx, name, sec,
2763-
elf_sec_name(obj, elf_sec_by_idx(obj, sec)) ?: "<?>");
2804+
pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
2805+
idx, name, sec,
2806+
elf_sec_name(obj, elf_sec_by_idx(obj, sec)) ?: "<?>");
27642807
continue;
27652808
}
27662809

@@ -2778,7 +2821,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
27782821
obj->efile.bss = data;
27792822
obj->efile.bss_shndx = idx;
27802823
} else {
2781-
pr_debug("elf: skipping section(%d) %s\n", idx, name);
2824+
pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name, sh.sh_size);
27822825
}
27832826
}
27842827

0 commit comments

Comments
 (0)