Make it possible for code to query the dynamic linker's default search path.
We're not going to have init(1) set LD_LIBRARY_PATH globally on 64-bit.
This patch makes it possible for libnativehelper to set LD_LIBRARY_PATH
in each Java VM (to support System.loadLibrary) without also hard-coding
the default search path there.
Change-Id: If13961fae976e06dd80d5ef522f31e8b7eb01154
diff --git a/libdl/libdl.c b/libdl/libdl.c
index 548364c..12ab39b 100644
--- a/libdl/libdl.c
+++ b/libdl/libdl.c
@@ -15,6 +15,8 @@
*/
#include <dlfcn.h>
+#include <stdlib.h>
+
/* These are stubs for functions that are actually defined
* in the dynamic linker (dlfcn.c), and hijacked at runtime.
*/
@@ -24,6 +26,7 @@
int dladdr(const void *addr, Dl_info *info) { 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) { }
#if defined(__arm__)
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 207e03c..8501ad6 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -54,6 +54,11 @@
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) {
ScopedPthreadMutexLocker locker(&gDlMutex);
do_android_update_LD_LIBRARY_PATH(ld_library_path);
@@ -143,20 +148,6 @@
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
#define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \
{ name_offset, \
@@ -182,47 +173,55 @@
# define ELF_SYM_INITIALIZER ELF32_SYM_INITIALIZER
#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[] = {
// Total length of libdl_info.strtab, including trailing 0.
// This is actually the STH_UNDEF entry. Technically, it's
// 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.
ELF_SYM_INITIALIZER(sizeof(ANDROID_LIBDL_STRTAB) - 1, NULL, 0),
- ELF_SYM_INITIALIZER( 0, &dlopen, 1),
- ELF_SYM_INITIALIZER( 7, &dlclose, 1),
- ELF_SYM_INITIALIZER(15, &dlsym, 1),
- ELF_SYM_INITIALIZER(21, &dlerror, 1),
- ELF_SYM_INITIALIZER(29, &dladdr, 1),
- ELF_SYM_INITIALIZER(36, &android_update_LD_LIBRARY_PATH, 1),
- ELF_SYM_INITIALIZER(67, &dl_iterate_phdr, 1),
+ ELF_SYM_INITIALIZER( 0, &dlopen, 1),
+ ELF_SYM_INITIALIZER( 7, &dlclose, 1),
+ ELF_SYM_INITIALIZER( 15, &dlsym, 1),
+ ELF_SYM_INITIALIZER( 21, &dlerror, 1),
+ ELF_SYM_INITIALIZER( 29, &dladdr, 1),
+ ELF_SYM_INITIALIZER( 36, &android_update_LD_LIBRARY_PATH, 1),
+ ELF_SYM_INITIALIZER( 67, &android_get_LD_LIBRARY_PATH, 1),
+ ELF_SYM_INITIALIZER( 95, &dl_iterate_phdr, 1),
#if defined(__arm__)
- ELF_SYM_INITIALIZER(83, &dl_unwind_find_exidx, 1),
+ ELF_SYM_INITIALIZER(111, &dl_unwind_find_exidx, 1),
#endif
};
// 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
-// { 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).
+// 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).
//
-// (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
-// stubbing them out in libdl.
+// Note that adding any new symbols here requires stubbing them out in libdl.
static unsigned gLibDlBuckets[1] = { 1 };
#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
-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
// This is used by the dynamic linker. Every process gets these symbols for free.
diff --git a/linker/linker.cpp b/linker/linker.cpp
index f4e426d..f58e222 100755
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -50,13 +50,6 @@
#include "linker_environ.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 <<<
*
* Do NOT use malloc() and friends or pthread_*() code here.
@@ -91,7 +84,7 @@
static soinfo* sonext = &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__)
"/vendor/lib64",
"/system/lib64",
@@ -102,6 +95,12 @@
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 const char* gLdPaths[LDPATH_MAX + 1];
@@ -708,7 +707,7 @@
// 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);
if (fd == -1) {
- fd = open_library_on_path(name, gSoPaths);
+ fd = open_library_on_path(name, gDefaultLdPaths);
}
return fd;
}
@@ -828,6 +827,10 @@
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) {
if (!get_AT_SECURE()) {
parse_LD_LIBRARY_PATH(ld_library_path);
diff --git a/linker/linker.h b/linker/linker.h
index 9afd9e1..67a86ab 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -196,6 +196,7 @@
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);
soinfo* do_dlopen(const char* name, int flags);
int do_dlclose(soinfo* si);