blob: ae9df52aa3ea3f6b62eb3146c48ce40f7a5eec7f [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Doug Kwan94304352009-10-23 18:11:40 -07002 * Copyright (C) 2008, 2009 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * 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.
14 *
15 * 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.
27 */
28
Elliott Hughes46882792012-08-03 16:49:39 -070029#include <dlfcn.h>
30#include <errno.h>
31#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080032#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070037#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080038#include <sys/param.h>
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -080039#include <sys/personality.h>
Elliott Hughes46882792012-08-03 16:49:39 -070040#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070042#include <new>
43
Elliott Hughes46882792012-08-03 16:49:39 -070044// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070045#include "private/bionic_tls.h"
46#include "private/KernelArgumentBlock.h"
47#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070048#include "private/ScopedFd.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070049#include "private/ScopeGuard.h"
50#include "private/UniquePtr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051
52#include "linker.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080053#include "linker_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010055#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020056#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080057#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080058#include "linker_reloc_iterators.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
61 *
62 * Do NOT use malloc() and friends or pthread_*() code here.
63 * Don't use printf() either; it's caused mysterious memory
64 * corruption in the past.
65 * The linker runs before we bring up libc and it's easiest
66 * to make sure it does not depend on any complex libc features
67 *
68 * open issues / todo:
69 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071 * - after linking, set as much stuff as possible to READONLY
72 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070073 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080075// Override macros to use C++ style casts
76#undef ELF_ST_TYPE
77#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
78
Dmitriy Ivanov489e4982014-05-19 15:19:52 -070079#if defined(__LP64__)
80#define SEARCH_NAME(x) x
81#else
82// Nvidia drivers are relying on the bug:
83// http://code.google.com/p/android/issues/detail?id=6670
84// so we continue to use base-name lookup for lp32
85static const char* get_base_name(const char* name) {
86 const char* bname = strrchr(name, '/');
87 return bname ? bname + 1 : name;
88}
89#define SEARCH_NAME(x) get_base_name(x)
90#endif
91
Elliott Hughes0266ae52014-02-10 17:46:57 -080092static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
Elliott Hughes1728b232014-05-14 10:02:03 -070094static LinkerAllocator<soinfo> g_soinfo_allocator;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070095static LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020096
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070097static soinfo* solist;
98static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -070099static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800100
Elliott Hughes1728b232014-05-14 10:02:03 -0700101static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700102#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700103 "/vendor/lib64",
104 "/system/lib64",
105#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700106 "/vendor/lib",
107 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700108#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700109 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700110};
David Bartleybc3a5c22009-06-02 18:27:28 -0700111
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800112#define LDPATH_BUFSIZE (LDPATH_MAX*64)
113#define LDPATH_MAX 8
114
115#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
116#define LDPRELOAD_MAX 8
117
Elliott Hughes1728b232014-05-14 10:02:03 -0700118static char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
119static const char* g_ld_library_paths[LDPATH_MAX + 1];
Elliott Hughes124fae92012-10-31 14:20:03 -0700120
Elliott Hughes1728b232014-05-14 10:02:03 -0700121static char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
122static const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600123
Elliott Hughes1728b232014-05-14 10:02:03 -0700124static soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600125
Elliott Hughes1728b232014-05-14 10:02:03 -0700126__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700128__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700129
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700131struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700132 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700133};
134
135static linker_stats_t linker_stats;
136
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800137void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700138 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700139}
140#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800141void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700142}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143#endif
144
145#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800146uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800147#endif
148
Elliott Hughes46882792012-08-03 16:49:39 -0700149// You shouldn't try to call memory-allocating functions in the dynamic linker.
150// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700151#define DISALLOW_ALLOCATION(return_type, name, ...) \
152 return_type name __VA_ARGS__ \
153 { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700154 __libc_fatal("ERROR: " #name " called from the dynamic linker!\n"); \
Dima Zavin2e855792009-05-20 18:28:09 -0700155 }
Kito Cheng812fd422014-03-25 22:53:56 +0800156DISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
157DISALLOW_ALLOCATION(void, free, (void* u __unused));
158DISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
159DISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
Dima Zavin2e855792009-05-20 18:28:09 -0700160
161static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700162
Elliott Hughes650be4e2013-03-05 18:47:58 -0800163char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700164 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700165}
166
Elliott Hughes650be4e2013-03-05 18:47:58 -0800167size_t linker_get_error_buffer_size() {
168 return sizeof(__linker_dl_err_buf);
169}
170
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700171// This function is an empty stub where GDB locates a breakpoint to get notified
172// about linker activity.
Elliott Hughes5419b942012-10-16 15:54:46 -0700173extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174
Elliott Hughes1728b232014-05-14 10:02:03 -0700175static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700176static r_debug _r_debug = {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800177static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800179static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700180 // Copy the necessary fields into the debug structure.
181 link_map* map = &(info->link_map_head);
182 map->l_addr = info->load_bias;
Dmitriy Ivanovc9d16582014-10-24 14:46:12 -0700183 map->l_name = info->name;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700184 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700186 // Stick the new library at the end of the list.
187 // gdb tends to care more about libc than it does
188 // about leaf libraries, and ordering it this way
189 // reduces the back-and-forth over the wire.
190 if (r_debug_tail) {
191 r_debug_tail->l_next = map;
192 map->l_prev = r_debug_tail;
193 map->l_next = 0;
194 } else {
195 _r_debug.r_map = map;
196 map->l_prev = 0;
197 map->l_next = 0;
198 }
199 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200}
201
Elliott Hughesbedfe382012-08-14 14:07:59 -0700202static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700203 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700204
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700205 if (r_debug_tail == map) {
206 r_debug_tail = map->l_prev;
207 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700208
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700209 if (map->l_prev) {
210 map->l_prev->l_next = map->l_next;
211 }
212 if (map->l_next) {
213 map->l_next->l_prev = map->l_prev;
214 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700215}
216
Elliott Hughesbedfe382012-08-14 14:07:59 -0700217static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800218 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700219 // GDB already knows about the main executable
220 return;
221 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700223 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700225 _r_debug.r_state = r_debug::RT_ADD;
226 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700228 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700230 _r_debug.r_state = r_debug::RT_CONSISTENT;
231 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700232}
233
Elliott Hughesbedfe382012-08-14 14:07:59 -0700234static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800235 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700236 // GDB already knows about the main executable
237 return;
238 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700239
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700240 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700241
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700242 _r_debug.r_state = r_debug::RT_DELETE;
243 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700244
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700245 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700246
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700247 _r_debug.r_state = r_debug::RT_CONSISTENT;
248 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800249}
250
Elliott Hughes18a206c2012-10-29 17:37:13 -0700251void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800252 _r_debug.r_state = r_debug::RT_ADD;
253 rtld_db_dlactivity();
254 _r_debug.r_state = r_debug::RT_CONSISTENT;
255 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800256}
257
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700258LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
259 return g_soinfo_links_allocator.alloc();
260}
261
262void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
263 g_soinfo_links_allocator.free(entry);
264}
265
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700266static soinfo* soinfo_alloc(const char* name, struct stat* file_stat, off64_t file_offset, uint32_t rtld_flags) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200267 if (strlen(name) >= SOINFO_NAME_LEN) {
268 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700269 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200270 }
271
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700272 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700273
Magnus Malmbornba98d922012-09-12 13:00:55 +0200274 sonext->next = si;
275 sonext = si;
276
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700277 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200278 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800279}
280
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800281static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700282 if (si == nullptr) {
283 return;
284 }
285
286 if (si->base != 0 && si->size != 0) {
287 munmap(reinterpret_cast<void*>(si->base), si->size);
288 }
289
290 soinfo *prev = nullptr, *trav;
291
292 TRACE("name %s: freeing soinfo @ %p", si->name, si);
293
294 for (trav = solist; trav != nullptr; trav = trav->next) {
295 if (trav == si) {
296 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700297 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700298 prev = trav;
299 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800300
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700301 if (trav == nullptr) {
302 // si was not in solist
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -0800303 DL_ERR("name \"%s\"@%p is not in solist!", si->name, si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700304 return;
305 }
Elliott Hughes46882792012-08-03 16:49:39 -0700306
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700307 // clear links to/from si
308 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700309
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700310 // prev will never be null, because the first entry in solist is
311 // always the static libdl_info.
312 prev->next = si->next;
313 if (si == sonext) {
314 sonext = prev;
315 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800316
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700317 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800318}
319
Elliott Hughescade4c32012-12-20 14:42:14 -0800320static void parse_path(const char* path, const char* delimiters,
321 const char** array, char* buf, size_t buf_size, size_t max_count) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700322 if (path == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800323 return;
324 }
325
326 size_t len = strlcpy(buf, path, buf_size);
327
328 size_t i = 0;
329 char* buf_p = buf;
330 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
331 if (*array[i] != '\0') {
332 ++i;
333 }
334 }
335
336 // Forget the last path if we had to truncate; this occurs if the 2nd to
337 // last char isn't '\0' (i.e. wasn't originally a delimiter).
338 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700339 array[i - 1] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800340 } else {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700341 array[i] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800342 }
343}
344
345static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700346 parse_path(path, ":", g_ld_library_paths,
347 g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800348}
349
350static void parse_LD_PRELOAD(const char* path) {
351 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes1728b232014-05-14 10:02:03 -0700352 parse_path(path, " :", g_ld_preload_names,
353 g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800354}
355
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700356#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700357
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700358// For a given PC, find the .so that it belongs to.
359// Returns the base address of the .ARM.exidx section
360// for that .so, and the number of 8-byte entries
361// in that section (via *pcount).
362//
363// Intended to be called by libc's __gnu_Unwind_Find_exidx().
364//
365// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800366_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800367 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800368
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700369 for (soinfo* si = solist; si != 0; si = si->next) {
370 if ((addr >= si->base) && (addr < (si->base + si->size))) {
371 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800372 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800373 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700374 }
375 *pcount = 0;
376 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800377}
Elliott Hughes46882792012-08-03 16:49:39 -0700378
Christopher Ferris24053a42013-08-19 17:45:09 -0700379#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700380
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700381// Here, we only have to provide a callback to iterate across all the
382// loaded libraries. gcc_eh does the rest.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800383int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700384 int rv = 0;
385 for (soinfo* si = solist; si != nullptr; si = si->next) {
386 dl_phdr_info dl_info;
387 dl_info.dlpi_addr = si->link_map_head.l_addr;
388 dl_info.dlpi_name = si->link_map_head.l_name;
389 dl_info.dlpi_phdr = si->phdr;
390 dl_info.dlpi_phnum = si->phnum;
391 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
392 if (rv != 0) {
393 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800394 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700395 }
396 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800397}
Elliott Hughes46882792012-08-03 16:49:39 -0700398
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800399ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name) {
400 return is_gnu_hash() ? gnu_lookup(symbol_name) : elf_lookup(symbol_name);
401}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800403static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
404 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
405 ELF_ST_BIND(s->st_info) == STB_WEAK) {
406 return s->st_shndx != SHN_UNDEF;
407 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
408 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
409 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->name);
410 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800412 return false;
413}
414
415ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name) {
416 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800417 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800418
419 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800420 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
421 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800422
423 // test against bloom filter
424 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
425 return nullptr;
426 }
427
428 // bloom test says "probably yes"...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800429 uint32_t n = bucket_[hash % nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800430
431 if (n == 0) {
432 return nullptr;
433 }
434
435 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800436 ElfW(Sym)* s = symtab_ + n;
437 if (((chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800438 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
439 is_symbol_global_and_defined(this, s)) {
440 return s;
441 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800442 } while ((chain_[n++] & 1) == 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800443
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800444 return nullptr;
445}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800446
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800447ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name) {
448 uint32_t hash = symbol_name.elf_hash();
449
450 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800451 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800452
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800453 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
454 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800455 if (strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && is_symbol_global_and_defined(this, s)) {
456 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
457 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
458 static_cast<size_t>(s->st_size));
459 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800460 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800461 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800462
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700463 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800464 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700465
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700466 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800467}
468
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700469soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700470 memset(this, 0, sizeof(*this));
471
472 strlcpy(this->name, name, sizeof(this->name));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800473 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800474 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700475
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700476 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800477 this->st_dev_ = file_stat->st_dev;
478 this->st_ino_ = file_stat->st_ino;
479 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700480 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700481
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800482 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700483}
484
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800485
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800486uint32_t SymbolName::elf_hash() {
487 if (!has_elf_hash_) {
488 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
489 uint32_t h = 0, g;
490
491 while (*name) {
492 h = (h << 4) + *name++;
493 g = h & 0xf0000000;
494 h ^= g;
495 h ^= g >> 24;
496 }
497
498 elf_hash_ = h;
499 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700500 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800501
502 return elf_hash_;
503}
504
505uint32_t SymbolName::gnu_hash() {
506 if (!has_gnu_hash_) {
507 uint32_t h = 5381;
508 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
509 while (*name != 0) {
510 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
511 }
512
513 gnu_hash_ = h;
514 has_gnu_hash_ = true;
515 }
516
517 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800518}
519
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800520ElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700521 const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800522 SymbolName symbol_name(name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700523 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700524
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700525 /* "This element's presence in a shared object library alters the dynamic linker's
526 * symbol resolution algorithm for references within the library. Instead of starting
527 * a symbol search with the executable file, the dynamic linker starts from the shared
528 * object itself. If the shared object fails to supply the referenced symbol, the
529 * dynamic linker then searches the executable file and other shared objects as usual."
530 *
531 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
532 *
533 * Note that this is unlikely since static linker avoids generating
534 * relocations for -Bsymbolic linked dynamic executables.
535 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700536 if (si_from->has_DT_SYMBOLIC) {
537 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->name, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800538 s = si_from->find_symbol_by_name(symbol_name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700539 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700540 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700541 }
542 }
543
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700544 // 1. Look for it in global_group
545 if (s == nullptr) {
546 global_group.visit([&](soinfo* global_si) {
547 DEBUG("%s: looking up %s in %s (from global group)", si_from->name, name, global_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800548 s = global_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700549 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700550 *si_found_in = global_si;
551 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700552 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700553
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700554 return true;
555 });
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700556 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700557
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700558 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700559 if (s == nullptr) {
560 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700561 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700562 // we already did this - skip
563 return true;
564 }
565
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700566 DEBUG("%s: looking up %s in %s (from local group)", si_from->name, name, local_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800567 s = local_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700568 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700569 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700570 return false;
571 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700572
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700573 return true;
574 });
575 }
576
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700577 if (s != nullptr) {
578 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
579 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700580 si_from->name, name, reinterpret_cast<void*>(s->st_value),
581 (*si_found_in)->name, reinterpret_cast<void*>((*si_found_in)->base),
582 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700583 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700584
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700585 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700586}
587
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800588class ProtectedDataGuard {
589 public:
590 ProtectedDataGuard() {
591 if (ref_count_++ == 0) {
592 protect_data(PROT_READ | PROT_WRITE);
593 }
594 }
595
596 ~ProtectedDataGuard() {
597 if (ref_count_ == 0) { // overflow
598 __libc_fatal("Too many nested calls to dlopen()");
599 }
600
601 if (--ref_count_ == 0) {
602 protect_data(PROT_READ);
603 }
604 }
605 private:
606 void protect_data(int protection) {
607 g_soinfo_allocator.protect_all(protection);
608 g_soinfo_links_allocator.protect_all(protection);
609 }
610
611 static size_t ref_count_;
612};
613
614size_t ProtectedDataGuard::ref_count_ = 0;
615
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700616// Each size has it's own allocator.
617template<size_t size>
618class SizeBasedAllocator {
619 public:
620 static void* alloc() {
621 return allocator_.alloc();
622 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700623
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700624 static void free(void* ptr) {
625 allocator_.free(ptr);
626 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700627
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700628 private:
629 static LinkerBlockAllocator allocator_;
630};
631
632template<size_t size>
633LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
634
635template<typename T>
636class TypeBasedAllocator {
637 public:
638 static T* alloc() {
639 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
640 }
641
642 static void free(T* ptr) {
643 SizeBasedAllocator<sizeof(T)>::free(ptr);
644 }
645};
646
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700647class LoadTask {
648 public:
649 struct deleter_t {
650 void operator()(LoadTask* t) {
651 TypeBasedAllocator<LoadTask>::free(t);
652 }
653 };
654
655 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
656
657 static deleter_t deleter;
658
659 static LoadTask* create(const char* name, soinfo* needed_by) {
660 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
661 return new (ptr) LoadTask(name, needed_by);
662 }
663
664 const char* get_name() const {
665 return name_;
666 }
667
668 soinfo* get_needed_by() const {
669 return needed_by_;
670 }
671 private:
672 LoadTask(const char* name, soinfo* needed_by)
673 : name_(name), needed_by_(needed_by) {}
674
675 const char* name_;
676 soinfo* needed_by_;
677
678 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
679};
680
Ningsheng Jiane93be992014-09-16 15:22:10 +0800681LoadTask::deleter_t LoadTask::deleter;
682
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700683template <typename T>
684using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
685
686typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700687typedef linked_list_t<const char> StringLinkedList;
688typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700689
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700690
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700691// This function walks down the tree of soinfo dependencies
692// in breadth-first order and
693// * calls action(soinfo* si) for each node, and
694// * terminates walk if action returns false.
695//
696// walk_dependencies_tree returns false if walk was terminated
697// by the action and true otherwise.
698template<typename F>
699static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700700 SoinfoLinkedList visit_list;
701 SoinfoLinkedList visited;
702
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700703 for (size_t i = 0; i < root_soinfos_size; ++i) {
704 visit_list.push_back(root_soinfos[i]);
705 }
706
707 soinfo* si;
708 while ((si = visit_list.pop_front()) != nullptr) {
709 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700710 continue;
711 }
712
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700713 if (!action(si)) {
714 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700715 }
716
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700717 visited.push_back(si);
718
719 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700720 visit_list.push_back(child);
721 });
722 }
723
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700724 return true;
725}
726
727
728// This is used by dlsym(3). It performs symbol lookup only within the
729// specified soinfo object and its dependencies in breadth first order.
730ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
731 ElfW(Sym)* result = nullptr;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800732 SymbolName symbol_name(name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700733
734
735 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800736 result = current_soinfo->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700737 if (result != nullptr) {
738 *found = current_soinfo;
739 return false;
740 }
741
742 return true;
743 });
744
745 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800746}
747
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800748/* This is used by dlsym(3) to performs a global symbol lookup. If the
749 start value is null (for RTLD_DEFAULT), the search starts at the
750 beginning of the global solist. Otherwise the search starts at the
751 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700752 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700753ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800754 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800755
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700756 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800757 start = solist;
758 }
759
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700760 ElfW(Sym)* s = nullptr;
761 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700762 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
763 continue;
764 }
765
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800766 s = si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700767 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800768 *found = si;
769 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600770 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800771 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600772
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700773 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700774 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
775 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800776 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800777
Elliott Hughescade4c32012-12-20 14:42:14 -0800778 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800779}
780
Kito Chengfa8c05d2013-03-12 14:58:06 +0800781soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800782 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700783 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800784 if (address >= si->base && address - si->base < si->size) {
785 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600786 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800787 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700788 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600789}
790
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800791ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
792 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
793}
794
795static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
796 return sym->st_shndx != SHN_UNDEF &&
797 soaddr >= sym->st_value &&
798 soaddr < sym->st_value + sym->st_size;
799}
800
801ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800802 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800803
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800804 for (size_t i = 0; i < nbucket_; ++i) {
805 uint32_t n = bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800806
807 if (n == 0) {
808 continue;
809 }
810
811 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800812 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800813 if (symbol_matches_soaddr(sym, soaddr)) {
814 return sym;
815 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800816 } while ((chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800817 }
818
819 return nullptr;
820}
821
822ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800823 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600824
Kito Chengfa8c05d2013-03-12 14:58:06 +0800825 // Search the library's symbol table for any defined symbol which
826 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800827 for (size_t i = 0; i < nchain_; ++i) {
828 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800829 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800830 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600831 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800832 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600833
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700834 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600835}
836
Elliott Hughes124fae92012-10-31 14:20:03 -0700837static int open_library_on_path(const char* name, const char* const paths[]) {
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +0000838 char buf[512];
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700839 for (size_t i = 0; paths[i] != nullptr; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800840 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700841 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700842 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700843 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800844 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700845 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
846 if (fd != -1) {
847 return fd;
848 }
849 }
850 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800851}
852
Elliott Hughes124fae92012-10-31 14:20:03 -0700853static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700854 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800855
Elliott Hughes124fae92012-10-31 14:20:03 -0700856 // If the name contains a slash, we should attempt to open it directly and not search the paths.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700857 if (strchr(name, '/') != nullptr) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700858 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
859 if (fd != -1) {
860 return fd;
861 }
862 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700863#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700864 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700865#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700866 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800867
Elliott Hughes124fae92012-10-31 14:20:03 -0700868 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Elliott Hughes1728b232014-05-14 10:02:03 -0700869 int fd = open_library_on_path(name, g_ld_library_paths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700870 if (fd == -1) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700871 fd = open_library_on_path(name, kDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700872 }
873 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800874}
875
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700876template<typename F>
877static void for_each_dt_needed(const soinfo* si, F action) {
878 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
879 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700880 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700881 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700882 }
883}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800884
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700885static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700886 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700887 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700888 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700889
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700890 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
891 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700892 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
893 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700894 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700895 } else {
896 // Open the file.
897 fd = open_library(name);
898 if (fd == -1) {
899 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700900 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700901 }
902
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700903 file_guard.reset(fd);
904 }
905
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700906 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700907 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700908 return nullptr;
909 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800910 if (file_offset < 0) {
911 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
912 return nullptr;
913 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700914
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700915 struct stat file_stat;
916 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700917 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700918 return nullptr;
919 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800920 if (file_offset >= file_stat.st_size) {
921 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64, name, file_offset, file_stat.st_size);
922 return nullptr;
923 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700924
925 // Check for symlink and other situations where
926 // file can have different names.
927 for (soinfo* si = solist; si != nullptr; si = si->next) {
928 if (si->get_st_dev() != 0 &&
929 si->get_st_ino() != 0 &&
930 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700931 si->get_st_ino() == file_stat.st_ino &&
932 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700933 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
934 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700935 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700936 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700937
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700938 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700939 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700940 return nullptr;
941 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700942
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700943 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700944 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700945 if (!elf_reader.Load(extinfo)) {
946 return nullptr;
947 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800948
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700949 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700950 if (si == nullptr) {
951 return nullptr;
952 }
953 si->base = elf_reader.load_start();
954 si->size = elf_reader.load_size();
955 si->load_bias = elf_reader.load_bias();
956 si->phnum = elf_reader.phdr_count();
957 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700958
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800959 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700960 soinfo_free(si);
961 return nullptr;
962 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700963
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700964 for_each_dt_needed(si, [&] (const char* name) {
965 load_tasks.push_back(LoadTask::create(name, si));
966 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700967
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700968 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800969}
970
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700971static soinfo *find_loaded_library_by_name(const char* name) {
972 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700973 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700974 if (!strcmp(search_name, si->name)) {
975 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200976 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700977 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700978 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200979}
980
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700981static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200982
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700983 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700984
985 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700986 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700987 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700988 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700989 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700990 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800991
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700992 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800993}
994
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700995static void soinfo_unload(soinfo* si);
996
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700997// TODO: this is slightly unusual way to construct
998// the global group for relocation. Not every RTLD_GLOBAL
999// library is included in this group for backwards-compatibility
1000// reasons.
1001//
1002// This group consists of the main executable, LD_PRELOADs
1003// and libraries with the DF_1_GLOBAL flag set.
1004static soinfo::soinfo_list_t make_global_group() {
1005 soinfo::soinfo_list_t global_group;
1006 for (soinfo* si = somain; si != nullptr; si = si->next) {
1007 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1008 global_group.push_back(si);
1009 }
1010 }
1011
1012 return global_group;
1013}
1014
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001015static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
1016 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001017 // Step 0: prepare.
1018 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001019 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001020 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001021 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001022 }
1023
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001024 // Construct global_group.
1025 soinfo::soinfo_list_t global_group = make_global_group();
1026
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001027 // If soinfos array is null allocate one on stack.
1028 // The array is needed in case of failure; for example
1029 // when library_names[] = {libone.so, libtwo.so} and libone.so
1030 // is loaded correctly but libtwo.so failed for some reason.
1031 // In this case libone.so should be unloaded on return.
1032 // See also implementation of failure_guard below.
1033
1034 if (soinfos == nullptr) {
1035 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1036 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1037 memset(soinfos, 0, soinfos_size);
1038 }
1039
1040 // list of libraries to link - see step 2.
1041 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001042
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001043 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001044 // Housekeeping
1045 load_tasks.for_each([] (LoadTask* t) {
1046 LoadTask::deleter(t);
1047 });
1048
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001049 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001050 soinfo_unload(soinfos[i]);
1051 }
1052 });
1053
1054 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
1055 for (LoadTask::unique_ptr task(load_tasks.pop_front()); task.get() != nullptr; task.reset(load_tasks.pop_front())) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001056 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001057 if (si == nullptr) {
1058 return false;
1059 }
1060
1061 soinfo* needed_by = task->get_needed_by();
1062
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001063 if (needed_by != nullptr) {
1064 needed_by->add_child(si);
1065 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001066
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001067 if (si->is_linked()) {
1068 si->increment_ref_count();
1069 }
1070
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001071 // When ld_preloads is not null, the first
1072 // ld_preloads_count libs are in fact ld_preloads.
1073 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001074 // Add LD_PRELOADed libraries to the global group for future runs.
1075 // There is no need to explicitly add them to the global group
1076 // for this run because they are going to appear in the local
1077 // group in the correct order.
1078 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001079 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001080 }
1081
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001082 if (soinfos_count < library_names_count) {
1083 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001084 }
1085 }
1086
1087 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001088 soinfo::soinfo_list_t local_group;
1089 walk_dependencies_tree(
1090 start_with == nullptr ? soinfos : &start_with,
1091 start_with == nullptr ? soinfos_count : 1,
1092 [&] (soinfo* si) {
1093 local_group.push_back(si);
1094 return true;
1095 });
1096
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001097 // We need to increment ref_count in case
1098 // the root of the local group was not linked.
1099 bool was_local_group_root_linked = local_group.front()->is_linked();
1100
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001101 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001102 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001103 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001104 return false;
1105 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001106 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001107 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001108
1109 return true;
1110 });
1111
1112 if (linked) {
1113 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001114 }
1115
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001116 if (!was_local_group_root_linked) {
1117 local_group.front()->increment_ref_count();
1118 }
1119
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001120 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001121}
1122
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001123static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001124 soinfo* si;
1125
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001126 if (name == nullptr) {
1127 si = somain;
1128 } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001129 return nullptr;
1130 }
1131
Elliott Hughesd23736e2012-11-01 15:16:56 -07001132 return si;
1133}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001134
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001135static void soinfo_unload(soinfo* root) {
1136 // Note that the library can be loaded but not linked;
1137 // in which case there is no root but we still need
1138 // to walk the tree and unload soinfos involved.
1139 //
1140 // This happens on unsuccessful dlopen, when one of
1141 // the DT_NEEDED libraries could not be linked/found.
1142 if (root->is_linked()) {
1143 root = root->get_local_group_root();
1144 }
1145
1146 if (!root->can_unload()) {
1147 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->name);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001148 return;
1149 }
1150
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001151 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001152
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001153 if (ref_count == 0) {
1154 soinfo::soinfo_list_t local_unload_list;
1155 soinfo::soinfo_list_t external_unload_list;
1156 soinfo::soinfo_list_t depth_first_list;
1157 depth_first_list.push_back(root);
1158 soinfo* si = nullptr;
1159
1160 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001161 if (local_unload_list.contains(si)) {
1162 continue;
1163 }
1164
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001165 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001166
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001167 if (si->has_min_version(0)) {
1168 soinfo* child = nullptr;
1169 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001170 TRACE("%s@%p needs to unload %s@%p", si->name, si, child->name, child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001171 if (local_unload_list.contains(child)) {
1172 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001173 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001174 external_unload_list.push_back(child);
1175 } else {
1176 depth_first_list.push_front(child);
1177 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001178 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001179 } else {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001180#ifdef __LP64__
1181 __libc_fatal("soinfo for \"%s\"@%p has no version", si->name, si);
1182#else
1183 PRINT("warning: soinfo for \"%s\"@%p has no version", si->name, si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001184 for_each_dt_needed(si, [&] (const char* library_name) {
1185 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1186 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1187 if (needed != nullptr) {
1188 // Not found: for example if symlink was deleted between dlopen and dlclose
1189 // Since we cannot really handle errors at this point - print and continue.
1190 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
1191 return;
1192 } else if (local_unload_list.contains(needed)) {
1193 // already visited
1194 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001195 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001196 // external group
1197 external_unload_list.push_back(needed);
1198 } else {
1199 // local group
1200 depth_first_list.push_front(needed);
1201 }
1202 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001203#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001204 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001205 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001206
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001207 local_unload_list.for_each([](soinfo* si) {
1208 si->call_destructors();
1209 });
1210
1211 while ((si = local_unload_list.pop_front()) != nullptr) {
1212 notify_gdb_of_unload(si);
1213 soinfo_free(si);
1214 }
1215
1216 while ((si = external_unload_list.pop_front()) != nullptr) {
1217 soinfo_unload(si);
1218 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001219 } else {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001220 TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->name, ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001221 }
1222}
1223
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001224void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001225 // Use basic string manipulation calls to avoid snprintf.
1226 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1227 // When debug malloc is enabled, this call returns 0. This in turn causes
1228 // snprintf to do nothing, which causes libraries to fail to load.
1229 // See b/17302493 for further details.
1230 // Once the above bug is fixed, this code can be modified to use
1231 // snprintf again.
1232 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1233 if (buffer_size < required_len) {
1234 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1235 buffer_size, required_len);
1236 }
1237 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1238 *end = ':';
1239 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001240}
1241
Elliott Hughescade4c32012-12-20 14:42:14 -08001242void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
1243 if (!get_AT_SECURE()) {
1244 parse_LD_LIBRARY_PATH(ld_library_path);
1245 }
1246}
1247
Elliott Hughes1a586292014-06-03 16:23:08 -07001248soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001249 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001250 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001251 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001252 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001253 if (extinfo != nullptr) {
1254 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1255 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1256 return nullptr;
1257 }
1258 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001259 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1260 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001261 return nullptr;
1262 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001263 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001264
1265 ProtectedDataGuard guard;
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001266 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001267 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001268 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001269 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001270 return si;
1271}
1272
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001273void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001274 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001275 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001276}
1277
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001278static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1279 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1280 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1281 ElfW(Addr) ifunc_addr = ifunc_resolver();
1282 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p", ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001283
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001284 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001285}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001286
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001287#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001288#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001289static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
1290 return rela->r_addend;
1291}
1292#else
1293static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
1294 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE || ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
1295 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
1296 }
1297 return 0;
1298}
1299#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001300
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001301template<typename ElfRelIteratorT>
1302bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
1303 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
1304 const auto rel = rel_iterator.next();
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001305 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
1306 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
1307
1308 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001309 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001310 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001311 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001312
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001313 DEBUG("Processing '%s' relocation at index %zd", this->name, idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001314 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001315 continue;
1316 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001317
1318 ElfW(Sym)* s = nullptr;
1319 soinfo* lsi = nullptr;
1320
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001321 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001322 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001323 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001324 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001325 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001326 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001327 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001328 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001329 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001330 }
1331
1332 /* IHI0044C AAELF 4.5.1.1:
1333
1334 Libraries are not searched to resolve weak references.
1335 It is not an error for a weak reference to remain unsatisfied.
1336
1337 During linking, the value of an undefined weak reference is:
1338 - Zero if the relocation type is absolute
1339 - The address of the place if the relocation is pc-relative
1340 - The address of nominal base address if the relocation
1341 type is base-relative.
1342 */
1343
1344 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001345 case R_GENERIC_JUMP_SLOT:
1346 case R_GENERIC_GLOB_DAT:
1347 case R_GENERIC_RELATIVE:
1348 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001349#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001350 case R_AARCH64_ABS64:
1351 case R_AARCH64_ABS32:
1352 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001353#elif defined(__x86_64__)
1354 case R_X86_64_32:
1355 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001356#elif defined(__arm__)
1357 case R_ARM_ABS32:
1358#elif defined(__i386__)
1359 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001360#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001361 /*
1362 * The sym_addr was initialized to be zero above, or the relocation
1363 * code below does not care about value of sym_addr.
1364 * No need to do anything.
1365 */
1366 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001367#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00001368 case R_X86_64_PC32:
1369 sym_addr = reloc;
1370 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001371#elif defined(__i386__)
1372 case R_386_PC32:
1373 sym_addr = reloc;
1374 break;
1375#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001376 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001377 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001378 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001379 }
1380 } else {
1381 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001382 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001383 }
1384 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001385 }
1386
1387 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001388 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001389 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001390 MARK(rel->r_offset);
1391 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
1392 reinterpret_cast<void*>(reloc),
1393 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1394
1395 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001396 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001397 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001398 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001399 MARK(rel->r_offset);
1400 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
1401 reinterpret_cast<void*>(reloc),
1402 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1403 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001404 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001405 case R_GENERIC_RELATIVE:
1406 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001407 MARK(rel->r_offset);
1408 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
1409 reinterpret_cast<void*>(reloc),
1410 reinterpret_cast<void*>(base + addend));
1411 *reinterpret_cast<ElfW(Addr)*>(reloc) = (base + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001412 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001413 case R_GENERIC_IRELATIVE:
1414 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001415 MARK(rel->r_offset);
1416 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
1417 reinterpret_cast<void*>(reloc),
1418 reinterpret_cast<void*>(base + addend));
1419 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001420 break;
1421
1422#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001423 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001424 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001425 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001426 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001427 reloc, (sym_addr + addend), sym_name);
1428 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001429 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001430 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001431 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001432 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001433 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001434 reloc, (sym_addr + addend), sym_name);
1435 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1436 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1437 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001438 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001439 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001440 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001441 static_cast<ElfW(Addr)>(INT32_MIN),
1442 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001443 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001444 }
1445 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001446 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001447 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001448 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001449 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001450 reloc, (sym_addr + addend), sym_name);
1451 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1452 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1453 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001454 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001455 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001456 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001457 static_cast<ElfW(Addr)>(INT16_MIN),
1458 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001459 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001460 }
1461 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001462 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001463 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001464 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001465 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001466 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1467 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend) - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001468 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001469 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001470 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001471 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001472 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001473 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1474 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1475 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1476 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001477 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001478 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001479 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001480 static_cast<ElfW(Addr)>(INT32_MIN),
1481 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001482 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001483 }
1484 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001485 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001486 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001487 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001488 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001489 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1490 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1491 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1492 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001493 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001494 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001495 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001496 static_cast<ElfW(Addr)>(INT16_MIN),
1497 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001498 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001499 }
1500 break;
1501
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001502 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001503 /*
1504 * ET_EXEC is not supported so this should not happen.
1505 *
1506 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1507 *
1508 * Section 4.7.1.10 "Dynamic relocations"
1509 * R_AARCH64_COPY may only appear in executable objects where e_type is
1510 * set to ET_EXEC.
1511 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001512 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001513 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001514 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001515 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001516 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001517 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001518 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001519 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001520 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001521 break;
1522#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001523 case R_X86_64_32:
1524 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001525 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001526 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1527 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001528 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001529 break;
1530 case R_X86_64_64:
1531 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001532 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001533 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1534 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001535 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001536 break;
1537 case R_X86_64_PC32:
1538 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001539 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001540 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1541 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1542 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001543 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001544 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001545#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001546 case R_ARM_ABS32:
1547 count_relocation(kRelocAbsolute);
1548 MARK(rel->r_offset);
1549 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1550 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1551 break;
1552 case R_ARM_REL32:
1553 count_relocation(kRelocRelative);
1554 MARK(rel->r_offset);
1555 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1556 reloc, sym_addr, rel->r_offset, sym_name);
1557 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1558 break;
1559 case R_ARM_COPY:
1560 /*
1561 * ET_EXEC is not supported so this should not happen.
1562 *
1563 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1564 *
1565 * Section 4.7.1.10 "Dynamic relocations"
1566 * R_ARM_COPY may only appear in executable objects where e_type is
1567 * set to ET_EXEC.
1568 */
1569 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001570 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001571#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001572 case R_386_32:
1573 count_relocation(kRelocRelative);
1574 MARK(rel->r_offset);
1575 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1576 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1577 break;
1578 case R_386_PC32:
1579 count_relocation(kRelocRelative);
1580 MARK(rel->r_offset);
1581 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1582 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1583 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1584 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001585#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001586 default:
1587 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001588 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001589 }
1590 }
1591 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001592}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001593#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001594
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001595void soinfo::call_array(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001596 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001597 return;
1598 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001599
Elliott Hughesc6200592013-09-30 18:43:46 -07001600 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001601
1602 int begin = reverse ? (count - 1) : 0;
1603 int end = reverse ? -1 : count;
1604 int step = reverse ? -1 : 1;
1605
1606 for (int i = begin; i != end; i += step) {
1607 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001608 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001609 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001610
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001611 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001612}
1613
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001614void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001615 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001616 return;
1617 }
1618
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001619 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001620 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001621 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001622}
1623
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001624void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001625 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1626 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001627 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001628}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001629
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001630void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001631 if (constructors_called) {
1632 return;
1633 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001634
Elliott Hughesd23736e2012-11-01 15:16:56 -07001635 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1636 // protect against recursive constructor calls. One simple example of constructor recursion
1637 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1638 // 1. The program depends on libc, so libc's constructor is called here.
1639 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1640 // 3. dlopen() calls the constructors on the newly created
1641 // soinfo for libc_malloc_debug_leak.so.
1642 // 4. The debug .so depends on libc, so CallConstructors is
1643 // called again with the libc soinfo. If it doesn't trigger the early-
1644 // out above, the libc constructor will be called again (recursively!).
1645 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001646
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001647 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001648 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001649 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001650 name, preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001651 }
1652
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001653 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001654 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001655 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001656
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001657 TRACE("\"%s\": calling constructors", name);
1658
1659 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001660 call_function("DT_INIT", init_func_);
1661 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001662}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001663
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001664void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001665 if (!constructors_called) {
1666 return;
1667 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001668 TRACE("\"%s\": calling destructors", name);
1669
1670 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001671 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001672
1673 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001674 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001675
1676 // This is needed on second call to dlopen
1677 // after library has been unloaded with RTLD_NODELETE
1678 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001679}
1680
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001681void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001682 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001683 child->parents_.push_back(this);
1684 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001685 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001686}
1687
1688void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001689 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001690 return;
1691 }
1692
1693 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001694 children_.for_each([&] (soinfo* child) {
1695 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001696 return parent == this;
1697 });
1698 });
1699
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001700 parents_.for_each([&] (soinfo* parent) {
1701 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001702 return child == this;
1703 });
1704 });
1705
1706 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001707 parents_.clear();
1708 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001709}
1710
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001711dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001712 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001713 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001714 }
1715
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001716 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001717};
1718
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001719ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001720 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001721 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001722 }
1723
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001724 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001725}
1726
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001727off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001728 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001729 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001730 }
1731
1732 return 0;
1733}
1734
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001735uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001736 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001737 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001738 }
1739
1740 return 0;
1741}
1742
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001743uint32_t soinfo::get_dt_flags_1() const {
1744 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001745 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001746 }
1747
1748 return 0;
1749}
1750void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1751 if (has_min_version(1)) {
1752 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001753 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001754 }
1755
1756 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001757 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001758 }
1759
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001760 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001761 }
1762}
1763
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001764// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001765// 'this->flags' does not have FLAG_NEW_SOINFO set.
1766static soinfo::soinfo_list_t g_empty_list;
1767
1768soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001769 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001770 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001771 }
1772
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001773 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001774}
1775
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001776soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001777 if (has_min_version(0)) {
1778 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001779 }
1780
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001781 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001782}
1783
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001784ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1785 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1786 return call_ifunc_resolver(s->st_value + load_bias);
1787 }
1788
1789 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1790}
1791
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001792const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001793 if (has_min_version(1) && (index >= strtab_size_)) {
1794 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001795 }
1796
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001797 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001798}
1799
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001800bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001801 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001802}
1803
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001804bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001805 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001806}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001807
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001808bool soinfo::is_linked() const {
1809 return (flags_ & FLAG_LINKED) != 0;
1810}
1811
1812bool soinfo::is_main_executable() const {
1813 return (flags_ & FLAG_EXE) != 0;
1814}
1815
1816void soinfo::set_linked() {
1817 flags_ |= FLAG_LINKED;
1818}
1819
1820void soinfo::set_linker_flag() {
1821 flags_ |= FLAG_LINKER;
1822}
1823
1824void soinfo::set_main_executable() {
1825 flags_ |= FLAG_EXE;
1826}
1827
1828void soinfo::increment_ref_count() {
1829 local_group_root_->ref_count_++;
1830}
1831
1832size_t soinfo::decrement_ref_count() {
1833 return --local_group_root_->ref_count_;
1834}
1835
1836soinfo* soinfo::get_local_group_root() const {
1837 return local_group_root_;
1838}
1839
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001840/* Force any of the closed stdin, stdout and stderr to be associated with
1841 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001842static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001843 int dev_null, i, status;
1844 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001845
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001846 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
1847 if (dev_null < 0) {
1848 DL_ERR("cannot open /dev/null: %s", strerror(errno));
1849 return -1;
1850 }
1851 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001852
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001853 /* If any of the stdio file descriptors is valid and not associated
1854 with /dev/null, dup /dev/null to it. */
1855 for (i = 0; i < 3; i++) {
1856 /* If it is /dev/null already, we are done. */
1857 if (i == dev_null) {
1858 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001859 }
1860
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001861 TRACE("[ Nullifying stdio file descriptor %d]", i);
1862 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
1863
1864 /* If file is opened, we are good. */
1865 if (status != -1) {
1866 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001867 }
1868
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001869 /* The only error we allow is that the file descriptor does not
1870 exist, in which case we dup /dev/null to it. */
1871 if (errno != EBADF) {
1872 DL_ERR("fcntl failed: %s", strerror(errno));
1873 return_value = -1;
1874 continue;
1875 }
1876
1877 /* Try dupping /dev/null to this stdio file descriptor and
1878 repeat if there is a signal. Note that any errors in closing
1879 the stdio descriptor are lost. */
1880 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
1881 if (status < 0) {
1882 DL_ERR("dup2 failed: %s", strerror(errno));
1883 return_value = -1;
1884 continue;
1885 }
1886 }
1887
1888 /* If /dev/null is not one of the stdio file descriptors, close it. */
1889 if (dev_null > 2) {
1890 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
1891 status = TEMP_FAILURE_RETRY(close(dev_null));
1892 if (status == -1) {
1893 DL_ERR("close failed: %s", strerror(errno));
1894 return_value = -1;
1895 }
1896 }
1897
1898 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001899}
1900
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001901bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08001902 /* Extract dynamic section */
1903 ElfW(Word) dynamic_flags = 0;
1904 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07001905
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001906 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001907 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001908 if (!relocating_linker) {
1909 INFO("[ linking %s ]", name);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001910 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001911 }
1912
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001913 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001914 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001915 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001916 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001917 return false;
1918 } else {
1919 if (!relocating_linker) {
1920 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001921 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001922 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001923
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001924#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001925 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
1926 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001927#endif
1928
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001929 // Extract useful information from dynamic section.
1930 uint32_t needed_count = 0;
1931 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
1932 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1933 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1934 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001935 case DT_SONAME:
1936 // TODO: glibc dynamic linker uses this name for
1937 // initial library lookup; consider doing the same here.
1938 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001939
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001940 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001941 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001942 // in case of --hash-style=both, we prefer gnu
1943 break;
1944 }
1945
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001946 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
1947 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
1948 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
1949 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001950 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001951
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001952 case DT_GNU_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001953 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001954 // in case of --hash-style=both, we prefer gnu
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001955 nchain_ = 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001956 }
1957
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001958 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001959 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001960 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
1961 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001962
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001963 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
1964 bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001965 // amend chain for symndx = header[1]
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001966 chain_ = bucket_ + nbucket_ - reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001967
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001968 if (!powerof2(gnu_maskwords_)) {
1969 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two", gnu_maskwords_, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001970 return false;
1971 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001972 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001973
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001974 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001975 break;
1976
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001977 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001978 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001979 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001980
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001981 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001982 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001983 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001984
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001985 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001986 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001987 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001988
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001989 case DT_SYMENT:
1990 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001991 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"", static_cast<size_t>(d->d_un.d_val), name);
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001992 return false;
1993 }
1994 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001995
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001996 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07001997#if defined(USE_RELA)
1998 if (d->d_un.d_val != DT_RELA) {
1999 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002000 return false;
2001 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002002#else
2003 if (d->d_un.d_val != DT_REL) {
2004 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
2005 return false;
2006 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002007#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002008 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002009
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002010 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002011#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002012 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002013#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002014 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002015#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002016 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002017
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002018 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002019#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002020 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002021#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002022 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002023#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002024 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002025
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002026 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002027#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002028 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002029 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002030#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002031 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2032 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002033
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002034 case DT_DEBUG:
2035 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2036 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002037// FIXME: not working currently for N64
2038// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002039// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002040// read-only, but the DYNAMIC header claims it is writable.
2041#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002042 if ((dynamic_flags & PF_W) != 0) {
2043 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2044 }
Chris Dearman99186652014-02-06 20:36:51 -08002045#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002046 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002047#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002048 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002049 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002050 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002051
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002052 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002053 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002054 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002055
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002056 case DT_RELAENT:
2057 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002058 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002059 return false;
2060 }
2061 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002062
2063 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002064 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002065 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002066
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002067 case DT_REL:
2068 DL_ERR("unsupported DT_REL in \"%s\"", name);
2069 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002070
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002071 case DT_RELSZ:
2072 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2073 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002074#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002075 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002076 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002077 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002078
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002079 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002080 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002081 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002082
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002083 case DT_RELENT:
2084 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002085 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002086 return false;
2087 }
2088 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002089
2090 // "Indicates that all RELATIVE relocations have been concatenated together,
2091 // and specifies the RELATIVE relocation count."
2092 //
2093 // TODO: Spec also mentions that this can be used to optimize relocation process;
2094 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002095 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002096 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002097 case DT_RELA:
2098 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2099 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002100#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002101 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002102 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2103 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002104 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002105
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002106 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002107 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2108 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002109 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002110
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002111 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002112 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2113 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002114 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002115
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002116 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002117 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002118 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002119
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002120 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002121 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2122 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002123 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002124
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002125 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002126 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002127 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002128
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002129 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002130 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2131 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002132 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002133
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002134 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002135 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002136 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002137
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002138 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002139#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002140 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2141 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002142#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002143 has_text_relocations = true;
2144 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002145#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002146
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002147 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002148 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002149 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002150
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002151 case DT_NEEDED:
2152 ++needed_count;
2153 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002154
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002155 case DT_FLAGS:
2156 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002157#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002158 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2159 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002160#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002161 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002162#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002163 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002164 if (d->d_un.d_val & DF_SYMBOLIC) {
2165 has_DT_SYMBOLIC = true;
2166 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002167 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002168
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002169 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002170 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002171
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002172 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002173 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2174 }
2175 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002176#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002177 case DT_MIPS_RLD_MAP:
2178 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2179 {
2180 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2181 *dp = &_r_debug;
2182 }
2183 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002184 case DT_MIPS_RLD_MAP2:
2185 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2186 {
2187 r_debug** dp = reinterpret_cast<r_debug**>(reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
2188 *dp = &_r_debug;
2189 }
2190 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002191
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002192 case DT_MIPS_RLD_VERSION:
2193 case DT_MIPS_FLAGS:
2194 case DT_MIPS_BASE_ADDRESS:
2195 case DT_MIPS_UNREFEXTNO:
2196 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002197
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002198 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002199 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002200 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002201
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002202 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002203 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002204 break;
2205
2206 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002207 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002208 break;
2209#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002210 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2211 case DT_BIND_NOW:
2212 break;
2213
2214 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002215 case DT_VERSYM:
2216 case DT_VERDEF:
2217 case DT_VERDEFNUM:
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002218 case DT_VERNEED:
2219 case DT_VERNEEDNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002220 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002221
2222 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002223 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002224 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002225 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2226 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002227 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002228 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002229 }
2230
2231 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002232 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002233
2234 // Sanity checks.
2235 if (relocating_linker && needed_count != 0) {
2236 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2237 return false;
2238 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002239 if (nbucket_ == 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002240 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" (new hash type from the future?)", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002241 return false;
2242 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002243 if (strtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002244 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2245 return false;
2246 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002247 if (symtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002248 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2249 return false;
2250 }
2251 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002252}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002253
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002254bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group, const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002255
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002256 local_group_root_ = local_group.front();
2257 if (local_group_root_ == nullptr) {
2258 local_group_root_ = this;
2259 }
2260
Elliott Hughese4d792a2013-10-28 14:19:05 -07002261#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002262 if (has_text_relocations) {
2263 // Make segments writable to allow text relocations to work properly. We will later call
2264 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2265 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2266 "security hardening. Please fix.", name);
2267 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2268 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2269 name, strerror(errno));
2270 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002271 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002272 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002273#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002274
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002275#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002276 if (rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002277 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002278 if (!relocate(plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002279 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002280 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002281 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002282 if (plt_rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002283 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002284 if (!relocate(plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002285 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002286 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002287 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002288#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002289 if (rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002290 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002291 if (!relocate(plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002292 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002293 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002294 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002295 if (plt_rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002296 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002297 if (!relocate(plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002298 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002299 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002300 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002301#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002302
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002303#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08002304 if (!mips_relocate_got(global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002305 return false;
2306 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002307#endif
2308
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002309 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002310
Elliott Hughese4d792a2013-10-28 14:19:05 -07002311#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002312 if (has_text_relocations) {
2313 // All relocations are done, we can protect our segments back to read-only.
2314 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2315 DL_ERR("can't protect segments for \"%s\": %s",
2316 name, strerror(errno));
2317 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002318 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002319 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002320#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002321
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002322 /* We can also turn on GNU RELRO protection */
2323 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2324 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2325 name, strerror(errno));
2326 return false;
2327 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002328
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002329 /* Handle serializing/sharing the RELRO segment */
2330 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2331 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2332 extinfo->relro_fd) < 0) {
2333 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2334 name, strerror(errno));
2335 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002336 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002337 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2338 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2339 extinfo->relro_fd) < 0) {
2340 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2341 name, strerror(errno));
2342 return false;
2343 }
2344 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002345
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002346 notify_gdb_of_load(this);
2347 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002348}
2349
Nick Kralevich468319c2011-11-11 15:53:17 -08002350/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002351 * This function add vdso to internal dso list.
2352 * It helps to stack unwinding through signal handlers.
2353 * Also, it makes bionic more like glibc.
2354 */
Kito Cheng812fd422014-03-25 22:53:56 +08002355static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002356#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002357 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002358 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002359 return;
2360 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002361
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002362 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002363
Elliott Hughes0266ae52014-02-10 17:46:57 -08002364 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2365 si->phnum = ehdr_vdso->e_phnum;
2366 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2367 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002368 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002369
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002370 si->prelink_image();
2371 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002372#endif
2373}
2374
2375/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002376 * This is linker soinfo for GDB. See details below.
2377 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002378#if defined(__LP64__)
2379#define LINKER_PATH "/system/bin/linker64"
2380#else
2381#define LINKER_PATH "/system/bin/linker"
2382#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002383static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002384
2385/* gdb expects the linker to be in the debug shared object list.
2386 * Without this, gdb has trouble locating the linker's ".text"
2387 * and ".plt" sections. Gdb could also potentially use this to
2388 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2389 * Don't use soinfo_alloc(), because the linker shouldn't
2390 * be on the soinfo list.
2391 */
2392static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002393 linker_soinfo_for_gdb.base = linker_base;
2394
2395 /*
2396 * Set the dynamic field in the link map otherwise gdb will complain with
2397 * the following:
2398 * warning: .dynamic section for "/system/bin/linker" is not at the
2399 * expected address (wrong library or version mismatch?)
2400 */
2401 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2402 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2403 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002404 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002405 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2406}
2407
2408/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002409 * This code is called after the linker has linked itself and
2410 * fixed it's own GOT. It is safe to make references to externs
2411 * and other non-local data at this point.
2412 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002413static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002414#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002415 struct timeval t0, t1;
2416 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002417#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002418
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002419 // Initialize environment functions, and get to the ELF aux vectors table.
2420 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002421
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002422 // If this is a setuid/setgid program, close the security hole described in
2423 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2424 if (get_AT_SECURE()) {
2425 nullify_closed_stdio();
2426 }
2427
2428 debuggerd_init();
2429
2430 // Get a few environment variables.
2431 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2432 if (LD_DEBUG != nullptr) {
2433 g_ld_debug_verbosity = atoi(LD_DEBUG);
2434 }
2435
2436 // Normally, these are cleaned by linker_env_init, but the test
2437 // doesn't cost us anything.
2438 const char* ldpath_env = nullptr;
2439 const char* ldpreload_env = nullptr;
2440 if (!get_AT_SECURE()) {
2441 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2442 ldpreload_env = linker_env_get("LD_PRELOAD");
2443 }
2444
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -08002445#if !defined(__LP64__)
2446 if (personality(PER_LINUX32) == -1) {
2447 __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
2448 }
2449#endif
2450
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002451 INFO("[ android linker & debugger ]");
2452
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002453 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002454 if (si == nullptr) {
2455 exit(EXIT_FAILURE);
2456 }
2457
2458 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002459 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002460 link_map* map = &(si->link_map_head);
2461
2462 map->l_addr = 0;
2463 map->l_name = args.argv[0];
2464 map->l_prev = nullptr;
2465 map->l_next = nullptr;
2466
2467 _r_debug.r_map = map;
2468 r_debug_tail = map;
2469
2470 init_linker_info_for_gdb(linker_base);
2471
2472 // Extract information passed from the kernel.
2473 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2474 si->phnum = args.getauxval(AT_PHNUM);
2475 si->entry = args.getauxval(AT_ENTRY);
2476
2477 /* Compute the value of si->base. We can't rely on the fact that
2478 * the first entry is the PHDR because this will not be true
2479 * for certain executables (e.g. some in the NDK unit test suite)
2480 */
2481 si->base = 0;
2482 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2483 si->load_bias = 0;
2484 for (size_t i = 0; i < si->phnum; ++i) {
2485 if (si->phdr[i].p_type == PT_PHDR) {
2486 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2487 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2488 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002489 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002490 }
2491 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002492
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002493 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2494 if (elf_hdr->e_type != ET_DYN) {
2495 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2496 exit(EXIT_FAILURE);
2497 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002498
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002499 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2500 parse_LD_LIBRARY_PATH(ldpath_env);
2501 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002502
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002503 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002504
Dmitriy Ivanov67181252015-01-07 15:48:25 -08002505 if (!si->prelink_image()) {
2506 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2507 exit(EXIT_FAILURE);
2508 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002509
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002510 // add somain to global group
2511 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2512
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002513 // Load ld_preloads and dependencies.
2514 StringLinkedList needed_library_name_list;
2515 size_t needed_libraries_count = 0;
2516 size_t ld_preloads_count = 0;
2517 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2518 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2519 ++needed_libraries_count;
2520 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002521
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002522 for_each_dt_needed(si, [&](const char* name) {
2523 needed_library_name_list.push_back(name);
2524 ++needed_libraries_count;
2525 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002526
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002527 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002528
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002529 memset(needed_library_names, 0, sizeof(needed_library_names));
2530 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002531
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002532 if (needed_libraries_count > 0 && !find_libraries(si, needed_library_names, needed_libraries_count, nullptr, g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002533 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2534 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002535 } else if (needed_libraries_count == 0) {
2536 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
2537 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2538 exit(EXIT_FAILURE);
2539 }
2540 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002541 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002542
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002543 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002544
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002545 {
2546 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06002547
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002548 si->call_pre_init_constructors();
2549
2550 /* After the prelink_image, the si->load_bias is initialized.
2551 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2552 * We need to update this value for so exe here. So Unwind_Backtrace
2553 * for some arch like x86 could work correctly within so exe.
2554 */
2555 map->l_addr = si->load_bias;
2556 si->call_constructors();
2557 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002558
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002559#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002560 gettimeofday(&t1, nullptr);
2561 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2562 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2563 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002564#endif
2565#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002566 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2567 linker_stats.count[kRelocAbsolute],
2568 linker_stats.count[kRelocRelative],
2569 linker_stats.count[kRelocCopy],
2570 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002571#endif
2572#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002573 {
2574 unsigned n;
2575 unsigned i;
2576 unsigned count = 0;
2577 for (n = 0; n < 4096; n++) {
2578 if (bitmask[n]) {
2579 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002580#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002581 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002582#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002583 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002584#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002585 if (x & 1) {
2586 count++;
2587 }
2588 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002589 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002590 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002591 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002592 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2593 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002594#endif
2595
2596#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002597 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002598#endif
2599
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002600 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2601 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002602}
Nick Kralevich468319c2011-11-11 15:53:17 -08002603
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002604/* Compute the load-bias of an existing executable. This shall only
2605 * be used to compute the load bias of an executable or shared library
2606 * that was loaded by the kernel itself.
2607 *
2608 * Input:
2609 * elf -> address of ELF header, assumed to be at the start of the file.
2610 * Return:
2611 * load bias, i.e. add the value of any p_vaddr in the file to get
2612 * the corresponding address in memory.
2613 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002614static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2615 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002616 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002617 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002618
Elliott Hughes0266ae52014-02-10 17:46:57 -08002619 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002620 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002621 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002622 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002623 }
2624 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002625}
2626
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002627extern "C" void _start();
2628
Nick Kralevich468319c2011-11-11 15:53:17 -08002629/*
2630 * This is the entry point for the linker, called from begin.S. This
2631 * method is responsible for fixing the linker's own relocations, and
2632 * then calling __linker_init_post_relocation().
2633 *
2634 * Because this method is called before the linker has fixed it's own
2635 * relocations, any attempt to reference an extern variable, extern
2636 * function, or other GOT reference will generate a segfault.
2637 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002638extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002639 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002640
Elliott Hughes0266ae52014-02-10 17:46:57 -08002641 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002642 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002643 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002644 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002645
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002646 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002647
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002648 // If the linker is not acting as PT_INTERP entry_point is equal to
2649 // _start. Which means that the linker is running as an executable and
2650 // already linked by PT_INTERP.
2651 //
2652 // This happens when user tries to run 'adb shell /system/bin/linker'
2653 // see also https://code.google.com/p/android/issues/detail?id=63174
2654 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2655 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2656 }
2657
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002658 linker_so.base = linker_addr;
2659 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2660 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002661 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002662 linker_so.phdr = phdr;
2663 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002664 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07002665
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002666 // This might not be obvious... The reasons why we pass g_empty_list
2667 // in place of local_group here are (1) we do not really need it, because
2668 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2669 // itself without having to look into local_group and (2) allocators
2670 // are not yet initialized, and therefore we cannot use linked_list.push_*
2671 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002672 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002673 // It would be nice to print an error message, but if the linker
2674 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002675 // call write() (because it involves a GOT reference). We may as
2676 // well try though...
2677 const char* msg = "CANNOT LINK EXECUTABLE: ";
2678 write(2, msg, strlen(msg));
2679 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2680 write(2, "\n", 1);
2681 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002682 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002683
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002684 __libc_init_tls(args);
2685
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002686 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002687 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002688
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002689 // Initialize static variables. Note that in order to
2690 // get correct libdl_info we need to call constructors
2691 // before get_libdl_info().
2692 solist = get_libdl_info();
2693 sonext = get_libdl_info();
2694
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002695 // We have successfully fixed our own relocations. It's safe to run
2696 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002697 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002698 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002699
Elliott Hughes611f9562015-01-23 10:43:58 -08002700 INFO("[ jumping to _start ]");
2701
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002702 // Return the address that the calling assembly stub should jump to.
2703 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002704}