[RFC 22/31] objtool: Make find_symbol_containing() less arbitrary

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

In the rare case of overlapping symbols, find_symbol_containing() just
returns the first one it finds. Make it less arbitrary by returning the
smallest symbol with size > 0.

Eventually we should consider making such overlapping symbols illegal.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
tools/objtool/elf.c | 25 ++++++++++++++++++++-----
1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 7f89b0a99886..49528e7835aa 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -195,14 +195,29 @@ struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
{
struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
- struct symbol *iter;
+ struct symbol *sym = NULL, *tmp;

- __sym_for_each(iter, tree, offset, offset) {
- if (iter->type != STT_SECTION)
- return iter;
+ __sym_for_each(tmp, tree, offset, offset) {
+ if (tmp->len) {
+ if (!sym) {
+ sym = tmp;
+ continue;
+ }
+
+ if (sym->offset != tmp->offset || sym->len != tmp->len) {
+ /*
+ * In the rare case of overlapping symbols,
+ * pick the smaller one.
+ *
+ * TODO: outlaw overlapping symbols
+ */
+ if (tmp->len < sym->len)
+ sym = tmp;
+ }
+ }
}

- return NULL;
+ return sym;
}

/*
--
2.45.2