From: Stephen Brennan <stephen.s.brennan@oracle.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: dwarves@vger.kernel.org, linux-debuggers@vger.kernel.org,
Stephen Brennan <stephen.s.brennan@oracle.com>,
Alan Maguire <alan.maguire@oracle.com>
Subject: [PATCH dwarves v2 2/2] btf_encoder: fix DATASEC assignment for ET_REL files
Date: Mon, 30 Nov 2026 10:46:37 -0800 [thread overview]
Message-ID: <20261130184641.84850-3-stephen.s.brennan@oracle.com> (raw)
In-Reply-To: <20261130184641.84850-1-stephen.s.brennan@oracle.com>
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
prev parent reply other threads:[~2026-11-30 18:47 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130184641.84850-3-stephen.s.brennan@oracle.com \
--to=stephen.s.brennan@oracle.com \
--cc=acme@kernel.org \
--cc=alan.maguire@oracle.com \
--cc=dwarves@vger.kernel.org \
--cc=linux-debuggers@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox