Hank Fu
Hank Fu

Reputation: 43

How to get address of function or global variable from its name in executable elf file for C?

How to get address of function or global variable from its name in executable elf file? dlsym() can get address of function in dynamic library file. But it does not work for executable elf file. Before I parse the ELF file ,I want to check whether there is one library that provide same function for executable elf file? Then I can get address of function or global variable from its name.

Upvotes: 2

Views: 772

Answers (1)

Florian Weimer
Florian Weimer

Reputation: 33757

If you compile the executable file with -Wl,-E (or it's various variants such as -export-dynamic), then the linker will produce a dynamic section for the executable, and you can access its symbols with dlsym and a NULL handle, e.g., dlsym (NULL, "foo").

Upvotes: 1

Related Questions