2011-11-01 48 views
3

我必须从加载了LD_PRELOAD的共享库中调用主要可执行文件中的函数。从共享对象调用主要可执行文件中的函数

可执行文件导出所有符号并包含调试信息。不幸的是,我无法访问它的源代码。

当试图加载该共享库时,我收到未定义的符号错误。 有没有办法做到这一点?

PS: 目标平台是FreeBSD/x86。

+0

可能重复的[如何从我的库中调用可执行文件中的函数?](http://stackoverflow.com/questions/6292473/how-to-call-function-in-executable-from-my-library) – ninjalj

+0

否,我的问题有点不同,因为我没有主要的可执行文件的来源。 – agrudzinski

回答

2

你可以做一个typedef创建一个函数指针,并使用“的dlsym()”来获得符号的地址 。然后,您可以通过函数指针调用该函数,如正常的 函数调用。注意:您不需要dlopen(),因为带有符号导出的主可执行文件 已加载到进程地址空间中。

原型:

void *dlsym(void *handle, const char *symbol); 

假设导出的功能是:

int foo(char *arg); 

你的函数指针:

typedef (int)(*fooPtr)(char *); 

在你的代码:

/* You can send NULL for first argument */ 
fooPtr fp = dlsym(NULL, "foo"); 
assert(0 != fp); 
int ret = fp("hello world"); 
0
gcc -Wl,--export-dynamic 

...应该做的伎俩。

Documentation on --export-dynamic

+0

正如我所说我没有访问主要可执行文件的来源。 – agrudzinski

+0

然后我觉得你运气不好。您只能引用导出的符号。 – Nemo

+0

嗯,没有办法生成动态导出表? IDA向我展示了所有导出的函数+可执行文件被编译为调试信息 – agrudzinski