Created attachment 12736 [details] A patch A testcase is on pr96385 branch at: https://gitlab.com/x86-gcc/gcc-bugs [hjl@gnu-cfl-2 gcc-bugs]$ make gcc -B./ -O2 -g -o ar -Wl,--as-needed arparse.o arlex.o ar.o not-ranlib.o arsup.o rename.o binemul.o emul_vanilla.o bucomm.o version.o filemode.o libbfd-2.35-3.fc33.so libiberty.a -Wl,-R,. ./ar rc libfoo.a not-ranlib.o [hjl@gnu-cfl-2 gcc-bugs]$ readelf --dyn-syms -rW ar | grep bfd_scan_vma 5: 0000000000000000 0 FUNC GLOBAL DEFAULT UND bfd_scan_vma [hjl@gnu-cfl-2 gcc-bugs]$ LTO removes the reference to fd_scan_vma. But ld puts it in dynamic symbol table. The reference is generated by GCC bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96385 Remove -g fixes the GCC bug: [hjl@gnu-cfl-2 gcc-bugs]$ gcc -B./ -O2 -g0 -o ar -Wl,--as-needed arparse.o arlex.o ar.o not-ranlib.o arsup.o rename.o binemul.o emul_vanilla.o bucomm.o version.o filemode.o libbfd-2.35-3.fc33.so libiberty.a -Wl,-R,. [hjl@gnu-cfl-2 gcc-bugs]$ readelf --dyn-syms -rW ar | grep bfd_scan_vma [hjl@gnu-cfl-2 gcc-bugs]$ Here is the patch to remove these symbols from dynamic symbol table.
[hjl@gnu-cfl-2 pr26324]$ cat pr15146a.c extern int xxx; int bar (void) { return xxx; } int main () { return 0; } [hjl@gnu-cfl-2 pr26324]$ cat pr15146b.c int xxx = 3; [hjl@gnu-cfl-2 pr26324]$ cat pr15146c.c [hjl@gnu-cfl-2 pr26324]$ make gcc -B./ -O2 -g -o x -Wl,-R,. \ -Wl,--no-copy-dt-needed-entries -Wl,--no-as-needed pr15146a.o pr15146c.so ./ld: /tmp/ccHirkTe.debug.temp.o: undefined reference to symbol 'xxx' ./ld: ./pr15146b.so: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status make: *** [Makefile:15: x] Error 1 [hjl@gnu-cfl-2 pr26324]$