Search code examples
cdynamic-linkingdladdr

Memory semantics of dladdr() out parameter


What are the memory semantics of dladdr()?

#define _GNU_SOURCE
#include <dlfcn.h>

int dladdr(void *addr, Dl_info *info);

typedef struct {
    const char *dli_fname;  /* Pathname of shared object that contains address */
    void       *dli_fbase;  /* Base address at which shared object is loaded */
    const char *dli_sname;  /* Name of symbol whose definition overlaps addr */
    void       *dli_saddr;  /* Exact address of symbol named in dli_sname */
} Dl_info;

From reading the man page, it's unclear whether dli_fname:

  • Points to a constant string and never needs to be freed
  • Points to a heap array, with the user responsible for deleting it
  • Points to global array (probably not, since it claims to be reentrant)

I suppose the same question applies to dli_sname, but I suspect that indeed points to a constant string (the symbol itself).

Is the user responsible for deleting dli_fname returned by dladdr()?


Solution

  • These strings are valid until the object is unloaded via dlclose (either directly or indirectly).

    The const char * conveys that the string must not be freed by the dladdr caller because free expects a void *, not a const void *.