Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using dladdr in release

Tags:

c++

c

xcode

gcc

clang

Im writing a profiling tool for my App and im using dladdr to convert function pointer to name. Everything work fine as long as Im in debug but as soon as I compile in release (using XCode 5.1) all name conversion fail and return NULL ex:

#include <dlfcn.h>
int main( int argc, char **argv )
{
    Dl_info info;

    if( dladdr( main, &info ) != 0 )
    { fprintf( stderr, "%s\n", info.dli_sname ); }

I tried multiple compiler flags that I pass to the C/C++ Flags: -export-dynamic -fPIC as well as -Wl,--export-dynamic nothing works... Is there a way to still compile with optimization and retain the function address -> name translation functionalities of dladdr?

like image 644
McBob Avatar asked Sep 19 '14 02:09

McBob


1 Answers

clang and gcc support the -rdynamic option as a linker option. This should provide the ability for dladdr to function as expected in release mode. -rdynamic is defined as:

-rdynamic Pass the flag -export-dynamic to the ELF linker, on targets that support it. This instructs the linker to add all symbols, not only used ones, to the dynamic symbol table. This option is needed for some uses of dlopen or to allow obtaining backtraces from within a program.

More information on gcc linker options can be found here

like image 138
Michael Petch Avatar answered Oct 18 '22 02:10

Michael Petch