[PATCH v3] elf: Only use function size from regular object

Alan Modra amodra@gmail.com
Tue Sep 15 02:42:58 GMT 2026


On Mon, Sep 14, 2026 at 08:35:32PM +0930, Alan Modra wrote:
> So pr34585 checks out, modulo the recent _init/_fini change.  The
> existing code doesn't do as I thought, and it's been that way for a
> very long time.  See commit 3d7c42c988b5.

Something like the following might be the way to go.  I'm *not* going
to apply this, as all I've done is run the testuite over 100+ targets
and corrected the fails (mips sgi) found in HJ's original testcases.

More analysis needs to be done in order to check this is correct.
I question
a) whether all cases where a definition is not used escape hitting
   this code (via skip),
b) whether removing the SHN_UNDEF is correct (commit 83ad0046cfa7)
   ie. was that patch to cover the h->size == 0 case which I remove,
   and whether we might now get unwanted size change warnings?

I don't intend to work on this pr, so I'd be quite happy if someone
wants to take over.

diff --git a/bfd/elflink.c b/bfd/elflink.c
index d5956b8bd2f..1cafd413e31 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -5629,11 +5629,10 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
 	    }
 
 	  /* Remember the symbol size if it isn't undefined.  */
-	  if (isym->st_size != 0
-	      && isym->st_shndx != SHN_UNDEF
-	      && (definition || h->size == 0))
+	  if (definition)
 	    {
-	      if (h->size != 0
+	      if (isym->st_size != 0
+		  && h->size != 0
 		  && h->size != isym->st_size
 		  && ! size_change_ok)
 		{
diff --git a/ld/testsuite/ld-arm/preempt-app.sym b/ld/testsuite/ld-arm/preempt-app.sym
index 31c01cc8975..391d9104d71 100644
--- a/ld/testsuite/ld-arm/preempt-app.sym
+++ b/ld/testsuite/ld-arm/preempt-app.sym
@@ -4,7 +4,7 @@ Symbol table for image contains 12 entries:
  +..: +00000000 +0 +NOTYPE +LOCAL +DEFAULT +UND 
  +..: +........ +0 +NOTYPE +GLOBAL +DEFAULT +10 _bss_end__
  +..: +........ +0 +NOTYPE +GLOBAL +DEFAULT +10 __bss_start__
- +..: +.......1 +20 +FUNC +GLOBAL +DEFAULT +6 lib_func1
+ +..: +.......1 +0 +FUNC +GLOBAL +DEFAULT +6 lib_func1
  +..: +.......0 +0 +FUNC +GLOBAL +DEFAULT +6 app_func2
  +..: +........ +0 +NOTYPE +GLOBAL +DEFAULT +10 __bss_end__
  +..: +........ +0 +NOTYPE +GLOBAL +DEFAULT +10 __bss_start
diff --git a/ld/testsuite/ld-elf/pr34585a.rd b/ld/testsuite/ld-elf/pr34585a.rd
new file mode 100644
index 00000000000..edbcd567388
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr34585a.rd
@@ -0,0 +1,5 @@
+Symbol table '\.dynsym' contains [0-9]+ entries:
+ +Num: +Value +Size Type +Bind +Vis +Ndx Name
+#...
+ +[0-9]+: +[0-9a-f]+ +0 +FUNC +GLOBAL +DEFAULT .* foo
+#pass
diff --git a/ld/testsuite/ld-elf/pr34585a.s b/ld/testsuite/ld-elf/pr34585a.s
new file mode 100644
index 00000000000..ae6de206e8a
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr34585a.s
@@ -0,0 +1,8 @@
+	.text
+	.globl foo
+	.type foo, %function
+foo:
+	.space 40
+	.size foo, . - foo
+
+	.section .note.GNU-stack,"",%progbits
diff --git a/ld/testsuite/ld-elf/pr34585b.rd b/ld/testsuite/ld-elf/pr34585b.rd
new file mode 100644
index 00000000000..a4a8d25d693
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr34585b.rd
@@ -0,0 +1,5 @@
+Symbol table '\.dynsym' contains [0-9]+ entries:
+ +Num: +Value +Size Type +Bind +Vis +Ndx Name
+#...
+ +[0-9]+: +[0-9a-f]+ +40 +FUNC +GLOBAL +DEFAULT .* foo
+#pass
diff --git a/ld/testsuite/ld-elf/pr34585b.s b/ld/testsuite/ld-elf/pr34585b.s
new file mode 100644
index 00000000000..d131a216aeb
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr34585b.s
@@ -0,0 +1,7 @@
+	.text
+	.globl foo
+	.type foo, %function
+foo:
+	.space 4
+
+	.section .note.GNU-stack,"",%progbits
diff --git a/ld/testsuite/ld-elf/pr34585c.s b/ld/testsuite/ld-elf/pr34585c.s
new file mode 100644
index 00000000000..b3cbebb7276
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr34585c.s
@@ -0,0 +1,9 @@
+	.text
+	.globl foo
+	.weak foo
+	.type foo, %function
+foo:
+	.space 40
+	.size foo, . - foo
+
+	.section .note.GNU-stack,"",%progbits
diff --git a/ld/testsuite/ld-elf/pr34585d.s b/ld/testsuite/ld-elf/pr34585d.s
new file mode 100644
index 00000000000..556e6c9d78a
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr34585d.s
@@ -0,0 +1,8 @@
+	.text
+	.globl foo
+	.weak foo
+	.type foo, %function
+foo:
+	.space 4
+
+	.section .note.GNU-stack,"",%progbits
diff --git a/ld/testsuite/ld-elf/shared.exp b/ld/testsuite/ld-elf/shared.exp
index ae70ad2cc1a..eedddbb76d7 100644
--- a/ld/testsuite/ld-elf/shared.exp
+++ b/ld/testsuite/ld-elf/shared.exp
@@ -372,6 +372,54 @@ if { [check_gc_sections_available] } {
     }
 }
 
+run_ld_link_tests [list \
+    [list \
+	"Build pr34585a.so" \
+	"$LFLAGS -shared" \
+	"" \
+	"$AFLAGS_PIC" \
+	{pr34585a.s} \
+	{} \
+	"pr34585a.so" \
+    ] \
+    [list \
+	"Build pr34585b.so" \
+	"$LFLAGS -shared" \
+	"tmpdir/pr34585a.so" \
+	"$AFLAGS_PIC" \
+	{pr34585b.s} \
+	{{readelf {--dyn-syms --wide} pr34585a.rd}} \
+	"pr34585b.so" \
+    ] \
+    [list \
+	"Build pr34585c.so" \
+	"$LFLAGS -shared tmpdir/pr34585a.so" \
+	"" \
+	"$AFLAGS_PIC" \
+	{pr34585b.s} \
+	{{readelf {--dyn-syms --wide} pr34585a.rd}} \
+	"pr34585c.so" \
+    ] \
+    [list \
+	"Build pr34585d.so" \
+	"$LFLAGS -shared" \
+	"" \
+	"$AFLAGS_PIC" \
+	{pr34585a.s pr34585c.s} \
+	{{readelf {--dyn-syms --wide} pr34585b.rd}} \
+	"pr34585d.so" \
+    ] \
+    [list \
+	"Build pr34585e.so" \
+	"$LFLAGS -shared" \
+	"" \
+	"$AFLAGS_PIC" \
+	{pr34585b.s pr34585d.s} \
+	{{readelf {--dyn-syms --wide} pr34585a.rd}} \
+	"pr34585e.so" \
+    ] \
+]
+
 set ASFLAGS $old_ASFLAGS
 
 run_ld_link_tests {


-- 
Alan Modra


More information about the Binutils mailing list