About dladdr function.

By | April 14, 2021

dladdr function from Unix-like OS looks similar as GetProcAddress Win32 API function. However dladdr obtains more information related to requested function than function entry point address as GetProcAddress does. Besides GetProcAddress requires the handle of the DLL module that contains the requested function, dladdr needs only the function name. Definitely in both cases DLL or shared library should be already loaded by running process. Below is example how
dladdr returns information about sysctl function (the example was compiled and tested on Mac OSX):


#include <stdio.h>
#include <sys/sysctl.h>
#include <dlfcn.h>
int main(int n, char ** s)
{
   Dl_info info;
   if(dladdr((void*)sysctl, &info))
   {
      printf("Function name: %s (Entry point address: 0x%llx)\n",
         info.dli_sname, (long long)info.dli_saddr);
      printf("From library: %s (Base address: 0x%llx)\n",
   ;   ;   info.dli_fname, (long long)info.dli_fbase);
   }
   return 0;
}

Besides function entry point dladdr retrieves information about shared library which requested function belongs to:


# g++ -g -o fbyaddress fbyaddress.cpp
# ./fbyaddress
Function name: sysctl (Entry point address: 0x7fff7ed47fdc)
From library: /usr/lib/system/libsystem_c.dylib (Base address: 0x7fff7ed1b000)

Leave a Reply

Your email address will not be published. Required fields are marked *