Look into local group on dlsym with RTLD_DEFAULT
Fix dlsym to look into local group when called with
RTLD_DEFAULT and RTLD_NEXT.
Bug: 17512583
Change-Id: I541354e89539c712af2ea4ec751e546913027084
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 64df7a5..479e831 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -101,16 +101,11 @@
soinfo* found = nullptr;
ElfW(Sym)* sym = nullptr;
- if (handle == RTLD_DEFAULT) {
- sym = dlsym_linear_lookup(symbol, &found, nullptr);
- } else if (handle == RTLD_NEXT) {
- void* caller_addr = __builtin_return_address(0);
- soinfo* si = find_containing_library(caller_addr);
+ void* caller_addr = __builtin_return_address(0);
+ soinfo* caller = find_containing_library(caller_addr);
- sym = nullptr;
- if (si && si->next) {
- sym = dlsym_linear_lookup(symbol, &found, si->next);
- }
+ if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
+ sym = dlsym_linear_lookup(symbol, &found, caller, handle);
} else {
sym = dlsym_handle_lookup(reinterpret_cast<soinfo*>(handle), &found, symbol);
}
diff --git a/linker/linker.cpp b/linker/linker.cpp
index ebf125e..002a4f9 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -738,15 +738,21 @@
beginning of the global solist. Otherwise the search starts at the
specified soinfo (for RTLD_NEXT).
*/
-ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
+ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* caller, void* handle) {
SymbolName symbol_name(name);
- if (start == nullptr) {
- start = solist;
+ soinfo* start = solist;
+
+ if (handle == RTLD_NEXT) {
+ if (caller == nullptr || caller->next == nullptr) {
+ return nullptr;
+ } else {
+ start = caller->next;
+ }
}
ElfW(Sym)* s = nullptr;
- for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
+ for (soinfo* si = start; si != nullptr; si = si->next) {
if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
continue;
}
@@ -758,6 +764,30 @@
}
}
+ // If not found - look into local_group unless
+ // caller is part of the global group in which
+ // case we already did it.
+ if (s == nullptr && caller != nullptr &&
+ (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
+ soinfo* local_group_root = caller->get_local_group_root();
+
+ if (handle == RTLD_DEFAULT) {
+ start = local_group_root;
+ }
+
+ for (soinfo* si = start; si != nullptr; si = si->next) {
+ if (si->get_local_group_root() != local_group_root) {
+ break;
+ }
+
+ s = si->find_symbol_by_name(symbol_name);
+ if (s != nullptr) {
+ *found = si;
+ break;
+ }
+ }
+ }
+
if (s != nullptr) {
TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
diff --git a/linker/linker.h b/linker/linker.h
index bf3e7bf..ec3d8f0 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -351,7 +351,7 @@
soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo);
void do_dlclose(soinfo* si);
-ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
+ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* caller, void* handle);
soinfo* find_containing_library(const void* addr);
ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name);