在OS X上进行链接时包装符号 [英] Wrapping symbols during linking on OS X

查看:128
本文介绍了在OS X上进行链接时包装符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在链接期间用另一个符号包裹一个符号。据我所知这很容易用ld --wrap选项完成,但在OS X上它不可用。有'-idefinition:indirection',这里我怎么骗主,所以它会打印42:



a.cpp:

  int foo(){
return 1;
}



b.cpp:

  int wrap_foo(){
return 42;
}

main.cpp:

  #include  
int foo();
int wrap_foo();

int main(){
printf(%d\\\
,foo());
}

我如何构建和链接它们:

  $ gcc -c * .o 
$ gcc -Wl,-i__Z3foov:__ Z8wrap_foov * .o
重复符号__Z3foov:
ao
bo
ld:体系结构x86_64的重复符号

甚至可能实现我想要的?




编辑:
在我的任务中,我无法控制a.cpp和main.cpp(只有它们的对象),但是我可以编写任何代码并在链接前用对象进行任何操作。



我已经发现以下quistion之前有关类似的任务(并描述它更好)
GNU gcc / ld - 在调用者和被调用者定义在同一个目标文件中的情况下调用符号
重新读取答案我看到案件是一样的,象征已经存在,我会有一个碰撞。



如何在不触及源代码的情况下从ao中移除foo?

解决方案

这是否必须在链接与运行时完成?注意完全确定你想要做什么,所以只是问。



使用该链接器命令的问题是它创建一个新的符号,这就是为什么你会得到

  -alias symbol_name alternate_symbol_name 
为符号
symbol_name创建一个名为alternate_symbol_name的别名。默认情况下,别名符号具有全局可见性 -
ity。此选项在-idef:indir选项之前。

如果您没有定义foo的a.cpp(即您正在省略foo),你可以做这样的事情:

  tmp $ gcc -Wl,-alias,__ Z8wrap_foov,__ Z3foov bo main.o 
tmp $ ./a.out
42

你总是可以做的是.cpp有另一个 foo ,例如 original_foo 。这样,如果你想实现,你可以使用别名映射到 foo 。不完美,但它会达到你想要的。你已经在通过链接调整它的努力,所以它似乎可能被接受。


I'm trying to wrap one symbol by another during link. As I understand this is easily done with ld --wrap option, but on OS X it's not available. There is '-idefinition:indirection', here how I'm trying to cheat main so it will print 42:

a.cpp:

int foo() {
    return 1;
}

b.cpp:

int wrap_foo() {
    return 42;
}

main.cpp:

#include <cstdio>

int foo();
int wrap_foo();

int main() {
    printf("%d\n", foo());
}

How I build and link them:

$ gcc -c *.o
$ gcc -Wl,-i__Z3foov:__Z8wrap_foov *.o
duplicate symbol __Z3foov in:
    a.o
    b.o
ld: 1 duplicate symbol for architecture x86_64

Is it even possible to achieve what I want?


EDIT: In my task I don't have control over a.cpp and main.cpp (have only objects for them), but I can write any code and do whatever I need with the objects before link.

I've found following quistion before which related to similar task (and describes it better) GNU gcc/ld - wrapping a call to symbol with caller and callee defined in the same object file Rereading the answers I see that the case is same, symbol already present and I will have a collision.

How to remove foo from a.o without touching source code?

解决方案

Does this have to be done at link versus runtime? Note entirely sure what you're trying to do, so just asking.

The problem with using that linker command is that it creates a new symbol, which is why you get the duplicate symbol.

-alias symbol_name alternate_symbol_name
                 Create an alias named alternate_symbol_name for the symbol
                 symbol_name.  By default the alias symbol has global visibil-
                 ity.  This option was previous the -idef:indir option.

If you don't have a.cpp which defines foo (ie. you are omitting foo), you can do something like this:

tmp$ gcc -Wl,-alias,__Z8wrap_foov,__Z3foov b.o main.o
tmp$ ./a.out
42

What you could always do is have a.cpp have another foo like original_foo. This way if you wanted that implementation, you could use alias to map that to foo. Not perfect, but it would achieve roughly what you want. You're already going through the effort of adjusting it at link, so it would seem potentially acceptable.

这篇关于在OS X上进行链接时包装符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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