BPF List
 help / color / mirror / Atom feed
From: Alan Maguire <alan.maguire@oracle.com>
To: acme@kernel.org
Cc: yonghong.song@linux.dev, dwarves@vger.kernel.org, ast@kernel.org,
	andrii@kernel.org, bpf@vger.kernel.org, daniel@iogearbox.net,
	song@kernel.org, eddyz87@gmail.com, olsajiri@gmail.com,
	stephen.s.brennan@oracle.com, laura.nao@collabora.com,
	ubizjak@gmail.com, Alan Maguire <alan.maguire@oracle.com>,
	Cong Wang <xiyou.wangcong@gmail.com>
Subject: [PATCH dwarves] btf_encoder: verify 0 address DWARF variables are really in ELF section
Date: Tue, 17 Dec 2024 10:36:29 +0000	[thread overview]
Message-ID: <20241217103629.2383809-1-alan.maguire@oracle.com> (raw)

We use the DWARF location information to match a variable with its
associated ELF section.  In the case of per-CPU variables their
ELF section address range starts at 0, so any 0 address variables will
appear to belong in that ELF section.  However, for "discard" sections
DWARF encodes the associated variables with address location 0 so
we need to double-check that address 0 variables really are in the
associated section by checking the ELF symbol table.

This resolves an issue exposed by CONFIG_DEBUG_FORCE_WEAK_PER_CPU=y
kernel builds where __pcpu_* dummary variables in a .discard section
get misclassified as belonging in the per-CPU variable section since
they specify location address 0.

Reported-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
 btf_encoder.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/btf_encoder.c b/btf_encoder.c
index 3754884..04f547c 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -2189,6 +2189,26 @@ static bool filter_variable_name(const char *name)
 	return false;
 }
 
+bool variable_in_sec(struct btf_encoder *encoder, const char *name, size_t shndx)
+{
+	uint32_t sym_sec_idx;
+	uint32_t core_id;
+	GElf_Sym sym;
+
+	elf_symtab__for_each_symbol_index(encoder->symtab, core_id, sym, sym_sec_idx) {
+		const char *sym_name;
+
+		if (sym_sec_idx != shndx || elf_sym__type(&sym) != STT_OBJECT)
+			continue;
+		sym_name = elf_sym__name(&sym, encoder->symtab);
+		if (!sym_name)
+			continue;
+		if (strcmp(name, sym_name) == 0)
+			return true;
+	}
+	return false;
+}
+
 static int btf_encoder__encode_cu_variables(struct btf_encoder *encoder)
 {
 	struct cu *cu = encoder->cu;
@@ -2258,6 +2278,13 @@ static int btf_encoder__encode_cu_variables(struct btf_encoder *encoder)
 		if (filter_variable_name(name))
 			continue;
 
+		/* A 0 address may be in a "discard" section; DWARF provides
+		 * location information with address 0 for such variables.
+		 * Ensure the variable really is in this section by checking
+		 * the ELF symtab.
+		 */
+		if (addr == 0 && !variable_in_sec(encoder, name, shndx))
+			continue;
 		/* Check for invalid BTF names */
 		if (!btf_name_valid(name)) {
 			dump_invalid_symbol("Found invalid variable name when encoding btf",
-- 
2.31.1


             reply	other threads:[~2024-12-17 10:36 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-17 10:36 Alan Maguire [this message]
2024-12-17 14:28 ` [PATCH dwarves] btf_encoder: verify 0 address DWARF variables are really in ELF section Jiri Olsa
2024-12-17 21:35 ` Stephen Brennan
2025-01-26  4:55 ` Cong Wang
2025-01-26 20:04   ` Cong Wang
2025-01-27 11:17     ` Alan Maguire
2025-01-31 20:18       ` Cong Wang
2025-03-12 16:53 ` Alan Maguire

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=20241217103629.2383809-1-alan.maguire@oracle.com \
    --to=alan.maguire@oracle.com \
    --cc=acme@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dwarves@vger.kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=laura.nao@collabora.com \
    --cc=olsajiri@gmail.com \
    --cc=song@kernel.org \
    --cc=stephen.s.brennan@oracle.com \
    --cc=ubizjak@gmail.com \
    --cc=xiyou.wangcong@gmail.com \
    --cc=yonghong.song@linux.dev \
    /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