Returning a shared library symbol table
Asked Answered
M

5

20

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.

Marathon answered 22/4, 2010 at 20:38 Comment(0)
G
23

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?

Groundage answered 22/4, 2010 at 20:50 Comment(0)
D
2

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)

Dubious answered 22/4, 2010 at 20:50 Comment(0)
D
1

The Boost.DLL offers this functionality through the library_info::symbols function. Adapted from the tutorial on Querying libraries for symbols:

// Class `library_info` can extract information from a library
boost::dll::library_info inf(libpath);

// Getting exported symbols
std::vector<std::string> exports = inf.symbols();

// Printing symbols
for (std::size_t j = 0; j < exports.size(); ++j) {
    std::cout << exports[j] << std::endl;
}

Note that this only works for the symbols that nm lists without the --dynamic flag, i.e., those in the .symtab section. It seems like some libraries do not export any symbols in that section. I have opened a feature request to support falling back to the .dynsym section in that case.

Domella answered 29/6, 2020 at 17:0 Comment(0)
H
-3
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.

Homogenesis answered 22/4, 2010 at 20:43 Comment(1)
It sounds to me like the OP is looking to get a list of exported symbols from the library. Which I don't think is possible without doing some parsing of sections in the ELF structure.Waits
R
-3

The linux nm command could be used: http://man.yolinux.com/cgi-bin/man2html?cgi_command=nm

Rippy answered 24/2, 2015 at 18:2 Comment(1)
The question appears to be looking for a programatic means of querying the symbol table of a shared library. nm is a command line utility whose text output format varies by platform.Backslide

© 2022 - 2024 — McMap. All rights reserved.