我怎样才能从共享对象中删除一个符号? [英] How can I remove a symbol from a shared object?

查看:99
本文介绍了我怎样才能从共享对象中删除一个符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用GCC,我怎样才能从共享对象中删除一个符号我创建了共享对象后?如果我必须用C操纵符号三个文件富()这样的:

  //交流转换器
INT富(){返回0xdead; }
INT巴兹(){返回1; }

  // b.c
INT富(){返回0xbeef; }
INT栏(){返回0; }

  // C.C
的#includestdio.h中
EXTERN INT富();
EXTERN INT巴();
EXTERN INT巴兹();
INT的main(){printf的(0X%X,为0x%X,0X%X \\ n,富(),酒吧(),巴兹());返回0; }

然后我编译和运行,如:

 %GCC交流转换器--shared -fPIC -o a.so
GCC%--shared b.c -fPIC -o b.so
%SETENV LD_LIBRARY_PATH。 #出口LD_LIBRARY_PATH =。对于bash系统
GCC%C.C a.so b.so -oÇ
% 。/C
0xdead,为0x0,为0x1

我怎样才能让这个 a.so 不再有符号富()我已经后创建 a.so ?我想在富() b.so 定义为用来代替 A。所以通过删除富()从符号 a.so 。后富() a.so 删除,重新运行 C 应该产生的打印输出:

  0xbeef,为0x0,为0x1

在这个玩具的例子,我知道我可以简单地重新排序libary名字当我编译 CC a.so b.so ,但我怎么能真正删除 a.so ?我想象,删除后富() a.so ,这对的grep的nm 输出会产生什么:

 纳米-a a.so | grep的富

而现在它返回:

  000000000000063c牛逼富


解决方案

您应该可以使用 -N - 带钢 objcopy把符号)选项来实现你想要的:

  $ objcopy把-N富a.so

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:

// 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:

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 nm output would yield nothing:

nm -a a.so | grep foo

Whereas right now it returns:

000000000000063c T foo

解决方案

You should be able to use the -N (--strip-symbol) option of objcopy to achieve what you want:

$ objcopy -N foo a.so

这篇关于我怎样才能从共享对象中删除一个符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