[RFC 18/31] objtool: Disallow duplicate prefix symbols

Josh Poimboeuf
2024-09-03T04:00:38Z

Error out if a duplicate prefix symbol is attempted.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
tools/objtool/check.c | 5 ++++-
tools/objtool/elf.c | 9 +++++++++
tools/objtool/include/objtool/elf.h | 3 +++
3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index af945854dd72..236dc7871f01 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -3866,6 +3866,7 @@ static void add_prefix_symbol(struct objtool_file *file, struct symbol *func)
for (prev = prev_insn_same_sec(file, insn);
prev;
prev = prev_insn_same_sec(file, prev)) {
+ struct symbol *sym_pfx;
u64 offset;

if (prev->type != INSN_NOP)
@@ -3879,7 +3880,9 @@ static void add_prefix_symbol(struct objtool_file *file, struct symbol *func)
if (offset < opts.prefix)
continue;

- elf_create_prefix_symbol(file->elf, func, opts.prefix);
+ sym_pfx = elf_create_prefix_symbol(file->elf, func, opts.prefix);
+ if (!sym_pfx)
+ ERROR("duplicate prefix symbol for %s\n", func->name);

break;
}
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 0c95d7cdf0f5..748c170b931a 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -767,11 +767,20 @@ elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, size_t size)
size_t namelen = strlen(orig->name) + sizeof("__pfx_");
char name[SYM_NAME_LEN];
unsigned long offset;
+ struct symbol *sym;

snprintf(name, namelen, "__pfx_%s", orig->name);

+ sym = orig;
offset = orig->sym.st_value - size;

+ sec_for_each_sym_continue_reverse(orig->sec, sym) {
+ if (sym->offset < offset)
+ break;
+ if (sym->offset == offset && !strcmp(sym->name, name))
+ return NULL;
+ }
+
return elf_create_symbol(elf, name, orig->sec,
GELF_ST_BIND(orig->sym.st_info),
GELF_ST_TYPE(orig->sym.st_info),
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index e91bbe7f07bf..5ac5e7cdddee 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -399,6 +399,9 @@ static inline void set_reloc_type(struct elf *elf, struct reloc *reloc, unsigned
#define sec_for_each_sym(sec, sym) \
list_for_each_entry(sym, &sec->symbol_list, list)

+#define sec_for_each_sym_continue_reverse(sec, sym) \
+ list_for_each_entry_continue_reverse(sym, &sec->symbol_list, list)
+
#define for_each_sym(elf, sym) \
for (struct section *__sec, *__fake = (struct section *)1; \
__fake; __fake = NULL) \
--
2.45.2