I know how to use dlsym()
to find symbols keyed by a string - when these symbols are exported by a shared library which I've dlopen()
ed. But - what about other code? Just object code I've linked statically. Is it possible to somehow lookup symbols?
Notes:
You can indeed just use dlsym()
for that purpose.. You just have to export all symbols to the dynamic symbol table. Link the binary with gcc -rdynamic
for that.
Example:
#include <stdio.h>
#include <dlfcn.h>
void foo (void) {
puts("foo");
}
int main (void) {
void (*foo)(void) = dlsym(NULL, "foo");
foo();
return 0;
}
Compile with: gcc -rdynamic -O2 dl.c -o dl -ldl
$ ./dl
foo
$