Bug 26324 - Unused symbols in dynamic symbol table
Summary: Unused symbols in dynamic symbol table
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.36
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-07-31 11:41 UTC by H.J. Lu
Modified: 2020-07-31 15:26 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
A patch (2.12 KB, patch)
2020-07-31 11:41 UTC, H.J. Lu
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2020-07-31 11:41:28 UTC
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.
Comment 1 H.J. Lu 2020-07-31 15:25:38 UTC
[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]$