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
This commit is contained in:
parent
0b9a72ce11
commit
b648a8a57e
@ -50,15 +50,29 @@ extern void* dlsym(void* handle, const char* symbol);
|
|||||||
extern int dladdr(const void* addr, Dl_info *info);
|
extern int dladdr(const void* addr, Dl_info *info);
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
|
#if defined(__LP64__)
|
||||||
|
RTLD_NOW = 2,
|
||||||
|
#else
|
||||||
RTLD_NOW = 0,
|
RTLD_NOW = 0,
|
||||||
|
#endif
|
||||||
RTLD_LAZY = 1,
|
RTLD_LAZY = 1,
|
||||||
|
|
||||||
RTLD_LOCAL = 0,
|
RTLD_LOCAL = 0,
|
||||||
|
#if defined(__LP64__)
|
||||||
|
RTLD_GLOBAL = 0x00100,
|
||||||
|
#else
|
||||||
RTLD_GLOBAL = 2,
|
RTLD_GLOBAL = 2,
|
||||||
|
#endif
|
||||||
|
RTLD_NOLOAD = 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if defined (__LP64__)
|
||||||
|
#define RTLD_DEFAULT ((void*) 0)
|
||||||
|
#define RTLD_NEXT ((void*) -1L)
|
||||||
|
#else
|
||||||
#define RTLD_DEFAULT ((void*) 0xffffffff)
|
#define RTLD_DEFAULT ((void*) 0xffffffff)
|
||||||
#define RTLD_NEXT ((void*) 0xfffffffe)
|
#define RTLD_NEXT ((void*) 0xfffffffe)
|
||||||
|
#endif
|
||||||
|
|
||||||
__END_DECLS
|
__END_DECLS
|
||||||
|
|
||||||
|
@ -65,10 +65,10 @@ void android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
|
|||||||
do_android_update_LD_LIBRARY_PATH(ld_library_path);
|
do_android_update_LD_LIBRARY_PATH(ld_library_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* android_dlopen_ext(const char* filename, int flags, const android_dlextinfo* extinfo)
|
static void* dlopen_ext(const char* filename, int flags, const android_dlextinfo* extinfo, const void* caller_addr) {
|
||||||
{
|
|
||||||
ScopedPthreadMutexLocker locker(&g_dl_mutex);
|
ScopedPthreadMutexLocker locker(&g_dl_mutex);
|
||||||
soinfo* result = do_dlopen(filename, flags, extinfo);
|
soinfo* caller_soinfo = find_containing_library(caller_addr);
|
||||||
|
soinfo* result = do_dlopen(filename, flags, caller_soinfo, extinfo);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
__bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
|
__bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -76,8 +76,14 @@ void* android_dlopen_ext(const char* filename, int flags, const android_dlextinf
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* android_dlopen_ext(const char* filename, int flags, const android_dlextinfo* extinfo) {
|
||||||
|
void* caller_addr = __builtin_return_address(0);
|
||||||
|
return dlopen_ext(filename, flags, extinfo, caller_addr);
|
||||||
|
}
|
||||||
|
|
||||||
void* dlopen(const char* filename, int flags) {
|
void* dlopen(const char* filename, int flags) {
|
||||||
return android_dlopen_ext(filename, flags, NULL);
|
void* caller_addr = __builtin_return_address(0);
|
||||||
|
return dlopen_ext(filename, flags, NULL, caller_addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* dlsym(void* handle, const char* symbol) {
|
void* dlsym(void* handle, const char* symbol) {
|
||||||
@ -97,8 +103,8 @@ void* dlsym(void* handle, const char* symbol) {
|
|||||||
if (handle == RTLD_DEFAULT) {
|
if (handle == RTLD_DEFAULT) {
|
||||||
sym = dlsym_linear_lookup(symbol, &found, NULL);
|
sym = dlsym_linear_lookup(symbol, &found, NULL);
|
||||||
} else if (handle == RTLD_NEXT) {
|
} else if (handle == RTLD_NEXT) {
|
||||||
void* ret_addr = __builtin_return_address(0);
|
void* caller_addr = __builtin_return_address(0);
|
||||||
soinfo* si = find_containing_library(ret_addr);
|
soinfo* si = find_containing_library(caller_addr);
|
||||||
|
|
||||||
sym = NULL;
|
sym = NULL;
|
||||||
if (si && si->next) {
|
if (si && si->next) {
|
||||||
@ -151,7 +157,9 @@ int dladdr(const void* addr, Dl_info* info) {
|
|||||||
|
|
||||||
int dlclose(void* handle) {
|
int dlclose(void* handle) {
|
||||||
ScopedPthreadMutexLocker locker(&g_dl_mutex);
|
ScopedPthreadMutexLocker locker(&g_dl_mutex);
|
||||||
return do_dlclose(reinterpret_cast<soinfo*>(handle));
|
do_dlclose(reinterpret_cast<soinfo*>(handle));
|
||||||
|
// dlclose has no defined errors.
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// name_offset: starting index of the name in libdl_info.strtab
|
// name_offset: starting index of the name in libdl_info.strtab
|
||||||
|
@ -682,7 +682,7 @@ static int open_library(const char* name) {
|
|||||||
return fd;
|
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.
|
// Open the file.
|
||||||
int fd = open_library(name);
|
int fd = open_library(name);
|
||||||
if (fd == -1) {
|
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.
|
// Read the ELF header and load the segments.
|
||||||
if (!elf_reader.Load(extinfo)) {
|
if (!elf_reader.Load(extinfo)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -748,33 +752,37 @@ static soinfo *find_loaded_library_by_name(const char* name) {
|
|||||||
return NULL;
|
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) {
|
if (name == NULL) {
|
||||||
return somain;
|
return somain;
|
||||||
}
|
}
|
||||||
|
|
||||||
soinfo* si = find_loaded_library_by_name(name);
|
soinfo* si = find_loaded_library_by_name(name);
|
||||||
if (si != NULL) {
|
|
||||||
if (si->flags & FLAG_LINKED) {
|
// Library might still be loaded, the accurate detection
|
||||||
return si;
|
// 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);
|
||||||
}
|
}
|
||||||
DL_ERR("OOPS: recursive link to \"%s\"", si->name);
|
|
||||||
|
if (si != NULL && (si->flags & FLAG_LINKED) == 0) {
|
||||||
|
DL_ERR("recursive link to \"%s\"", si->name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("[ '%s' has not been loaded yet. Locating...]", name);
|
return si;
|
||||||
return load_library(name, extinfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static soinfo* find_library(const char* name, const android_dlextinfo* extinfo) {
|
static soinfo* find_library(const char* name, int dlflags, const android_dlextinfo* extinfo) {
|
||||||
soinfo* si = find_library_internal(name, extinfo);
|
soinfo* si = find_library_internal(name, dlflags, extinfo);
|
||||||
if (si != NULL) {
|
if (si != NULL) {
|
||||||
si->ref_count++;
|
si->ref_count++;
|
||||||
}
|
}
|
||||||
return si;
|
return si;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int soinfo_unload(soinfo* si) {
|
static void soinfo_unload(soinfo* si) {
|
||||||
if (si->ref_count == 1) {
|
if (si->ref_count == 1) {
|
||||||
TRACE("unloading '%s'", si->name);
|
TRACE("unloading '%s'", si->name);
|
||||||
si->CallDestructors();
|
si->CallDestructors();
|
||||||
@ -789,7 +797,14 @@ static int soinfo_unload(soinfo* si) {
|
|||||||
if (d->d_tag == DT_NEEDED) {
|
if (d->d_tag == DT_NEEDED) {
|
||||||
const char* library_name = si->strtab + d->d_un.d_val;
|
const char* library_name = si->strtab + d->d_un.d_val;
|
||||||
TRACE("%s needs to unload %s", si->name, library_name);
|
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--;
|
si->ref_count--;
|
||||||
TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, 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) {
|
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) {
|
soinfo* do_dlopen(const char* name, int flags, soinfo* caller, const android_dlextinfo* extinfo) {
|
||||||
if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
|
if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NOLOAD)) != 0) {
|
||||||
DL_ERR("invalid flags to dlopen: %x", flags);
|
DL_ERR("invalid flags to dlopen: %x", flags);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -824,20 +838,21 @@ soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
protect_data(PROT_READ | PROT_WRITE);
|
protect_data(PROT_READ | PROT_WRITE);
|
||||||
soinfo* si = find_library(name, extinfo);
|
soinfo* si = find_library(name, flags, extinfo);
|
||||||
if (si != NULL) {
|
if (si != NULL) {
|
||||||
si->CallConstructors();
|
si->CallConstructors();
|
||||||
somain->add_child(si);
|
if (caller != NULL) {
|
||||||
|
caller->add_child(si);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
protect_data(PROT_READ);
|
protect_data(PROT_READ);
|
||||||
return si;
|
return si;
|
||||||
}
|
}
|
||||||
|
|
||||||
int do_dlclose(soinfo* si) {
|
void do_dlclose(soinfo* si) {
|
||||||
protect_data(PROT_READ | PROT_WRITE);
|
protect_data(PROT_READ | PROT_WRITE);
|
||||||
int result = soinfo_unload(si);
|
soinfo_unload(si);
|
||||||
protect_data(PROT_READ);
|
protect_data(PROT_READ);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(USE_RELA)
|
#if defined(USE_RELA)
|
||||||
@ -1435,6 +1450,10 @@ void soinfo::CallDestructors() {
|
|||||||
|
|
||||||
// DT_FINI should be called after DT_FINI_ARRAY if both are present.
|
// DT_FINI should be called after DT_FINI_ARRAY if both are present.
|
||||||
CallFunction("DT_FINI", fini_func);
|
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) {
|
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));
|
memset(g_ld_preloads, 0, sizeof(g_ld_preloads));
|
||||||
size_t preload_count = 0;
|
size_t preload_count = 0;
|
||||||
for (size_t i = 0; g_ld_preload_names[i] != NULL; i++) {
|
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) {
|
if (lsi != NULL) {
|
||||||
g_ld_preloads[preload_count++] = lsi;
|
g_ld_preloads[preload_count++] = lsi;
|
||||||
} else {
|
} else {
|
||||||
@ -1829,7 +1848,7 @@ static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) {
|
|||||||
if (d->d_tag == DT_NEEDED) {
|
if (d->d_tag == DT_NEEDED) {
|
||||||
const char* library_name = si->strtab + d->d_un.d_val;
|
const char* library_name = si->strtab + d->d_un.d_val;
|
||||||
DEBUG("%s needs %s", si->name, library_name);
|
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) {
|
if (lsi == NULL) {
|
||||||
strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
|
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",
|
DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
|
||||||
|
@ -231,8 +231,8 @@ extern soinfo* get_libdl_info();
|
|||||||
|
|
||||||
void do_android_get_LD_LIBRARY_PATH(char*, size_t);
|
void do_android_get_LD_LIBRARY_PATH(char*, size_t);
|
||||||
void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
|
void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
|
||||||
soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo);
|
soinfo* do_dlopen(const char* name, int flags, soinfo* caller, const android_dlextinfo* extinfo);
|
||||||
int do_dlclose(soinfo* si);
|
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* start);
|
||||||
soinfo* find_containing_library(const void* addr);
|
soinfo* find_containing_library(const void* addr);
|
||||||
|
@ -266,6 +266,18 @@ build_type := target
|
|||||||
build_target := SHARED_LIBRARY
|
build_target := SHARED_LIBRARY
|
||||||
include $(LOCAL_PATH)/Android.build.mk
|
include $(LOCAL_PATH)/Android.build.mk
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
# Library used by dlfcn tests
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
libtest_simple_src_files := \
|
||||||
|
dlopen_testlib_simple.cpp
|
||||||
|
|
||||||
|
module := libtest_simple
|
||||||
|
build_type := target
|
||||||
|
build_target := SHARED_LIBRARY
|
||||||
|
include $(LOCAL_PATH)/Android.build.mk
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Library used by atexit tests
|
# Library used by atexit tests
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
@ -50,6 +50,18 @@ TEST(dlfcn, dlsym_in_self) {
|
|||||||
ASSERT_EQ(0, dlclose(self));
|
ASSERT_EQ(0, dlclose(self));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(dlfcn, dlopen_noload) {
|
||||||
|
void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
|
||||||
|
ASSERT_TRUE(handle == NULL);
|
||||||
|
handle = dlopen("libtest_simple.so", RTLD_NOW);
|
||||||
|
void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
|
||||||
|
ASSERT_TRUE(handle != NULL);
|
||||||
|
ASSERT_TRUE(handle2 != NULL);
|
||||||
|
ASSERT_TRUE(handle == handle2);
|
||||||
|
ASSERT_EQ(0, dlclose(handle));
|
||||||
|
ASSERT_EQ(0, dlclose(handle2));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(dlfcn, dlopen_failure) {
|
TEST(dlfcn, dlopen_failure) {
|
||||||
void* self = dlopen("/does/not/exist", RTLD_NOW);
|
void* self = dlopen("/does/not/exist", RTLD_NOW);
|
||||||
ASSERT_TRUE(self == NULL);
|
ASSERT_TRUE(self == NULL);
|
||||||
|
23
tests/dlopen_testlib_simple.cpp
Normal file
23
tests/dlopen_testlib_simple.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2014 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
uint32_t dlopen_testlib_taxicab_number = 1729;
|
||||||
|
|
||||||
|
bool dlopen_testlib_simple_func() {
|
||||||
|
return true;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user