Linux debuggers
 help / color / mirror / Atom feed
* [PATCH dwarves v2 0/2] Fix DATASEC assignment for global_var VARs
@ 2026-11-30 18:46 Stephen Brennan
  2026-11-30 18:46 ` [PATCH dwarves v2 1/2] tests: prep_release: use $VISUAL, not $EDITOR Stephen Brennan
  2026-11-30 18:46 ` [PATCH dwarves v2 2/2] btf_encoder: fix DATASEC assignment for ET_REL files Stephen Brennan
  0 siblings, 2 replies; 3+ messages in thread
From: Stephen Brennan @ 2026-11-30 18:46 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: dwarves, linux-debuggers, Stephen Brennan, Alan Maguire

Hello Arnaldo & Alan,

This respins my earlier set of quick fixes onto the correct upstream branch.
Due to Alan's changes in btf_encoder.c, it was best to switch my approach on
patch 2 to use the new cached sh_flags. The regresison test is unchanged and
still passes. The full test suite passes on my machine.

v1: https://lore.kernel.org/dwarves/20261119185236.4113166-1-stephen.s.brennan@oracle.com/

Stephen Brennan (2):
  tests: prep_release: use $VISUAL, not $EDITOR
  btf_encoder: fix DATASEC assignment for ET_REL files

 btf_encoder.c         |  3 +++
 tests/btf_datasec.sh  | 56 ++++++++++++++++++++++++++++++++++++++++++-
 tests/prep_release.sh |  2 +-
 3 files changed, 59 insertions(+), 2 deletions(-)

-- 
2.52.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH dwarves v2 1/2] tests: prep_release: use $VISUAL, not $EDITOR
  2026-11-30 18:46 [PATCH dwarves v2 0/2] Fix DATASEC assignment for global_var VARs Stephen Brennan
@ 2026-11-30 18:46 ` Stephen Brennan
  2026-11-30 18:46 ` [PATCH dwarves v2 2/2] btf_encoder: fix DATASEC assignment for ET_REL files Stephen Brennan
  1 sibling, 0 replies; 3+ messages in thread
From: Stephen Brennan @ 2026-11-30 18:46 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: dwarves, linux-debuggers, Stephen Brennan, Alan Maguire

The script uses $VISUAL and only falls back to $EDITOR. Systems with
$VISUAL set will open the editor during tests, which is not what we
want.

Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
---
 tests/prep_release.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/prep_release.sh b/tests/prep_release.sh
index f8b86ff..62728ec 100755
--- a/tests/prep_release.sh
+++ b/tests/prep_release.sh
@@ -642,7 +642,7 @@ esac
 exit 0
 EOF
 chmod +x "$outdir/mark-editor"
-export EDITOR="sh $outdir/mark-editor"
+export VISUAL="sh $outdir/mark-editor"
 if ! bash "$script" --yes --no-commit --no-tarball \
 		--no-build-check --no-tag > "$outdir/run.log" 2>&1; then
 	error_log "FAIL: prep-release.sh exited with an error:"
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH dwarves v2 2/2] btf_encoder: fix DATASEC assignment for ET_REL files
  2026-11-30 18:46 [PATCH dwarves v2 0/2] Fix DATASEC assignment for global_var VARs Stephen Brennan
  2026-11-30 18:46 ` [PATCH dwarves v2 1/2] tests: prep_release: use $VISUAL, not $EDITOR Stephen Brennan
@ 2026-11-30 18:46 ` Stephen Brennan
  1 sibling, 0 replies; 3+ messages in thread
From: Stephen Brennan @ 2026-11-30 18:46 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: dwarves, linux-debuggers, Stephen Brennan, Alan Maguire

When building a kernel with the global_var feature, I encountered some
kernel modules whose BTF encoded a DATASEC named ".debug_info" and
assigned some variables, such as "__this_module" to this section.

For ET_REL files, libdwfl assigns a synthetic address to allocated
sections, starting at around 0x10000. Non-allocated sections, like
.debug_info, start at address 0. If large enough, a non-allocated
section's address range can extend past 0x10000, into the synthetic
address range of an allocated section. This can happen with Linux kernel
modules, resulting in overlapping address ranges.

To find the relevant ELF section for a variable, get_elf_section() uses
the first match, so if a variable's address falls into the overlapping
range, and the non-allocated section appears first, it is assigned to
the non-allocated section, in this case ".debug_info". To fix this,
only consider sections with SHF_ALLOC flag set, as these are the only
sections which could contain a variable.

Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
---
 btf_encoder.c        |  3 +++
 tests/btf_datasec.sh | 56 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/btf_encoder.c b/btf_encoder.c
