[RFC 27/31] objtool: Fix weak symbol detection

Josh Poimboeuf posted 31 patches 1 year, 5 months ago
[RFC 27/31] objtool: Fix weak symbol detection
Posted by Josh Poimboeuf 1 year, 5 months ago
find_symbol_hole_containing() fails to find a symbol hole (aka stripped
weak symbol) if its section has no symbols before the hole.  This breaks
weak symbol detection if -ffunction-sections is enabled.

Fix it by allowing the interval tree to contain section symbols, which
are always at offset zero for a given section.

Fixes a bunch of (-ffunction-sections) warnings like:

  vmlinux.o: warning: objtool: .text.__x64_sys_io_setup+0x10: unreachable instruction
  vmlinux.o: warning: objtool: .text.__x64_sys_io_setup+0x14: unreachable instruction
  vmlinux.o: warning: objtool: .text.__x64_sys_io_setup+0x19: unreachable instruction
  vmlinux.o: warning: objtool: .text.__x64_sys_io_setup+0x20: unreachable instruction

Fixes: 4adb23686795 ("objtool: Ignore extra-symbol code")
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 tools/objtool/elf.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 471df0336aa7..3109277804cc 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -110,7 +110,7 @@ struct symbol_hole {
 };
 
 /*
- * Find !section symbol where @offset is after it.
+ * Find the last symbol before @offset.
  */
 static int symbol_hole_by_offset(const void *key, const struct rb_node *node)
 {
@@ -121,8 +121,7 @@ static int symbol_hole_by_offset(const void *key, const struct rb_node *node)
 		return -1;
 
 	if (sh->offset >= s->offset + s->len) {
-		if (s->type != STT_SECTION)
-			sh->sym = s;
+		sh->sym = s;
 		return 1;
 	}
 
@@ -416,7 +415,8 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
 	sym->len = sym->sym.st_size;
 
 	__sym_for_each(s, &sym->sec->symbol_tree, sym->offset, sym->offset) {
-		if (s->offset == sym->offset && s->type == sym->type)
+		if (s->type == sym->type && s->offset == sym->offset &&
+		    s->len == sym->len)
 			s->alias = sym;
 	}
 
@@ -433,9 +433,13 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
 	/*
 	 * Don't store empty STT_NOTYPE symbols in the rbtree.  They
 	 * can exist within a function, confusing the sorting.
+	 *
+	 * TODO: is this still true?
 	 */
-	if (!sym->len)
+#if 0
+	if (sym->type == STT_NOTYPE && !sym->len)
 		__sym_remove(sym, &sym->sec->symbol_tree);
+#endif
 }
 
 static void read_symbols(struct elf *elf)
-- 
2.45.2
Re: [RFC 27/31] objtool: Fix weak symbol detection
Posted by Peter Zijlstra 1 year, 5 months ago
On Mon, Sep 02, 2024 at 09:00:10PM -0700, Josh Poimboeuf wrote:
> @@ -433,9 +433,13 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
>  	/*
>  	 * Don't store empty STT_NOTYPE symbols in the rbtree.  They
>  	 * can exist within a function, confusing the sorting.
> +	 *
> +	 * TODO: is this still true?

a2e38dffcd93 ("objtool: Don't add empty symbols to the rbtree")

I don't think that changed.
Re: [RFC 27/31] objtool: Fix weak symbol detection
Posted by Josh Poimboeuf 1 year, 5 months ago
On Tue, Sep 03, 2024 at 10:26:45AM +0200, Peter Zijlstra wrote:
> On Mon, Sep 02, 2024 at 09:00:10PM -0700, Josh Poimboeuf wrote:
> > @@ -433,9 +433,13 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
> >  	/*
> >  	 * Don't store empty STT_NOTYPE symbols in the rbtree.  They
> >  	 * can exist within a function, confusing the sorting.
> > +	 *
> > +	 * TODO: is this still true?
> 
> a2e38dffcd93 ("objtool: Don't add empty symbols to the rbtree")
> 
> I don't think that changed.

But see two patches back where I fixed a bug in the insertion of
zero-length symbols.

I was thinking that might actually be the root cause of the above
commit.

-- 
Josh
Re: [RFC 27/31] objtool: Fix weak symbol detection
Posted by Peter Zijlstra 1 year, 5 months ago
On Tue, Sep 03, 2024 at 08:55:13PM -0700, Josh Poimboeuf wrote:
> On Tue, Sep 03, 2024 at 10:26:45AM +0200, Peter Zijlstra wrote:
> > On Mon, Sep 02, 2024 at 09:00:10PM -0700, Josh Poimboeuf wrote:
> > > @@ -433,9 +433,13 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
> > >  	/*
> > >  	 * Don't store empty STT_NOTYPE symbols in the rbtree.  They
> > >  	 * can exist within a function, confusing the sorting.
> > > +	 *
> > > +	 * TODO: is this still true?
> > 
> > a2e38dffcd93 ("objtool: Don't add empty symbols to the rbtree")
> > 
> > I don't think that changed.
> 
> But see two patches back where I fixed a bug in the insertion of
> zero-length symbols.
> 
> I was thinking that might actually be the root cause of the above
> commit.

Aah, yeah, might be. If we can reproduce the original problem, it should
be verifiable.
Re: [RFC 27/31] objtool: Fix weak symbol detection
Posted by Josh Poimboeuf 1 year, 5 months ago
On Wed, Sep 04, 2024 at 09:42:54AM +0200, Peter Zijlstra wrote:
> On Tue, Sep 03, 2024 at 08:55:13PM -0700, Josh Poimboeuf wrote:
> > On Tue, Sep 03, 2024 at 10:26:45AM +0200, Peter Zijlstra wrote:
> > > On Mon, Sep 02, 2024 at 09:00:10PM -0700, Josh Poimboeuf wrote:
> > > > @@ -433,9 +433,13 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
> > > >  	/*
> > > >  	 * Don't store empty STT_NOTYPE symbols in the rbtree.  They
> > > >  	 * can exist within a function, confusing the sorting.
> > > > +	 *
> > > > +	 * TODO: is this still true?
> > > 
> > > a2e38dffcd93 ("objtool: Don't add empty symbols to the rbtree")
> > > 
> > > I don't think that changed.
> > 
> > But see two patches back where I fixed a bug in the insertion of
> > zero-length symbols.
> > 
> > I was thinking that might actually be the root cause of the above
> > commit.
> 
> Aah, yeah, might be. If we can reproduce the original problem, it should
> be verifiable.

Indeed, I'll investigate.

-- 
Josh