Linux 如何从共享对象中删除符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9909528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I remove a symbol from a shared object?
提问by Ross Rogers
Using GCC, how can I remove a symbol from a shared object after I've created the shared object? If I have three files in C manipulating symbol foo()
like:
使用 GCC,如何在创建共享对象后从共享对象中删除符号?如果我在 C 操作符号中有三个文件,foo()
例如:
// a.c
int foo() { return 0xdead; }
int baz() { return 1; }
and
和
// b.c
int foo() { return 0xbeef; }
int bar() { return 0; }
and
和
// c.c
#include "stdio.h"
extern int foo();
extern int bar();
extern int baz();
int main() { printf("0x%x, 0x%x, 0x%x\n",foo(),bar(),baz()); return 0; }
Then I compile and run like:
然后我编译并运行:
% gcc a.c --shared -fPIC -o a.so
% gcc b.c --shared -fPIC -o b.so
% setenv LD_LIBRARY_PATH . # export LD_LIBRARY_PATH=. for bash systems
% gcc c.c a.so b.so -o c
% ./c
0xdead, 0x0, 0x1
How can I make it so that a.so
no longer has symbol foo()
after I've created a.so
? I want the foo()
defined in b.so
to be used instead of a.so
by deleting the foo()
symbol from a.so
. After foo()
is deleted from a.so
, rerunning c
should generate a printout of:
我怎样才能让它在我创建后a.so
不再有符号?我希望使用定义的 in而不是从 中删除符号。从中删除后,重新运行应生成以下打印输出:foo()
a.so
foo()
b.so
a.so
foo()
a.so
foo()
a.so
c
0xbeef, 0x0, 0x1
In this toy example, I know I can simply re-order the libary names when I compile c.c
with a.so
and b.so
, but how can I actually delete the symbol from a.so
? I imagine that after deleting foo()
from a.so
, this grep of the nmoutput would yield nothing:
在这个玩具示例中,我知道我可以在c.c
使用a.so
and编译时简单地重新排序库名称b.so
,但是我如何真正从 中删除符号a.so
?我想在删除foo()
from 之后a.so
,nm输出的这个 grep不会产生任何结果:
nm -a a.so | grep foo
Whereas right now it returns:
而现在它返回:
000000000000063c T foo
采纳答案by Frédéric Hamidi
回答by j?rgensen
Symbol visibility seems like a superior solution to select which functions are up for runtime linking and which are local to the library but still "extern" declared to allow usage from other .c files that comprise the library - http://gcc.gnu.org/wiki/Visibility.
符号可见性似乎是选择哪些函数可用于运行时链接以及哪些函数是库本地的但仍声明为允许从包含库的其他 .c 文件使用的“extern”的高级解决方案 - http://gcc.gnu。组织/维基/可见性。