Bug 28196

Summary: gold: if .symtab order is: __free_hook, __free_hook@GLIBC_2.2.5, version script incorrectly makes the symbol default versioned
Product: binutils Reporter: Fangrui Song <i>
Component: goldAssignee: Cary Coutant <ccoutant>
Status: UNCONFIRMED ---    
Severity: normal CC: ian
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description Fangrui Song 2021-08-05 18:03:37 UTC
cat > ./a.s <<eof
.symver __free_hook, __free_hook@GLIBC_2.2.5
.weak __free_hook
__free_hook:
  nop
  call __free_hook
eof
echo 'GLIBC_2.2.5 { __free_hook; };' > ./a.ver
as a.s -o a.o
gold -shared --version-script=a.ver a.o -o a.so
gold -r a.o -o b.o
gold -shared --version-script=a.ver b.o -o b.so
exit

In b.o's .symtab, __free_hook@GLIBC_2.2.5 is before __free_hook.
gold can incorrectly make __free_hook default versioned.

% readelf -W --dyn-syms a.so | grep free_hook
     5: 00000000000002c8     0 NOTYPE  WEAK   DEFAULT    7 __free_hook@GLIBC_2.2.5
% readelf -W --dyn-syms b.so | grep free_hook
     5: 00000000000002c8     0 NOTYPE  WEAK   DEFAULT    7 __free_hook@@GLIBC_2.2.5

% readelf -Ws a.o
...
     1: 0000000000000000     0 NOTYPE  WEAK   DEFAULT    1 __free_hook
     2: 0000000000000000     0 NOTYPE  WEAK   DEFAULT    1 __free_hook@GLIBC_2.2.5
% readelf -Ws b.o
...
     1: 0000000000000000     0 NOTYPE  WEAK   DEFAULT    1 __free_hook@GLIBC_2.2.5
     2: 0000000000000000     0 NOTYPE  WEAK   DEFAULT    1 __free_hook

(
Aside:
`.symver foo, foo@v1, remove` is created which can make this problem moot.

I repeated many times in https://sourceware.org/bugzilla/show_bug.cgi?id=25295
and some other threads, `, remove` should just be the default behavior of `.symver foo, foo@v1` when foo is defined.
.symver should just behave like "rename".
I guess probably only glibc may be unhappy with the default `, remove`.
The fix is probably not too difficult.

Of course, gas should be fixed first to make relocation symbols versioned: PR gas/28157
)