dtoolutil: Don't assume RTLD_DI_ORIGIN and RTLD_SELF are present, fall back to using RTLD_DI_LINKMAP if necessary

Closes #334
This commit is contained in:
Psychotropos 2018-05-29 22:10:30 +01:00 committed by rdb
parent 314cee133a
commit 3029780f88

View File

@ -577,8 +577,8 @@ read_args() {
} }
#endif #endif
#if defined(IS_FREEBSD) || (defined(IS_LINUX) && !defined(__ANDROID__)) #if defined(RTLD_DI_ORIGIN)
// FreeBSD and Linux have a function to get the origin of a loaded library. // When building with glibc/uClibc, we typically have access to RTLD_DI_ORIGIN in Unix-like operating systems.
char origin[PATH_MAX + 1]; char origin[PATH_MAX + 1];
@ -598,12 +598,16 @@ read_args() {
} }
#endif #endif
#if defined(IS_FREEBSD) #if !defined(RTLD_DI_ORIGIN) && defined(RTLD_DI_LINKMAP)
// On FreeBSD, we can use dlinfo to get the linked libraries. // On platforms without RTLD_DI_ORIGIN, we can use dlinfo with RTLD_DI_LINKMAP to get the origin of a loaded library.
if (_dtool_name.empty()) { if (_dtool_name.empty()) {
Link_map *map; struct link_map *map;
dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map); #ifdef RTLD_SELF
void *self = RTLD_SELF;
#else
void *self = dlopen(NULL, RTLD_NOW | RTLD_NOLOAD);
#endif
dlinfo(self, RTLD_DI_LINKMAP, &map);
while (map != NULL) { while (map != NULL) {
const char *tail = strrchr(map->l_name, '/'); const char *tail = strrchr(map->l_name, '/');