From eebad48efeeee858be83d754a6b1326133e51d36 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Thu, 18 Apr 2024 05:28:56 -0700 Subject: elf: Strip unreferenced weak undefined symbols Linker will resolve an undefined symbol only if it is referenced by relocation. Unreferenced weak undefined symbols serve no purpose. Weak undefined symbols appear in the dynamic symbol table only when they are referenced by dynamic relocation. Mark symbols with relocation and strip undefined weak symbols if they don't have relocation and aren't in the dynamic symbol table. bfd/ PR ld/31652 * elf-bfd.h (elf_link_hash_entry): Add has_reloc. * elf-vxworks.c (elf_vxworks_emit_relocs): Set has_reloc. * elflink.c (_bfd_elf_link_output_relocs): Likewise. (elf_link_output_extsym): Strip undefined weak symbols if they don't have relocation and aren't in the dynamic symbol table. ld/ PR ld/31652 * testsuite/ld-elf/elf.exp: Run undefweak tests. * testsuite/ld-elf/undefweak-1.rd: New file. * testsuite/ld-elf/undefweak-1a.s: Likewise. * testsuite/ld-elf/undefweak-1b.s: Likewise. * testsuite/ld-x86-64/weakundef-1.nd: Likewise. * testsuite/ld-x86-64/weakundef-1a.s: Likewise. * testsuite/ld-x86-64/weakundef-1b.s: Likewise. * testsuite/ld-x86-64/x86-64.exp: Run undefweak tests. --- ld/testsuite/ld-elf/elf.exp | 15 +++++++++++++++ ld/testsuite/ld-elf/undefweak-1.rd | 10 ++++++++++ ld/testsuite/ld-elf/undefweak-1a.s | 14 ++++++++++++++ ld/testsuite/ld-elf/undefweak-1b.s | 10 ++++++++++ ld/testsuite/ld-x86-64/weakundef-1.nd | 6 ++++++ ld/testsuite/ld-x86-64/weakundef-1a.s | 6 ++++++ ld/testsuite/ld-x86-64/weakundef-1b.s | 8 ++++++++ ld/testsuite/ld-x86-64/x86-64.exp | 18 ++++++++++++++++++ 8 files changed, 87 insertions(+) create mode 100644 ld/testsuite/ld-elf/undefweak-1.rd create mode 100644 ld/testsuite/ld-elf/undefweak-1a.s create mode 100644 ld/testsuite/ld-elf/undefweak-1b.s create mode 100644 ld/testsuite/ld-x86-64/weakundef-1.nd create mode 100644 ld/testsuite/ld-x86-64/weakundef-1a.s create mode 100644 ld/testsuite/ld-x86-64/weakundef-1b.s (limited to 'ld/testsuite') diff --git a/ld/testsuite/ld-elf/elf.exp b/ld/testsuite/ld-elf/elf.exp index 685b875..874ba66 100644 --- a/ld/testsuite/ld-elf/elf.exp +++ b/ld/testsuite/ld-elf/elf.exp @@ -171,6 +171,21 @@ run_ld_link_tests [list \ "implib" ] \ ] \[uses_genelf\] +run_ld_link_tests [list \ + [list "Generate undefweak-1.a" \ + "" "" \ + $hpux \ + {undefweak-1a.s} \ + {} \ + "undefweak-1.a" ] \ + [list "Generate undefweak-1.o" \ + "-r" "tmpdir/undefweak-1.a" \ + $hpux \ + {undefweak-1b.s} \ + {{readelf {-rsW} undefweak-1.rd}} \ + "undefweak-1.o" ] \ +] \[uses_genelf\] + #v850 gas complains about .tbss.var section attributes. if { [check_gc_sections_available] && ![istarget "v850-*-*"] } { run_ld_link_tests { diff --git a/ld/testsuite/ld-elf/undefweak-1.rd b/ld/testsuite/ld-elf/undefweak-1.rd new file mode 100644 index 0000000..5b48c13 --- /dev/null +++ b/ld/testsuite/ld-elf/undefweak-1.rd @@ -0,0 +1,10 @@ +#source: undefweak-1a.s +#source: undefweak-1b.s +#as: +#ld: -r +#readelf: -sW + +#failif +#... +.*: 0+ +0 +FUNC +WEAK +DEFAULT +UND +bar +#pass diff --git a/ld/testsuite/ld-elf/undefweak-1a.s b/ld/testsuite/ld-elf/undefweak-1a.s new file mode 100644 index 0000000..1d885fb --- /dev/null +++ b/ld/testsuite/ld-elf/undefweak-1a.s @@ -0,0 +1,14 @@ + .section .text.foobar,"axG",%progbits,foo,comdat + .weak foo + .type foo,%function +foo: + .nop + .size foo, . - foo + .weak bar + .set bar, foo + .text + .global baz + .type baz,%function +baz: + .dc.a foo + .size baz, . - baz diff --git a/ld/testsuite/ld-elf/undefweak-1b.s b/ld/testsuite/ld-elf/undefweak-1b.s new file mode 100644 index 0000000..8ba34ea --- /dev/null +++ b/ld/testsuite/ld-elf/undefweak-1b.s @@ -0,0 +1,10 @@ + .section .text.foobar,"axG",%progbits,foo,comdat + .weak foo + .type foo,%function +foo: + .nop + .size foo, . - foo + .text + .global _start + .set _start,foo + .dc.a baz diff --git a/ld/testsuite/ld-x86-64/weakundef-1.nd b/ld/testsuite/ld-x86-64/weakundef-1.nd new file mode 100644 index 0000000..c4c89bf --- /dev/null +++ b/ld/testsuite/ld-x86-64/weakundef-1.nd @@ -0,0 +1,6 @@ +#nm: -n +#target: x86_64-*-* + +#... + +w +bar +#pass diff --git a/ld/testsuite/ld-x86-64/weakundef-1a.s b/ld/testsuite/ld-x86-64/weakundef-1a.s new file mode 100644 index 0000000..b6eea32 --- /dev/null +++ b/ld/testsuite/ld-x86-64/weakundef-1a.s @@ -0,0 +1,6 @@ + .text + .global foo + .type foo,%function +foo: + .nop + .size foo, . - foo diff --git a/ld/testsuite/ld-x86-64/weakundef-1b.s b/ld/testsuite/ld-x86-64/weakundef-1b.s new file mode 100644 index 0000000..3d2da7d --- /dev/null +++ b/ld/testsuite/ld-x86-64/weakundef-1b.s @@ -0,0 +1,8 @@ + .weak bar + .text + .global _start + .type _start,%function +_start: + mov bar@GOTPCREL(%rip), %rax + mov foo@GOTPCREL(%rip), %rax + .size _start, . - _start diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp index e370f39..6546fff 100644 --- a/ld/testsuite/ld-x86-64/x86-64.exp +++ b/ld/testsuite/ld-x86-64/x86-64.exp @@ -264,6 +264,24 @@ run_ld_link_tests [list \ {objdump -drj.plt tlsdesc.pd}} \ "libtlsdesc.so" \ ] \ + [list \ + "Generate x86-64-weakundef-1.so" \ + "-shared -melf_x86_64" \ + "" \ + "--64" \ + {weakundef-1a.s} \ + {} \ + "x86-64-weakundef-1.so" \ + ] \ + [list \ + "Generate x86-64-weakundef-1" \ + "-melf_x86_64" \ + "tmpdir/x86-64-weakundef-1.so" \ + "--64" \ + {weakundef-1b.s} \ + {{nm -n weakundef-1.nd}} \ + "x86-64-weakundef-1" \ + ] \ ] set test_name "Mixed x86_64 and i386 input test 1" -- cgit v1.1