diff options
author | Dmitriy Ivanov <dimitry@google.com> | 2014-05-19 15:06:58 -0700 |
---|---|---|
committer | Dmitriy Ivanov <dimitry@google.com> | 2014-05-20 12:02:24 -0700 |
commit | b648a8a57ee42533a5bf127225a252f73ca2cbbc (patch) | |
tree | 09fdf06c6e8d5e6985107f4e8fdb2055c5bb6a6f /linker/linker.cpp | |
parent | 0b9a72ce1124f17d514b93c0d26dea76ce8069f0 (diff) |
Add RTLD_NOLOAD support and some related changes.
* Aligned RTLD_ values with glibc for lp64
* dlopen supports RTLD_NOLOAD flag
* soinfo_unload calls find_library(.., RTLD_NOLOAD)
instead of naive find_loaded_library_by_name()
* dlopen changed to add child to caller soinfo instead
of somain.
Bug: https://code.google.com/p/android/issues/detail?id=64069
Change-Id: I1a65f2c34f3e0edc6d2c41a2e408b58195feb640
Diffstat (limited to 'linker/linker.cpp')
-rw-r--r-- | linker/linker.cpp | 65 |
1 files changed, 42 insertions, 23 deletions
diff --git a/linker/linker.cpp b/linker/linker.cpp index 5861267f3..881bcc212 100644 --- a/linker/linker.cpp +++ b/linker/linker.cpp @@ -682,7 +682,7 @@ static int open_library(const char* name) { return fd; } -static soinfo* load_library(const char* name, const android_dlextinfo* extinfo) { +static soinfo* load_library(const char* name, int dlflags, const android_dlextinfo* extinfo) { // Open the file. int fd = open_library(name); if (fd == -1) { @@ -710,6 +710,10 @@ static soinfo* load_library(const char* name, const android_dlextinfo* extinfo) } } + if ((dlflags & RTLD_NOLOAD) != 0) { + return NULL; + } + // Read the ELF header and load the segments. if (!elf_reader.Load(extinfo)) { return NULL; @@ -748,33 +752,37 @@ static soinfo *find_loaded_library_by_name(const char* name) { return NULL; } -static soinfo* find_library_internal(const char* name, const android_dlextinfo* extinfo) { +static soinfo* find_library_internal(const char* name, int dlflags, const android_dlextinfo* extinfo) { if (name == NULL) { return somain; } soinfo* si = find_loaded_library_by_name(name); - if (si != NULL) { - if (si->flags & FLAG_LINKED) { - return si; - } - DL_ERR("OOPS: recursive link to \"%s\"", si->name); + + // Library might still be loaded, the accurate detection + // of this fact is done by load_library + if (si == NULL) { + TRACE("[ '%s' has not been found by name. Trying harder...]", name); + si = load_library(name, dlflags, extinfo); + } + + if (si != NULL && (si->flags & FLAG_LINKED) == 0) { + DL_ERR("recursive link to \"%s\"", si->name); return NULL; } - TRACE("[ '%s' has not been loaded yet. Locating...]", name); - return load_library(name, extinfo); + return si; } -static soinfo* find_library(const char* name, const android_dlextinfo* extinfo) { - soinfo* si = find_library_internal(name, extinfo); +static soinfo* find_library(const char* name, int dlflags, const android_dlextinfo* extinfo) { + soinfo* si = find_library_internal(name, dlflags, extinfo); if (si != NULL) { si->ref_count++; } return si; } -static int soinfo_unload(soinfo* si) { +static void soinfo_unload(soinfo* si) { if (si->ref_count == 1) { TRACE("unloading '%s'", si->name); si->CallDestructors(); @@ -789,7 +797,14 @@ static int soinfo_unload(soinfo* si) { if (d->d_tag == DT_NEEDED) { const char* library_name = si->strtab + d->d_un.d_val; TRACE("%s needs to unload %s", si->name, library_name); - soinfo_unload(find_loaded_library_by_name(library_name)); + soinfo* needed = find_library(library_name, RTLD_NOLOAD, NULL); + if (needed != NULL) { + soinfo_unload(needed); + } else { + // Not found: for example if symlink was deleted between dlopen and dlclose + // Since we cannot really handle errors at this point - print and continue. + PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name); + } } } } @@ -801,7 +816,6 @@ static int soinfo_unload(soinfo* si) { si->ref_count--; TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count); } - return 0; } void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { @@ -814,8 +828,8 @@ void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) { } } -soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) { - if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) { +soinfo* do_dlopen(const char* name, int flags, soinfo* caller, const android_dlextinfo* extinfo) { + if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NOLOAD)) != 0) { DL_ERR("invalid flags to dlopen: %x", flags); return NULL; } @@ -824,20 +838,21 @@ soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) return NULL; } protect_data(PROT_READ | PROT_WRITE); - soinfo* si = find_library(name, extinfo); + soinfo* si = find_library(name, flags, extinfo); if (si != NULL) { si->CallConstructors(); - somain->add_child(si); + if (caller != NULL) { + caller->add_child(si); + } } protect_data(PROT_READ); return si; } -int do_dlclose(soinfo* si) { +void do_dlclose(soinfo* si) { protect_data(PROT_READ | PROT_WRITE); - int result = soinfo_unload(si); + soinfo_unload(si); protect_data(PROT_READ); - return result; } #if defined(USE_RELA) @@ -1435,6 +1450,10 @@ void soinfo::CallDestructors() { // DT_FINI should be called after DT_FINI_ARRAY if both are present. CallFunction("DT_FINI", fini_func); + + // This is needed on second call to dlopen + // after library has been unloaded with RTLD_NODELETE + constructors_called = false; } void soinfo::add_child(soinfo* child) { @@ -1811,7 +1830,7 @@ static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) { memset(g_ld_preloads, 0, sizeof(g_ld_preloads)); size_t preload_count = 0; for (size_t i = 0; g_ld_preload_names[i] != NULL; i++) { - soinfo* lsi = find_library(g_ld_preload_names[i], NULL); + soinfo* lsi = find_library(g_ld_preload_names[i], 0, NULL); if (lsi != NULL) { g_ld_preloads[preload_count++] = lsi; } else { @@ -1829,7 +1848,7 @@ static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) { if (d->d_tag == DT_NEEDED) { const char* library_name = si->strtab + d->d_un.d_val; DEBUG("%s needs %s", si->name, library_name); - soinfo* lsi = find_library(library_name, NULL); + soinfo* lsi = find_library(library_name, 0, NULL); if (lsi == NULL) { strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf)); DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s", |