Skip to contentSkip to conversation

libbpf: skip empty sections in bpf_object__init_global_data_maps

PATCH Applied James Hilliard 4 participants 5 messages Jul 31, 2022 – Aug 4, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

Cc Jose E. Marchesi <[email protected]>
---
 tools/lib/bpf/libbpf.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +4 -0
index 50d41815f431..77e3797cf75a 100644
@@ -1642,6 +1642,10 @@ static int bpf_object__init_global_data_maps(struct bpf_object *obj)
 	for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
 		sec_desc = &obj->efile.secs[sec_idx];
 
+		/* Skip recognized sections with size 0. */
+		if (sec_desc->data && sec_desc->data->d_size == 0)
+			continue;
+
 		switch (sec_desc->sec_type) {
 		case SEC_DATA:
 			sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · James Hilliard

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.

Quoted 8 lines

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.

oops David send same change but I asked him to move the check to bpf_object__elf_collect [1] .. but with your explanation this fix actualy looks fine to me

jirka

[1] https://lore.kernel.org/bpf/YuKaFiZ+ksB5f0Ye@krava/

Quoted 25 lines
Cc Jose E. Marchesi <[email protected]>
---
 tools/lib/bpf/libbpf.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +4 -0
index 50d41815f431..77e3797cf75a 100644
@@ -1642,6 +1642,10 @@ static int bpf_object__init_global_data_maps(struct bpf_object *obj)
 	for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
 		sec_desc = &obj->efile.secs[sec_idx];
 
+		/* Skip recognized sections with size 0. */
+		if (sec_desc->data && sec_desc->data->d_size == 0)
+			continue;
+
 		switch (sec_desc->sec_type) {
 		case SEC_DATA:
 			sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
Signature · 1 line
2.34.1
#2 lore raw
Quoted 12 lines · Jiri Olsa
Quoted 11 lines · James Hilliard

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.

Quoted 6 lines
Quoted 2 lines

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

oops David send same change but I asked him to move the check to bpf_object__elf_collect [1] .. but with your explanation this fix actualy looks fine to me

FWIW, I only just got back to actually making that change. This patch has a much better explanation than the one I sent so +1 from me also

David

Quoted 31 lines

jirka

[1] https://lore.kernel.org/bpf/YuKaFiZ+ksB5f0Ye@krava/

Quoted 25 lines
Cc Jose E. Marchesi <[email protected]>
---
 tools/lib/bpf/libbpf.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +4 -0
index 50d41815f431..77e3797cf75a 100644
@@ -1642,6 +1642,10 @@ static int bpf_object__init_global_data_maps(struct bpf_object *obj)
 	for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
 		sec_desc = &obj->efile.secs[sec_idx];
 
+		/* Skip recognized sections with size 0. */
+		if (sec_desc->data && sec_desc->data->d_size == 0)
+			continue;
+
 		switch (sec_desc->sec_type) {
 		case SEC_DATA:
 			sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
Signature · 1 line
2.34.1
Acked-by
#3 lore raw
Quoted 18 lines · David Faust
Quoted 15 lines · Jiri Olsa
Quoted 13 lines · James Hilliard

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.

Quoted 7 lines
Quoted 3 lines

oops David send same change but I asked him to move the check to bpf_object__elf_collect [1] .. but with your explanation this fix actualy looks fine to me

FWIW, I only just got back to actually making that change. This patch has a much better explanation than the one I sent so +1 from me also

thanks, I'm acking this one then

jirka

Quoted 34 lines

David

Quoted 31 lines

jirka

[1] https://lore.kernel.org/bpf/YuKaFiZ+ksB5f0Ye@krava/

Quoted 25 lines
Cc Jose E. Marchesi <[email protected]>
---
 tools/lib/bpf/libbpf.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +4 -0
index 50d41815f431..77e3797cf75a 100644
@@ -1642,6 +1642,10 @@ static int bpf_object__init_global_data_maps(struct bpf_object *obj)
 	for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
 		sec_desc = &obj->efile.secs[sec_idx];
 
+		/* Skip recognized sections with size 0. */
+		if (sec_desc->data && sec_desc->data->d_size == 0)
+			continue;
+
 		switch (sec_desc->sec_type) {
 		case SEC_DATA:
 			sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
Signature · 1 line
2.34.1
#4 lore raw
In reply to James Hilliard
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Sun, 31 Jul 2022 17:26:49 -0600 you wrote:

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.

[...]

Here is the summary with links:
- libbpf: skip empty sections in bpf_object__init_global_data_maps

    https://git.kernel.org/bpf/bpf-next/c/47ea7417b074

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html