blob: fde4717144056be5d2cc74515ff502d96de02fe2 [file] [log] [blame]
Elliott Hughes3b297c42012-10-11 16:08:51 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08003 * All rights reserved.
Elliott Hughes3b297c42012-10-11 16:08:51 -07004 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
Elliott Hughes3b297c42012-10-11 16:08:51 -070014 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -080015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
Elliott Hughes3b297c42012-10-11 16:08:51 -070027 */
28
Elliott Hughes5419b942012-10-16 15:54:46 -070029#include "linker.h"
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070030#include "linker_cfi.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070031#include "linker_globals.h"
Dimitry Ivanov769b33f2016-07-21 11:33:40 -070032#include "linker_dlwarning.h"
Elliott Hughes5419b942012-10-16 15:54:46 -070033
Elliott Hughes3b297c42012-10-11 16:08:51 -070034#include <pthread.h>
35#include <stdio.h>
Elliott Hughes5419b942012-10-16 15:54:46 -070036#include <stdlib.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080037#include <string.h>
Dmitriy Ivanov19133522015-06-02 17:36:54 -070038#include <android/api-level.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070039
Elliott Hughes5419b942012-10-16 15:54:46 -070040#include <bionic/pthread_internal.h>
Elliott Hugheseb847bc2013-10-09 15:50:50 -070041#include "private/bionic_tls.h"
42#include "private/ScopedPthreadMutexLocker.h"
Elliott Hughes3b297c42012-10-11 16:08:51 -070043
Elliott Hughes212e0e32014-12-01 16:43:51 -080044static pthread_mutex_t g_dl_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
Elliott Hughes3b297c42012-10-11 16:08:51 -070045
Elliott Hughes5e071a12016-08-11 15:02:45 -070046static char* __bionic_set_dlerror(char* new_value) {
Elliott Hughes2a0b8732013-10-08 18:50:24 -070047 char** dlerror_slot = &reinterpret_cast<char**>(__get_tls())[TLS_SLOT_DLERROR];
Elliott Hughes5419b942012-10-16 15:54:46 -070048
Elliott Hughes5e071a12016-08-11 15:02:45 -070049 char* old_value = *dlerror_slot;
Elliott Hughes5419b942012-10-16 15:54:46 -070050 *dlerror_slot = new_value;
Elliott Hughes295082b2017-04-15 09:11:15 -070051 if (new_value != nullptr) LD_LOG(kLogErrors, "dlerror set to \"%s\"", new_value);
Elliott Hughes5419b942012-10-16 15:54:46 -070052 return old_value;
Elliott Hughes3b297c42012-10-11 16:08:51 -070053}
54
Elliott Hughes5419b942012-10-16 15:54:46 -070055static void __bionic_format_dlerror(const char* msg, const char* detail) {
56 char* buffer = __get_thread()->dlerror_buffer;
57 strlcpy(buffer, msg, __BIONIC_DLERROR_BUFFER_SIZE);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -070058 if (detail != nullptr) {
Elliott Hughes5419b942012-10-16 15:54:46 -070059 strlcat(buffer, ": ", __BIONIC_DLERROR_BUFFER_SIZE);
60 strlcat(buffer, detail, __BIONIC_DLERROR_BUFFER_SIZE);
61 }
62
63 __bionic_set_dlerror(buffer);
64}
65
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -080066char* __dlerror() {
Elliott Hughes5e071a12016-08-11 15:02:45 -070067 char* old_value = __bionic_set_dlerror(nullptr);
Elliott Hughes5419b942012-10-16 15:54:46 -070068 return old_value;
69}
70
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -080071void __android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Elliott Hughes1728b232014-05-14 10:02:03 -070072 ScopedPthreadMutexLocker locker(&g_dl_mutex);
Elliott Hughesa4aafd12014-01-13 16:37:47 -080073 do_android_get_LD_LIBRARY_PATH(buffer, buffer_size);
74}
75
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -080076void __android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Elliott Hughes1728b232014-05-14 10:02:03 -070077 ScopedPthreadMutexLocker locker(&g_dl_mutex);
Elliott Hughescade4c32012-12-20 14:42:14 -080078 do_android_update_LD_LIBRARY_PATH(ld_library_path);
79}
80
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -080081static void* dlopen_ext(const char* filename,
82 int flags,
83 const android_dlextinfo* extinfo,
84 const void* caller_addr) {
Elliott Hughes1728b232014-05-14 10:02:03 -070085 ScopedPthreadMutexLocker locker(&g_dl_mutex);
Dimitry Ivanovb996d602016-07-11 18:11:39 -070086 g_linker_logger.ResetState();
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -070087 void* result = do_dlopen(filename, flags, extinfo, caller_addr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -070088 if (result == nullptr) {
Elliott Hughes650be4e2013-03-05 18:47:58 -080089 __bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
Dmitriy Ivanov851135b2014-08-29 12:02:36 -070090 return nullptr;
Elliott Hughes3b297c42012-10-11 16:08:51 -070091 }
Elliott Hughes3b297c42012-10-11 16:08:51 -070092 return result;
93}
94
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -080095void* __android_dlopen_ext(const char* filename,
96 int flags,
97 const android_dlextinfo* extinfo,
98 const void* caller_addr) {
Dmitriy Ivanove5cfafe2015-07-17 10:36:10 -070099 return dlopen_ext(filename, flags, extinfo, caller_addr);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700100}
101
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800102void* __dlopen(const char* filename, int flags, const void* caller_addr) {
Dmitriy Ivanove5cfafe2015-07-17 10:36:10 -0700103 return dlopen_ext(filename, flags, nullptr, caller_addr);
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000104}
105
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800106void* dlsym_impl(void* handle, const char* symbol, const char* version, const void* caller_addr) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800107 ScopedPthreadMutexLocker locker(&g_dl_mutex);
Dimitry Ivanovb996d602016-07-11 18:11:39 -0700108 g_linker_logger.ResetState();
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800109 void* result;
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -0800110 if (!do_dlsym(handle, symbol, version, caller_addr, &result)) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800111 __bionic_format_dlerror(linker_get_error_buffer(), nullptr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700112 return nullptr;
Elliott Hughes3b297c42012-10-11 16:08:51 -0700113 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800114
115 return result;
Elliott Hughes3b297c42012-10-11 16:08:51 -0700116}
117
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800118void* __dlsym(void* handle, const char* symbol, const void* caller_addr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -0800119 return dlsym_impl(handle, symbol, nullptr, caller_addr);
120}
121
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800122void* __dlvsym(void* handle, const char* symbol, const char* version, const void* caller_addr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -0800123 return dlsym_impl(handle, symbol, version, caller_addr);
124}
125
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800126int __dladdr(const void* addr, Dl_info* info) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700127 ScopedPthreadMutexLocker locker(&g_dl_mutex);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800128 return do_dladdr(addr, info);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700129}
130
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800131int __dlclose(void* handle) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700132 ScopedPthreadMutexLocker locker(&g_dl_mutex);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700133 int result = do_dlclose(handle);
134 if (result != 0) {
135 __bionic_format_dlerror("dlclose failed", linker_get_error_buffer());
136 }
137 return result;
Elliott Hughes3b297c42012-10-11 16:08:51 -0700138}
139
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800140// This function is needed by libgcc.a (this is why there is no prefix for this one)
Andreas Gampedcb846c2016-12-06 02:10:13 +0000141int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700142 ScopedPthreadMutexLocker locker(&g_dl_mutex);
143 return do_dl_iterate_phdr(cb, data);
144}
145
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800146#if defined(__arm__)
147_Unwind_Ptr __dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
148 ScopedPthreadMutexLocker locker(&g_dl_mutex);
149 return do_dl_unwind_find_exidx(pc, pcount);
150}
151#endif
152
153void __android_set_application_target_sdk_version(uint32_t target) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -0700154 // lock to avoid modification in the middle of dlopen.
155 ScopedPthreadMutexLocker locker(&g_dl_mutex);
Dmitriy Ivanov79fd6682015-05-21 17:43:49 -0700156 set_application_target_sdk_version(target);
157}
158
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800159uint32_t __android_get_application_target_sdk_version() {
Dmitriy Ivanov79fd6682015-05-21 17:43:49 -0700160 return get_application_target_sdk_version();
161}
162
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800163void __android_dlwarning(void* obj, void (*f)(void*, const char*)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700164 ScopedPthreadMutexLocker locker(&g_dl_mutex);
165 get_dlwarning(obj, f);
166}
167
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800168bool __android_init_anonymous_namespace(const char* shared_libs_sonames,
169 const char* library_search_path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700170 ScopedPthreadMutexLocker locker(&g_dl_mutex);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800171 bool success = init_anonymous_namespace(shared_libs_sonames, library_search_path);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700172 if (!success) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800173 __bionic_format_dlerror("android_init_anonymous_namespace failed", linker_get_error_buffer());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700174 }
175
176 return success;
177}
178
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800179android_namespace_t* __android_create_namespace(const char* name,
180 const char* ld_library_path,
181 const char* default_library_path,
182 uint64_t type,
183 const char* permitted_when_isolated_path,
184 android_namespace_t* parent_namespace,
185 const void* caller_addr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700186 ScopedPthreadMutexLocker locker(&g_dl_mutex);
187
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700188 android_namespace_t* result = create_namespace(caller_addr,
189 name,
190 ld_library_path,
191 default_library_path,
192 type,
193 permitted_when_isolated_path,
194 parent_namespace);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700195
196 if (result == nullptr) {
197 __bionic_format_dlerror("android_create_namespace failed", linker_get_error_buffer());
198 }
199
200 return result;
201}
202
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800203bool __android_link_namespaces(android_namespace_t* namespace_from,
204 android_namespace_t* namespace_to,
205 const char* shared_libs_sonames) {
206 ScopedPthreadMutexLocker locker(&g_dl_mutex);
207
208 bool success = link_namespaces(namespace_from, namespace_to, shared_libs_sonames);
209
210 if (!success) {
211 __bionic_format_dlerror("android_link_namespaces failed", linker_get_error_buffer());
212 }
213
214 return success;
215}
216
Jiyong Park01de74e2017-04-03 23:10:37 +0900217android_namespace_t* __android_get_exported_namespace(const char* name) {
218 return get_exported_namespace(name);
219}
220
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700221void __cfi_fail(uint64_t CallSiteTypeId, void* Ptr, void *DiagData, void *CallerPc) {
222 CFIShadowWriter::CfiFail(CallSiteTypeId, Ptr, DiagData, CallerPc);
223}
224
Elliott Hughes3b297c42012-10-11 16:08:51 -0700225// name_offset: starting index of the name in libdl_info.strtab
226#define ELF32_SYM_INITIALIZER(name_offset, value, shndx) \
227 { name_offset, \
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700228 reinterpret_cast<Elf32_Addr>(value), \
Elliott Hughes3b297c42012-10-11 16:08:51 -0700229 /* st_size */ 0, \
Chih-Hung Hsieh1a5fd9c2016-06-10 11:07:21 -0700230 ((shndx) == 0) ? 0 : (STB_GLOBAL << 4), \
Elliott Hughes3b297c42012-10-11 16:08:51 -0700231 /* st_other */ 0, \
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700232 shndx, \
233 }
Elliott Hughes3b297c42012-10-11 16:08:51 -0700234
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700235#define ELF64_SYM_INITIALIZER(name_offset, value, shndx) \
236 { name_offset, \
Chih-Hung Hsieh1a5fd9c2016-06-10 11:07:21 -0700237 ((shndx) == 0) ? 0 : (STB_GLOBAL << 4), \
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700238 /* st_other */ 0, \
239 shndx, \
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700240 reinterpret_cast<Elf64_Addr>(value), \
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700241 /* st_size */ 0, \
242 }
243
Dmitriy Ivanov79fd6682015-05-21 17:43:49 -0700244static const char ANDROID_LIBDL_STRTAB[] =
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800245 // 0000000000111111 11112222222222333 333333344444444 44555555555566666 6666677777777778 8888888889999999999
246 // 0123456789012345 67890123456789012 345678901234567 89012345678901234 5678901234567890 1234567890123456789
247 "__loader_dlopen\0__loader_dlclose\0__loader_dlsym\0__loader_dlerror\0__loader_dladdr\0__loader_android_up"
248 // 1*
249 // 000000000011111111112 2222222223333333333444444444455555555 5566666666667777777777888 88888889999999999
250 // 012345678901234567890 1234567890123456789012345678901234567 8901234567890123456789012 34567890123456789
251 "date_LD_LIBRARY_PATH\0__loader_android_get_LD_LIBRARY_PATH\0__loader_dl_iterate_phdr\0__loader_android_"
252 // 2*
253 // 00000000001 1111111112222222222333333333344444444445555555555666 6666666777777777788888888889999999999
254 // 01234567890 1234567890123456789012345678901234567890123456789012 3456789012345678901234567890123456789
255 "dlopen_ext\0__loader_android_set_application_target_sdk_version\0__loader_android_get_application_targ"
256 // 3*
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800257 // 000000000011111 111112222222222333333333344444444445555555 5556666666666777777777788888888889 999999999
258 // 012345678901234 567890123456789012345678901234567890123456 7890123456789012345678901234567890 123456789
259 "et_sdk_version\0__loader_android_init_anonymous_namespace\0__loader_android_create_namespace\0__loader_"
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800260 // 4*
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800261 // 0000000 000111111111122222222223333 333333444444444455 555555556666666666777777777788888 888889999999999
262 // 0123456 789012345678901234567890123 456789012345678901 234567890123456789012345678901234 567890123456789
Jiyong Park01de74e2017-04-03 23:10:37 +0900263 "dlvsym\0__loader_android_dlwarning\0__loader_cfi_fail\0__loader_android_link_namespaces\0__loader_androi"
264 // 5*
265 // 0000000000111111111122222 22222
266 // 0123456789012345678901234 56789
267 "d_get_exported_namespace\0"
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800268#if defined(__arm__)
Jiyong Park01de74e2017-04-03 23:10:37 +0900269 // 525
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800270 "__loader_dl_unwind_find_exidx\0"
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800271#endif
Dmitriy Ivanov79fd6682015-05-21 17:43:49 -0700272 ;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800273
Elliott Hughes1728b232014-05-14 10:02:03 -0700274static ElfW(Sym) g_libdl_symtab[] = {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700275 // Total length of libdl_info.strtab, including trailing 0.
276 // This is actually the STH_UNDEF entry. Technically, it's
277 // supposed to have st_name == 0, but instead, it points to an index
278 // in the strtab with a \0 to make iterating through the symtab easier.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700279 ELFW(SYM_INITIALIZER)(sizeof(ANDROID_LIBDL_STRTAB) - 1, nullptr, 0),
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800280 ELFW(SYM_INITIALIZER)( 0, &__dlopen, 1),
281 ELFW(SYM_INITIALIZER)( 16, &__dlclose, 1),
282 ELFW(SYM_INITIALIZER)( 33, &__dlsym, 1),
283 ELFW(SYM_INITIALIZER)( 48, &__dlerror, 1),
284 ELFW(SYM_INITIALIZER)( 65, &__dladdr, 1),
285 ELFW(SYM_INITIALIZER)( 81, &__android_update_LD_LIBRARY_PATH, 1),
286 ELFW(SYM_INITIALIZER)(121, &__android_get_LD_LIBRARY_PATH, 1),
287 ELFW(SYM_INITIALIZER)(158, &dl_iterate_phdr, 1),
288 ELFW(SYM_INITIALIZER)(183, &__android_dlopen_ext, 1),
289 ELFW(SYM_INITIALIZER)(211, &__android_set_application_target_sdk_version, 1),
290 ELFW(SYM_INITIALIZER)(263, &__android_get_application_target_sdk_version, 1),
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800291 ELFW(SYM_INITIALIZER)(315, &__android_init_anonymous_namespace, 1),
292 ELFW(SYM_INITIALIZER)(357, &__android_create_namespace, 1),
293 ELFW(SYM_INITIALIZER)(391, &__dlvsym, 1),
294 ELFW(SYM_INITIALIZER)(407, &__android_dlwarning, 1),
295 ELFW(SYM_INITIALIZER)(434, &__cfi_fail, 1),
296 ELFW(SYM_INITIALIZER)(452, &__android_link_namespaces, 1),
Jiyong Park01de74e2017-04-03 23:10:37 +0900297 ELFW(SYM_INITIALIZER)(485, &__android_get_exported_namespace, 1),
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700298#if defined(__arm__)
Jiyong Park01de74e2017-04-03 23:10:37 +0900299 ELFW(SYM_INITIALIZER)(525, &__dl_unwind_find_exidx, 1),
Elliott Hughes3b297c42012-10-11 16:08:51 -0700300#endif
301};
302
Elliott Hughes22d62922012-10-12 10:50:21 -0700303// Fake out a hash table with a single bucket.
Elliott Hughes22d62922012-10-12 10:50:21 -0700304//
Elliott Hughes1728b232014-05-14 10:02:03 -0700305// A search of the hash table will look through g_libdl_symtab starting with index 1, then
306// use g_libdl_chains to find the next index to look at. g_libdl_chains should be set up to
307// walk through every element in g_libdl_symtab, and then end with 0 (sentinel value).
Elliott Hughes22d62922012-10-12 10:50:21 -0700308//
Elliott Hughes1728b232014-05-14 10:02:03 -0700309// That is, g_libdl_chains should look like { 0, 2, 3, ... N, 0 } where N is the number
310// of actual symbols, or nelems(g_libdl_symtab)-1 (since the first element of g_libdl_symtab is not
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800311// a real symbol). (See soinfo_elf_lookup().)
Elliott Hughes22d62922012-10-12 10:50:21 -0700312//
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800313// Note that adding any new symbols here requires stubbing them out in libdl.
Elliott Hughes1728b232014-05-14 10:02:03 -0700314static unsigned g_libdl_buckets[1] = { 1 };
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700315#if defined(__arm__)
Jiyong Park01de74e2017-04-03 23:10:37 +0900316static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0 };
Christopher Ferris24053a42013-08-19 17:45:09 -0700317#else
Jiyong Park01de74e2017-04-03 23:10:37 +0900318static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 0 };
Christopher Ferris24053a42013-08-19 17:45:09 -0700319#endif
Elliott Hughes3b297c42012-10-11 16:08:51 -0700320
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700321static uint8_t __libdl_info_buf[sizeof(soinfo)] __attribute__((aligned(8)));
322static soinfo* __libdl_info = nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700323
Elliott Hughes22d62922012-10-12 10:50:21 -0700324// This is used by the dynamic linker. Every process gets these symbols for free.
Dimitry Ivanovcd510cb2017-05-31 15:07:41 -0700325soinfo* get_libdl_info(const char* linker_path, const link_map& linker_map) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700326 if (__libdl_info == nullptr) {
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800327 __libdl_info = new (__libdl_info_buf) soinfo(&g_default_namespace, linker_path, nullptr, 0, 0);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700328 __libdl_info->flags_ |= FLAG_LINKED;
329 __libdl_info->strtab_ = ANDROID_LIBDL_STRTAB;
330 __libdl_info->symtab_ = g_libdl_symtab;
331 __libdl_info->nbucket_ = sizeof(g_libdl_buckets)/sizeof(unsigned);
332 __libdl_info->nchain_ = sizeof(g_libdl_chains)/sizeof(unsigned);
333 __libdl_info->bucket_ = g_libdl_buckets;
334 __libdl_info->chain_ = g_libdl_chains;
335 __libdl_info->ref_count_ = 1;
336 __libdl_info->strtab_size_ = sizeof(ANDROID_LIBDL_STRTAB);
337 __libdl_info->local_group_root_ = __libdl_info;
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800338 __libdl_info->soname_ = "ld-android.so";
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700339 __libdl_info->target_sdk_version_ = __ANDROID_API__;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700340 __libdl_info->generate_handle();
Dimitry Ivanovcd510cb2017-05-31 15:07:41 -0700341 __libdl_info->link_map_head.l_addr = linker_map.l_addr;
342 __libdl_info->link_map_head.l_name = linker_map.l_name;
343 __libdl_info->link_map_head.l_ld = linker_map.l_ld;
Mike Frysinger747d30e2015-10-20 14:06:25 -0400344#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700345 strlcpy(__libdl_info->old_name_, __libdl_info->soname_, sizeof(__libdl_info->old_name_));
346#endif
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700347 }
Elliott Hughes3b297c42012-10-11 16:08:51 -0700348
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700349 return __libdl_info;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700350}