如何从共享对象中删除符号?

Ros*_*ers 10 c linux gcc shared-libraries

使用GCC,如何在创建共享对象后从共享对象中删除符号?如果我在C中有三个文件操纵符号,foo()如:

// a.c
int foo() { return 0xdead; }
int baz() { return 1; }
Run Code Online (Sandbox Code Playgroud)

// b.c
int foo() { return 0xbeef; }
int bar() { return 0; }
Run Code Online (Sandbox Code Playgroud)

// 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; }
Run Code Online (Sandbox Code Playgroud)

然后我编译并运行如下:

% 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
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它在我创造之后a.so不再有符号?我希望使用定义的in 而不是删除符号.之后从删除,重新运行应该产生的打印输出:foo()a.sofoo()b.soa.so foo()a.sofoo()a.soc

0xbeef, 0x0, 0x1
Run Code Online (Sandbox Code Playgroud)

在这个玩具示例中,我知道我可以简单地在c.c使用a.so和编译时重新排序库名b.so,但是我怎样才能真正删除符号a.so呢?我想,删除之后foo()a.so,这将grep的的纳米输出会产生什么:

nm -a a.so | grep foo
Run Code Online (Sandbox Code Playgroud)

现在它返回:

000000000000063c T foo
Run Code Online (Sandbox Code Playgroud)

Fré*_*idi 14

您应该能够使用objcopy-N(--strip-symbol)选项来实现您想要的:

$ objcopy -N foo a.so
Run Code Online (Sandbox Code Playgroud)