Linux debuggers
 help / color / mirror / Atom feed
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 2/2] btf_encoder: fix DATASEC assignment for ET_REL files
Date: Thu, 19 Nov 2026 10:52:34 -0800	[thread overview]
Message-ID: <20261119185236.4113166-3-stephen.s.brennan@oracle.com> (raw)
In-Reply-To: <20261119185236.4113166-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 the above behavior.

Pahole iterates the sections in order, so if a variable's address falls
into the overlapping range, and the non-allocated section appears first,
pahole assigns it to the non-allocated section.

Currently, get_elf_section() has logic to filter which sections are
actually considered for variables. It uses a cached value of sh_type,
but we have no cached sh_flags. Rather than cache more data, we can just
filter out ineligible sections when we initialize the secinfo array,
allowing us to remove cached sh_type altogether. This keeps the
eligibility logic in one place, and removes the need to cache a field.

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

diff --git a/btf_encoder.c b/btf_encoder.c
index d6b9be6..8907b25 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -116,7 +116,6 @@ struct elf_secinfo {
 	uint64_t    addr;
 	const char *name;
 	uint64_t    sz;
-	uint32_t    type;
 	bool        include;
 	struct gobuffer secinfo;
 };
@@ -2658,11 +2657,6 @@ static size_t get_elf_section(struct btf_encoder *encoder, uint64_t addr)
 {
 	/* Start at index 1 to ignore initial SHT_NULL section */
 	for (size_t i = 1; i < encoder->seccnt; i++) {
-		/* Variables are only present in PROGBITS or NOBITS (.bss) */
-		if (!(encoder->secinfo[i].type == SHT_PROGBITS ||
-		     encoder->secinfo[i].type == SHT_NOBITS))
-			continue;
-
 		if (encoder->secinfo[i].addr <= addr &&
 		    (addr - encoder->secinfo[i].addr) < encoder->secinfo[i].sz)
 			return i;
@@ -2977,10 +2971,15 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam
 			Elf_Scn *sec = elf_section_by_idx(cu->elf, &shdr, shndx, &secname);
 			if (!sec)
 				goto out_delete;
+			/* Variables are only present in PROGBITS or NOBITS (.bss) */
+			if (!(shdr.sh_type == SHT_PROGBITS || shdr.sh_type == SHT_NOBITS))
+				continue;
+			/* Variables are only present in runtime allocated sections. */
+			if (!(shdr.sh_flags & SHF_ALLOC))
+				continue;
 			encoder->secinfo[shndx].addr = shdr.sh_addr;
 			encoder->secinfo[shndx].sz = shdr.sh_size;
 			encoder->secinfo[shndx].name = secname;
-			encoder->secinfo[shndx].type = shdr.sh_type;
 
 			if (encoder->encode_vars & BTF_VAR_GLOBAL)
 				encoder->secinfo[shndx].include = true;
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


  parent reply	other threads:[~2026-11-19 18:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 18:52 [PATCH dwarves 0/2] Fix DATASEC assignment for global_var VARs Stephen Brennan
2026-11-19 18:52 ` [PATCH dwarves 1/2] tests: prep_release: use $VISUAL, not $EDITOR Stephen Brennan
2026-11-19 18:52 ` Stephen Brennan [this message]
2026-11-27 16:12   ` [PATCH dwarves 2/2] btf_encoder: fix DATASEC assignment for ET_REL files Alan Maguire
2026-11-27 16:26     ` Arnaldo Melo
2026-11-30 18:27     ` Stephen Brennan

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=20261119185236.4113166-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