public inbox for dwarves@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH dwarves] btf_encoder: verify 0 address DWARF variables are really in ELF section
@ 2024-12-17 10:36 Alan Maguire
  2024-12-17 14:28 ` Jiri Olsa
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Alan Maguire @ 2024-12-17 10:36 UTC (permalink / raw)
  To: acme
  Cc: yonghong.song, dwarves, ast, andrii, bpf, daniel, song, eddyz87,
	olsajiri, stephen.s.brennan, laura.nao, ubizjak, Alan Maguire,
	Cong Wang

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


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

end of thread, other threads:[~2025-03-12 16:53 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-17 10:36 [PATCH dwarves] btf_encoder: verify 0 address DWARF variables are really in ELF section Alan Maguire
2024-12-17 14:28 ` 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

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