index 8537628..028ccdd 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -2730,6 +2730,9 @@ static size_t get_elf_section(struct btf_encoder *encoder, uint64_t addr)
 		if (!(encoder->secinfo[i].type == SHT_PROGBITS ||
 		     encoder->secinfo[i].type == SHT_NOBITS))
 			continue;
+		/* Variables are only present in runtime allocated sections. */
+		if (!(encoder->secinfo[i].flags & SHF_ALLOC))
+			continue;
 
 		if (encoder->secinfo[i].addr <= addr &&
 		    (addr - encoder->secinfo[i].addr) < encoder->secinfo[i].sz)
diff --git a/tests/btf_datasec.sh b/tests/btf_datasec.sh
index 470c7f2..31d590f 100755
--- a/tests/btf_datasec.sh
+++ b/tests/btf_datasec.sh
@@ -4,7 +4,8 @@
 #
 # Test BTF VAR and DATASEC encoding for global variables via
 # btf_encoder__add_var() and btf_encoder__add_datasec() in btf_encoder.c,
-# exercised through --btf_features=global_var on a small linked binary.
+# exercised through --btf_features=global_var on a small linked binary
+# and a relocatable object with overlapping non-allocated sections.
 #
 # External globals and statics must each produce a BTF_KIND_VAR entry with
 # the correct linkage, and their ELF section must appear as a
@@ -119,4 +120,57 @@ else
 	info_log "DATASEC VAR references: skipped (old bpftool format)"
 fi
 
+# For ET_REL files, libdwfl assigns synthetic addresses starting at 0x10000
+# to allocated sections. Non-allocated sections retain address zero, so a
+# sufficiently large one appears to overlap the variables' addresses.
+# Put one before .late_data to catch lookups that ignore SHF_ALLOC.
+cat > "$src" << 'EOF'
+asm(".pushsection .debug_padding, \"\"\n"
+    ".zero 0x20000\n"
+    ".popsection\n");
+int late_first __attribute__((section(".late_data"))) = 1;
+int late_second __attribute__((section(".late_data"))) = 2;
+EOF
+
+obj="$outdir/btf_datasec.o"
+btf="$outdir/btf_datasec_rel.btf"
+if ! $CC -g -O0 -c -o "$obj" "$src" 2>/dev/null; then
+	error_log "FAIL: relocatable object compilation failed"
+	test_fail
+fi
+
+if ! pahole --btf_features=global_var --btf_encode_detached="$btf" "$obj" 2>/dev/null; then
+	error_log "FAIL: relocatable object BTF encoding failed"
+	test_fail
+fi
+
+dump=$(bpftool btf dump file "$btf" 2>/dev/null)
+if ! echo "$dump" | grep -q "DATASEC '\.late_data' size=8 vlen=2"; then
+	error_log "FAIL: expected two variables in .late_data DATASEC"
+	test_fail
+fi
+if echo "$dump" | grep -q "DATASEC '\.debug_padding'"; then
+	error_log "FAIL: non-allocated .debug_padding encoded as a DATASEC"
+	test_fail
+fi
+
+for var in late_first late_second; do
+	if ! echo "$dump" | grep -q "VAR '$var'"; then
+		error_log "FAIL: BTF_KIND_VAR '$var' not found"
+		test_fail
+	fi
+done
+
+if echo "$dump" | grep -q "(VAR '"; then
+	for var in late_first late_second; do
+		if ! echo "$dump" | grep -q "(VAR '$var')"; then
+			error_log "FAIL: no DATASEC references VAR '$var'"
+			test_fail
+		fi
+	done
+else
+	info_log "DATASEC VAR references: skipped (old bpftool format)"
+fi
+info_log "Relocatable object ignores non-allocated sections: ok"
+
 test_pass
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-30 18:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 18:46 [PATCH dwarves v2 0/2] Fix DATASEC assignment for global_var VARs Stephen Brennan
2026-11-30 18:46 ` [PATCH dwarves v2 1/2] tests: prep_release: use $VISUAL, not $EDITOR Stephen Brennan
2026-11-30 18:46 ` [PATCH dwarves v2 2/2] btf_encoder: fix DATASEC assignment for ET_REL files Stephen Brennan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox