<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/91055>91055</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`--retain-symbols-file` is not compatible with GNU
</td>
</tr>
<tr>
<th>Labels</th>
<td>
lld:ELF
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
rui314
</td>
</tr>
</table>
<pre>
It looks like lld uses `--retain-symbols-file` to control whether a symbol is put into .dynsym or not. However, for GNU ld, the option doesn't seem to have any effect on .dynsym. Instead, it controls whether a symbol is put into the output .symtab or not.
Here is a test script:
```shell
cat <<EOF | cc -c -o a.o -xc -
void foo() {}
void bar() {}
void baz() {}
EOF
cat <<EOF > symbols
foo
baz
EOF
cc -shared -o b.so a.o -Wl,--retain-symbols-file=symbols
nm b.so
readelf -W --dyn-syms b.so
cc -fuse-ld=lld -shared -o c.so a.o -Wl,--retain-symbols-file=symbols
nm c.so
readelf -W --dyn-syms c.so
```
You can see that the symbols in .dynsym and .symtab are different between lld and GNU ld.
```
$ bash -x /tmp/foo
+ cat
+ cc -c -o a.o -xc -
+ cat
+ cc -shared -o b.so a.o -Wl,--retain-symbols-file=symbols
+ nm b.so
000000000000110f T baz
00000000000010f9 T foo
+ readelf -W --dyn-syms b.so
Symbol table '.dynsym' contains 8 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __cxa_finalize
2: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_registerTMCloneTable
3: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTable
4: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
5: 00000000000010f9 11 FUNC GLOBAL DEFAULT 10 foo
6: 0000000000001104 11 FUNC GLOBAL DEFAULT 10 bar
7: 000000000000110f 11 FUNC GLOBAL DEFAULT 10 baz
+ cc -fuse-ld=lld -shared -o c.so a.o -Wl,--retain-symbols-file=symbols
+ nm c.so
0000000000001674 t bar
000000000000167f T baz
0000000000003888 b completed.0
w __cxa_finalize
00000000000015b0 t deregister_tm_clones
0000000000001620 t __do_global_dtors_aux
00000000000026f0 d __do_global_dtors_aux_fini_array_entry
0000000000003860 d __dso_handle
0000000000002700 d _DYNAMIC
00000000000016a8 t _fini
0000000000001669 T foo
0000000000001660 t frame_dummy
00000000000026f8 d __frame_dummy_init_array_entry
0000000000000530 r __FRAME_END__
0000000000003868 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
000000000000168c t _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
00000000000015e0 t register_tm_clones
0000000000003868 d __TMC_END__
0000000000003868 d __TMC_LIST__
+ readelf -W --dyn-syms c.so
Symbol table '.dynsym' contains 7 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
2: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTable
3: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_registerTMCloneTable
4: 0000000000000000 0 FUNC WEAK DEFAULT UND __cxa_finalize
5: 0000000000001669 11 FUNC GLOBAL DEFAULT 10 foo
6: 000000000000167f 11 FUNC GLOBAL DEFAULT 10 baz
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV19P474S_TTuyyiV66Rp-9CH0ja76EK5upS72qfISSaN7zpxZTtA99NfOf2XQlhY-EUIYmZ8fMY-MxNzY8SmQpyS4RUZLnq8toXSU10LfxD0EpXtptcWpFK_DEjxC0HKDGqDBkhIPU-j5aLyzK5MlDReLiSSkIJVkKrKaiXhqUBboAYOeycQBra1BVFZBf1sV5ldCUpDpWwfvqsnfERN2BxypeHb6gFk5ka2QFBbK1QFmUJTETayYBBLt1TBH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