From 012cb4583a5f8564059142bb1900ea3a31e7cfa9 Mon Sep 17 00:00:00 2001 From: "Torne (Richard Coles)" Date: Thu, 6 Feb 2014 14:34:21 +0000 Subject: [PATCH] Add android_dlopen_ext() interface to linker. Add a function "android_dlopen_ext()", defined in . This is an extended version of dlopen() which takes a struct for passing additional parameters for Android-specific functionality. This will be used to support RELRO section sharing between separate processes. Bug: 13005501 Change-Id: I9c99b2f2a02ee329dedaeba09ef3a1113b17b2d6 --- libc/include/android/dlext.h | 39 +++++++++++++++++++++++++++++++++++++++ libdl/libdl.c | 9 ++++++--- linker/dlfcn.cpp | 29 ++++++++++++++++++----------- linker/linker.cpp | 6 +++++- linker/linker.h | 3 ++- 5 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 libc/include/android/dlext.h diff --git a/libc/include/android/dlext.h b/libc/include/android/dlext.h new file mode 100644 index 000000000..5de39c620 --- /dev/null +++ b/libc/include/android/dlext.h @@ -0,0 +1,39 @@ +/* + * 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. + */ + +#ifndef __ANDROID_DLEXT_H__ +#define __ANDROID_DLEXT_H__ + +#include +#include + +__BEGIN_DECLS + +/* bitfield definitions for android_dlextinfo.flags */ +enum { + /* Mask of valid bits */ + ANDROID_DLEXT_VALID_FLAG_BITS = 0, +}; + +typedef struct { + int flags; +} android_dlextinfo; + +extern void* android_dlopen_ext(const char* filename, int flag, const android_dlextinfo* extinfo); + +__END_DECLS + +#endif /* __ANDROID_DLEXT_H__ */ diff --git a/libdl/libdl.c b/libdl/libdl.c index 310db54f5..dca51b0b0 100644 --- a/libdl/libdl.c +++ b/libdl/libdl.c @@ -17,6 +17,7 @@ #include #include #include +#include // These are stubs for functions that are actually defined // in the dynamic linker and hijacked at runtime. @@ -27,11 +28,13 @@ void* dlsym(void* handle __unused, const char* symbol __unused) { return 0; } int dladdr(const void* addr __unused, Dl_info* info __unused) { return 0; } int dlclose(void* handle __unused) { return 0; } -void android_get_LD_LIBRARY_PATH(char* buffer __unused, size_t buffer_size __unused) { } -void android_update_LD_LIBRARY_PATH(const char* ld_library_path __unused) { } - #if defined(__arm__) _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc __unused, int* pcount __unused) { return 0; } #endif int dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data) __unused, void* data __unused) { return 0; } + +void android_get_LD_LIBRARY_PATH(char* buffer __unused, size_t buffer_size __unused) { } +void android_update_LD_LIBRARY_PATH(const char* ld_library_path __unused) { } + +void* android_dlopen_ext(const char* filename __unused, int flag __unused, const android_dlextinfo* extinfo __unused) { return 0; } diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp index 6e70808b2..0292bdf04 100644 --- a/linker/dlfcn.cpp +++ b/linker/dlfcn.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "private/bionic_tls.h" @@ -64,9 +65,10 @@ void android_update_LD_LIBRARY_PATH(const char* ld_library_path) { do_android_update_LD_LIBRARY_PATH(ld_library_path); } -void* dlopen(const char* filename, int flags) { +void* android_dlopen_ext(const char* filename, int flags, const android_dlextinfo* extinfo) +{ ScopedPthreadMutexLocker locker(&gDlMutex); - soinfo* result = do_dlopen(filename, flags); + soinfo* result = do_dlopen(filename, flags, extinfo); if (result == NULL) { __bionic_format_dlerror("dlopen failed", linker_get_error_buffer()); return NULL; @@ -74,6 +76,10 @@ void* dlopen(const char* filename, int flags) { return result; } +void* dlopen(const char* filename, int flags) { + return android_dlopen_ext(filename, flags, NULL); +} + void* dlsym(void* handle, const char* symbol) { ScopedPthreadMutexLocker locker(&gDlMutex); @@ -168,15 +174,15 @@ int dlclose(void* handle) { } #if defined(__arm__) - // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 9999900000000001 1 - // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 5678901234567890 1 + // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 9999900000000001 1111111112222222222 333333333344444444445 + // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 5678901234567890 1234567890123456789 012345678901234567890 # 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" + "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_iterate_phdr\0android_dlopen_ext\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 + // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 9999900000000001 1111111112222222222 + // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 5678901234567890 1234567890123456789 # define ANDROID_LIBDL_STRTAB \ - "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_iterate_phdr\0" + "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_iterate_phdr\0android_dlopen_ext\0" #else # error Unsupported architecture. Only arm, arm64, mips, mips64, x86 and x86_64 are presently supported. #endif @@ -195,8 +201,9 @@ static ElfW(Sym) gLibDlSymtab[] = { ELFW(SYM_INITIALIZER)( 36, &android_update_LD_LIBRARY_PATH, 1), ELFW(SYM_INITIALIZER)( 67, &android_get_LD_LIBRARY_PATH, 1), ELFW(SYM_INITIALIZER)( 95, &dl_iterate_phdr, 1), + ELFW(SYM_INITIALIZER)(111, &android_dlopen_ext, 1), #if defined(__arm__) - ELFW(SYM_INITIALIZER)(111, &dl_unwind_find_exidx, 1), + ELFW(SYM_INITIALIZER)(130, &dl_unwind_find_exidx, 1), #endif }; @@ -213,9 +220,9 @@ static ElfW(Sym) gLibDlSymtab[] = { // Note that adding any new symbols here requires stubbing them out in libdl. static unsigned gLibDlBuckets[1] = { 1 }; #if defined(__arm__) -static unsigned gLibDlChains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; +static unsigned gLibDlChains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 }; #else -static unsigned gLibDlChains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 0 }; +static unsigned gLibDlChains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 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 de7b04dfe..ba50d5ce4 100755 --- a/linker/linker.cpp +++ b/linker/linker.cpp @@ -811,11 +811,15 @@ 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, const android_dlextinfo* extinfo) { if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) { DL_ERR("invalid flags to dlopen: %x", flags); return NULL; } + if (extinfo != NULL && ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0)) { + DL_ERR("invalid extended flags to android_dlopen_ext: %x", extinfo->flags); + return NULL; + } set_soinfo_pool_protection(PROT_READ | PROT_WRITE); soinfo* si = find_library(name); if (si != NULL) { diff --git a/linker/linker.h b/linker/linker.h index b8d0587c6..93ab51d38 100644 --- a/linker/linker.h +++ b/linker/linker.h @@ -32,6 +32,7 @@ #include #include #include +#include #include "private/libc_logging.h" @@ -192,7 +193,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); +soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo); int do_dlclose(soinfo* si); ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start); -- 2.11.0