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 alink_mapstructure, which has a member namedl_addrthat 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 usedl_iterate_phdr()afterdlopen()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*_Phdrof typePT_DYNAMIC, and then locatingDT_SYMTAB,DT_STRTABentries, and iterating over all symbols in the dynamic symbol table. Use/usr/include/elf.hto 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?