版本脚本和隐藏的可见性 [英] version-script and hidden visibility
问题描述
使用gcc构建共享库时,可以使用-fvisibility = hidden来限制符号的可见性。我也刚刚知道你可以使用version-script选项来限制可视性。
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;
}
我编译这个:
gcc -fvisibility = hidden - Wl, - version-script = test.c -shared -o libtest.so
And I compile this with: gcc -fvisibility=hidden -Wl,--version-script= test.c -shared -o libtest.so
当我在此之后运行nm时,发现没有符号被导出。有无论如何,我可以设置隐藏的默认可见性,并使用版本脚本(或其他)来导出符号?
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?
推荐答案
你的问题没有意义:为什么要用链接器脚本与 -fvisibility
作斗争,当你可以使用链接器脚本正确地导出 时,你需要,并隐藏其他一切:
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:
因为我需要使用它的代码使用
__属性__((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
这篇关于版本脚本和隐藏的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!