IanPudney
IanPudney

Reputation: 6041

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:

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()?

Upvotes: 5

Views: 293

Answers (1)

Florian Weimer
Florian Weimer

Reputation: 33757

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 *.

Upvotes: 5

Related Questions