blob: 0db6e0f191ab309af0846634dd33049156e07be9 [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.
1034 *
1035 * FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1036 * We should explicitly disallow ET_DYN executables from having
1037 * R_AARCH64_COPY relocations.
1038 */
1039 DL_ERR("%s R_AARCH64_COPY relocations are not supported", si->name);
1040 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001041 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001042 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
1043 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001044 break;
1045 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001046 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
1047 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001048 break;
1049#elif defined(__x86_64__)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001050 case R_X86_64_JUMP_SLOT:
1051 count_relocation(kRelocAbsolute);
1052 MARK(rela->r_offset);
1053 TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1054 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001055 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001056 break;
1057 case R_X86_64_GLOB_DAT:
1058 count_relocation(kRelocAbsolute);
1059 MARK(rela->r_offset);
1060 TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1061 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001062 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001063 break;
1064 case R_X86_64_RELATIVE:
1065 count_relocation(kRelocRelative);
1066 MARK(rela->r_offset);
1067 if (sym) {
1068 DL_ERR("odd RELATIVE form...");
1069 return -1;
1070 }
1071 TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1072 static_cast<size_t>(si->base));
Elliott Hughes0266ae52014-02-10 17:46:57 -08001073 *reinterpret_cast<ElfW(Addr)*>(reloc) = si->base + rela->r_addend;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001074 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001075 case R_X86_64_32:
1076 count_relocation(kRelocRelative);
1077 MARK(rela->r_offset);
1078 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1079 static_cast<size_t>(sym_addr), sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001080 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001081 break;
Pavel Chupinc075c182013-10-16 19:13:58 +04001082 case R_X86_64_64:
1083 count_relocation(kRelocRelative);
1084 MARK(rela->r_offset);
1085 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1086 static_cast<size_t>(sym_addr), sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001087 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
Pavel Chupinc075c182013-10-16 19:13:58 +04001088 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001089 case R_X86_64_PC32:
1090 count_relocation(kRelocRelative);
1091 MARK(rela->r_offset);
1092 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1093 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1094 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001095 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001096 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001097#endif
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001098
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001099 default:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001100 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001101 return -1;
1102 }
1103 }
1104 return 0;
1105}
Chris Dearman99186652014-02-06 20:36:51 -08001106
1107#else // REL, not RELA.
1108
Elliott Hughes0266ae52014-02-10 17:46:57 -08001109static int soinfo_relocate(soinfo* si, ElfW(Rel)* rel, unsigned count, soinfo* needed[]) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001110 ElfW(Sym)* s;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001111 soinfo* lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001112
Elliott Hughes46882792012-08-03 16:49:39 -07001113 for (size_t idx = 0; idx < count; ++idx, ++rel) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001114 unsigned type = ELFW(R_TYPE)(rel->r_info);
1115 // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
1116 unsigned sym = ELFW(R_SYM)(rel->r_info);
1117 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + si->load_bias);
1118 ElfW(Addr) sym_addr = 0;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001119 const char* sym_name = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001120
Elliott Hughesc6200592013-09-30 18:43:46 -07001121 DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001122 if (type == 0) { // R_*_NONE
1123 continue;
1124 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001125 if (sym != 0) {
Elliott Hughesc62b8a42014-02-12 17:17:41 -08001126 sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001127 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001128 if (s == NULL) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001129 // We only allow an undefined symbol if this is a weak reference...
Elliott Hughesc62b8a42014-02-12 17:17:41 -08001130 s = &si->symtab[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001131 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -07001132 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -07001133 return -1;
1134 }
1135
1136 /* IHI0044C AAELF 4.5.1.1:
1137
1138 Libraries are not searched to resolve weak references.
1139 It is not an error for a weak reference to remain
1140 unsatisfied.
1141
1142 During linking, the value of an undefined weak reference is:
1143 - Zero if the relocation type is absolute
1144 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -07001145 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -07001146 type is base-relative.
1147 */
1148
1149 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001150#if defined(__arm__)
Doug Kwane8238072009-10-26 12:05:23 -07001151 case R_ARM_JUMP_SLOT:
1152 case R_ARM_GLOB_DAT:
1153 case R_ARM_ABS32:
1154 case R_ARM_RELATIVE: /* Don't care. */
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001155 // sym_addr was initialized to be zero above or relocation
1156 // code below does not care about value of sym_addr.
1157 // No need to do anything.
1158 break;
1159#elif defined(__i386__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001160 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001161 case R_386_GLOB_DAT:
1162 case R_386_32:
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001163 case R_386_RELATIVE: /* Don't care. */
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001164 // sym_addr was initialized to be zero above or relocation
1165 // code below does not care about value of sym_addr.
1166 // No need to do anything.
Doug Kwane8238072009-10-26 12:05:23 -07001167 break;
Doug Kwane8238072009-10-26 12:05:23 -07001168 case R_386_PC32:
1169 sym_addr = reloc;
1170 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001171#endif
Doug Kwane8238072009-10-26 12:05:23 -07001172
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001173#if defined(__arm__)
Doug Kwane8238072009-10-26 12:05:23 -07001174 case R_ARM_COPY:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001175 // Fall through. Can't really copy if weak symbol is not found at run-time.
1176#endif
Doug Kwane8238072009-10-26 12:05:23 -07001177 default:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001178 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Doug Kwane8238072009-10-26 12:05:23 -07001179 return -1;
1180 }
1181 } else {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001182 // We got a definition.
Elliott Hughes0266ae52014-02-10 17:46:57 -08001183 sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001184 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001185 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001186 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001187 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001188 }
1189
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001190 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001191#if defined(__arm__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001192 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001193 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001194 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001195 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001196 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001197 break;
1198 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001199 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001200 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001201 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001202 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001203 break;
1204 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001205 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001206 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001207 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001208 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001209 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001210 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001211 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001212 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001213 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001214 reloc, sym_addr, rel->r_offset, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001215 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001216 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001217 case R_ARM_COPY:
Dmitriy Ivanovb906e132014-05-12 09:06:14 -07001218 /*
1219 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1220 *
1221 * Section 4.7.1.10 "Dynamic relocations"
1222 * R_ARM_COPY may only appear in executable objects where e_type is
1223 * set to ET_EXEC.
1224 *
1225 * We explicitly disallow ET_DYN executables from having
1226 * R_ARM_COPY relocations.
1227 */
1228 DL_ERR("%s R_ARM_COPY relocations are not supported", si->name);
1229 return -1;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001230#elif defined(__i386__)
1231 case R_386_JMP_SLOT:
1232 count_relocation(kRelocAbsolute);
1233 MARK(rel->r_offset);
1234 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001235 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001236 break;
1237 case R_386_GLOB_DAT:
1238 count_relocation(kRelocAbsolute);
1239 MARK(rel->r_offset);
1240 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001241 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001242 break;
1243 case R_386_32:
1244 count_relocation(kRelocRelative);
1245 MARK(rel->r_offset);
1246 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001247 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001248 break;
1249 case R_386_PC32:
1250 count_relocation(kRelocRelative);
1251 MARK(rel->r_offset);
1252 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1253 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001254 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001255 break;
1256#elif defined(__mips__)
1257 case R_MIPS_REL32:
Chris Dearman99186652014-02-06 20:36:51 -08001258#if defined(__LP64__)
1259 // MIPS Elf64_Rel entries contain compound relocations
1260 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
1261 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
1262 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001263 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
Chris Dearman99186652014-02-06 20:36:51 -08001264 type, (unsigned)ELF64_R_TYPE2(rel->r_info),
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001265 (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
Chris Dearman99186652014-02-06 20:36:51 -08001266 return -1;
1267 }
1268#endif
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001269 count_relocation(kRelocAbsolute);
1270 MARK(rel->r_offset);
Chris Dearman99186652014-02-06 20:36:51 -08001271 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
1272 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001273 if (s) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001274 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001275 } else {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001276 *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001277 }
1278 break;
1279#endif
1280
1281#if defined(__arm__)
1282 case R_ARM_RELATIVE:
1283#elif defined(__i386__)
1284 case R_386_RELATIVE:
1285#endif
1286 count_relocation(kRelocRelative);
1287 MARK(rel->r_offset);
1288 if (sym) {
1289 DL_ERR("odd RELATIVE form...");
1290 return -1;
1291 }
1292 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1293 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base));
Elliott Hughes0266ae52014-02-10 17:46:57 -08001294 *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001295 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001296
1297 default:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001298 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001299 return -1;
1300 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001301 }
1302 return 0;
1303}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001304#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001305
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001306#if defined(__mips__)
Brian Carlstrom87c35852013-08-20 21:05:44 -07001307static bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
Chris Dearman99186652014-02-06 20:36:51 -08001308 ElfW(Addr)** got = si->plt_got;
Brian Carlstrom87c35852013-08-20 21:05:44 -07001309 if (got == NULL) {
1310 return true;
1311 }
1312 unsigned local_gotno = si->mips_local_gotno;
1313 unsigned gotsym = si->mips_gotsym;
1314 unsigned symtabno = si->mips_symtabno;
Elliott Hughes0266ae52014-02-10 17:46:57 -08001315 ElfW(Sym)* symtab = si->symtab;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001316
Chris Dearman99186652014-02-06 20:36:51 -08001317 // got[0] is the address of the lazy resolver function.
1318 // got[1] may be used for a GNU extension.
1319 // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
1320 // FIXME: maybe this should be in a separate routine?
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001321 if ((si->flags & FLAG_LINKER) == 0) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001322 size_t g = 0;
Chris Dearman99186652014-02-06 20:36:51 -08001323 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
1324 if (reinterpret_cast<intptr_t>(got[g]) < 0) {
1325 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001326 }
Chris Dearman99186652014-02-06 20:36:51 -08001327 // Relocate the local GOT entries.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001328 for (; g < local_gotno; g++) {
Chris Dearman99186652014-02-06 20:36:51 -08001329 got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001330 }
1331 }
1332
Chris Dearman99186652014-02-06 20:36:51 -08001333 // Now for the global GOT entries...
Elliott Hughes0266ae52014-02-10 17:46:57 -08001334 ElfW(Sym)* sym = symtab + gotsym;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001335 got = si->plt_got + local_gotno;
Brian Carlstrom87c35852013-08-20 21:05:44 -07001336 for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
Chris Dearman99186652014-02-06 20:36:51 -08001337 // This is an undefined reference... try to locate it.
1338 const char* sym_name = si->strtab + sym->st_name;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001339 soinfo* lsi;
Chris Dearman99186652014-02-06 20:36:51 -08001340 ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001341 if (s == NULL) {
Chris Dearman99186652014-02-06 20:36:51 -08001342 // We only allow an undefined symbol if this is a weak reference.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001343 s = &symtab[g];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001344 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001345 DL_ERR("cannot locate \"%s\"...", sym_name);
Brian Carlstrom87c35852013-08-20 21:05:44 -07001346 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001347 }
1348 *got = 0;
Chris Dearman99186652014-02-06 20:36:51 -08001349 } else {
1350 // FIXME: is this sufficient?
1351 // For reference see NetBSD link loader
1352 // 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
1353 *got = reinterpret_cast<ElfW(Addr)*>(lsi->load_bias + s->st_value);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001354 }
1355 }
Brian Carlstrom87c35852013-08-20 21:05:44 -07001356 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001357}
1358#endif
1359
Kito Cheng812fd422014-03-25 22:53:56 +08001360void soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001361 if (functions == NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001362 return;
1363 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001364
Elliott Hughesc6200592013-09-30 18:43:46 -07001365 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001366
1367 int begin = reverse ? (count - 1) : 0;
1368 int end = reverse ? -1 : count;
1369 int step = reverse ? -1 : 1;
1370
1371 for (int i = begin; i != end; i += step) {
1372 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1373 CallFunction("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001374 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001375
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001376 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001377}
1378
Kito Cheng812fd422014-03-25 22:53:56 +08001379void soinfo::CallFunction(const char* function_name __unused, linker_function_t function) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001380 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001381 return;
1382 }
1383
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001384 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001385 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001386 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001387
1388 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1389 // are still writable. This happens with our debug malloc (see http://b/7941716).
1390 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001391}
1392
Elliott Hughesd23736e2012-11-01 15:16:56 -07001393void soinfo::CallPreInitConstructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001394 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1395 // but ignored in a shared library.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001396 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1397}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001398
Elliott Hughesd23736e2012-11-01 15:16:56 -07001399void soinfo::CallConstructors() {
1400 if (constructors_called) {
1401 return;
1402 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001403
Elliott Hughesd23736e2012-11-01 15:16:56 -07001404 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1405 // protect against recursive constructor calls. One simple example of constructor recursion
1406 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1407 // 1. The program depends on libc, so libc's constructor is called here.
1408 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1409 // 3. dlopen() calls the constructors on the newly created
1410 // soinfo for libc_malloc_debug_leak.so.
1411 // 4. The debug .so depends on libc, so CallConstructors is
1412 // called again with the libc soinfo. If it doesn't trigger the early-
1413 // out above, the libc constructor will be called again (recursively!).
1414 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001415
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001416 if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
1417 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001418 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001419 name, preinit_array_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001420 }
1421
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001422 if (dynamic != NULL) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001423 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001424 if (d->d_tag == DT_NEEDED) {
1425 const char* library_name = strtab + d->d_un.d_val;
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001426 TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
1427 find_loaded_library(library_name)->CallConstructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001428 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001429 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001430 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001431
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001432 TRACE("\"%s\": calling constructors", name);
1433
1434 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001435 CallFunction("DT_INIT", init_func);
1436 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001437}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001438
Elliott Hughesd23736e2012-11-01 15:16:56 -07001439void soinfo::CallDestructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001440 TRACE("\"%s\": calling destructors", name);
1441
1442 // DT_FINI_ARRAY must be parsed in reverse order.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001443 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001444
1445 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001446 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001447}
1448
1449/* Force any of the closed stdin, stdout and stderr to be associated with
1450 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001451static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001452 int dev_null, i, status;
1453 int return_value = 0;
1454
David 'Digit' Turner16084162012-06-12 16:25:37 +02001455 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001456 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001457 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001458 return -1;
1459 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001460 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001461
1462 /* If any of the stdio file descriptors is valid and not associated
1463 with /dev/null, dup /dev/null to it. */
1464 for (i = 0; i < 3; i++) {
1465 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001466 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001467 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001468 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001469
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001470 TRACE("[ Nullifying stdio file descriptor %d]", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001471 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001472
Elliott Hughes46882792012-08-03 16:49:39 -07001473 /* If file is opened, we are good. */
1474 if (status != -1) {
1475 continue;
1476 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001477
1478 /* The only error we allow is that the file descriptor does not
1479 exist, in which case we dup /dev/null to it. */
1480 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001481 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001482 return_value = -1;
1483 continue;
1484 }
1485
1486 /* Try dupping /dev/null to this stdio file descriptor and
1487 repeat if there is a signal. Note that any errors in closing
1488 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001489 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001490 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001491 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001492 return_value = -1;
1493 continue;
1494 }
1495 }
1496
1497 /* If /dev/null is not one of the stdio file descriptors, close it. */
1498 if (dev_null > 2) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001499 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001500 status = TEMP_FAILURE_RETRY(close(dev_null));
1501 if (status == -1) {
1502 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001503 return_value = -1;
1504 }
1505 }
1506
1507 return return_value;
1508}
1509
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00001510static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001511 /* "base" might wrap around UINT32_MAX. */
Elliott Hughes0266ae52014-02-10 17:46:57 -08001512 ElfW(Addr) base = si->load_bias;
1513 const ElfW(Phdr)* phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001514 int phnum = si->phnum;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001515 bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001516
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001517 /* We can't debug anything until the linker is relocated */
1518 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001519 INFO("[ linking %s ]", si->name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001520 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001521 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001522
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001523 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001524 size_t dynamic_count;
Elliott Hughes0266ae52014-02-10 17:46:57 -08001525 ElfW(Word) dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001526 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001527 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001528 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001529 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001530 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001531 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001532 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001533 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001534 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001535 DEBUG("dynamic = %p", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001536 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001537 }
1538
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001539#if defined(__arm__)
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001540 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1541 &si->ARM_exidx, &si->ARM_exidx_count);
1542#endif
1543
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001544 // Extract useful information from dynamic section.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001545 uint32_t needed_count = 0;
Elliott Hughes0266ae52014-02-10 17:46:57 -08001546 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001547 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1548 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1549 switch (d->d_tag) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001550 case DT_HASH:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001551 si->nbucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[0];
1552 si->nchain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[1];
1553 si->bucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8);
1554 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 -08001555 break;
1556 case DT_STRTAB:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001557 si->strtab = reinterpret_cast<const char*>(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001558 break;
1559 case DT_SYMTAB:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001560 si->symtab = reinterpret_cast<ElfW(Sym)*>(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001561 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001562#if !defined(__LP64__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001563 case DT_PLTREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001564 if (d->d_un.d_val != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001565 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1566 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001567 }
1568 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001569#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001570 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001571#if defined(USE_RELA)
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001572 si->plt_rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001573#else
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001574 si->plt_rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001575#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001576 break;
1577 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001578#if defined(USE_RELA)
Elliott Hughes0266ae52014-02-10 17:46:57 -08001579 si->plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001580#else
Elliott Hughes0266ae52014-02-10 17:46:57 -08001581 si->plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001582#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001583 break;
Chris Dearman99186652014-02-06 20:36:51 -08001584#if defined(__mips__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001585 case DT_PLTGOT:
Chris Dearman99186652014-02-06 20:36:51 -08001586 // Used by mips and mips64.
1587 si->plt_got = reinterpret_cast<ElfW(Addr)**>(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001588 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001589#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001590 case DT_DEBUG:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001591 // Set the DT_DEBUG entry to the address of _r_debug for GDB
Chris Dearmancf239052013-01-11 15:32:20 -08001592 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08001593// FIXME: not working currently for N64
1594// The flags for the LOAD and DYNAMIC program headers do not agree.
1595// The LOAD section containng the dynamic table has been mapped as
1596// read-only, but the DYNAMIC header claims it is writable.
1597#if !(defined(__mips__) && defined(__LP64__))
Elliott Hughes99c32052013-01-14 09:56:21 -08001598 if ((dynamic_flags & PF_W) != 0) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001599 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
Elliott Hughes99c32052013-01-14 09:56:21 -08001600 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001601 break;
Chris Dearman99186652014-02-06 20:36:51 -08001602#endif
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001603#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001604 case DT_RELA:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001605 si->rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001606 break;
1607 case DT_RELASZ:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001608 si->rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001609 break;
1610 case DT_REL:
1611 DL_ERR("unsupported DT_REL in \"%s\"", si->name);
1612 return false;
1613 case DT_RELSZ:
1614 DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name);
1615 return false;
1616#else
1617 case DT_REL:
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001618 si->rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001619 break;
1620 case DT_RELSZ:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001621 si->rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001622 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001623 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001624 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1625 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001626#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001627 case DT_INIT:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001628 si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001629 DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001630 break;
1631 case DT_FINI:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001632 si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001633 DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001634 break;
1635 case DT_INIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001636 si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001637 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001638 break;
1639 case DT_INIT_ARRAYSZ:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001640 si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001641 break;
1642 case DT_FINI_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001643 si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001644 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001645 break;
1646 case DT_FINI_ARRAYSZ:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001647 si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001648 break;
1649 case DT_PREINIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001650 si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001651 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652 break;
1653 case DT_PREINIT_ARRAYSZ:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001654 si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001655 break;
1656 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07001657#if defined(__LP64__)
1658 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1659 return false;
1660#else
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001661 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001662 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07001663#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001664 case DT_SYMBOLIC:
1665 si->has_DT_SYMBOLIC = true;
1666 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001667 case DT_NEEDED:
1668 ++needed_count;
1669 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001670 case DT_FLAGS:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001671 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001672#if defined(__LP64__)
1673 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1674 return false;
1675#else
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001676 si->has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07001677#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001678 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001679 if (d->d_un.d_val & DF_SYMBOLIC) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001680 si->has_DT_SYMBOLIC = true;
1681 }
1682 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001683#if defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001684 case DT_STRSZ:
1685 case DT_SYMENT:
1686 case DT_RELENT:
1687 break;
1688 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001689 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001690 {
Benjamin Adolphi006f9ad2014-02-19 00:50:32 +01001691 r_debug** dp = reinterpret_cast<r_debug**>(base + d->d_un.d_ptr);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001692 *dp = &_r_debug;
1693 }
1694 break;
1695 case DT_MIPS_RLD_VERSION:
1696 case DT_MIPS_FLAGS:
1697 case DT_MIPS_BASE_ADDRESS:
1698 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001699 break;
1700
1701 case DT_MIPS_SYMTABNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001702 si->mips_symtabno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001703 break;
1704
1705 case DT_MIPS_LOCAL_GOTNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001706 si->mips_local_gotno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001707 break;
1708
1709 case DT_MIPS_GOTSYM:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001710 si->mips_gotsym = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001711 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001712#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001713
1714 default:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001715 DEBUG("Unused DT entry: type %p arg %p",
1716 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001717 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001718 }
1719 }
1720
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001721 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
1722 reinterpret_cast<void*>(si->base), si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001723
Elliott Hughes124fae92012-10-31 14:20:03 -07001724 // Sanity checks.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001725 if (relocating_linker && needed_count != 0) {
1726 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1727 return false;
1728 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001729 if (si->nbucket == 0) {
1730 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1731 return false;
1732 }
1733 if (si->strtab == 0) {
1734 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1735 return false;
1736 }
1737 if (si->symtab == 0) {
1738 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1739 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001740 }
1741
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001742 // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001743 if (si->flags & FLAG_EXE) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001744 memset(gLdPreloads, 0, sizeof(gLdPreloads));
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001745 size_t preload_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001746 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +00001747 soinfo* lsi = find_library(gLdPreloadNames[i], NULL);
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001748 if (lsi != NULL) {
1749 gLdPreloads[preload_count++] = lsi;
1750 } else {
1751 // As with glibc, failure to load an LD_PRELOAD library is just a warning.
1752 DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
1753 gLdPreloadNames[i], si->name, linker_get_error_buffer());
Matt Fischer4fd42c12009-12-31 12:09:10 -06001754 }
Matt Fischer4fd42c12009-12-31 12:09:10 -06001755 }
1756 }
1757
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001758 soinfo** needed = reinterpret_cast<soinfo**>(alloca((1 + needed_count) * sizeof(soinfo*)));
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001759 soinfo** pneeded = needed;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001760
Elliott Hughes0266ae52014-02-10 17:46:57 -08001761 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001762 if (d->d_tag == DT_NEEDED) {
1763 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001764 DEBUG("%s needs %s", si->name, library_name);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +00001765 soinfo* lsi = find_library(library_name, NULL);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001766 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001767 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001768 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001769 library_name, si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001770 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001771 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001772 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001773 }
1774 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001775 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001776
Elliott Hughese4d792a2013-10-28 14:19:05 -07001777#if !defined(__LP64__)
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001778 if (si->has_text_relocations) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001779 // Make segments writable to allow text relocations to work properly. We will later call
1780 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
Du Chenyang865119e2014-04-18 19:17:40 +08001781#if !defined(__i386__) // The platform itself has too many text relocations on x86.
Nick Kralevich3d4470c2013-10-22 12:06:36 -07001782 DL_WARN("%s has text relocations. This is wasting memory and prevents "
1783 "security hardening. Please fix.", si->name);
Du Chenyang865119e2014-04-18 19:17:40 +08001784#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001785 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1786 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1787 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001788 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001789 }
1790 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07001791#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001792
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001793#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001794 if (si->plt_rela != NULL) {
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001795 DEBUG("[ relocating %s plt ]\n", si->name);
Chris Dearman99186652014-02-06 20:36:51 -08001796 if (soinfo_relocate(si, si->plt_rela, si->plt_rela_count, needed)) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001797 return false;
1798 }
1799 }
1800 if (si->rela != NULL) {
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001801 DEBUG("[ relocating %s ]\n", si->name);
Chris Dearman99186652014-02-06 20:36:51 -08001802 if (soinfo_relocate(si, si->rela, si->rela_count, needed)) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001803 return false;
1804 }
1805 }
1806#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001807 if (si->plt_rel != NULL) {
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001808 DEBUG("[ relocating %s plt ]", si->name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001809 if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001810 return false;
1811 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001812 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001813 if (si->rel != NULL) {
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001814 DEBUG("[ relocating %s ]", si->name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001815 if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001816 return false;
1817 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001818 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001819#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001820
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001821#if defined(__mips__)
Brian Carlstrom87c35852013-08-20 21:05:44 -07001822 if (!mips_relocate_got(si, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001823 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001824 }
1825#endif
1826
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001827 si->flags |= FLAG_LINKED;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001828 DEBUG("[ finished linking %s ]", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001829
Elliott Hughese4d792a2013-10-28 14:19:05 -07001830#if !defined(__LP64__)
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001831 if (si->has_text_relocations) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001832 // All relocations are done, we can protect our segments back to read-only.
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001833 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1834 DL_ERR("can't protect segments for \"%s\": %s",
1835 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001836 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001837 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001838 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07001839#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001840
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001841 /* We can also turn on GNU RELRO protection */
1842 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001843 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1844 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001845 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001846 }
1847
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00001848 /* Handle serializing/sharing the RELRO segment */
1849 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
1850 if (phdr_table_serialize_gnu_relro(si->phdr, si->phnum, si->load_bias,
1851 extinfo->relro_fd) < 0) {
1852 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
1853 si->name, strerror(errno));
1854 return false;
1855 }
1856 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
1857 if (phdr_table_map_gnu_relro(si->phdr, si->phnum, si->load_bias,
1858 extinfo->relro_fd) < 0) {
1859 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
1860 si->name, strerror(errno));
1861 return false;
1862 }
1863 }
1864
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001865 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001866 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001867}
1868
Nick Kralevich468319c2011-11-11 15:53:17 -08001869/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001870 * This function add vdso to internal dso list.
1871 * It helps to stack unwinding through signal handlers.
1872 * Also, it makes bionic more like glibc.
1873 */
Kito Cheng812fd422014-03-25 22:53:56 +08001874static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001875#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08001876 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
1877 if (ehdr_vdso == NULL) {
1878 return;
1879 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001880
Elliott Hughes0266ae52014-02-10 17:46:57 -08001881 soinfo* si = soinfo_alloc("[vdso]");
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001882
Elliott Hughes0266ae52014-02-10 17:46:57 -08001883 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
1884 si->phnum = ehdr_vdso->e_phnum;
1885 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
1886 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1887 si->flags = 0;
1888 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001889
Torne (Richard Coles)0dcf06f2014-04-22 11:59:26 +01001890 soinfo_link_image(si, NULL);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001891#endif
1892}
1893
1894/*
Nick Kralevich468319c2011-11-11 15:53:17 -08001895 * This code is called after the linker has linked itself and
1896 * fixed it's own GOT. It is safe to make references to externs
1897 * and other non-local data at this point.
1898 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08001899static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001900 /* NOTE: we store the args pointer on a special location
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001901 * of the temporary TLS area in order to pass it to
1902 * the C Library's runtime initializer.
1903 *
1904 * The initializer must clear the slot and reset the TLS
1905 * to point to a different location to ensure that no other
1906 * shared library constructor can access it.
1907 */
Elliott Hughesd3920b32013-02-07 18:39:34 -08001908 __libc_init_tls(args);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001909
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001910#if TIMING
1911 struct timeval t0, t1;
1912 gettimeofday(&t0, 0);
1913#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001914
Elliott Hughes18a206c2012-10-29 17:37:13 -07001915 // Initialize environment functions, and get to the ELF aux vectors table.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001916 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001917
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07001918 // If this is a setuid/setgid program, close the security hole described in
1919 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1920 if (get_AT_SECURE()) {
1921 nullify_closed_stdio();
1922 }
1923
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001924 debuggerd_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001925
Elliott Hughes18a206c2012-10-29 17:37:13 -07001926 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001927 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1928 if (LD_DEBUG != NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001929 gLdDebugVerbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001930 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001931
Elliott Hughes18a206c2012-10-29 17:37:13 -07001932 // Normally, these are cleaned by linker_env_init, but the test
1933 // doesn't cost us anything.
1934 const char* ldpath_env = NULL;
1935 const char* ldpreload_env = NULL;
1936 if (!get_AT_SECURE()) {
1937 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1938 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001939 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001940
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001941 INFO("[ android linker & debugger ]");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001942
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001943 soinfo* si = soinfo_alloc(args.argv[0]);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001944 if (si == NULL) {
1945 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001946 }
1947
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001948 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001949 si->flags |= FLAG_EXE;
Elliott Hughes3a9c5d62014-02-10 13:31:13 -08001950 link_map* map = &(si->link_map_head);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001951
1952 map->l_addr = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001953 map->l_name = args.argv[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001954 map->l_prev = NULL;
1955 map->l_next = NULL;
1956
1957 _r_debug.r_map = map;
1958 r_debug_tail = map;
1959
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001960 /* gdb expects the linker to be in the debug shared object list.
1961 * Without this, gdb has trouble locating the linker's ".text"
1962 * and ".plt" sections. Gdb could also potentially use this to
1963 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1964 * Don't use soinfo_alloc(), because the linker shouldn't
1965 * be on the soinfo list.
Ben Cheng06f0e742012-08-10 16:07:02 -07001966 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001967 {
1968 static soinfo linker_soinfo;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001969#if defined(__LP64__)
Pavel Chupin1a57f9f2013-02-06 19:21:46 +04001970 strlcpy(linker_soinfo.name, "/system/bin/linker64", sizeof(linker_soinfo.name));
1971#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001972 strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
Pavel Chupin1a57f9f2013-02-06 19:21:46 +04001973#endif
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001974 linker_soinfo.flags = 0;
1975 linker_soinfo.base = linker_base;
1976
1977 /*
1978 * Set the dynamic field in the link map otherwise gdb will complain with
1979 * the following:
1980 * warning: .dynamic section for "/system/bin/linker" is not at the
1981 * expected address (wrong library or version mismatch?)
1982 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08001983 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08001984 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001985 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1986 &linker_soinfo.dynamic, NULL, NULL);
1987 insert_soinfo_into_debug_map(&linker_soinfo);
1988 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001989
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001990 // Extract information passed from the kernel.
Elliott Hughes0266ae52014-02-10 17:46:57 -08001991 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001992 si->phnum = args.getauxval(AT_PHNUM);
1993 si->entry = args.getauxval(AT_ENTRY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001994
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001995 /* Compute the value of si->base. We can't rely on the fact that
1996 * the first entry is the PHDR because this will not be true
1997 * for certain executables (e.g. some in the NDK unit test suite)
1998 */
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001999 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002000 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002001 si->load_bias = 0;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002002 for (size_t i = 0; i < si->phnum; ++i) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002003 if (si->phdr[i].p_type == PT_PHDR) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002004 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2005 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002006 break;
2007 }
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002008 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002009 si->dynamic = NULL;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002010 si->ref_count = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002011
Nick Kralevich2aebf542014-05-07 10:32:39 -07002012 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2013 if (elf_hdr->e_type != ET_DYN) {
2014 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2015 exit(EXIT_FAILURE);
2016 }
2017
Elliott Hughes46882792012-08-03 16:49:39 -07002018 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2019 parse_LD_LIBRARY_PATH(ldpath_env);
2020 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06002021
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02002022 somain = si;
2023
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002024 if (!soinfo_link_image(si, NULL)) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08002025 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
Elliott Hughes18a206c2012-10-29 17:37:13 -07002026 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002027 }
2028
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002029 add_vdso(args);
2030
Elliott Hughesd23736e2012-11-01 15:16:56 -07002031 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04002032
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002033 for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
2034 gLdPreloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08002035 }
2036
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002037 /* After the link_image, the si->load_bias is initialized.
2038 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2039 * We need to update this value for so exe here. So Unwind_Backtrace
2040 * for some arch like x86 could work correctly within so exe.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08002041 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08002042 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002043 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002044
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002045#if TIMING
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002046 gettimeofday(&t1, NULL);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002047 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002048 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002049 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002050#endif
2051#if STATS
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002052 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07002053 linker_stats.count[kRelocAbsolute],
2054 linker_stats.count[kRelocRelative],
2055 linker_stats.count[kRelocCopy],
2056 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002057#endif
2058#if COUNT_PAGES
2059 {
2060 unsigned n;
2061 unsigned i;
2062 unsigned count = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002063 for (n = 0; n < 4096; n++) {
2064 if (bitmask[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002065 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002066#if defined(__LP64__)
2067 for (i = 0; i < 32; i++) {
2068#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002069 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002070#endif
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002071 if (x & 1) {
2072 count++;
2073 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002074 x >>= 1;
2075 }
2076 }
2077 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002078 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002079 }
2080#endif
2081
2082#if TIMING || STATS || COUNT_PAGES
2083 fflush(stdout);
2084#endif
2085
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002086 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002087 return si->entry;
2088}
Nick Kralevich468319c2011-11-11 15:53:17 -08002089
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002090/* Compute the load-bias of an existing executable. This shall only
2091 * be used to compute the load bias of an executable or shared library
2092 * that was loaded by the kernel itself.
2093 *
2094 * Input:
2095 * elf -> address of ELF header, assumed to be at the start of the file.
2096 * Return:
2097 * load bias, i.e. add the value of any p_vaddr in the file to get
2098 * the corresponding address in memory.
2099 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002100static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2101 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002102 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002103 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002104
Elliott Hughes0266ae52014-02-10 17:46:57 -08002105 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002106 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002107 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002108 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002109 }
2110 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002111}
2112
Nick Kralevich468319c2011-11-11 15:53:17 -08002113/*
2114 * This is the entry point for the linker, called from begin.S. This
2115 * method is responsible for fixing the linker's own relocations, and
2116 * then calling __linker_init_post_relocation().
2117 *
2118 * Because this method is called before the linker has fixed it's own
2119 * relocations, any attempt to reference an extern variable, extern
2120 * function, or other GOT reference will generate a segfault.
2121 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002122extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002123 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002124
Elliott Hughes0266ae52014-02-10 17:46:57 -08002125 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
2126 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002127 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002128
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002129 soinfo linker_so;
2130 memset(&linker_so, 0, sizeof(soinfo));
Nick Kralevich468319c2011-11-11 15:53:17 -08002131
Elliott Hughesb93702a2013-12-21 16:07:45 -08002132 strcpy(linker_so.name, "[dynamic linker]");
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002133 linker_so.base = linker_addr;
2134 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2135 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002136 linker_so.dynamic = NULL;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002137 linker_so.phdr = phdr;
2138 linker_so.phnum = elf_hdr->e_phnum;
2139 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07002140
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002141 if (!soinfo_link_image(&linker_so, NULL)) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002142 // It would be nice to print an error message, but if the linker
2143 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002144 // call write() (because it involves a GOT reference). We may as
2145 // well try though...
2146 const char* msg = "CANNOT LINK EXECUTABLE: ";
2147 write(2, msg, strlen(msg));
2148 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2149 write(2, "\n", 1);
2150 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002151 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002152
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002153 // We have successfully fixed our own relocations. It's safe to run
2154 // the main part of the linker now.
Elliott Hughes0d787c12013-04-04 13:46:46 -07002155 args.abort_message_ptr = &gAbortMessage;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002156 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002157
2158 set_soinfo_pool_protection(PROT_READ);
2159
2160 // Return the address that the calling assembly stub should jump to.
2161 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002162}