Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Skip empty sections in bpf_object__init_global_data_maps

The GNU assembler generates an empty .bss section. This is a well
established behavior in GAS that happens in all supported targets.

The LLVM assembler doesn't generate an empty .bss section.

bpftool chokes on the empty .bss section.

Additionally in bpf_object__elf_collect the sec_desc->data is not
initialized when a section is not recognized. In this case, this
happens with .comment.

So we must check that sec_desc->data is initialized before checking
if the size is 0.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20220731232649.4668-1-james.hilliard1@gmail.com

authored by

James Hilliard and committed by
Andrii Nakryiko
47ea7417 f86d1fbb

+4
+4
tools/lib/bpf/libbpf.c
··· 1642 1642 for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) { 1643 1643 sec_desc = &obj->efile.secs[sec_idx]; 1644 1644 1645 + /* Skip recognized sections with size 0. */ 1646 + if (sec_desc->data && sec_desc->data->d_size == 0) 1647 + continue; 1648 + 1645 1649 switch (sec_desc->sec_type) { 1646 1650 case SEC_DATA: 1647 1651 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));