我正在尝试包装 GLIBCfstat
通过将我的库注入可执行文件来实现函数(可以是任何其他:这只是概念证明)。我通过将我的库放置在可执行文件的位置来做到这一点RPATH
用名字指着libc.so.6
.
我的库的源代码如下:
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dlfcn.h>
int fstat(int fd, struct stat *buf){
typeof(fstat) *old_fstat;
// Try with a printf...
printf("HOOT! fstat wrapped!");
old_fstat = dlsym(RTLD_NEXT, "fstat");
return (*old_fstat)(fd, buf);
}
我用它编译--version-script
(添加符号版本控制)并将其静态链接到 libgcc。
gcc -Wall -fPIC -c -o wrapperlib.o wrapperlib.c
gcc -shared -static-libgcc -fPIC -Wl,-soname -Wl,libc.so.6 -Wl,--version-script=wrapperlib.map,-Bstatic -o libc.so.6 wrapperlib.o
With wrapperlib.map
包含:
GLIBC_2.0 {
};
当我执行目标程序时,我的库被加载,但出现以下错误:
./target: relocation error: ./target: symbol __libc_start_main, version GLIBC_2.0 not defined in file libc.so.6 with link time reference
如果我提供我自己的实现__libc_start_main
我没有收到此错误(当然,它会崩溃)。
int __libc_start_main(int (*main) (int, char **, char **), int argc, char *argv, void (*init) (void), void (*fini) (void), void (*rtld_fini) (void), void *stack_end) {
system("echo I am in __libc_start_main");
printf("printf: I am in __libc_start_main");
fflush(stdin);
return 0;
}
为什么会出现重定位错误__libc_start_main
并且不在system
?
(顺便说一下,调用system
确实有效,但致电printf
不产生输出。我在这里遗漏了一些明显的东西吗?)
谢谢
编辑:
跑步目标与LD_DEBUG=all
输出这个:http://pastebin.com/iVVbwf6n http://pastebin.com/iVVbwf6n