when linking an elf application or share library, I want to choose which symbols to export. By default, when linking an application no function symbol is exported and when linking a shared library all function symbols are exported. Is there any way to control which symbols to export? When linking an application, I can use -rdynamic
or -Wl,--export-dynamic
to get all symbols, and I can use -Wl,--dynamic-list <symfile>
to get only some symbols. However when linking a library, are those options ignored?
0
votes
Possible duplicate of Limiting visibility of symbols when linking shared libraries
– Employed Russian
2 Answers
3
votes
Found out after testing a little:
for ELF applications, you can use
-rdynamic
or-Wl,--export-dynamic
to export all symbols, or you can use-Wl,--dynamic-list <sym-file>
to export only some symbols when linking your application throughgcc
.for ELF libraries, you can't use
-rdynamic
,-Wl,--export-dynamic
or-Wl,--dynamic-list <symfile>
, you must use-Wl,--version-script=<verfile>
when linking your library throughgcc
.
The version-script and the sym-file are almost the same, except that for sym-file you do not code a version and a scope. Documentation: gnu ld
2
votes
Is there any way to control which symbols to export?
The usual way to control symbol visibility in shared library is either
- Use linker script, as described here, or
- Use
__attribute__((visibility("default")))
on symbols you explicitly want to export and build with-fvisibility=hidden
(which will hide everything else).