Check System V ABI, Chapter 5 . For the lazy, there is a standard way to do this for systems that support the binary ELF format:
#include <link.h> off_t load_offset; for (Elf64_Dyn *dyn = _DYNAMIC; dyn->d_tag != DT_NULL; ++dyn) { if (dyn->d_tag == DT_DEBUG) { struct r_debug *r_debug = (struct r_debug *) dyn->d_un.d_ptr; struct link_map *link_map = r_debug->r_map; while (link_map) { if (strcmp(link_map->l_name, libname) == 0) { load_offset = (off_t)link_map->l_addr; break; } link_map = link_map->l_next; } break; } }
It does not depend on the GNU extension.
On GNU systems, the ElfW (Dyn) macro returns either Elf64_Dyn or Elf32_Dyn, which is convenient.
Erwan legrand
source share