Returning a shared library symbol table

2019-02-06 10:47发布

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.

4条回答
该账号已被封号
2楼-- · 2019-02-06 10:54

I don't think there is a published API for this. You can either use the nm tool from binutils or examine its source code: http://sourceware.org/cgi-bin/cvsweb.cgi/src/binutils/?cvsroot=src

http://sourceware.org/cgi-bin/cvsweb.cgi/src/binutils/nm.c?rev=1.63&content-type=text/x-cvsweb-markup&cvsroot=src

(obviously assuming elf)

查看更多
forever°为你锁心
3楼-- · 2019-02-06 10:55

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?

查看更多
地球回转人心会变
4楼-- · 2019-02-06 10:57
void *dlsym(void *restrict handle, const char *restrict name);

Return Value

If handle does not refer to a valid object opened by dlopen(), or if the named symbol cannot be found within any of the objects associated with handle, dlsym() shall return NULL. More detailed diagnostic information shall be available through dlerror().

( Source: http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html )

In other words, if the symbol isn't found, dlsym() will return NULL. Not sure if that's what you're looking for, but that is the simplest way I can find.

查看更多
别忘想泡老子
5楼-- · 2019-02-06 11:04
登录 后发表回答