手动指定特定链接符号的重新映射 [英] Manually specify remapping of specific linking symbols
问题描述
是否无需修改这两个源文件,是否有办法获取通过编译它们而生成的目标文件,并说服链接程序将main_v1.c中的foo
链接到bar.c中的bar
?
Without modifying these two source files, is there a way to take the object files produced by compiling them, and convincing a linker to link foo
in main_v1.c to bar
in bar.c?
main_v1.c
void foo(void);
int main(void)
{
foo();
}
bar.c
#include <stdio.h>
void bar(void)
{
puts("bar()!");
}
修改目标文件本身是一个公平的游戏,但是假设我们甚至可能没有可用的源代码.该平台是Linux.
Modifying the object files themselves is fair game but assume that we might not even have the source code available. The platform is Linux.
通过坚持对main_v1.c进行适度更改,并链接到其他映射"目标文件,这是一种仅使用标准C即可几乎获得所需结果的方法.
By insisting on a modest change to main_v1.c, and linking in an additional "mapping" object file, here's a way to almost get the desired result with just standard C.
main_v2.c
extern void (*const foo)(void);
int main(void)
{
foo();
}
bar.c 不变.
map.c
void bar(void);
void (*const foo)(void) = bar;
如果目标文件是使用lto编译的,则甚至消除了函数指针取消引用(使用最新的gcc).这是一个相当不错的结果,但是如果将main()
修改为可以直接调用bar()
,则在链接后会直接内嵌bar()
本身,因此还有改进的余地.
If the object files are compiled with lto, the function pointer dereference is even elided (using a recent gcc). This is a pretty good result but if main()
is modified to have a direct call to bar()
, then bar()
itself is inlined after linking so there's room for improvement.
推荐答案
This would be a job for the GNU ld
--wrap
option
我们假设您已经编译了main_v1.o
,并且可能与您的
main_v1.c
,同样是根据您的bar.c
编译而成的bar.o
.
You have, we assume, a main_v1.o
compiled and perhaps unchangeable from your
main_v1.c
and likewise a bar.o
compiled from your bar.c
.
现在编写另一个源文件:
Now write another source file:
wrap.c
extern void bar(void);
void __wrap_foo(void)
{
bar();
}
将其编译为wrap.o
并将其与先前的目标文件链接,如下所示:
Compile it to wrap.o
and link it with the prior object files like so:
$ gcc -c wrap.c
$ gcc -o prog -Wl,--wrap=foo main_v1.o bar.o wrap.o
然后:
$ ./prog
bar()!
这篇关于手动指定特定链接符号的重新映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!