When using gcc
to build a shared library, it's possible to limit the visibility of the symbols using -fvisibility=hidden
. I also just learned you can limit visibility using the version-script option to ld
.
Now I want to know if it's possible to combine these. Say I have a program with the following:
void foobar() {}
void say_hello() {}
Then I have the version script file with:
{
global:
foobar;
}
And I compile this with:
gcc -fvisibility=hidden -Wl,--version-script=<version-script> test.c -shared -o libtest.so
When I run nm
on this afterwards, I find that no symbols are exported. Is there anyway that I can set the default visibility to hidden and use the version-script (or something else) to export symbols?
Usually __attribute__((visibility("default"))) is used in combination with -fvisibility=hidden linker flag. The latter would make "hidden" the default visibility for all symbols of a shared library. Back to the original example from folly, class FOLLY_EXPORT OptionalEmptyException : public std::runtime_error { ... 12.
A script version is a draft of a script in which certain portions have been re-written. These versions are counted after a script has been "locked" for budgeting and scheduling purposes. The changes between Version 1 and Version 2, for example, will marked by different colored pages (e.g., blue).
Parts of LLVM are compiled with -fvisibility=hidden . This option forces the default visibility of all symbols to be hidden , which prevents them from being visible across library boundaries.
Your question makes no sense: why fight -fvisibility
with a linker script, when you can use the linker script to export exactly what you need, and hide everything else:
{
global: foobar;
local: *;
};
Update:
Because the code I need to use this on uses
__attribute__((visibility("default")))
...
The linker script works just fine with symbols so marked. Example:
// t.c
int __attribute__((visibility("default"))) foo() { return 1; }
int bar() { return 2; }
int __attribute__((visibility("default"))) exported() { return 3; }
// t.lds
{
global: exported;
local: *;
};
gcc t.c -Wl,--version-script=t.lds -fPIC -shared -o t.so && nm -D t.so
w _Jv_RegisterClasses
w __cxa_finalize
w __gmon_start__
00000000000004f2 T exported
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With