Merge "Add R_X86_64_64 to the list of possible weak relocs"
diff --git a/libc/include/dlfcn.h b/libc/include/dlfcn.h
index 7daa8f7..8dde08c 100644
--- a/libc/include/dlfcn.h
+++ b/libc/include/dlfcn.h
@@ -50,15 +50,29 @@
extern int dladdr(const void* addr, Dl_info *info);
enum {
+#if defined(__LP64__)
+ RTLD_NOW = 2,
+#else
RTLD_NOW = 0,
+#endif
RTLD_LAZY = 1,
RTLD_LOCAL = 0,
+#if defined(__LP64__)
+ RTLD_GLOBAL = 0x00100,
+#else
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_NEXT ((void*) 0xfffffffe)
+#endif
__END_DECLS
diff --git a/libc/tools/check-symbols-glibc.py b/libc/tools/check-symbols-glibc.py
new file mode 100755
index 0000000..43b213f
--- /dev/null
+++ b/libc/tools/check-symbols-glibc.py
@@ -0,0 +1,120 @@
+#!/usr/bin/python
+
+import glob
+import os
+import re
+import string
+import subprocess
+import sys
+
+toolchain = os.environ['ANDROID_TOOLCHAIN']
+arch = re.sub(r'.*/linux-x86/([^/]+)/.*', r'\1', toolchain)
+
+def GetSymbolsFromSo(so_file):
+
+ # Example readelf output:
+ # 264: 0001623c 4 FUNC GLOBAL DEFAULT 8 cabsf
+ # 266: 00016244 4 FUNC GLOBAL DEFAULT 8 dremf
+ # 267: 00019018 4 OBJECT GLOBAL DEFAULT 11 __fe_dfl_env
+ # 268: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_dcmplt
+
+ r = re.compile(r' +\d+: [0-9a-f]+ +\d+ (I?FUNC|OBJECT) +\S+ +\S+ +\d+ (\S+)')
+
+ symbols = set()
+
+ for line in subprocess.check_output(['readelf', '--dyn-syms', '-W', so_file]).split('\n'):
+ if ' HIDDEN ' in line or ' UND ' in line:
+ continue
+ m = r.match(line)
+ if m:
+ symbol = m.group(2)
+ symbol = re.sub('@.*', '', symbol)
+ symbols.add(symbol)
+
+ return symbols
+
+def GetSymbolsFromAndroidSo(*files):
+ symbols = set()
+ for f in files:
+ symbols = symbols | GetSymbolsFromSo('%s/system/lib64/%s' % (os.environ['ANDROID_PRODUCT_OUT'], f))
+ return symbols
+
+def GetSymbolsFromSystemSo(*files):
+ symbols = set()
+ for f in files:
+ f = glob.glob('/lib/x86_64-linux-gnu/%s' % f)[-1]
+ symbols = symbols | GetSymbolsFromSo(f)
+ return symbols
+
+glibc = GetSymbolsFromSystemSo('libc.so.*', 'librt.so.*', 'libpthread.so.*', 'libresolv.so.*', 'libm.so.*')
+bionic = GetSymbolsFromAndroidSo('libc.so', 'libm.so')
+
+# bionic includes various BSD symbols to ease porting other BSD-licensed code.
+bsd_stuff = set([
+ 'getprogname',
+ 'setprogname',
+ 'strlcat',
+ 'strlcpy',
+ 'wcslcat',
+ 'wcslcpy'
+])
+# Some symbols are part of the FORTIFY implementation.
+FORTIFY_stuff = set([
+ '__FD_CLR_chk',
+ '__FD_ISSET_chk',
+ '__FD_SET_chk',
+ '__stack_chk_guard',
+ '__stpncpy_chk2',
+ '__strchr_chk',
+ '__strlcat_chk',
+ '__strlcpy_chk',
+ '__strlen_chk',
+ '__strncpy_chk2',
+ '__strrchr_chk',
+ '__umask_chk'
+])
+# bionic exposes various Linux features that glibc doesn't.
+linux_stuff = set([
+ 'getauxval',
+ 'gettid',
+ 'tgkill'
+])
+# Some standard stuff isn't yet in the versions of glibc we're using.
+std_stuff = set([
+ 'at_quick_exit'
+])
+# These have mangled names in glibc, with a macro taking the "obvious" name.
+weird_stuff = set([
+ 'fstat',
+ 'fstat64',
+ 'fstatat',
+ 'fstatat64',
+ 'isfinite',
+ 'isfinitef',
+ 'isfinitel',
+ 'isnormal',
+ 'isnormalf',
+ 'isnormall',
+ 'lstat',
+ 'lstat64',
+ 'mknod',
+ 'mknodat',
+ 'stat',
+ 'stat64',
+])
+
+print 'glibc:'
+for symbol in sorted(glibc):
+ print symbol
+
+print
+print 'bionic:'
+for symbol in sorted(bionic):
+ print symbol
+
+print
+print 'in bionic but not glibc:'
+for symbol in sorted((bionic - bsd_stuff - FORTIFY_stuff - linux_stuff - std_stuff - weird_stuff).difference(glibc)):
+ print symbol
+
+sys.exit(0)
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 85e91c3..8ef1212 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -65,10 +65,10 @@
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);
- 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) {
__bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
return NULL;
@@ -76,8 +76,14 @@
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) {
- 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) {
@@ -97,8 +103,8 @@
if (handle == RTLD_DEFAULT) {
sym = dlsym_linear_lookup(symbol, &found, NULL);
} else if (handle == RTLD_NEXT) {
- void* ret_addr = __builtin_return_address(0);
- soinfo* si = find_containing_library(ret_addr);
+ void* caller_addr = __builtin_return_address(0);
+ soinfo* si = find_containing_library(caller_addr);
sym = NULL;
if (si && si->next) {
@@ -151,7 +157,9 @@
int dlclose(void* handle) {
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
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 38ae546..9f458b4 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -65,6 +65,19 @@
* and NOEXEC
*/
+#if defined(__LP64__)
+#define SEARCH_NAME(x) x
+#else
+// Nvidia drivers are relying on the bug:
+// http://code.google.com/p/android/issues/detail?id=6670
+// so we continue to use base-name lookup for lp32
+static const char* get_base_name(const char* name) {
+ const char* bname = strrchr(name, '/');
+ return bname ? bname + 1 : name;
+}
+#define SEARCH_NAME(x) get_base_name(x)
+#endif
+
static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo);
static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
@@ -656,7 +669,8 @@
}
// ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
#if defined(__LP64__)
- return -1;
+ // TODO: uncomment this after bug b/7465467 is fixed.
+ // return -1;
#endif
}
@@ -668,7 +682,7 @@
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) {
@@ -696,13 +710,16 @@
}
}
+ if ((dlflags & RTLD_NOLOAD) != 0) {
+ return NULL;
+ }
+
// Read the ELF header and load the segments.
if (!elf_reader.Load(extinfo)) {
return NULL;
}
- const char* bname = strrchr(name, '/');
- soinfo* si = soinfo_alloc(bname ? bname + 1 : name, &file_stat);
+ soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat);
if (si == NULL) {
return NULL;
}
@@ -725,48 +742,47 @@
return si;
}
-static soinfo *find_loaded_library(const char* name) {
- // TODO: don't use basename only for determining libraries
- // http://code.google.com/p/android/issues/detail?id=6670
-
- const char* bname = strrchr(name, '/');
- bname = bname ? bname + 1 : name;
-
- for (soinfo* si = solist; si != NULL; si = si->next) {
- if (!strcmp(bname, si->name)) {
- return si;
- }
+static soinfo *find_loaded_library_by_name(const char* name) {
+ const char* search_name = SEARCH_NAME(name);
+ for (soinfo* si = solist; si != NULL; si = si->next) {
+ if (!strcmp(search_name, si->name)) {
+ return si;
}
- 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) {
return somain;
}
- soinfo* si = find_loaded_library(name);
- if (si != NULL) {
- if (si->flags & FLAG_LINKED) {
- return si;
- }
- DL_ERR("OOPS: recursive link to \"%s\"", si->name);
+ soinfo* si = find_loaded_library_by_name(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();
@@ -781,7 +797,14 @@
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(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);
+ }
}
}
}
@@ -793,7 +816,6 @@
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) {
@@ -806,8 +828,8 @@
}
}
-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;
}
@@ -816,20 +838,21 @@
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)
@@ -1428,6 +1451,10 @@
// 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) {
@@ -1804,7 +1831,7 @@
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 {
@@ -1822,7 +1849,7 @@
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",
diff --git a/linker/linker.h b/linker/linker.h
index e5aca6e..9d4099d 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -231,8 +231,8 @@
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, const android_dlextinfo* extinfo);
-int do_dlclose(soinfo* si);
+soinfo* do_dlopen(const char* name, int flags, soinfo* caller, const android_dlextinfo* extinfo);
+void do_dlclose(soinfo* si);
ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
soinfo* find_containing_library(const void* addr);
diff --git a/tests/Android.mk b/tests/Android.mk
index 9a17c10..51f10ca 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -267,6 +267,18 @@
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
# -----------------------------------------------------------------------------
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index 3b3c0f6..434e38f 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -50,6 +50,18 @@
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) {
void* self = dlopen("/does/not/exist", RTLD_NOW);
ASSERT_TRUE(self == NULL);
diff --git a/tests/dlopen_testlib_simple.cpp b/tests/dlopen_testlib_simple.cpp
new file mode 100644
index 0000000..afe54b4
--- /dev/null
+++ b/tests/dlopen_testlib_simple.cpp
@@ -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;
+}