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

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

问题描述

例如:

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

如果没有错误,初始化将指向功能SD_Init共享库libSDK.so。

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

然而,这需要了解符号SDL_Init的存在。

However this requires knowing the symbol "SDL_Init" exists.

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

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.

推荐答案

有没有libc的函数来做到这一点。然而,你可以自己写一个(虽然code是较为复杂)。

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

在Linux中,的dlopen()实际上返回 link_map 结构的地址,其中有一个名为成员 l_addr ,它指向加载的共享对象的基地址(假设您的系统没有随机共享库的位置,并且您的库尚未prelinked)。

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).

在Linux中,一个肯定的方式找到基地址(精灵* _Ehdr 的地址)是使用 dl_iterate_phdr()的dlopen() ING库。

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.

有ELF头,你应该能够遍历导出的符号(动态符号表)的列表,首先定位类型的精灵* _Phdr PT_DYNAMIC ,然后定位 DT_SYMTAB DT_STRTAB 项,并遍历过在动态符号表中的所有符号。使用 /usr/include/elf.h 来指导你。

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.

此外,你可以使用 libelf的,但是我不能指导你,因为我没有与之previous经验。

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天全站免登陆