返回共享库符号表 [英] Returning a shared library symbol table

查看:151
本文介绍了返回共享库符号表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

  void * sdl_library = dlopen(libSDL.so,RTLD_LAZY); 
void * initializer = dlsym(sdl_library,SDL_Init);

假设没有错误,初始化程序将指向共享库libSDK.so中的SD_Init函数。 p>

但是,这需要知道符号SDL_Init存在。



是否可能查询库的所有符号?例如,在这种情况下,它将返回SDL_Init,函数指针和由libSDL.so导出的任何其他符号。

解决方案

是没有libc的功能来做到这一点。但是,您可以自己编写一个代码(尽管代码有些涉及)。



在Linux上, dlopen()实际上返回一个 link_map 结构的地址,其中有一个名为 l_addr 的成员指向加载共享对象(假设您的系统不会随机分配共享库位置,并且您的库未被预先链接)。



在Linux上,一个确定的方法来找到基础地址( Elf * _Ehdr 的地址)将使用 dl_iterate_phdr() dlopen( )

拥有ELF标题后,您应该能够遍历导出符号列表(动态符号表) ,首先找到 PT_DYNAMIC 类型的 Elf * _Phdr ,然后找到 DT_SYMTAB DT_STRTAB 条目,并迭代动态符号表中的所有符号。使用 /usr/include/elf.h 来指导您。



此外,您可以使用 libelf ,但由于我以前没有经验,我无法指导您。 p>

最后注意到,这个练习有点徒劳:你会得到一个定义的函数的列表,但是你不知道如何调用它们(他们期待什么参数)那么有什么意义?


For instance:

void* sdl_library = dlopen("libSDL.so", RTLD_LAZY);
void* initializer = dlsym(sdl_library,"SDL_Init");

Assuming no errors, initializer will point to the function SD_Init in the shared library libSDK.so.

However this requires knowing the symbol "SDL_Init" exists.

Is it possibly to query a library for all its symbols? Eg, in this case it would return SDL_Init, the function pointer, and any other symbols exported by libSDL.so.

解决方案

There is no libc function to do that. However, you can write one yourself (though the code is somewhat involved).

On Linux, dlopen() in fact returns the address of a link_map structure, which has a member named l_addr that points to the base address of the loaded shared object (assuming your system doesn't randomize shared library placement, and that your library has not been prelinked).

On Linux, a sure way to find the base address (the address of Elf*_Ehdr) is to use dl_iterate_phdr() after dlopen()ing the library.

Having the ELF header, you should be able to iterate over a list of exported symbols (the dynamic symbol table), by first locating the Elf*_Phdr of type PT_DYNAMIC, and then locating DT_SYMTAB, DT_STRTAB entries, and iterating over all symbols in the dynamic symbol table. Use /usr/include/elf.h to guide you.

Additionally, you could use libelf, but I'm unable to guide you since I don't have previous experience with it.

Finally note that the exercise is somewhat futile: you'll get a list of defined functions, but you'll have no idea how to call them (what parameters they expect), so what's the point?

这篇关于返回共享库符号表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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