Merge "Make it possible for code to query the dynamic linker's default search path."

This commit is contained in:
Elliott Hughes 2014-01-14 20:03:20 +00:00 committed by Gerrit Code Review
commit d971f72905
4 changed files with 53 additions and 47 deletions

View File

@ -15,6 +15,8 @@
*/ */
#include <dlfcn.h> #include <dlfcn.h>
#include <stdlib.h>
/* These are stubs for functions that are actually defined /* These are stubs for functions that are actually defined
* in the dynamic linker (dlfcn.c), and hijacked at runtime. * in the dynamic linker (dlfcn.c), and hijacked at runtime.
*/ */
@ -24,6 +26,7 @@ void *dlsym(void *handle, const char *symbol) { return 0; }
int dladdr(const void *addr, Dl_info *info) { return 0; } int dladdr(const void *addr, Dl_info *info) { return 0; }
int dlclose(void *handle) { return 0; } int dlclose(void *handle) { return 0; }
void android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { }
void android_update_LD_LIBRARY_PATH(const char* ld_library_path) { } void android_update_LD_LIBRARY_PATH(const char* ld_library_path) { }
#if defined(__arm__) #if defined(__arm__)

View File

@ -54,6 +54,11 @@ const char* dlerror() {
return old_value; return old_value;
} }
void android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
ScopedPthreadMutexLocker locker(&gDlMutex);
do_android_get_LD_LIBRARY_PATH(buffer, buffer_size);
}
void android_update_LD_LIBRARY_PATH(const char* ld_library_path) { void android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
ScopedPthreadMutexLocker locker(&gDlMutex); ScopedPthreadMutexLocker locker(&gDlMutex);
do_android_update_LD_LIBRARY_PATH(ld_library_path); do_android_update_LD_LIBRARY_PATH(ld_library_path);
@ -143,20 +148,6 @@ int dlclose(void* handle) {
return do_dlclose(reinterpret_cast<soinfo*>(handle)); return do_dlclose(reinterpret_cast<soinfo*>(handle));
} }
#if defined(__arm__)
// 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888 8888888
// 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012 3456789
#define ANDROID_LIBDL_STRTAB \
"dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0dl_iterate_phdr\0dl_unwind_find_exidx\0"
#elif defined(__aarch64__) || defined(__i386__) || defined(__mips__) || defined(__x86_64__)
// 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667
// 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890
#define ANDROID_LIBDL_STRTAB \
"dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0dl_iterate_phdr\0"
#else
#error Unsupported architecture. Only aarch64, arm, mips, x86, and x86_64 are presently supported.
#endif
// name_offset: starting index of the name in libdl_info.strtab // name_offset: starting index of the name in libdl_info.strtab
#define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \ #define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \
{ name_offset, \ { name_offset, \
@ -182,47 +173,55 @@ int dlclose(void* handle) {
# define ELF_SYM_INITIALIZER ELF32_SYM_INITIALIZER # define ELF_SYM_INITIALIZER ELF32_SYM_INITIALIZER
#endif #endif
#if defined(__arm__)
// 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 9999900000000001 1
// 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 5678901234567890 1
# define ANDROID_LIBDL_STRTAB \
"dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_iterate_phdr\0dl_unwind_find_exidx\0"
#elif defined(__aarch64__) || defined(__i386__) || defined(__mips__) || defined(__x86_64__)
// 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 9999900000000001 1
// 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 5678901234567890 1
# define ANDROID_LIBDL_STRTAB \
"dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_iterate_phdr\0"
#else
# error Unsupported architecture. Only aarch64, arm, mips, x86, and x86_64 are presently supported.
#endif
static Elf_Sym gLibDlSymtab[] = { static Elf_Sym gLibDlSymtab[] = {
// Total length of libdl_info.strtab, including trailing 0. // Total length of libdl_info.strtab, including trailing 0.
// This is actually the STH_UNDEF entry. Technically, it's // This is actually the STH_UNDEF entry. Technically, it's
// supposed to have st_name == 0, but instead, it points to an index // supposed to have st_name == 0, but instead, it points to an index
// in the strtab with a \0 to make iterating through the symtab easier. // in the strtab with a \0 to make iterating through the symtab easier.
ELF_SYM_INITIALIZER(sizeof(ANDROID_LIBDL_STRTAB) - 1, NULL, 0), ELF_SYM_INITIALIZER(sizeof(ANDROID_LIBDL_STRTAB) - 1, NULL, 0),
ELF_SYM_INITIALIZER( 0, &dlopen, 1), ELF_SYM_INITIALIZER( 0, &dlopen, 1),
ELF_SYM_INITIALIZER( 7, &dlclose, 1), ELF_SYM_INITIALIZER( 7, &dlclose, 1),
ELF_SYM_INITIALIZER(15, &dlsym, 1), ELF_SYM_INITIALIZER( 15, &dlsym, 1),
ELF_SYM_INITIALIZER(21, &dlerror, 1), ELF_SYM_INITIALIZER( 21, &dlerror, 1),
ELF_SYM_INITIALIZER(29, &dladdr, 1), ELF_SYM_INITIALIZER( 29, &dladdr, 1),
ELF_SYM_INITIALIZER(36, &android_update_LD_LIBRARY_PATH, 1), ELF_SYM_INITIALIZER( 36, &android_update_LD_LIBRARY_PATH, 1),
ELF_SYM_INITIALIZER(67, &dl_iterate_phdr, 1), ELF_SYM_INITIALIZER( 67, &android_get_LD_LIBRARY_PATH, 1),
ELF_SYM_INITIALIZER( 95, &dl_iterate_phdr, 1),
#if defined(__arm__) #if defined(__arm__)
ELF_SYM_INITIALIZER(83, &dl_unwind_find_exidx, 1), ELF_SYM_INITIALIZER(111, &dl_unwind_find_exidx, 1),
#endif #endif
}; };
// Fake out a hash table with a single bucket. // Fake out a hash table with a single bucket.
// A search of the hash table will look through
// gLibDlSymtab starting with index [1], then
// use gLibDlChains to find the next index to
// look at. gLibDlChains should be set up to
// walk through every element in gLibDlSymtab,
// and then end with 0 (sentinel value).
// //
// That is, gLibDlChains should look like // A search of the hash table will look through gLibDlSymtab starting with index 1, then
// { 0, 2, 3, ... N, 0 } where N is the number // use gLibDlChains to find the next index to look at. gLibDlChains should be set up to
// of actual symbols, or nelems(gLibDlSymtab)-1 // walk through every element in gLibDlSymtab, and then end with 0 (sentinel value).
// (since the first element of gLibDlSymtab is not
// a real symbol).
// //
// (see soinfo_elf_lookup()) // That is, gLibDlChains should look like { 0, 2, 3, ... N, 0 } where N is the number
// of actual symbols, or nelems(gLibDlSymtab)-1 (since the first element of gLibDlSymtab is not
// a real symbol). (See soinfo_elf_lookup().)
// //
// Note that adding any new symbols here requires // Note that adding any new symbols here requires stubbing them out in libdl.
// stubbing them out in libdl.
static unsigned gLibDlBuckets[1] = { 1 }; static unsigned gLibDlBuckets[1] = { 1 };
#if defined(__arm__) #if defined(__arm__)
static unsigned gLibDlChains[9] = { 0, 2, 3, 4, 5, 6, 7, 8, 0 }; static unsigned gLibDlChains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
#else #else
static unsigned gLibDlChains[8] = { 0, 2, 3, 4, 5, 6, 7, 0 }; static unsigned gLibDlChains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 0 };
#endif #endif
// This is used by the dynamic linker. Every process gets these symbols for free. // This is used by the dynamic linker. Every process gets these symbols for free.

View File

@ -50,13 +50,6 @@
#include "linker_environ.h" #include "linker_environ.h"
#include "linker_phdr.h" #include "linker_phdr.h"
/* Assume average path length of 64 and max 8 paths */
#define LDPATH_BUFSIZE 512
#define LDPATH_MAX 8
#define LDPRELOAD_BUFSIZE 512
#define LDPRELOAD_MAX 8
/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<< /* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
* *
* Do NOT use malloc() and friends or pthread_*() code here. * Do NOT use malloc() and friends or pthread_*() code here.
@ -91,7 +84,7 @@ static soinfo* solist = &libdl_info;
static soinfo* sonext = &libdl_info; static soinfo* sonext = &libdl_info;
static soinfo* somain; /* main process, always the one after libdl_info */ static soinfo* somain; /* main process, always the one after libdl_info */
static const char* const gSoPaths[] = { static const char* const gDefaultLdPaths[] = {
#if defined(__LP64__) #if defined(__LP64__)
"/vendor/lib64", "/vendor/lib64",
"/system/lib64", "/system/lib64",
@ -102,6 +95,12 @@ static const char* const gSoPaths[] = {
NULL NULL
}; };
#define LDPATH_BUFSIZE (LDPATH_MAX*64)
#define LDPATH_MAX 8
#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
#define LDPRELOAD_MAX 8
static char gLdPathsBuffer[LDPATH_BUFSIZE]; static char gLdPathsBuffer[LDPATH_BUFSIZE];
static const char* gLdPaths[LDPATH_MAX + 1]; static const char* gLdPaths[LDPATH_MAX + 1];
@ -708,7 +707,7 @@ static int open_library(const char* name) {
// Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths. // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
int fd = open_library_on_path(name, gLdPaths); int fd = open_library_on_path(name, gLdPaths);
if (fd == -1) { if (fd == -1) {
fd = open_library_on_path(name, gSoPaths); fd = open_library_on_path(name, gDefaultLdPaths);
} }
return fd; return fd;
} }
@ -828,6 +827,10 @@ static int soinfo_unload(soinfo* si) {
return 0; return 0;
} }
void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
snprintf(buffer, buffer_size, "%s:%s", gDefaultLdPaths[0], gDefaultLdPaths[1]);
}
void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) { void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
if (!get_AT_SECURE()) { if (!get_AT_SECURE()) {
parse_LD_LIBRARY_PATH(ld_library_path); parse_LD_LIBRARY_PATH(ld_library_path);

View File

@ -196,6 +196,7 @@ struct soinfo {
extern soinfo libdl_info; extern soinfo libdl_info;
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); soinfo* do_dlopen(const char* name, int flags);
int do_dlclose(soinfo* si); int do_dlclose(soinfo* si);