blob: b61e041d3391f3c142662519d62154b0ea9edea4 [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>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037#include <sys/atomics.h>
Elliott Hughes46882792012-08-03 16:49:39 -070038#include <sys/mman.h>
39#include <sys/stat.h>
40#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Elliott Hughes46882792012-08-03 16:49:39 -070042// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070043#include "private/bionic_tls.h"
44#include "private/KernelArgumentBlock.h"
45#include "private/ScopedPthreadMutexLocker.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046
47#include "linker.h"
48#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010049#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020050#include "linker_phdr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
53 *
54 * Do NOT use malloc() and friends or pthread_*() code here.
55 * Don't use printf() either; it's caused mysterious memory
56 * corruption in the past.
57 * The linker runs before we bring up libc and it's easiest
58 * to make sure it does not depend on any complex libc features
59 *
60 * open issues / todo:
61 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063 * - after linking, set as much stuff as possible to READONLY
64 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070065 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000067static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo);
Elliott Hughes0266ae52014-02-10 17:46:57 -080068static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069
Magnus Malmbornba98d922012-09-12 13:00:55 +020070// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
71// maps, each a single page in size. The pages are broken up into as many struct soinfo
72// objects as will fit, and they're all threaded together on a free list.
73#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
74struct soinfo_pool_t {
75 soinfo_pool_t* next;
76 soinfo info[SOINFO_PER_POOL];
77};
78static struct soinfo_pool_t* gSoInfoPools = NULL;
79static soinfo* gSoInfoFreeList = NULL;
80
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080081static soinfo* solist = &libdl_info;
82static soinfo* sonext = &libdl_info;
83static soinfo* somain; /* main process, always the one after libdl_info */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084
Elliott Hughesa4aafd12014-01-13 16:37:47 -080085static const char* const gDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070086#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070087 "/vendor/lib64",
88 "/system/lib64",
89#else
Elliott Hughes124fae92012-10-31 14:20:03 -070090 "/vendor/lib",
91 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -070092#endif
Elliott Hughes124fae92012-10-31 14:20:03 -070093 NULL
94};
David Bartleybc3a5c22009-06-02 18:27:28 -070095
Elliott Hughesa4aafd12014-01-13 16:37:47 -080096#define LDPATH_BUFSIZE (LDPATH_MAX*64)
97#define LDPATH_MAX 8
98
99#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
100#define LDPRELOAD_MAX 8
101
Elliott Hughes124fae92012-10-31 14:20:03 -0700102static char gLdPathsBuffer[LDPATH_BUFSIZE];
103static const char* gLdPaths[LDPATH_MAX + 1];
104
105static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
106static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600107
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800108static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600109
Elliott Hughes650be4e2013-03-05 18:47:58 -0800110__LIBC_HIDDEN__ int gLdDebugVerbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111
Elliott Hughes0d787c12013-04-04 13:46:46 -0700112__LIBC_HIDDEN__ abort_msg_t* gAbortMessage = NULL; // For debuggerd.
113
Elliott Hughesbedfe382012-08-14 14:07:59 -0700114enum RelocationKind {
115 kRelocAbsolute = 0,
116 kRelocRelative,
117 kRelocCopy,
118 kRelocSymbol,
119 kRelocMax
120};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100121
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700123struct linker_stats_t {
124 int count[kRelocMax];
125};
126
127static linker_stats_t linker_stats;
128
129static void count_relocation(RelocationKind kind) {
130 ++linker_stats.count[kind];
131}
132#else
133static void count_relocation(RelocationKind) {
134}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135#endif
136
137#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700138static unsigned bitmask[4096];
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100139#if defined(__LP64__)
140#define MARK(offset) \
141 do { \
142 if ((((offset) >> 12) >> 5) < 4096) \
143 bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800144 } while (0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100145#else
Elliott Hughesbedfe382012-08-14 14:07:59 -0700146#define MARK(offset) \
147 do { \
148 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800149 } while (0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100150#endif
Elliott Hughesbedfe382012-08-14 14:07:59 -0700151#else
152#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800153#endif
154
Elliott Hughes46882792012-08-03 16:49:39 -0700155// You shouldn't try to call memory-allocating functions in the dynamic linker.
156// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700157#define DISALLOW_ALLOCATION(return_type, name, ...) \
158 return_type name __VA_ARGS__ \
159 { \
Elliott Hughes46882792012-08-03 16:49:39 -0700160 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700161 __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
162 write(2, msg, strlen(msg)); \
163 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700164 }
Kito Cheng812fd422014-03-25 22:53:56 +0800165DISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
166DISALLOW_ALLOCATION(void, free, (void* u __unused));
167DISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
168DISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
Dima Zavin2e855792009-05-20 18:28:09 -0700169
Dima Zavin03531952009-05-29 17:30:25 -0700170static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700171static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700172
Elliott Hughes650be4e2013-03-05 18:47:58 -0800173char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700174 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700175}
176
Elliott Hughes650be4e2013-03-05 18:47:58 -0800177size_t linker_get_error_buffer_size() {
178 return sizeof(__linker_dl_err_buf);
179}
180
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181/*
182 * This function is an empty stub where GDB locates a breakpoint to get notified
183 * about linker activity.
184 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700185extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800187static r_debug _r_debug = {1, NULL, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
188static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189
Elliott Hughes3b297c42012-10-11 16:08:51 -0700190static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800192static void insert_soinfo_into_debug_map(soinfo* info) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700193 // Copy the necessary fields into the debug structure.
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800194 link_map* map = &(info->link_map_head);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400195 map->l_addr = info->load_bias;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800196 map->l_name = reinterpret_cast<char*>(info->name);
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800197 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800198
199 /* Stick the new library at the end of the list.
200 * gdb tends to care more about libc than it does
201 * about leaf libraries, and ordering it this way
202 * reduces the back-and-forth over the wire.
203 */
204 if (r_debug_tail) {
205 r_debug_tail->l_next = map;
206 map->l_prev = r_debug_tail;
207 map->l_next = 0;
208 } else {
209 _r_debug.r_map = map;
210 map->l_prev = 0;
211 map->l_next = 0;
212 }
213 r_debug_tail = map;
214}
215
Elliott Hughesbedfe382012-08-14 14:07:59 -0700216static void remove_soinfo_from_debug_map(soinfo* info) {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800217 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700218
Elliott Hughesbedfe382012-08-14 14:07:59 -0700219 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700220 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700221 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700222
Elliott Hughesbedfe382012-08-14 14:07:59 -0700223 if (map->l_prev) {
224 map->l_prev->l_next = map->l_next;
225 }
226 if (map->l_next) {
227 map->l_next->l_prev = map->l_prev;
228 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700229}
230
Elliott Hughesbedfe382012-08-14 14:07:59 -0700231static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800232 if (info->flags & FLAG_EXE) {
233 // GDB already knows about the main executable
234 return;
235 }
236
Elliott Hughes3b297c42012-10-11 16:08:51 -0700237 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800239 _r_debug.r_state = r_debug::RT_ADD;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240 rtld_db_dlactivity();
241
242 insert_soinfo_into_debug_map(info);
243
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800244 _r_debug.r_state = r_debug::RT_CONSISTENT;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700246}
247
Elliott Hughesbedfe382012-08-14 14:07:59 -0700248static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700249 if (info->flags & FLAG_EXE) {
250 // GDB already knows about the main executable
251 return;
252 }
253
Elliott Hughes3b297c42012-10-11 16:08:51 -0700254 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700255
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800256 _r_debug.r_state = r_debug::RT_DELETE;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700257 rtld_db_dlactivity();
258
259 remove_soinfo_from_debug_map(info);
260
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800261 _r_debug.r_state = r_debug::RT_CONSISTENT;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700262 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800263}
264
Elliott Hughes18a206c2012-10-29 17:37:13 -0700265void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800266 _r_debug.r_state = r_debug::RT_ADD;
267 rtld_db_dlactivity();
268 _r_debug.r_state = r_debug::RT_CONSISTENT;
269 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800270}
271
Magnus Malmbornba98d922012-09-12 13:00:55 +0200272static bool ensure_free_list_non_empty() {
273 if (gSoInfoFreeList != NULL) {
274 return true;
275 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800276
Magnus Malmbornba98d922012-09-12 13:00:55 +0200277 // Allocate a new pool.
278 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
279 PROT_READ|PROT_WRITE,
280 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
281 if (pool == MAP_FAILED) {
282 return false;
283 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800284
Magnus Malmbornba98d922012-09-12 13:00:55 +0200285 // Add the pool to our list of pools.
286 pool->next = gSoInfoPools;
287 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800288
Magnus Malmbornba98d922012-09-12 13:00:55 +0200289 // Chain the entries in the new pool onto the free list.
290 gSoInfoFreeList = &pool->info[0];
291 soinfo* next = NULL;
292 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
293 pool->info[i].next = next;
294 next = &pool->info[i];
295 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800296
Magnus Malmbornba98d922012-09-12 13:00:55 +0200297 return true;
298}
299
Elliott Hughesd23736e2012-11-01 15:16:56 -0700300static void set_soinfo_pool_protection(int protection) {
301 for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
302 if (mprotect(p, sizeof(*p), protection) == -1) {
303 abort(); // Can't happen.
304 }
305 }
306}
307
Magnus Malmbornba98d922012-09-12 13:00:55 +0200308static soinfo* soinfo_alloc(const char* name) {
309 if (strlen(name) >= SOINFO_NAME_LEN) {
310 DL_ERR("library name \"%s\" too long", name);
311 return NULL;
312 }
313
314 if (!ensure_free_list_non_empty()) {
315 DL_ERR("out of memory when loading \"%s\"", name);
316 return NULL;
317 }
318
319 // Take the head element off the free list.
320 soinfo* si = gSoInfoFreeList;
321 gSoInfoFreeList = gSoInfoFreeList->next;
322
323 // Initialize the new element.
324 memset(si, 0, sizeof(soinfo));
325 strlcpy(si->name, name, sizeof(si->name));
326 sonext->next = si;
327 sonext = si;
328
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700329 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200330 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800331}
332
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800333static void soinfo_free(soinfo* si) {
Elliott Hughes46882792012-08-03 16:49:39 -0700334 if (si == NULL) {
335 return;
336 }
337
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338 soinfo *prev = NULL, *trav;
339
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700340 TRACE("name %s: freeing soinfo @ %p", si->name, si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800341
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800342 for (trav = solist; trav != NULL; trav = trav->next) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800343 if (trav == si)
344 break;
345 prev = trav;
346 }
347 if (trav == NULL) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800348 /* si was not in solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700349 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800350 return;
351 }
352
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100353 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800354 always the static libdl_info.
355 */
356 prev->next = si->next;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800357 if (si == sonext) {
358 sonext = prev;
359 }
Magnus Malmbornba98d922012-09-12 13:00:55 +0200360 si->next = gSoInfoFreeList;
361 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800362}
363
Elliott Hughescade4c32012-12-20 14:42:14 -0800364
365static void parse_path(const char* path, const char* delimiters,
366 const char** array, char* buf, size_t buf_size, size_t max_count) {
367 if (path == NULL) {
368 return;
369 }
370
371 size_t len = strlcpy(buf, path, buf_size);
372
373 size_t i = 0;
374 char* buf_p = buf;
375 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
376 if (*array[i] != '\0') {
377 ++i;
378 }
379 }
380
381 // Forget the last path if we had to truncate; this occurs if the 2nd to
382 // last char isn't '\0' (i.e. wasn't originally a delimiter).
383 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
384 array[i - 1] = NULL;
385 } else {
386 array[i] = NULL;
387 }
388}
389
390static void parse_LD_LIBRARY_PATH(const char* path) {
391 parse_path(path, ":", gLdPaths,
392 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
393}
394
395static void parse_LD_PRELOAD(const char* path) {
396 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
397 parse_path(path, " :", gLdPreloadNames,
398 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
399}
400
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700401#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700402
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800403/* For a given PC, find the .so that it belongs to.
404 * Returns the base address of the .ARM.exidx section
405 * for that .so, and the number of 8-byte entries
406 * in that section (via *pcount).
407 *
408 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
409 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700410 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411 */
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800412_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800413 unsigned addr = (unsigned)pc;
414
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800415 for (soinfo* si = solist; si != 0; si = si->next) {
Nick Kralevich468319c2011-11-11 15:53:17 -0800416 if ((addr >= si->base) && (addr < (si->base + si->size))) {
417 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900418 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800419 }
420 }
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800421 *pcount = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800422 return NULL;
423}
Elliott Hughes46882792012-08-03 16:49:39 -0700424
Christopher Ferris24053a42013-08-19 17:45:09 -0700425#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700426
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800427/* Here, we only have to provide a callback to iterate across all the
428 * loaded libraries. gcc_eh does the rest. */
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800429int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800430 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700431 for (soinfo* si = solist; si != NULL; si = si->next) {
432 dl_phdr_info dl_info;
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800433 dl_info.dlpi_addr = si->link_map_head.l_addr;
434 dl_info.dlpi_name = si->link_map_head.l_name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800435 dl_info.dlpi_phdr = si->phdr;
436 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700437 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
438 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800439 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700440 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800441 }
442 return rv;
443}
Elliott Hughes46882792012-08-03 16:49:39 -0700444
Elliott Hughes0266ae52014-02-10 17:46:57 -0800445static ElfW(Sym)* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
446 ElfW(Sym)* symtab = si->symtab;
447 const char* strtab = si->strtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800448
Elliott Hughes0266ae52014-02-10 17:46:57 -0800449 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
450 name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800451
Elliott Hughes0266ae52014-02-10 17:46:57 -0800452 for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
453 ElfW(Sym)* s = symtab + n;
454 if (strcmp(strtab + s->st_name, name)) continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800455
Elliott Hughes0266ae52014-02-10 17:46:57 -0800456 /* only concern ourselves with global and weak symbol definitions */
457 switch (ELF_ST_BIND(s->st_info)) {
458 case STB_GLOBAL:
459 case STB_WEAK:
460 if (s->st_shndx == SHN_UNDEF) {
461 continue;
462 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800463
Elliott Hughes0266ae52014-02-10 17:46:57 -0800464 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
465 name, si->name, reinterpret_cast<void*>(s->st_value),
466 static_cast<size_t>(s->st_size));
467 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800468 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800469 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800470
Elliott Hughes0266ae52014-02-10 17:46:57 -0800471 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800472}
473
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800474static unsigned elfhash(const char* _name) {
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800475 const unsigned char* name = reinterpret_cast<const unsigned char*>(_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800476 unsigned h = 0, g;
477
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800478 while (*name) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800479 h = (h << 4) + *name++;
480 g = h & 0xf0000000;
481 h ^= g;
482 h ^= g >> 24;
483 }
484 return h;
485}
486
Elliott Hughes0266ae52014-02-10 17:46:57 -0800487static ElfW(Sym)* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
Doug Kwan94304352009-10-23 18:11:40 -0700488 unsigned elf_hash = elfhash(name);
Elliott Hughes0266ae52014-02-10 17:46:57 -0800489 ElfW(Sym)* s = NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700490
Pavel Chupinc77c4342012-10-31 13:55:51 +0400491 if (si != NULL && somain != NULL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200492 /*
Pavel Chupinc77c4342012-10-31 13:55:51 +0400493 * Local scope is executable scope. Just start looking into it right away
494 * for the shortcut.
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200495 */
496
Pavel Chupinc77c4342012-10-31 13:55:51 +0400497 if (si == somain) {
498 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200499 if (s != NULL) {
Pavel Chupinc77c4342012-10-31 13:55:51 +0400500 *lsi = si;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200501 goto done;
502 }
Pavel Chupinc77c4342012-10-31 13:55:51 +0400503 } else {
504 /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200505
Pavel Chupinc77c4342012-10-31 13:55:51 +0400506 /*
507 * If this object was built with symbolic relocations disabled, the
508 * first place to look to resolve external references is the main
509 * executable.
510 */
Nick Kralevich468319c2011-11-11 15:53:17 -0800511
Pavel Chupinc77c4342012-10-31 13:55:51 +0400512 if (!si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700513 DEBUG("%s: looking up %s in executable %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700514 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400515 s = soinfo_elf_lookup(somain, elf_hash, name);
516 if (s != NULL) {
517 *lsi = somain;
518 goto done;
519 }
520 }
521
522 /* Look for symbols in the local scope (the object who is
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700523 * searching). This happens with C++ templates on x86 for some
Pavel Chupinc77c4342012-10-31 13:55:51 +0400524 * reason.
525 *
526 * Notes on weak symbols:
527 * The ELF specs are ambiguous about treatment of weak definitions in
528 * dynamic linking. Some systems return the first definition found
529 * and some the first non-weak definition. This is system dependent.
530 * Here we return the first definition found for simplicity. */
531
532 s = soinfo_elf_lookup(si, elf_hash, name);
533 if (s != NULL) {
534 *lsi = si;
535 goto done;
536 }
537
538 /*
539 * If this object was built with -Bsymbolic and symbol is not found
540 * in the local scope, try to find the symbol in the main executable.
541 */
542
543 if (si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700544 DEBUG("%s: looking up %s in executable %s after local scope",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700545 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400546 s = soinfo_elf_lookup(somain, elf_hash, name);
547 if (s != NULL) {
548 *lsi = somain;
549 goto done;
550 }
551 }
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200552 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700553 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700554
Matt Fischer4fd42c12009-12-31 12:09:10 -0600555 /* Next, look for it in the preloads list */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800556 for (int i = 0; gLdPreloads[i] != NULL; i++) {
557 s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
558 if (s != NULL) {
559 *lsi = gLdPreloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600560 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200561 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600562 }
563
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800564 for (int i = 0; needed[i] != NULL; i++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700565 DEBUG("%s: looking up %s in %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700566 si->name, name, needed[i]->name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200567 s = soinfo_elf_lookup(needed[i], elf_hash, name);
568 if (s != NULL) {
569 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200570 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200571 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700572 }
573
574done:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800575 if (s != NULL) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700576 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
577 "found in %s, base = %p, load bias = %p",
578 si->name, name, reinterpret_cast<void*>(s->st_value),
579 (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
580 reinterpret_cast<void*>((*lsi)->load_bias));
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700581 return s;
582 }
583
Doug Kwan94304352009-10-23 18:11:40 -0700584 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700585}
586
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800587/* This is used by dlsym(3). It performs symbol lookup only within the
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700588 specified soinfo object and not in any of its dependencies.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800589
590 TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
591 that it should do a breadth first search through the dependency
592 tree. This agrees with the ELF spec (aka System V Application
593 Binary Interface) where in Chapter 5 it discuss resolving "Shared
594 Object Dependencies" in breadth first search order.
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700595 */
Elliott Hughes0266ae52014-02-10 17:46:57 -0800596ElfW(Sym)* dlsym_handle_lookup(soinfo* si, const char* name) {
David 'Digit' Turner16084162012-06-12 16:25:37 +0200597 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598}
599
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800600/* This is used by dlsym(3) to performs a global symbol lookup. If the
601 start value is null (for RTLD_DEFAULT), the search starts at the
602 beginning of the global solist. Otherwise the search starts at the
603 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700604 */
Elliott Hughes0266ae52014-02-10 17:46:57 -0800605ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800606 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800607
Elliott Hughescade4c32012-12-20 14:42:14 -0800608 if (start == NULL) {
609 start = solist;
610 }
611
Elliott Hughes0266ae52014-02-10 17:46:57 -0800612 ElfW(Sym)* s = NULL;
Elliott Hughescade4c32012-12-20 14:42:14 -0800613 for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
614 s = soinfo_elf_lookup(si, elf_hash, name);
615 if (s != NULL) {
616 *found = si;
617 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600618 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800619 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600620
Elliott Hughescade4c32012-12-20 14:42:14 -0800621 if (s != NULL) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700622 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
623 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800624 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800625
Elliott Hughescade4c32012-12-20 14:42:14 -0800626 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800627}
628
Kito Chengfa8c05d2013-03-12 14:58:06 +0800629soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800630 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800631 for (soinfo* si = solist; si != NULL; si = si->next) {
632 if (address >= si->base && address - si->base < si->size) {
633 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600634 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800635 }
636 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600637}
638
Elliott Hughes0266ae52014-02-10 17:46:57 -0800639ElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr) {
640 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - si->base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600641
Kito Chengfa8c05d2013-03-12 14:58:06 +0800642 // Search the library's symbol table for any defined symbol which
643 // contains this address.
644 for (size_t i = 0; i < si->nchain; ++i) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800645 ElfW(Sym)* sym = &si->symtab[i];
Kito Chengfa8c05d2013-03-12 14:58:06 +0800646 if (sym->st_shndx != SHN_UNDEF &&
647 soaddr >= sym->st_value &&
648 soaddr < sym->st_value + sym->st_size) {
649 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600650 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800651 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600652
Kito Chengfa8c05d2013-03-12 14:58:06 +0800653 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600654}
655
Elliott Hughes124fae92012-10-31 14:20:03 -0700656static int open_library_on_path(const char* name, const char* const paths[]) {
657 char buf[512];
658 for (size_t i = 0; paths[i] != NULL; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800659 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700660 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700661 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700662 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800663 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700664 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
665 if (fd != -1) {
666 return fd;
667 }
668 }
669 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800670}
671
Elliott Hughes124fae92012-10-31 14:20:03 -0700672static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700673 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800674
Elliott Hughes124fae92012-10-31 14:20:03 -0700675 // If the name contains a slash, we should attempt to open it directly and not search the paths.
676 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700677 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
678 if (fd != -1) {
679 return fd;
680 }
681 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700682#if defined(__LP64__)
683 return -1;
684#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700685 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800686
Elliott Hughes124fae92012-10-31 14:20:03 -0700687 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
688 int fd = open_library_on_path(name, gLdPaths);
689 if (fd == -1) {
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800690 fd = open_library_on_path(name, gDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700691 }
692 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800693}
694
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000695static soinfo* load_library(const char* name, const android_dlextinfo* extinfo) {
Elliott Hughes46882792012-08-03 16:49:39 -0700696 // Open the file.
Elliott Hughes650be4e2013-03-05 18:47:58 -0800697 int fd = open_library(name);
698 if (fd == -1) {
Elliott Hughes46882792012-08-03 16:49:39 -0700699 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800700 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700701 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800702
Elliott Hughes650be4e2013-03-05 18:47:58 -0800703 // Read the ELF header and load the segments.
704 ElfReader elf_reader(name, fd);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000705 if (!elf_reader.Load(extinfo)) {
Elliott Hughes46882792012-08-03 16:49:39 -0700706 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800707 }
708
Elliott Hughes650be4e2013-03-05 18:47:58 -0800709 const char* bname = strrchr(name, '/');
710 soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
711 if (si == NULL) {
Elliott Hughes46882792012-08-03 16:49:39 -0700712 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200713 }
Elliott Hughes650be4e2013-03-05 18:47:58 -0800714 si->base = elf_reader.load_start();
715 si->size = elf_reader.load_size();
716 si->load_bias = elf_reader.load_bias();
717 si->flags = 0;
718 si->entry = 0;
719 si->dynamic = NULL;
720 si->phnum = elf_reader.phdr_count();
721 si->phdr = elf_reader.loaded_phdr();
722 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800723}
724
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800725static soinfo *find_loaded_library(const char* name) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200726 // TODO: don't use basename only for determining libraries
727 // http://code.google.com/p/android/issues/detail?id=6670
728
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800729 const char* bname = strrchr(name, '/');
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200730 bname = bname ? bname + 1 : name;
731
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800732 for (soinfo* si = solist; si != NULL; si = si->next) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800733 if (!strcmp(bname, si->name)) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200734 return si;
735 }
736 }
737 return NULL;
738}
739
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000740static soinfo* find_library_internal(const char* name, const android_dlextinfo* extinfo) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700741 if (name == NULL) {
742 return somain;
743 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200744
Elliott Hughesd23736e2012-11-01 15:16:56 -0700745 soinfo* si = find_loaded_library(name);
746 if (si != NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700747 if (si->flags & FLAG_LINKED) {
748 return si;
749 }
750 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
751 return NULL;
752 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800753
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700754 TRACE("[ '%s' has not been loaded yet. Locating...]", name);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000755 si = load_library(name, extinfo);
Elliott Hughescade4c32012-12-20 14:42:14 -0800756 if (si == NULL) {
757 return NULL;
758 }
759
760 // At this point we know that whatever is loaded @ base is a valid ELF
761 // shared library whose segments are properly mapped in.
Weiwu Chen5ceb8892013-12-03 19:47:34 +0800762 TRACE("[ find_library_internal base=%p size=%zu name='%s' ]",
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700763 reinterpret_cast<void*>(si->base), si->size, si->name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800764
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000765 if (!soinfo_link_image(si, extinfo)) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800766 munmap(reinterpret_cast<void*>(si->base), si->size);
767 soinfo_free(si);
768 return NULL;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700769 }
770
771 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800772}
773
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000774static soinfo* find_library(const char* name, const android_dlextinfo* extinfo) {
775 soinfo* si = find_library_internal(name, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700776 if (si != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700777 si->ref_count++;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700778 }
779 return si;
780}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700781
Elliott Hughesd23736e2012-11-01 15:16:56 -0700782static int soinfo_unload(soinfo* si) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700783 if (si->ref_count == 1) {
784 TRACE("unloading '%s'", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700785 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800786
Elliott Hughes0266ae52014-02-10 17:46:57 -0800787 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800788 if (d->d_tag == DT_NEEDED) {
789 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughes8147d3c2013-05-09 14:19:58 -0700790 TRACE("%s needs to unload %s", si->name, library_name);
791 soinfo_unload(find_loaded_library(library_name));
Elliott Hughesd23736e2012-11-01 15:16:56 -0700792 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800793 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700794
795 munmap(reinterpret_cast<void*>(si->base), si->size);
796 notify_gdb_of_unload(si);
797 soinfo_free(si);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700798 si->ref_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700799 } else {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700800 si->ref_count--;
Elliott Hughesc6200592013-09-30 18:43:46 -0700801 TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700802 }
803 return 0;
804}
805
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800806void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
807 snprintf(buffer, buffer_size, "%s:%s", gDefaultLdPaths[0], gDefaultLdPaths[1]);
808}
809
Elliott Hughescade4c32012-12-20 14:42:14 -0800810void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
811 if (!get_AT_SECURE()) {
812 parse_LD_LIBRARY_PATH(ld_library_path);
813 }
814}
815
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000816soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Elliott Hughese66190d2012-12-18 15:57:55 -0800817 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
818 DL_ERR("invalid flags to dlopen: %x", flags);
819 return NULL;
820 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000821 if (extinfo != NULL && ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0)) {
822 DL_ERR("invalid extended flags to android_dlopen_ext: %x", extinfo->flags);
823 return NULL;
824 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700825 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000826 soinfo* si = find_library(name, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700827 if (si != NULL) {
828 si->CallConstructors();
829 }
830 set_soinfo_pool_protection(PROT_READ);
831 return si;
832}
833
834int do_dlclose(soinfo* si) {
835 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
836 int result = soinfo_unload(si);
837 set_soinfo_pool_protection(PROT_READ);
838 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800839}
840
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700841#if defined(USE_RELA)
Chris Dearman99186652014-02-06 20:36:51 -0800842static int soinfo_relocate(soinfo* si, ElfW(Rela)* rela, unsigned count, soinfo* needed[]) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800843 ElfW(Sym)* s;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700844 soinfo* lsi;
845
846 for (size_t idx = 0; idx < count; ++idx, ++rela) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800847 unsigned type = ELFW(R_TYPE)(rela->r_info);
848 unsigned sym = ELFW(R_SYM)(rela->r_info);
849 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + si->load_bias);
850 ElfW(Addr) sym_addr = 0;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800851 const char* sym_name = NULL;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700852
853 DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
854 if (type == 0) { // R_*_NONE
855 continue;
856 }
857 if (sym != 0) {
Elliott Hughesc62b8a42014-02-12 17:17:41 -0800858 sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700859 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
860 if (s == NULL) {
861 // We only allow an undefined symbol if this is a weak reference...
Elliott Hughesc62b8a42014-02-12 17:17:41 -0800862 s = &si->symtab[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700863 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
864 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
865 return -1;
866 }
867
868 /* IHI0044C AAELF 4.5.1.1:
869
870 Libraries are not searched to resolve weak references.
871 It is not an error for a weak reference to remain unsatisfied.
872
873 During linking, the value of an undefined weak reference is:
874 - Zero if the relocation type is absolute
875 - The address of the place if the relocation is pc-relative
876 - The address of nominal base address if the relocation
877 type is base-relative.
878 */
879
880 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100881#if defined(__aarch64__)
882 case R_AARCH64_JUMP_SLOT:
883 case R_AARCH64_GLOB_DAT:
884 case R_AARCH64_ABS64:
885 case R_AARCH64_ABS32:
886 case R_AARCH64_ABS16:
887 case R_AARCH64_RELATIVE:
888 /*
889 * The sym_addr was initialized to be zero above, or the relocation
890 * code below does not care about value of sym_addr.
891 * No need to do anything.
892 */
893 break;
894#elif defined(__x86_64__)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700895 case R_X86_64_JUMP_SLOT:
896 case R_X86_64_GLOB_DAT:
897 case R_X86_64_32:
898 case R_X86_64_RELATIVE:
899 // No need to do anything.
900 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700901 case R_X86_64_PC32:
902 sym_addr = reloc;
903 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700904#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700905 default:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800906 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700907 return -1;
908 }
909 } else {
910 // We got a definition.
Elliott Hughes0266ae52014-02-10 17:46:57 -0800911 sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700912 }
913 count_relocation(kRelocSymbol);
914 } else {
915 s = NULL;
916 }
917
918 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100919#if defined(__aarch64__)
920 case R_AARCH64_JUMP_SLOT:
921 count_relocation(kRelocAbsolute);
922 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -0800923 TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
924 reloc, (sym_addr + rela->r_addend), sym_name);
925 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100926 break;
927 case R_AARCH64_GLOB_DAT:
928 count_relocation(kRelocAbsolute);
929 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -0800930 TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
931 reloc, (sym_addr + rela->r_addend), sym_name);
932 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100933 break;
934 case R_AARCH64_ABS64:
935 count_relocation(kRelocAbsolute);
936 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -0800937 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
938 reloc, (sym_addr + rela->r_addend), sym_name);
939 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100940 break;
941 case R_AARCH64_ABS32:
942 count_relocation(kRelocAbsolute);
943 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -0800944 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
945 reloc, (sym_addr + rela->r_addend), sym_name);
946 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
947 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
948 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100949 } else {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800950 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
951 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
952 static_cast<ElfW(Addr)>(INT32_MIN),
953 static_cast<ElfW(Addr)>(UINT32_MAX));
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100954 return -1;
955 }
956 break;
957 case R_AARCH64_ABS16:
958 count_relocation(kRelocAbsolute);
959 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -0800960 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
961 reloc, (sym_addr + rela->r_addend), sym_name);
962 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
963 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
964 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100965 } else {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800966 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
967 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
968 static_cast<ElfW(Addr)>(INT16_MIN),
969 static_cast<ElfW(Addr)>(UINT16_MAX));
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100970 return -1;
971 }
972 break;
973 case R_AARCH64_PREL64:
974 count_relocation(kRelocRelative);
975 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -0800976 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
977 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
978 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100979 break;
980 case R_AARCH64_PREL32:
981 count_relocation(kRelocRelative);
982 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -0800983 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
984 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
985 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
986 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
987 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100988 } else {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800989 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
990 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
991 static_cast<ElfW(Addr)>(INT32_MIN),
992 static_cast<ElfW(Addr)>(UINT32_MAX));
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100993 return -1;
994 }
995 break;
996 case R_AARCH64_PREL16:
997 count_relocation(kRelocRelative);
998 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -0800999 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
1000 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1001 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1002 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1003 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001004 } else {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001005 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1006 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1007 static_cast<ElfW(Addr)>(INT16_MIN),
1008 static_cast<ElfW(Addr)>(UINT16_MAX));
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001009 return -1;
1010 }
1011 break;
1012
1013 case R_AARCH64_RELATIVE:
1014 count_relocation(kRelocRelative);
1015 MARK(rela->r_offset);
1016 if (sym) {
1017 DL_ERR("odd RELATIVE form...");
1018 return -1;
1019 }
Elliott Hughes0266ae52014-02-10 17:46:57 -08001020 TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
1021 reloc, (si->base + rela->r_addend));
1022 *reinterpret_cast<ElfW(Addr)*>(reloc) = (si->base + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001023 break;
1024
1025 case R_AARCH64_COPY:
Dmitriy Ivanovb906e132014-05-12 09:06:14 -07001026 /*
1027 * ET_EXEC is not supported so this should not happen.
1028 *
1029 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1030 *
1031 * Section 4.7.1.10 "Dynamic relocations"
1032 * R_AARCH64_COPY may only appear in executable objects where e_type is
1033 * set to ET_EXEC.
Dmitriy Ivanovb906e132014-05-12 09:06:14 -07001034 */
1035 DL_ERR("%s R_AARCH64_COPY relocations are not supported", si->name);
1036 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001037 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001038 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
1039 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001040 break;
1041 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001042 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
1043 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001044 break;
1045#elif defined(__x86_64__)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001046 case R_X86_64_JUMP_SLOT:
1047 count_relocation(kRelocAbsolute);
1048 MARK(rela->r_offset);
1049 TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1050 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001051 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001052 break;
1053 case R_X86_64_GLOB_DAT:
1054 count_relocation(kRelocAbsolute);
1055 MARK(rela->r_offset);
1056 TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1057 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001058 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001059 break;
1060 case R_X86_64_RELATIVE:
1061 count_relocation(kRelocRelative);
1062 MARK(rela->r_offset);
1063 if (sym) {
1064 DL_ERR("odd RELATIVE form...");
1065 return -1;
1066 }
1067 TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1068 static_cast<size_t>(si->base));
Elliott Hughes0266ae52014-02-10 17:46:57 -08001069 *reinterpret_cast<ElfW(Addr)*>(reloc) = si->base + rela->r_addend;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001070 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001071 case R_X86_64_32:
1072 count_relocation(kRelocRelative);
1073 MARK(rela->r_offset);
1074 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1075 static_cast<size_t>(sym_addr), sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001076 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001077 break;
Pavel Chupinc075c182013-10-16 19:13:58 +04001078 case R_X86_64_64:
1079 count_relocation(kRelocRelative);
1080 MARK(rela->r_offset);
1081 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1082 static_cast<size_t>(sym_addr), sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001083 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
Pavel Chupinc075c182013-10-16 19:13:58 +04001084 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001085 case R_X86_64_PC32:
1086 count_relocation(kRelocRelative);
1087 MARK(rela->r_offset);
1088 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1089 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1090 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001091 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001092 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001093#endif
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001094
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001095 default:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001096 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001097 return -1;
1098 }
1099 }
1100 return 0;
1101}
Chris Dearman99186652014-02-06 20:36:51 -08001102
1103#else // REL, not RELA.
1104
Elliott Hughes0266ae52014-02-10 17:46:57 -08001105static int soinfo_relocate(soinfo* si, ElfW(Rel)* rel, unsigned count, soinfo* needed[]) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001106 ElfW(Sym)* s;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001107 soinfo* lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001108
Elliott Hughes46882792012-08-03 16:49:39 -07001109 for (size_t idx = 0; idx < count; ++idx, ++rel) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001110 unsigned type = ELFW(R_TYPE)(rel->r_info);
1111 // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
1112 unsigned sym = ELFW(R_SYM)(rel->r_info);
1113 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + si->load_bias);
1114 ElfW(Addr) sym_addr = 0;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001115 const char* sym_name = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001116
Elliott Hughesc6200592013-09-30 18:43:46 -07001117 DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001118 if (type == 0) { // R_*_NONE
1119 continue;
1120 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001121 if (sym != 0) {
Elliott Hughesc62b8a42014-02-12 17:17:41 -08001122 sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001123 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001124 if (s == NULL) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001125 // We only allow an undefined symbol if this is a weak reference...
Elliott Hughesc62b8a42014-02-12 17:17:41 -08001126 s = &si->symtab[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001127 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -07001128 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -07001129 return -1;
1130 }
1131
1132 /* IHI0044C AAELF 4.5.1.1:
1133
1134 Libraries are not searched to resolve weak references.
1135 It is not an error for a weak reference to remain
1136 unsatisfied.
1137
1138 During linking, the value of an undefined weak reference is:
1139 - Zero if the relocation type is absolute
1140 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -07001141 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -07001142 type is base-relative.
1143 */
1144
1145 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001146#if defined(__arm__)
Doug Kwane8238072009-10-26 12:05:23 -07001147 case R_ARM_JUMP_SLOT:
1148 case R_ARM_GLOB_DAT:
1149 case R_ARM_ABS32:
1150 case R_ARM_RELATIVE: /* Don't care. */
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001151 // sym_addr was initialized to be zero above or relocation
1152 // code below does not care about value of sym_addr.
1153 // No need to do anything.
1154 break;
1155#elif defined(__i386__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001156 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001157 case R_386_GLOB_DAT:
1158 case R_386_32:
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001159 case R_386_RELATIVE: /* Don't care. */
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001160 // sym_addr was initialized to be zero above or relocation
1161 // code below does not care about value of sym_addr.
1162 // No need to do anything.
Doug Kwane8238072009-10-26 12:05:23 -07001163 break;
Doug Kwane8238072009-10-26 12:05:23 -07001164 case R_386_PC32:
1165 sym_addr = reloc;
1166 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001167#endif
Doug Kwane8238072009-10-26 12:05:23 -07001168
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001169#if defined(__arm__)
Doug Kwane8238072009-10-26 12:05:23 -07001170 case R_ARM_COPY:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001171 // Fall through. Can't really copy if weak symbol is not found at run-time.
1172#endif
Doug Kwane8238072009-10-26 12:05:23 -07001173 default:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001174 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Doug Kwane8238072009-10-26 12:05:23 -07001175 return -1;
1176 }
1177 } else {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001178 // We got a definition.
Elliott Hughes0266ae52014-02-10 17:46:57 -08001179 sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001180 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001181 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001182 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001183 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001184 }
1185
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001186 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001187#if defined(__arm__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001188 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001189 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001190 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001191 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001192 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001193 break;
1194 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001195 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001196 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001197 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001198 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001199 break;
1200 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001201 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001202 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001203 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001204 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001205 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001206 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001207 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001208 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001209 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001210 reloc, sym_addr, rel->r_offset, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001211 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001212 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001213 case R_ARM_COPY:
Dmitriy Ivanovb906e132014-05-12 09:06:14 -07001214 /*
Dmitriy Ivanov6275f202014-05-12 11:36:56 -07001215 * ET_EXEC is not supported so this should not happen.
1216 *
Dmitriy Ivanovb906e132014-05-12 09:06:14 -07001217 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1218 *
1219 * Section 4.7.1.10 "Dynamic relocations"
1220 * R_ARM_COPY may only appear in executable objects where e_type is
1221 * set to ET_EXEC.
Dmitriy Ivanovb906e132014-05-12 09:06:14 -07001222 */
1223 DL_ERR("%s R_ARM_COPY relocations are not supported", si->name);
1224 return -1;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001225#elif defined(__i386__)
1226 case R_386_JMP_SLOT:
1227 count_relocation(kRelocAbsolute);
1228 MARK(rel->r_offset);
1229 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001230 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001231 break;
1232 case R_386_GLOB_DAT:
1233 count_relocation(kRelocAbsolute);
1234 MARK(rel->r_offset);
1235 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001236 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001237 break;
1238 case R_386_32:
1239 count_relocation(kRelocRelative);
1240 MARK(rel->r_offset);
1241 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001242 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001243 break;
1244 case R_386_PC32:
1245 count_relocation(kRelocRelative);
1246 MARK(rel->r_offset);
1247 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1248 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001249 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001250 break;
1251#elif defined(__mips__)
1252 case R_MIPS_REL32:
Chris Dearman99186652014-02-06 20:36:51 -08001253#if defined(__LP64__)
1254 // MIPS Elf64_Rel entries contain compound relocations
1255 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
1256 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
1257 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001258 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
Chris Dearman99186652014-02-06 20:36:51 -08001259 type, (unsigned)ELF64_R_TYPE2(rel->r_info),
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001260 (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
Chris Dearman99186652014-02-06 20:36:51 -08001261 return -1;
1262 }
1263#endif
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001264 count_relocation(kRelocAbsolute);
1265 MARK(rel->r_offset);
Chris Dearman99186652014-02-06 20:36:51 -08001266 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
1267 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001268 if (s) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001269 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001270 } else {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001271 *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001272 }
1273 break;
1274#endif
1275
1276#if defined(__arm__)
1277 case R_ARM_RELATIVE:
1278#elif defined(__i386__)
1279 case R_386_RELATIVE:
1280#endif
1281 count_relocation(kRelocRelative);
1282 MARK(rel->r_offset);
1283 if (sym) {
1284 DL_ERR("odd RELATIVE form...");
1285 return -1;
1286 }
1287 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1288 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base));
Elliott Hughes0266ae52014-02-10 17:46:57 -08001289 *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001290 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001291
1292 default:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001293 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001294 return -1;
1295 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001296 }
1297 return 0;
1298}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001299#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001300
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001301#if defined(__mips__)
Brian Carlstrom87c35852013-08-20 21:05:44 -07001302static bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
Chris Dearman99186652014-02-06 20:36:51 -08001303 ElfW(Addr)** got = si->plt_got;
Brian Carlstrom87c35852013-08-20 21:05:44 -07001304 if (got == NULL) {
1305 return true;
1306 }
1307 unsigned local_gotno = si->mips_local_gotno;
1308 unsigned gotsym = si->mips_gotsym;
1309 unsigned symtabno = si->mips_symtabno;
Elliott Hughes0266ae52014-02-10 17:46:57 -08001310 ElfW(Sym)* symtab = si->symtab;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001311
Chris Dearman99186652014-02-06 20:36:51 -08001312 // got[0] is the address of the lazy resolver function.
1313 // got[1] may be used for a GNU extension.
1314 // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
1315 // FIXME: maybe this should be in a separate routine?
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001316 if ((si->flags & FLAG_LINKER) == 0) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001317 size_t g = 0;
Chris Dearman99186652014-02-06 20:36:51 -08001318 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
1319 if (reinterpret_cast<intptr_t>(got[g]) < 0) {
1320 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001321 }
Chris Dearman99186652014-02-06 20:36:51 -08001322 // Relocate the local GOT entries.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001323 for (; g < local_gotno; g++) {
Chris Dearman99186652014-02-06 20:36:51 -08001324 got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001325 }
1326 }
1327
Chris Dearman99186652014-02-06 20:36:51 -08001328 // Now for the global GOT entries...
Elliott Hughes0266ae52014-02-10 17:46:57 -08001329 ElfW(Sym)* sym = symtab + gotsym;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001330 got = si->plt_got + local_gotno;
Brian Carlstrom87c35852013-08-20 21:05:44 -07001331 for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
Chris Dearman99186652014-02-06 20:36:51 -08001332 // This is an undefined reference... try to locate it.
1333 const char* sym_name = si->strtab + sym->st_name;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001334 soinfo* lsi;
Chris Dearman99186652014-02-06 20:36:51 -08001335 ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001336 if (s == NULL) {
Chris Dearman99186652014-02-06 20:36:51 -08001337 // We only allow an undefined symbol if this is a weak reference.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001338 s = &symtab[g];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001339 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001340 DL_ERR("cannot locate \"%s\"...", sym_name);
Brian Carlstrom87c35852013-08-20 21:05:44 -07001341 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001342 }
1343 *got = 0;
Chris Dearman99186652014-02-06 20:36:51 -08001344 } else {
1345 // FIXME: is this sufficient?
1346 // For reference see NetBSD link loader
1347 // http://cvsweb.netbsd.org/bsdweb.cgi/src/libexec/ld.elf_so/arch/mips/mips_reloc.c?rev=1.53&content-type=text/x-cvsweb-markup
1348 *got = reinterpret_cast<ElfW(Addr)*>(lsi->load_bias + s->st_value);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001349 }
1350 }
Brian Carlstrom87c35852013-08-20 21:05:44 -07001351 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001352}
1353#endif
1354
Kito Cheng812fd422014-03-25 22:53:56 +08001355void soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001356 if (functions == NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001357 return;
1358 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001359
Elliott Hughesc6200592013-09-30 18:43:46 -07001360 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001361
1362 int begin = reverse ? (count - 1) : 0;
1363 int end = reverse ? -1 : count;
1364 int step = reverse ? -1 : 1;
1365
1366 for (int i = begin; i != end; i += step) {
1367 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1368 CallFunction("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001369 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001370
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001371 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001372}
1373
Kito Cheng812fd422014-03-25 22:53:56 +08001374void soinfo::CallFunction(const char* function_name __unused, linker_function_t function) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001375 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001376 return;
1377 }
1378
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001379 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001380 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001381 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001382
1383 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1384 // are still writable. This happens with our debug malloc (see http://b/7941716).
1385 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001386}
1387
Elliott Hughesd23736e2012-11-01 15:16:56 -07001388void soinfo::CallPreInitConstructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001389 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1390 // but ignored in a shared library.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001391 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1392}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001393
Elliott Hughesd23736e2012-11-01 15:16:56 -07001394void soinfo::CallConstructors() {
1395 if (constructors_called) {
1396 return;
1397 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001398
Elliott Hughesd23736e2012-11-01 15:16:56 -07001399 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1400 // protect against recursive constructor calls. One simple example of constructor recursion
1401 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1402 // 1. The program depends on libc, so libc's constructor is called here.
1403 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1404 // 3. dlopen() calls the constructors on the newly created
1405 // soinfo for libc_malloc_debug_leak.so.
1406 // 4. The debug .so depends on libc, so CallConstructors is
1407 // called again with the libc soinfo. If it doesn't trigger the early-
1408 // out above, the libc constructor will be called again (recursively!).
1409 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001410
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001411 if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
1412 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001413 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001414 name, preinit_array_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001415 }
1416
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001417 if (dynamic != NULL) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001418 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001419 if (d->d_tag == DT_NEEDED) {
1420 const char* library_name = strtab + d->d_un.d_val;
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001421 TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
1422 find_loaded_library(library_name)->CallConstructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001423 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001424 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001425 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001426
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001427 TRACE("\"%s\": calling constructors", name);
1428
1429 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001430 CallFunction("DT_INIT", init_func);
1431 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001432}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001433
Elliott Hughesd23736e2012-11-01 15:16:56 -07001434void soinfo::CallDestructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001435 TRACE("\"%s\": calling destructors", name);
1436
1437 // DT_FINI_ARRAY must be parsed in reverse order.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001438 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001439
1440 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001441 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001442}
1443
1444/* Force any of the closed stdin, stdout and stderr to be associated with
1445 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001446static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001447 int dev_null, i, status;
1448 int return_value = 0;
1449
David 'Digit' Turner16084162012-06-12 16:25:37 +02001450 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001451 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001452 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001453 return -1;
1454 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001455 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001456
1457 /* If any of the stdio file descriptors is valid and not associated
1458 with /dev/null, dup /dev/null to it. */
1459 for (i = 0; i < 3; i++) {
1460 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001461 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001462 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001463 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001464
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001465 TRACE("[ Nullifying stdio file descriptor %d]", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001466 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001467
Elliott Hughes46882792012-08-03 16:49:39 -07001468 /* If file is opened, we are good. */
1469 if (status != -1) {
1470 continue;
1471 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001472
1473 /* The only error we allow is that the file descriptor does not
1474 exist, in which case we dup /dev/null to it. */
1475 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001476 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001477 return_value = -1;
1478 continue;
1479 }
1480
1481 /* Try dupping /dev/null to this stdio file descriptor and
1482 repeat if there is a signal. Note that any errors in closing
1483 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001484 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001485 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001486 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001487 return_value = -1;
1488 continue;
1489 }
1490 }
1491
1492 /* If /dev/null is not one of the stdio file descriptors, close it. */
1493 if (dev_null > 2) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001494 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001495 status = TEMP_FAILURE_RETRY(close(dev_null));
1496 if (status == -1) {
1497 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001498 return_value = -1;
1499 }
1500 }
1501
1502 return return_value;
1503}
1504
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00001505static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001506 /* "base" might wrap around UINT32_MAX. */
Elliott Hughes0266ae52014-02-10 17:46:57 -08001507 ElfW(Addr) base = si->load_bias;
1508 const ElfW(Phdr)* phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001509 int phnum = si->phnum;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001510 bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001511
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001512 /* We can't debug anything until the linker is relocated */
1513 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001514 INFO("[ linking %s ]", si->name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001515 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001516 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001517
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001518 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001519 size_t dynamic_count;
Elliott Hughes0266ae52014-02-10 17:46:57 -08001520 ElfW(Word) dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001521 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001522 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001523 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001524 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001525 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001526 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001527 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001528 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001529 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001530 DEBUG("dynamic = %p", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001531 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001532 }
1533
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001534#if defined(__arm__)
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001535 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1536 &si->ARM_exidx, &si->ARM_exidx_count);
1537#endif
1538
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001539 // Extract useful information from dynamic section.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001540 uint32_t needed_count = 0;
Elliott Hughes0266ae52014-02-10 17:46:57 -08001541 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001542 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1543 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1544 switch (d->d_tag) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001545 case DT_HASH:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001546 si->nbucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[0];
1547 si->nchain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[1];
1548 si->bucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8);
1549 si->chain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001550 break;
1551 case DT_STRTAB:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001552 si->strtab = reinterpret_cast<const char*>(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001553 break;
1554 case DT_SYMTAB:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001555 si->symtab = reinterpret_cast<ElfW(Sym)*>(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001556 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001557#if !defined(__LP64__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001558 case DT_PLTREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001559 if (d->d_un.d_val != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001560 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1561 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001562 }
1563 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001564#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001565 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001566#if defined(USE_RELA)
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001567 si->plt_rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001568#else
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001569 si->plt_rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001570#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001571 break;
1572 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001573#if defined(USE_RELA)
Elliott Hughes0266ae52014-02-10 17:46:57 -08001574 si->plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001575#else
Elliott Hughes0266ae52014-02-10 17:46:57 -08001576 si->plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001577#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001578 break;
Chris Dearman99186652014-02-06 20:36:51 -08001579#if defined(__mips__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001580 case DT_PLTGOT:
Chris Dearman99186652014-02-06 20:36:51 -08001581 // Used by mips and mips64.
1582 si->plt_got = reinterpret_cast<ElfW(Addr)**>(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001583 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001584#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001585 case DT_DEBUG:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001586 // Set the DT_DEBUG entry to the address of _r_debug for GDB
Chris Dearmancf239052013-01-11 15:32:20 -08001587 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08001588// FIXME: not working currently for N64
1589// The flags for the LOAD and DYNAMIC program headers do not agree.
1590// The LOAD section containng the dynamic table has been mapped as
1591// read-only, but the DYNAMIC header claims it is writable.
1592#if !(defined(__mips__) && defined(__LP64__))
Elliott Hughes99c32052013-01-14 09:56:21 -08001593 if ((dynamic_flags & PF_W) != 0) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001594 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
Elliott Hughes99c32052013-01-14 09:56:21 -08001595 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001596 break;
Chris Dearman99186652014-02-06 20:36:51 -08001597#endif
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001598#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001599 case DT_RELA:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001600 si->rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001601 break;
1602 case DT_RELASZ:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001603 si->rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001604 break;
1605 case DT_REL:
1606 DL_ERR("unsupported DT_REL in \"%s\"", si->name);
1607 return false;
1608 case DT_RELSZ:
1609 DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name);
1610 return false;
1611#else
1612 case DT_REL:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001613 si->rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001614 break;
1615 case DT_RELSZ:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001616 si->rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001617 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001618 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001619 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1620 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001621#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001622 case DT_INIT:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001623 si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001624 DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001625 break;
1626 case DT_FINI:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001627 si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001628 DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001629 break;
1630 case DT_INIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001631 si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001632 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001633 break;
1634 case DT_INIT_ARRAYSZ:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001635 si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001636 break;
1637 case DT_FINI_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001638 si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001639 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001640 break;
1641 case DT_FINI_ARRAYSZ:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001642 si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001643 break;
1644 case DT_PREINIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001645 si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001646 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001647 break;
1648 case DT_PREINIT_ARRAYSZ:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001649 si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001650 break;
1651 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07001652#if defined(__LP64__)
1653 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1654 return false;
1655#else
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001656 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001657 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07001658#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001659 case DT_SYMBOLIC:
1660 si->has_DT_SYMBOLIC = true;
1661 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001662 case DT_NEEDED:
1663 ++needed_count;
1664 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001665 case DT_FLAGS:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001666 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001667#if defined(__LP64__)
1668 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1669 return false;
1670#else
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001671 si->has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07001672#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001673 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001674 if (d->d_un.d_val & DF_SYMBOLIC) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001675 si->has_DT_SYMBOLIC = true;
1676 }
1677 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001678#if defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001679 case DT_STRSZ:
1680 case DT_SYMENT:
1681 case DT_RELENT:
1682 break;
1683 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001684 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001685 {
Benjamin Adolphi006f9ad2014-02-19 00:50:32 +01001686 r_debug** dp = reinterpret_cast<r_debug**>(base + d->d_un.d_ptr);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001687 *dp = &_r_debug;
1688 }
1689 break;
1690 case DT_MIPS_RLD_VERSION:
1691 case DT_MIPS_FLAGS:
1692 case DT_MIPS_BASE_ADDRESS:
1693 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001694 break;
1695
1696 case DT_MIPS_SYMTABNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001697 si->mips_symtabno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001698 break;
1699
1700 case DT_MIPS_LOCAL_GOTNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001701 si->mips_local_gotno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001702 break;
1703
1704 case DT_MIPS_GOTSYM:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001705 si->mips_gotsym = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001706 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001707#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001708
1709 default:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001710 DEBUG("Unused DT entry: type %p arg %p",
1711 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001712 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001713 }
1714 }
1715
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001716 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
1717 reinterpret_cast<void*>(si->base), si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001718
Elliott Hughes124fae92012-10-31 14:20:03 -07001719 // Sanity checks.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001720 if (relocating_linker && needed_count != 0) {
1721 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1722 return false;
1723 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001724 if (si->nbucket == 0) {
1725 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1726 return false;
1727 }
1728 if (si->strtab == 0) {
1729 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1730 return false;
1731 }
1732 if (si->symtab == 0) {
1733 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1734 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001735 }
1736
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001737 // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001738 if (si->flags & FLAG_EXE) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001739 memset(gLdPreloads, 0, sizeof(gLdPreloads));
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001740 size_t preload_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001741 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +00001742 soinfo* lsi = find_library(gLdPreloadNames[i], NULL);
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001743 if (lsi != NULL) {
1744 gLdPreloads[preload_count++] = lsi;
1745 } else {
1746 // As with glibc, failure to load an LD_PRELOAD library is just a warning.
1747 DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
1748 gLdPreloadNames[i], si->name, linker_get_error_buffer());
Matt Fischer4fd42c12009-12-31 12:09:10 -06001749 }
Matt Fischer4fd42c12009-12-31 12:09:10 -06001750 }
1751 }
1752
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001753 soinfo** needed = reinterpret_cast<soinfo**>(alloca((1 + needed_count) * sizeof(soinfo*)));
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001754 soinfo** pneeded = needed;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001755
Elliott Hughes0266ae52014-02-10 17:46:57 -08001756 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001757 if (d->d_tag == DT_NEEDED) {
1758 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001759 DEBUG("%s needs %s", si->name, library_name);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +00001760 soinfo* lsi = find_library(library_name, NULL);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001761 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001762 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001763 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001764 library_name, si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001765 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001766 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001767 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001768 }
1769 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001770 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001771
Elliott Hughese4d792a2013-10-28 14:19:05 -07001772#if !defined(__LP64__)
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001773 if (si->has_text_relocations) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001774 // Make segments writable to allow text relocations to work properly. We will later call
1775 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
Du Chenyang865119e2014-04-18 19:17:40 +08001776#if !defined(__i386__) // The platform itself has too many text relocations on x86.
Nick Kralevich3d4470c2013-10-22 12:06:36 -07001777 DL_WARN("%s has text relocations. This is wasting memory and prevents "
1778 "security hardening. Please fix.", si->name);
Du Chenyang865119e2014-04-18 19:17:40 +08001779#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001780 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1781 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1782 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001783 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001784 }
1785 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07001786#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001787
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001788#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001789 if (si->plt_rela != NULL) {
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001790 DEBUG("[ relocating %s plt ]\n", si->name);
Chris Dearman99186652014-02-06 20:36:51 -08001791 if (soinfo_relocate(si, si->plt_rela, si->plt_rela_count, needed)) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001792 return false;
1793 }
1794 }
1795 if (si->rela != NULL) {
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001796 DEBUG("[ relocating %s ]\n", si->name);
Chris Dearman99186652014-02-06 20:36:51 -08001797 if (soinfo_relocate(si, si->rela, si->rela_count, needed)) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001798 return false;
1799 }
1800 }
1801#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001802 if (si->plt_rel != NULL) {
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001803 DEBUG("[ relocating %s plt ]", si->name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001804 if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001805 return false;
1806 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001807 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001808 if (si->rel != NULL) {
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001809 DEBUG("[ relocating %s ]", si->name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001810 if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001811 return false;
1812 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001813 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001814#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001815
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001816#if defined(__mips__)
Brian Carlstrom87c35852013-08-20 21:05:44 -07001817 if (!mips_relocate_got(si, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001818 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001819 }
1820#endif
1821
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001822 si->flags |= FLAG_LINKED;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001823 DEBUG("[ finished linking %s ]", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001824
Elliott Hughese4d792a2013-10-28 14:19:05 -07001825#if !defined(__LP64__)
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001826 if (si->has_text_relocations) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001827 // All relocations are done, we can protect our segments back to read-only.
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001828 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1829 DL_ERR("can't protect segments for \"%s\": %s",
1830 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001831 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001832 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001833 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07001834#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001835
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001836 /* We can also turn on GNU RELRO protection */
1837 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001838 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1839 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001840 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001841 }
1842
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00001843 /* Handle serializing/sharing the RELRO segment */
1844 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
1845 if (phdr_table_serialize_gnu_relro(si->phdr, si->phnum, si->load_bias,
1846 extinfo->relro_fd) < 0) {
1847 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
1848 si->name, strerror(errno));
1849 return false;
1850 }
1851 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
1852 if (phdr_table_map_gnu_relro(si->phdr, si->phnum, si->load_bias,
1853 extinfo->relro_fd) < 0) {
1854 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
1855 si->name, strerror(errno));
1856 return false;
1857 }
1858 }
1859
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001860 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001861 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001862}
1863
Nick Kralevich468319c2011-11-11 15:53:17 -08001864/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001865 * This function add vdso to internal dso list.
1866 * It helps to stack unwinding through signal handlers.
1867 * Also, it makes bionic more like glibc.
1868 */
Kito Cheng812fd422014-03-25 22:53:56 +08001869static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001870#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08001871 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
1872 if (ehdr_vdso == NULL) {
1873 return;
1874 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001875
Elliott Hughes0266ae52014-02-10 17:46:57 -08001876 soinfo* si = soinfo_alloc("[vdso]");
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001877
Elliott Hughes0266ae52014-02-10 17:46:57 -08001878 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
1879 si->phnum = ehdr_vdso->e_phnum;
1880 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
1881 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1882 si->flags = 0;
1883 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001884
Torne (Richard Coles)0dcf06f2014-04-22 11:59:26 +01001885 soinfo_link_image(si, NULL);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001886#endif
1887}
1888
1889/*
Nick Kralevich468319c2011-11-11 15:53:17 -08001890 * This code is called after the linker has linked itself and
1891 * fixed it's own GOT. It is safe to make references to externs
1892 * and other non-local data at this point.
1893 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08001894static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001895 /* NOTE: we store the args pointer on a special location
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001896 * of the temporary TLS area in order to pass it to
1897 * the C Library's runtime initializer.
1898 *
1899 * The initializer must clear the slot and reset the TLS
1900 * to point to a different location to ensure that no other
1901 * shared library constructor can access it.
1902 */
Elliott Hughesd3920b32013-02-07 18:39:34 -08001903 __libc_init_tls(args);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001904
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001905#if TIMING
1906 struct timeval t0, t1;
1907 gettimeofday(&t0, 0);
1908#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001909
Elliott Hughes18a206c2012-10-29 17:37:13 -07001910 // Initialize environment functions, and get to the ELF aux vectors table.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001911 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001912
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07001913 // If this is a setuid/setgid program, close the security hole described in
1914 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1915 if (get_AT_SECURE()) {
1916 nullify_closed_stdio();
1917 }
1918
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001919 debuggerd_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001920
Elliott Hughes18a206c2012-10-29 17:37:13 -07001921 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001922 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1923 if (LD_DEBUG != NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001924 gLdDebugVerbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001925 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001926
Elliott Hughes18a206c2012-10-29 17:37:13 -07001927 // Normally, these are cleaned by linker_env_init, but the test
1928 // doesn't cost us anything.
1929 const char* ldpath_env = NULL;
1930 const char* ldpreload_env = NULL;
1931 if (!get_AT_SECURE()) {
1932 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1933 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001934 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001935
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001936 INFO("[ android linker & debugger ]");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001937
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001938 soinfo* si = soinfo_alloc(args.argv[0]);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001939 if (si == NULL) {
1940 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001941 }
1942
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001943 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001944 si->flags |= FLAG_EXE;
Elliott Hughes3a9c5d62014-02-10 13:31:13 -08001945 link_map* map = &(si->link_map_head);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001946
1947 map->l_addr = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001948 map->l_name = args.argv[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001949 map->l_prev = NULL;
1950 map->l_next = NULL;
1951
1952 _r_debug.r_map = map;
1953 r_debug_tail = map;
1954
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001955 /* gdb expects the linker to be in the debug shared object list.
1956 * Without this, gdb has trouble locating the linker's ".text"
1957 * and ".plt" sections. Gdb could also potentially use this to
1958 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1959 * Don't use soinfo_alloc(), because the linker shouldn't
1960 * be on the soinfo list.
Ben Cheng06f0e742012-08-10 16:07:02 -07001961 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001962 {
1963 static soinfo linker_soinfo;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001964#if defined(__LP64__)
Pavel Chupin1a57f9f2013-02-06 19:21:46 +04001965 strlcpy(linker_soinfo.name, "/system/bin/linker64", sizeof(linker_soinfo.name));
1966#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001967 strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
Pavel Chupin1a57f9f2013-02-06 19:21:46 +04001968#endif
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001969 linker_soinfo.flags = 0;
1970 linker_soinfo.base = linker_base;
1971
1972 /*
1973 * Set the dynamic field in the link map otherwise gdb will complain with
1974 * the following:
1975 * warning: .dynamic section for "/system/bin/linker" is not at the
1976 * expected address (wrong library or version mismatch?)
1977 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08001978 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001979 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001980 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1981 &linker_soinfo.dynamic, NULL, NULL);
1982 insert_soinfo_into_debug_map(&linker_soinfo);
1983 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001984
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001985 // Extract information passed from the kernel.
Elliott Hughes0266ae52014-02-10 17:46:57 -08001986 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001987 si->phnum = args.getauxval(AT_PHNUM);
1988 si->entry = args.getauxval(AT_ENTRY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001989
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001990 /* Compute the value of si->base. We can't rely on the fact that
1991 * the first entry is the PHDR because this will not be true
1992 * for certain executables (e.g. some in the NDK unit test suite)
1993 */
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001994 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001995 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001996 si->load_bias = 0;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001997 for (size_t i = 0; i < si->phnum; ++i) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001998 if (si->phdr[i].p_type == PT_PHDR) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001999 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2000 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002001 break;
2002 }
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002003 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002004 si->dynamic = NULL;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002005 si->ref_count = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002006
Nick Kralevich2aebf542014-05-07 10:32:39 -07002007 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2008 if (elf_hdr->e_type != ET_DYN) {
2009 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2010 exit(EXIT_FAILURE);
2011 }
2012
Elliott Hughes46882792012-08-03 16:49:39 -07002013 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2014 parse_LD_LIBRARY_PATH(ldpath_env);
2015 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06002016
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02002017 somain = si;
2018
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002019 if (!soinfo_link_image(si, NULL)) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08002020 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
Elliott Hughes18a206c2012-10-29 17:37:13 -07002021 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002022 }
2023
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002024 add_vdso(args);
2025
Elliott Hughesd23736e2012-11-01 15:16:56 -07002026 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04002027
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002028 for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
2029 gLdPreloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08002030 }
2031
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002032 /* After the link_image, the si->load_bias is initialized.
2033 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2034 * We need to update this value for so exe here. So Unwind_Backtrace
2035 * for some arch like x86 could work correctly within so exe.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08002036 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08002037 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002038 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002039
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002040#if TIMING
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002041 gettimeofday(&t1, NULL);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002042 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002043 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002044 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002045#endif
2046#if STATS
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002047 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07002048 linker_stats.count[kRelocAbsolute],
2049 linker_stats.count[kRelocRelative],
2050 linker_stats.count[kRelocCopy],
2051 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002052#endif
2053#if COUNT_PAGES
2054 {
2055 unsigned n;
2056 unsigned i;
2057 unsigned count = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002058 for (n = 0; n < 4096; n++) {
2059 if (bitmask[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002060 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002061#if defined(__LP64__)
2062 for (i = 0; i < 32; i++) {
2063#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002064 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002065#endif
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002066 if (x & 1) {
2067 count++;
2068 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002069 x >>= 1;
2070 }
2071 }
2072 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002073 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002074 }
2075#endif
2076
2077#if TIMING || STATS || COUNT_PAGES
2078 fflush(stdout);
2079#endif
2080
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002081 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002082 return si->entry;
2083}
Nick Kralevich468319c2011-11-11 15:53:17 -08002084
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002085/* Compute the load-bias of an existing executable. This shall only
2086 * be used to compute the load bias of an executable or shared library
2087 * that was loaded by the kernel itself.
2088 *
2089 * Input:
2090 * elf -> address of ELF header, assumed to be at the start of the file.
2091 * Return:
2092 * load bias, i.e. add the value of any p_vaddr in the file to get
2093 * the corresponding address in memory.
2094 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002095static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2096 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002097 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002098 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002099
Elliott Hughes0266ae52014-02-10 17:46:57 -08002100 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002101 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002102 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002103 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002104 }
2105 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002106}
2107
Nick Kralevich468319c2011-11-11 15:53:17 -08002108/*
2109 * This is the entry point for the linker, called from begin.S. This
2110 * method is responsible for fixing the linker's own relocations, and
2111 * then calling __linker_init_post_relocation().
2112 *
2113 * Because this method is called before the linker has fixed it's own
2114 * relocations, any attempt to reference an extern variable, extern
2115 * function, or other GOT reference will generate a segfault.
2116 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002117extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002118 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002119
Elliott Hughes0266ae52014-02-10 17:46:57 -08002120 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
2121 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002122 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002123
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002124 soinfo linker_so;
2125 memset(&linker_so, 0, sizeof(soinfo));
Nick Kralevich468319c2011-11-11 15:53:17 -08002126
Elliott Hughesb93702a2013-12-21 16:07:45 -08002127 strcpy(linker_so.name, "[dynamic linker]");
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002128 linker_so.base = linker_addr;
2129 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2130 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002131 linker_so.dynamic = NULL;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002132 linker_so.phdr = phdr;
2133 linker_so.phnum = elf_hdr->e_phnum;
2134 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07002135
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002136 if (!soinfo_link_image(&linker_so, NULL)) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002137 // It would be nice to print an error message, but if the linker
2138 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002139 // call write() (because it involves a GOT reference). We may as
2140 // well try though...
2141 const char* msg = "CANNOT LINK EXECUTABLE: ";
2142 write(2, msg, strlen(msg));
2143 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2144 write(2, "\n", 1);
2145 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002146 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002147
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002148 // We have successfully fixed our own relocations. It's safe to run
2149 // the main part of the linker now.
Elliott Hughes0d787c12013-04-04 13:46:46 -07002150 args.abort_message_ptr = &gAbortMessage;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002151 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002152
2153 set_soinfo_pool_protection(PROT_READ);
2154
2155 // Return the address that the calling assembly stub should jump to.
2156 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002157}