blob: 6bf18ac9b9b1dec2cfe2582d52f910c2cd03b998 [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>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <linux/auxvec.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
David Bartleybc3a5c22009-06-02 18:27:28 -070052/* Assume average path length of 64 and max 8 paths */
53#define LDPATH_BUFSIZE 512
54#define LDPATH_MAX 8
55
Matt Fischer4fd42c12009-12-31 12:09:10 -060056#define LDPRELOAD_BUFSIZE 512
57#define LDPRELOAD_MAX 8
58
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
60 *
61 * Do NOT use malloc() and friends or pthread_*() code here.
62 * Don't use printf() either; it's caused mysterious memory
63 * corruption in the past.
64 * The linker runs before we bring up libc and it's easiest
65 * to make sure it does not depend on any complex libc features
66 *
67 * open issues / todo:
68 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069 * - are we doing everything we should for ARM_COPY relocations?
70 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071 * - after linking, set as much stuff as possible to READONLY
72 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070073 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074
Elliott Hughes124fae92012-10-31 14:20:03 -070075static bool soinfo_link_image(soinfo* si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076
Magnus Malmbornba98d922012-09-12 13:00:55 +020077// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
78// maps, each a single page in size. The pages are broken up into as many struct soinfo
79// objects as will fit, and they're all threaded together on a free list.
80#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
81struct soinfo_pool_t {
82 soinfo_pool_t* next;
83 soinfo info[SOINFO_PER_POOL];
84};
85static struct soinfo_pool_t* gSoInfoPools = NULL;
86static soinfo* gSoInfoFreeList = NULL;
87
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080088static soinfo* solist = &libdl_info;
89static soinfo* sonext = &libdl_info;
90static soinfo* somain; /* main process, always the one after libdl_info */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091
Elliott Hughes124fae92012-10-31 14:20:03 -070092static const char* const gSoPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070093#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070094 "/vendor/lib64",
95 "/system/lib64",
96#else
Elliott Hughes124fae92012-10-31 14:20:03 -070097 "/vendor/lib",
98 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -070099#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700100 NULL
101};
David Bartleybc3a5c22009-06-02 18:27:28 -0700102
Elliott Hughes124fae92012-10-31 14:20:03 -0700103static char gLdPathsBuffer[LDPATH_BUFSIZE];
104static const char* gLdPaths[LDPATH_MAX + 1];
105
106static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
107static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600108
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800109static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600110
Elliott Hughes650be4e2013-03-05 18:47:58 -0800111__LIBC_HIDDEN__ int gLdDebugVerbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112
Elliott Hughes0d787c12013-04-04 13:46:46 -0700113__LIBC_HIDDEN__ abort_msg_t* gAbortMessage = NULL; // For debuggerd.
114
Elliott Hughesbedfe382012-08-14 14:07:59 -0700115enum RelocationKind {
116 kRelocAbsolute = 0,
117 kRelocRelative,
118 kRelocCopy,
119 kRelocSymbol,
120 kRelocMax
121};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100122
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700124struct linker_stats_t {
125 int count[kRelocMax];
126};
127
128static linker_stats_t linker_stats;
129
130static void count_relocation(RelocationKind kind) {
131 ++linker_stats.count[kind];
132}
133#else
134static void count_relocation(RelocationKind) {
135}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136#endif
137
138#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700139static unsigned bitmask[4096];
140#define MARK(offset) \
141 do { \
142 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
143 } while(0)
144#else
145#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146#endif
147
Elliott Hughes46882792012-08-03 16:49:39 -0700148// You shouldn't try to call memory-allocating functions in the dynamic linker.
149// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700150#define DISALLOW_ALLOCATION(return_type, name, ...) \
151 return_type name __VA_ARGS__ \
152 { \
Elliott Hughes46882792012-08-03 16:49:39 -0700153 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700154 __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
155 write(2, msg, strlen(msg)); \
156 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700157 }
Elliott Hughes46882792012-08-03 16:49:39 -0700158#define UNUSED __attribute__((unused))
159DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
160DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
161DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
162DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
Dima Zavin2e855792009-05-20 18:28:09 -0700163
Dima Zavin03531952009-05-29 17:30:25 -0700164static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700165static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700166
Elliott Hughes650be4e2013-03-05 18:47:58 -0800167char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700168 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700169}
170
Elliott Hughes650be4e2013-03-05 18:47:58 -0800171size_t linker_get_error_buffer_size() {
172 return sizeof(__linker_dl_err_buf);
173}
174
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175/*
176 * This function is an empty stub where GDB locates a breakpoint to get notified
177 * about linker activity.
178 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700179extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180
Elliott Hughes0d787c12013-04-04 13:46:46 -0700181static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity, RT_CONSISTENT, 0};
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700182static link_map_t* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183
Elliott Hughes3b297c42012-10-11 16:08:51 -0700184static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185
Elliott Hughesbedfe382012-08-14 14:07:59 -0700186static void insert_soinfo_into_debug_map(soinfo * info) {
187 // Copy the necessary fields into the debug structure.
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700188 link_map_t* map = &(info->link_map);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189 map->l_addr = info->base;
190 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800191 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192
193 /* Stick the new library at the end of the list.
194 * gdb tends to care more about libc than it does
195 * about leaf libraries, and ordering it this way
196 * reduces the back-and-forth over the wire.
197 */
198 if (r_debug_tail) {
199 r_debug_tail->l_next = map;
200 map->l_prev = r_debug_tail;
201 map->l_next = 0;
202 } else {
203 _r_debug.r_map = map;
204 map->l_prev = 0;
205 map->l_next = 0;
206 }
207 r_debug_tail = map;
208}
209
Elliott Hughesbedfe382012-08-14 14:07:59 -0700210static void remove_soinfo_from_debug_map(soinfo* info) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700211 link_map_t* map = &(info->link_map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700212
Elliott Hughesbedfe382012-08-14 14:07:59 -0700213 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700214 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700215 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700216
Elliott Hughesbedfe382012-08-14 14:07:59 -0700217 if (map->l_prev) {
218 map->l_prev->l_next = map->l_next;
219 }
220 if (map->l_next) {
221 map->l_next->l_prev = map->l_prev;
222 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700223}
224
Elliott Hughesbedfe382012-08-14 14:07:59 -0700225static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226 if (info->flags & FLAG_EXE) {
227 // GDB already knows about the main executable
228 return;
229 }
230
Elliott Hughes3b297c42012-10-11 16:08:51 -0700231 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800232
233 _r_debug.r_state = RT_ADD;
234 rtld_db_dlactivity();
235
236 insert_soinfo_into_debug_map(info);
237
238 _r_debug.r_state = RT_CONSISTENT;
239 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700240}
241
Elliott Hughesbedfe382012-08-14 14:07:59 -0700242static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700243 if (info->flags & FLAG_EXE) {
244 // GDB already knows about the main executable
245 return;
246 }
247
Elliott Hughes3b297c42012-10-11 16:08:51 -0700248 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700249
250 _r_debug.r_state = RT_DELETE;
251 rtld_db_dlactivity();
252
253 remove_soinfo_from_debug_map(info);
254
255 _r_debug.r_state = RT_CONSISTENT;
256 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800257}
258
Elliott Hughes18a206c2012-10-29 17:37:13 -0700259void notify_gdb_of_libraries() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260 _r_debug.r_state = RT_ADD;
261 rtld_db_dlactivity();
262 _r_debug.r_state = RT_CONSISTENT;
263 rtld_db_dlactivity();
264}
265
Magnus Malmbornba98d922012-09-12 13:00:55 +0200266static bool ensure_free_list_non_empty() {
267 if (gSoInfoFreeList != NULL) {
268 return true;
269 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800270
Magnus Malmbornba98d922012-09-12 13:00:55 +0200271 // Allocate a new pool.
272 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
273 PROT_READ|PROT_WRITE,
274 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
275 if (pool == MAP_FAILED) {
276 return false;
277 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800278
Magnus Malmbornba98d922012-09-12 13:00:55 +0200279 // Add the pool to our list of pools.
280 pool->next = gSoInfoPools;
281 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282
Magnus Malmbornba98d922012-09-12 13:00:55 +0200283 // Chain the entries in the new pool onto the free list.
284 gSoInfoFreeList = &pool->info[0];
285 soinfo* next = NULL;
286 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
287 pool->info[i].next = next;
288 next = &pool->info[i];
289 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800290
Magnus Malmbornba98d922012-09-12 13:00:55 +0200291 return true;
292}
293
Elliott Hughesd23736e2012-11-01 15:16:56 -0700294static void set_soinfo_pool_protection(int protection) {
295 for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
296 if (mprotect(p, sizeof(*p), protection) == -1) {
297 abort(); // Can't happen.
298 }
299 }
300}
301
Magnus Malmbornba98d922012-09-12 13:00:55 +0200302static soinfo* soinfo_alloc(const char* name) {
303 if (strlen(name) >= SOINFO_NAME_LEN) {
304 DL_ERR("library name \"%s\" too long", name);
305 return NULL;
306 }
307
308 if (!ensure_free_list_non_empty()) {
309 DL_ERR("out of memory when loading \"%s\"", name);
310 return NULL;
311 }
312
313 // Take the head element off the free list.
314 soinfo* si = gSoInfoFreeList;
315 gSoInfoFreeList = gSoInfoFreeList->next;
316
317 // Initialize the new element.
318 memset(si, 0, sizeof(soinfo));
319 strlcpy(si->name, name, sizeof(si->name));
320 sonext->next = si;
321 sonext = si;
322
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700323 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200324 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800325}
326
Elliott Hughes46882792012-08-03 16:49:39 -0700327static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800328{
Elliott Hughes46882792012-08-03 16:49:39 -0700329 if (si == NULL) {
330 return;
331 }
332
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333 soinfo *prev = NULL, *trav;
334
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700335 TRACE("name %s: freeing soinfo @ %p", si->name, si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800337 for (trav = solist; trav != NULL; trav = trav->next) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338 if (trav == si)
339 break;
340 prev = trav;
341 }
342 if (trav == NULL) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800343 /* si was not in solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700344 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800345 return;
346 }
347
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100348 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800349 always the static libdl_info.
350 */
351 prev->next = si->next;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800352 if (si == sonext) {
353 sonext = prev;
354 }
Magnus Malmbornba98d922012-09-12 13:00:55 +0200355 si->next = gSoInfoFreeList;
356 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800357}
358
Elliott Hughescade4c32012-12-20 14:42:14 -0800359
360static void parse_path(const char* path, const char* delimiters,
361 const char** array, char* buf, size_t buf_size, size_t max_count) {
362 if (path == NULL) {
363 return;
364 }
365
366 size_t len = strlcpy(buf, path, buf_size);
367
368 size_t i = 0;
369 char* buf_p = buf;
370 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
371 if (*array[i] != '\0') {
372 ++i;
373 }
374 }
375
376 // Forget the last path if we had to truncate; this occurs if the 2nd to
377 // last char isn't '\0' (i.e. wasn't originally a delimiter).
378 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
379 array[i - 1] = NULL;
380 } else {
381 array[i] = NULL;
382 }
383}
384
385static void parse_LD_LIBRARY_PATH(const char* path) {
386 parse_path(path, ":", gLdPaths,
387 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
388}
389
390static void parse_LD_PRELOAD(const char* path) {
391 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
392 parse_path(path, " :", gLdPreloadNames,
393 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
394}
395
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700396#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700397
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398/* For a given PC, find the .so that it belongs to.
399 * Returns the base address of the .ARM.exidx section
400 * for that .so, and the number of 8-byte entries
401 * in that section (via *pcount).
402 *
403 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
404 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700405 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800407_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
408{
409 soinfo *si;
410 unsigned addr = (unsigned)pc;
411
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700412 for (si = solist; si != 0; si = si->next) {
Nick Kralevich468319c2011-11-11 15:53:17 -0800413 if ((addr >= si->base) && (addr < (si->base + si->size))) {
414 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900415 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416 }
417 }
418 *pcount = 0;
419 return NULL;
420}
Elliott Hughes46882792012-08-03 16:49:39 -0700421
Christopher Ferris24053a42013-08-19 17:45:09 -0700422#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700423
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800424/* Here, we only have to provide a callback to iterate across all the
425 * loaded libraries. gcc_eh does the rest. */
426int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700427dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428 void *data)
429{
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 Hughesca0c11b2013-03-12 10:40:45 -0700433 dl_info.dlpi_addr = si->link_map.l_addr;
434 dl_info.dlpi_name = si->link_map.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 Hughesc6200592013-09-30 18:43:46 -0700445static Elf_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
446 Elf_Sym* symtab = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800447 const char* strtab = si->strtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800448
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700449 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
Christopher Ferris6bec5b72013-06-03 17:27:49 -0700452 for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700453 Elf_Sym* s = symtab + n;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800454 if (strcmp(strtab + s->st_name, name)) continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800455
Doug Kwane8238072009-10-26 12:05:23 -0700456 /* only concern ourselves with global and weak symbol definitions */
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700457 switch (ELF_ST_BIND(s->st_info)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800458 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700459 case STB_WEAK:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800460 if (s->st_shndx == SHN_UNDEF) {
Robin Burchell439fa8e2012-07-05 09:21:07 +0200461 continue;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800462 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800463
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700464 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));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800467 return s;
468 }
469 }
470
Doug Kwan94304352009-10-23 18:11:40 -0700471 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) {
475 const unsigned char* name = (const unsigned char*) _name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800476 unsigned h = 0, g;
477
478 while(*name) {
479 h = (h << 4) + *name++;
480 g = h & 0xf0000000;
481 h ^= g;
482 h ^= g >> 24;
483 }
484 return h;
485}
486
Elliott Hughesc6200592013-09-30 18:43:46 -0700487static Elf_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 Hughesc6200592013-09-30 18:43:46 -0700489 Elf_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
493 /*
Pavel Chupinc77c4342012-10-31 13:55:51 +0400494 * Local scope is executable scope. Just start looking into it right away
495 * for the shortcut.
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200496 */
497
Pavel Chupinc77c4342012-10-31 13:55:51 +0400498 if (si == somain) {
499 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200500 if (s != NULL) {
Pavel Chupinc77c4342012-10-31 13:55:51 +0400501 *lsi = si;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200502 goto done;
503 }
Pavel Chupinc77c4342012-10-31 13:55:51 +0400504 } else {
505 /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200506
Pavel Chupinc77c4342012-10-31 13:55:51 +0400507 /*
508 * If this object was built with symbolic relocations disabled, the
509 * first place to look to resolve external references is the main
510 * executable.
511 */
Nick Kralevich468319c2011-11-11 15:53:17 -0800512
Pavel Chupinc77c4342012-10-31 13:55:51 +0400513 if (!si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700514 DEBUG("%s: looking up %s in executable %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700515 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400516 s = soinfo_elf_lookup(somain, elf_hash, name);
517 if (s != NULL) {
518 *lsi = somain;
519 goto done;
520 }
521 }
522
523 /* Look for symbols in the local scope (the object who is
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700524 * searching). This happens with C++ templates on x86 for some
Pavel Chupinc77c4342012-10-31 13:55:51 +0400525 * reason.
526 *
527 * Notes on weak symbols:
528 * The ELF specs are ambiguous about treatment of weak definitions in
529 * dynamic linking. Some systems return the first definition found
530 * and some the first non-weak definition. This is system dependent.
531 * Here we return the first definition found for simplicity. */
532
533 s = soinfo_elf_lookup(si, elf_hash, name);
534 if (s != NULL) {
535 *lsi = si;
536 goto done;
537 }
538
539 /*
540 * If this object was built with -Bsymbolic and symbol is not found
541 * in the local scope, try to find the symbol in the main executable.
542 */
543
544 if (si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700545 DEBUG("%s: looking up %s in executable %s after local scope",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700546 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400547 s = soinfo_elf_lookup(somain, elf_hash, name);
548 if (s != NULL) {
549 *lsi = somain;
550 goto done;
551 }
552 }
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200553 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700554 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700555
Matt Fischer4fd42c12009-12-31 12:09:10 -0600556 /* Next, look for it in the preloads list */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800557 for (int i = 0; gLdPreloads[i] != NULL; i++) {
558 s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
559 if (s != NULL) {
560 *lsi = gLdPreloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600561 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200562 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600563 }
564
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800565 for (int i = 0; needed[i] != NULL; i++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700566 DEBUG("%s: looking up %s in %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700567 si->name, name, needed[i]->name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200568 s = soinfo_elf_lookup(needed[i], elf_hash, name);
569 if (s != NULL) {
570 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200571 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200572 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700573 }
574
575done:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800576 if (s != NULL) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700577 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
578 "found in %s, base = %p, load bias = %p",
579 si->name, name, reinterpret_cast<void*>(s->st_value),
580 (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
581 reinterpret_cast<void*>((*lsi)->load_bias));
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700582 return s;
583 }
584
Doug Kwan94304352009-10-23 18:11:40 -0700585 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700586}
587
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800588/* This is used by dlsym(3). It performs symbol lookup only within the
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700589 specified soinfo object and not in any of its dependencies.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800590
591 TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
592 that it should do a breadth first search through the dependency
593 tree. This agrees with the ELF spec (aka System V Application
594 Binary Interface) where in Chapter 5 it discuss resolving "Shared
595 Object Dependencies" in breadth first search order.
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700596 */
Elliott Hughesc6200592013-09-30 18:43:46 -0700597Elf_Sym* dlsym_handle_lookup(soinfo* si, const char* name) {
David 'Digit' Turner16084162012-06-12 16:25:37 +0200598 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800599}
600
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800601/* This is used by dlsym(3) to performs a global symbol lookup. If the
602 start value is null (for RTLD_DEFAULT), the search starts at the
603 beginning of the global solist. Otherwise the search starts at the
604 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700605 */
Elliott Hughesc6200592013-09-30 18:43:46 -0700606Elf_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800607 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800608
Elliott Hughescade4c32012-12-20 14:42:14 -0800609 if (start == NULL) {
610 start = solist;
611 }
612
Elliott Hughesc6200592013-09-30 18:43:46 -0700613 Elf_Sym* s = NULL;
Elliott Hughescade4c32012-12-20 14:42:14 -0800614 for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
615 s = soinfo_elf_lookup(si, elf_hash, name);
616 if (s != NULL) {
617 *found = si;
618 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600619 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800620 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600621
Elliott Hughescade4c32012-12-20 14:42:14 -0800622 if (s != NULL) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700623 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
624 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800625 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800626
Elliott Hughescade4c32012-12-20 14:42:14 -0800627 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800628}
629
Kito Chengfa8c05d2013-03-12 14:58:06 +0800630soinfo* find_containing_library(const void* p) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700631 Elf_Addr address = reinterpret_cast<Elf_Addr>(p);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800632 for (soinfo* si = solist; si != NULL; si = si->next) {
633 if (address >= si->base && address - si->base < si->size) {
634 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600635 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800636 }
637 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600638}
639
Elliott Hughesc6200592013-09-30 18:43:46 -0700640Elf_Sym* dladdr_find_symbol(soinfo* si, const void* addr) {
641 Elf_Addr soaddr = reinterpret_cast<Elf_Addr>(addr) - si->base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600642
Kito Chengfa8c05d2013-03-12 14:58:06 +0800643 // Search the library's symbol table for any defined symbol which
644 // contains this address.
645 for (size_t i = 0; i < si->nchain; ++i) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700646 Elf_Sym* sym = &si->symtab[i];
Kito Chengfa8c05d2013-03-12 14:58:06 +0800647 if (sym->st_shndx != SHN_UNDEF &&
648 soaddr >= sym->st_value &&
649 soaddr < sym->st_value + sym->st_size) {
650 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600651 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800652 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600653
Kito Chengfa8c05d2013-03-12 14:58:06 +0800654 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600655}
656
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800658static void dump(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800659{
Elliott Hughesc6200592013-09-30 18:43:46 -0700660 Elf_Sym* s = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800661 for (unsigned n = 0; n < si->nchain; n++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700662 TRACE("%04d> %08x: %02x %04x %08x %08x %s", n, s,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800663 s->st_info, s->st_shndx, s->st_value, s->st_size,
664 si->strtab + s->st_name);
665 s++;
666 }
667}
668#endif
669
Elliott Hughes124fae92012-10-31 14:20:03 -0700670static int open_library_on_path(const char* name, const char* const paths[]) {
671 char buf[512];
672 for (size_t i = 0; paths[i] != NULL; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800673 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700674 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700675 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700676 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800677 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700678 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
679 if (fd != -1) {
680 return fd;
681 }
682 }
683 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800684}
685
Elliott Hughes124fae92012-10-31 14:20:03 -0700686static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700687 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800688
Elliott Hughes124fae92012-10-31 14:20:03 -0700689 // If the name contains a slash, we should attempt to open it directly and not search the paths.
690 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700691 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
692 if (fd != -1) {
693 return fd;
694 }
695 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700696 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800697
Elliott Hughes124fae92012-10-31 14:20:03 -0700698 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
699 int fd = open_library_on_path(name, gLdPaths);
700 if (fd == -1) {
701 fd = open_library_on_path(name, gSoPaths);
702 }
703 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800704}
705
Elliott Hughes124fae92012-10-31 14:20:03 -0700706static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700707 // Open the file.
Elliott Hughes650be4e2013-03-05 18:47:58 -0800708 int fd = open_library(name);
709 if (fd == -1) {
Elliott Hughes46882792012-08-03 16:49:39 -0700710 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800711 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700712 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800713
Elliott Hughes650be4e2013-03-05 18:47:58 -0800714 // Read the ELF header and load the segments.
715 ElfReader elf_reader(name, fd);
716 if (!elf_reader.Load()) {
Elliott Hughes46882792012-08-03 16:49:39 -0700717 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800718 }
719
Elliott Hughes650be4e2013-03-05 18:47:58 -0800720 const char* bname = strrchr(name, '/');
721 soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
722 if (si == NULL) {
Elliott Hughes46882792012-08-03 16:49:39 -0700723 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200724 }
Elliott Hughes650be4e2013-03-05 18:47:58 -0800725 si->base = elf_reader.load_start();
726 si->size = elf_reader.load_size();
727 si->load_bias = elf_reader.load_bias();
728 si->flags = 0;
729 si->entry = 0;
730 si->dynamic = NULL;
731 si->phnum = elf_reader.phdr_count();
732 si->phdr = elf_reader.loaded_phdr();
733 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800734}
735
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200736static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800737{
738 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700739 const char *bname;
740
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200741 // TODO: don't use basename only for determining libraries
742 // http://code.google.com/p/android/issues/detail?id=6670
743
744 bname = strrchr(name, '/');
745 bname = bname ? bname + 1 : name;
746
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800747 for (si = solist; si != NULL; si = si->next) {
748 if (!strcmp(bname, si->name)) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200749 return si;
750 }
751 }
752 return NULL;
753}
754
Elliott Hughesd23736e2012-11-01 15:16:56 -0700755static soinfo* find_library_internal(const char* name) {
756 if (name == NULL) {
757 return somain;
758 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200759
Elliott Hughesd23736e2012-11-01 15:16:56 -0700760 soinfo* si = find_loaded_library(name);
761 if (si != NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700762 if (si->flags & FLAG_LINKED) {
763 return si;
764 }
765 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
766 return NULL;
767 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800768
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700769 TRACE("[ '%s' has not been loaded yet. Locating...]", name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700770 si = load_library(name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800771 if (si == NULL) {
772 return NULL;
773 }
774
775 // At this point we know that whatever is loaded @ base is a valid ELF
776 // shared library whose segments are properly mapped in.
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700777 TRACE("[ init_library base=%p sz=0x%08x name='%s' ]",
778 reinterpret_cast<void*>(si->base), si->size, si->name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800779
780 if (!soinfo_link_image(si)) {
781 munmap(reinterpret_cast<void*>(si->base), si->size);
782 soinfo_free(si);
783 return NULL;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700784 }
785
786 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800787}
788
Elliott Hughesd23736e2012-11-01 15:16:56 -0700789static soinfo* find_library(const char* name) {
790 soinfo* si = find_library_internal(name);
791 if (si != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700792 si->ref_count++;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700793 }
794 return si;
795}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700796
Elliott Hughesd23736e2012-11-01 15:16:56 -0700797static int soinfo_unload(soinfo* si) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700798 if (si->ref_count == 1) {
799 TRACE("unloading '%s'", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700800 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800801
Elliott Hughesc6200592013-09-30 18:43:46 -0700802 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800803 if (d->d_tag == DT_NEEDED) {
804 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughes8147d3c2013-05-09 14:19:58 -0700805 TRACE("%s needs to unload %s", si->name, library_name);
806 soinfo_unload(find_loaded_library(library_name));
Elliott Hughesd23736e2012-11-01 15:16:56 -0700807 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800808 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700809
810 munmap(reinterpret_cast<void*>(si->base), si->size);
811 notify_gdb_of_unload(si);
812 soinfo_free(si);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700813 si->ref_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700814 } else {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700815 si->ref_count--;
Elliott Hughesc6200592013-09-30 18:43:46 -0700816 TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700817 }
818 return 0;
819}
820
Elliott Hughescade4c32012-12-20 14:42:14 -0800821void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
822 if (!get_AT_SECURE()) {
823 parse_LD_LIBRARY_PATH(ld_library_path);
824 }
825}
826
Elliott Hughese66190d2012-12-18 15:57:55 -0800827soinfo* do_dlopen(const char* name, int flags) {
828 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
829 DL_ERR("invalid flags to dlopen: %x", flags);
830 return NULL;
831 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700832 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
833 soinfo* si = find_library(name);
834 if (si != NULL) {
835 si->CallConstructors();
836 }
837 set_soinfo_pool_protection(PROT_READ);
838 return si;
839}
840
841int do_dlclose(soinfo* si) {
842 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
843 int result = soinfo_unload(si);
844 set_soinfo_pool_protection(PROT_READ);
845 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800846}
847
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700848#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700849static int soinfo_relocate_a(soinfo* si, Elf_Rela* rela, unsigned count, soinfo* needed[]) {
850 Elf_Sym* symtab = si->symtab;
851 const char* strtab = si->strtab;
852 Elf_Sym* s;
853 Elf_Rela* start = rela;
854 soinfo* lsi;
855
856 for (size_t idx = 0; idx < count; ++idx, ++rela) {
857 unsigned type = ELF_R_TYPE(rela->r_info);
858 unsigned sym = ELF_R_SYM(rela->r_info);
859 Elf_Addr reloc = static_cast<Elf_Addr>(rela->r_offset + si->load_bias);
860 Elf_Addr sym_addr = 0;
861 char* sym_name = NULL;
862
863 DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
864 if (type == 0) { // R_*_NONE
865 continue;
866 }
867 if (sym != 0) {
868 sym_name = (char *)(strtab + symtab[sym].st_name);
869 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
870 if (s == NULL) {
871 // We only allow an undefined symbol if this is a weak reference...
872 s = &symtab[sym];
873 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
874 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
875 return -1;
876 }
877
878 /* IHI0044C AAELF 4.5.1.1:
879
880 Libraries are not searched to resolve weak references.
881 It is not an error for a weak reference to remain unsatisfied.
882
883 During linking, the value of an undefined weak reference is:
884 - Zero if the relocation type is absolute
885 - The address of the place if the relocation is pc-relative
886 - The address of nominal base address if the relocation
887 type is base-relative.
888 */
889
890 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700891#if defined(__x86_64__)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700892 case R_X86_64_JUMP_SLOT:
893 case R_X86_64_GLOB_DAT:
894 case R_X86_64_32:
895 case R_X86_64_RELATIVE:
896 // No need to do anything.
897 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700898 case R_X86_64_PC32:
899 sym_addr = reloc;
900 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700901#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700902
903 default:
904 DL_ERR("unknown weak reloc type %d @ %p (%d)", type, rela, (int) (rela - start));
905 return -1;
906 }
907 } else {
908 // We got a definition.
909 sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias);
910 }
911 count_relocation(kRelocSymbol);
912 } else {
913 s = NULL;
914 }
915
916 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700917#if defined(__x86_64__)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700918 case R_X86_64_JUMP_SLOT:
919 count_relocation(kRelocAbsolute);
920 MARK(rela->r_offset);
921 TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
922 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
923 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
924 break;
925 case R_X86_64_GLOB_DAT:
926 count_relocation(kRelocAbsolute);
927 MARK(rela->r_offset);
928 TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
929 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
930 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
931 break;
932 case R_X86_64_RELATIVE:
933 count_relocation(kRelocRelative);
934 MARK(rela->r_offset);
935 if (sym) {
936 DL_ERR("odd RELATIVE form...");
937 return -1;
938 }
939 TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
940 static_cast<size_t>(si->base));
941 *reinterpret_cast<Elf_Addr*>(reloc) = si->base + rela->r_addend;
942 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700943 case R_X86_64_32:
944 count_relocation(kRelocRelative);
945 MARK(rela->r_offset);
946 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
947 static_cast<size_t>(sym_addr), sym_name);
948 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
949 break;
Pavel Chupinc075c182013-10-16 19:13:58 +0400950 case R_X86_64_64:
951 count_relocation(kRelocRelative);
952 MARK(rela->r_offset);
953 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
954 static_cast<size_t>(sym_addr), sym_name);
955 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
956 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700957 case R_X86_64_PC32:
958 count_relocation(kRelocRelative);
959 MARK(rela->r_offset);
960 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
961 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
962 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
963 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend - reloc;
964 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700965#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700966 default:
967 DL_ERR("unknown reloc type %d @ %p (%d)", type, rela, (int) (rela - start));
968 return -1;
969 }
970 }
971 return 0;
972}
973#else
Elliott Hughesc6200592013-09-30 18:43:46 -0700974static int soinfo_relocate(soinfo* si, Elf_Rel* rel, unsigned count,
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800975 soinfo* needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800976{
Elliott Hughesc6200592013-09-30 18:43:46 -0700977 Elf_Sym* symtab = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800978 const char* strtab = si->strtab;
Elliott Hughesc6200592013-09-30 18:43:46 -0700979 Elf_Sym* s;
980 Elf_Rel* start = rel;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800981 soinfo* lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800982
Elliott Hughes46882792012-08-03 16:49:39 -0700983 for (size_t idx = 0; idx < count; ++idx, ++rel) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700984 unsigned type = ELF_R_TYPE(rel->r_info);
985 // TODO: don't use unsigned for 'sym'. Use uint32_t or Elf_Addr instead.
986 unsigned sym = ELF_R_SYM(rel->r_info);
Elliott Hughesc6200592013-09-30 18:43:46 -0700987 Elf_Addr reloc = static_cast<Elf_Addr>(rel->r_offset + si->load_bias);
988 Elf_Addr sym_addr = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800989 char* sym_name = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800990
Elliott Hughesc6200592013-09-30 18:43:46 -0700991 DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700992 if (type == 0) { // R_*_NONE
993 continue;
994 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800995 if (sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -0700996 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200997 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800998 if (s == NULL) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700999 // We only allow an undefined symbol if this is a weak reference...
Doug Kwane8238072009-10-26 12:05:23 -07001000 s = &symtab[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001001 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -07001002 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -07001003 return -1;
1004 }
1005
1006 /* IHI0044C AAELF 4.5.1.1:
1007
1008 Libraries are not searched to resolve weak references.
1009 It is not an error for a weak reference to remain
1010 unsatisfied.
1011
1012 During linking, the value of an undefined weak reference is:
1013 - Zero if the relocation type is absolute
1014 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -07001015 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -07001016 type is base-relative.
1017 */
1018
1019 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001020#if defined(__arm__)
Doug Kwane8238072009-10-26 12:05:23 -07001021 case R_ARM_JUMP_SLOT:
1022 case R_ARM_GLOB_DAT:
1023 case R_ARM_ABS32:
1024 case R_ARM_RELATIVE: /* Don't care. */
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001025 // sym_addr was initialized to be zero above or relocation
1026 // code below does not care about value of sym_addr.
1027 // No need to do anything.
1028 break;
1029#elif defined(__i386__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001030 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001031 case R_386_GLOB_DAT:
1032 case R_386_32:
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001033 case R_386_RELATIVE: /* Don't care. */
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001034 // sym_addr was initialized to be zero above or relocation
1035 // code below does not care about value of sym_addr.
1036 // No need to do anything.
Doug Kwane8238072009-10-26 12:05:23 -07001037 break;
Doug Kwane8238072009-10-26 12:05:23 -07001038 case R_386_PC32:
1039 sym_addr = reloc;
1040 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001041#endif
Doug Kwane8238072009-10-26 12:05:23 -07001042
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001043#if defined(__arm__)
Doug Kwane8238072009-10-26 12:05:23 -07001044 case R_ARM_COPY:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001045 // Fall through. Can't really copy if weak symbol is not found at run-time.
1046#endif
Doug Kwane8238072009-10-26 12:05:23 -07001047 default:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001048 DL_ERR("unknown weak reloc type %d @ %p (%d)", type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -07001049 return -1;
1050 }
1051 } else {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001052 // We got a definition.
Elliott Hughesc6200592013-09-30 18:43:46 -07001053 sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001054 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001055 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001056 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001057 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001058 }
1059
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001060 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001061#if defined(__arm__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001062 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001063 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001064 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001065 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001066 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001067 break;
1068 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001069 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001070 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001071 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001072 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001073 break;
1074 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001075 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001076 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001077 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001078 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001079 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001080 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001081 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001082 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001083 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001084 reloc, sym_addr, rel->r_offset, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001085 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr - rel->r_offset;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001086 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001087 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001088 if ((si->flags & FLAG_EXE) == 0) {
1089 /*
1090 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1091 *
1092 * Section 4.7.1.10 "Dynamic relocations"
1093 * R_ARM_COPY may only appear in executable objects where e_type is
1094 * set to ET_EXEC.
1095 *
1096 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1097 * We should explicitly disallow ET_DYN executables from having
1098 * R_ARM_COPY relocations.
1099 */
1100 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1101 return -1;
1102 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001103 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001104 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001105 TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s", reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001106 if (reloc == sym_addr) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001107 Elf_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001108
1109 if (src == NULL) {
1110 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1111 return -1;
1112 }
1113 if (lsi->has_DT_SYMBOLIC) {
1114 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1115 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1116 return -1;
1117 }
1118 if (s->st_size < src->st_size) {
1119 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1120 si->name, s->st_size, src->st_size);
1121 return -1;
1122 }
1123 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1124 } else {
1125 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001126 return -1;
1127 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001128 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001129#elif defined(__i386__)
1130 case R_386_JMP_SLOT:
1131 count_relocation(kRelocAbsolute);
1132 MARK(rel->r_offset);
1133 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1134 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
1135 break;
1136 case R_386_GLOB_DAT:
1137 count_relocation(kRelocAbsolute);
1138 MARK(rel->r_offset);
1139 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1140 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
1141 break;
1142 case R_386_32:
1143 count_relocation(kRelocRelative);
1144 MARK(rel->r_offset);
1145 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1146 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
1147 break;
1148 case R_386_PC32:
1149 count_relocation(kRelocRelative);
1150 MARK(rel->r_offset);
1151 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1152 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1153 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr - reloc);
1154 break;
1155#elif defined(__mips__)
1156 case R_MIPS_REL32:
1157 count_relocation(kRelocAbsolute);
1158 MARK(rel->r_offset);
1159 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s",
1160 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1161 if (s) {
1162 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
1163 } else {
1164 *reinterpret_cast<Elf_Addr*>(reloc) += si->base;
1165 }
1166 break;
1167#endif
1168
1169#if defined(__arm__)
1170 case R_ARM_RELATIVE:
1171#elif defined(__i386__)
1172 case R_386_RELATIVE:
1173#endif
1174 count_relocation(kRelocRelative);
1175 MARK(rel->r_offset);
1176 if (sym) {
1177 DL_ERR("odd RELATIVE form...");
1178 return -1;
1179 }
1180 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1181 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base));
1182 *reinterpret_cast<Elf_Addr*>(reloc) += si->base;
1183 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001184
1185 default:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001186 DL_ERR("unknown reloc type %d @ %p (%d)", type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001187 return -1;
1188 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001189 }
1190 return 0;
1191}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001192#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001193
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001194#if defined(__mips__)
Brian Carlstrom87c35852013-08-20 21:05:44 -07001195static bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
1196 unsigned* got = si->plt_got;
1197 if (got == NULL) {
1198 return true;
1199 }
1200 unsigned local_gotno = si->mips_local_gotno;
1201 unsigned gotsym = si->mips_gotsym;
1202 unsigned symtabno = si->mips_symtabno;
Elliott Hughesc6200592013-09-30 18:43:46 -07001203 Elf_Sym* symtab = si->symtab;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001204
1205 /*
1206 * got[0] is address of lazy resolver function
1207 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001208 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001209 * (should be _rtld_bind_start)
1210 * FIXME: maybe this should be in a separate routine
1211 */
1212
1213 if ((si->flags & FLAG_LINKER) == 0) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001214 size_t g = 0;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001215 got[g++] = 0xdeadbeef;
1216 if (got[g] & 0x80000000) {
1217 got[g++] = 0xdeadfeed;
1218 }
1219 /*
1220 * Relocate the local GOT entries need to be relocated
1221 */
1222 for (; g < local_gotno; g++) {
1223 got[g] += si->load_bias;
1224 }
1225 }
1226
1227 /* Now for the global GOT entries */
Elliott Hughesc6200592013-09-30 18:43:46 -07001228 Elf_Sym* sym = symtab + gotsym;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001229 got = si->plt_got + local_gotno;
Brian Carlstrom87c35852013-08-20 21:05:44 -07001230 for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001231 const char* sym_name;
Elliott Hughesc6200592013-09-30 18:43:46 -07001232 Elf_Sym* s;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001233 soinfo* lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001234
1235 /* This is an undefined reference... try to locate it */
1236 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001237 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001238 if (s == NULL) {
1239 /* We only allow an undefined symbol if this is a weak
1240 reference.. */
1241 s = &symtab[g];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001242 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001243 DL_ERR("cannot locate \"%s\"...", sym_name);
Brian Carlstrom87c35852013-08-20 21:05:44 -07001244 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001245 }
1246 *got = 0;
1247 }
1248 else {
1249 /* FIXME: is this sufficient?
1250 * For reference see NetBSD link loader
1251 * 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
1252 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001253 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001254 }
1255 }
Brian Carlstrom87c35852013-08-20 21:05:44 -07001256 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001257}
1258#endif
1259
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001260void soinfo::CallArray(const char* array_name UNUSED, linker_function_t* functions, size_t count, bool reverse) {
1261 if (functions == NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001262 return;
1263 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001264
Elliott Hughesc6200592013-09-30 18:43:46 -07001265 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001266
1267 int begin = reverse ? (count - 1) : 0;
1268 int end = reverse ? -1 : count;
1269 int step = reverse ? -1 : 1;
1270
1271 for (int i = begin; i != end; i += step) {
1272 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1273 CallFunction("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001274 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001275
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001276 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001277}
1278
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001279void soinfo::CallFunction(const char* function_name UNUSED, linker_function_t function) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001280 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001281 return;
1282 }
1283
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001284 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001285 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001286 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001287
1288 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1289 // are still writable. This happens with our debug malloc (see http://b/7941716).
1290 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001291}
1292
Elliott Hughesd23736e2012-11-01 15:16:56 -07001293void soinfo::CallPreInitConstructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001294 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1295 // but ignored in a shared library.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001296 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1297}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001298
Elliott Hughesd23736e2012-11-01 15:16:56 -07001299void soinfo::CallConstructors() {
1300 if (constructors_called) {
1301 return;
1302 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001303
Elliott Hughesd23736e2012-11-01 15:16:56 -07001304 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1305 // protect against recursive constructor calls. One simple example of constructor recursion
1306 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1307 // 1. The program depends on libc, so libc's constructor is called here.
1308 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1309 // 3. dlopen() calls the constructors on the newly created
1310 // soinfo for libc_malloc_debug_leak.so.
1311 // 4. The debug .so depends on libc, so CallConstructors is
1312 // called again with the libc soinfo. If it doesn't trigger the early-
1313 // out above, the libc constructor will be called again (recursively!).
1314 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001315
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001316 if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
1317 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001318 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001319 name, preinit_array_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001320 }
1321
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001322 if (dynamic != NULL) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001323 for (Elf_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001324 if (d->d_tag == DT_NEEDED) {
1325 const char* library_name = strtab + d->d_un.d_val;
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001326 TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
1327 find_loaded_library(library_name)->CallConstructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001328 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001329 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001330 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001331
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001332 TRACE("\"%s\": calling constructors", name);
1333
1334 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001335 CallFunction("DT_INIT", init_func);
1336 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001337}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001338
Elliott Hughesd23736e2012-11-01 15:16:56 -07001339void soinfo::CallDestructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001340 TRACE("\"%s\": calling destructors", name);
1341
1342 // DT_FINI_ARRAY must be parsed in reverse order.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001343 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001344
1345 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001346 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001347}
1348
1349/* Force any of the closed stdin, stdout and stderr to be associated with
1350 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001351static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001352 int dev_null, i, status;
1353 int return_value = 0;
1354
David 'Digit' Turner16084162012-06-12 16:25:37 +02001355 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001356 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001357 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001358 return -1;
1359 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001360 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001361
1362 /* If any of the stdio file descriptors is valid and not associated
1363 with /dev/null, dup /dev/null to it. */
1364 for (i = 0; i < 3; i++) {
1365 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001366 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001367 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001368 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001369
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001370 TRACE("[ Nullifying stdio file descriptor %d]", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001371 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001372
Elliott Hughes46882792012-08-03 16:49:39 -07001373 /* If file is opened, we are good. */
1374 if (status != -1) {
1375 continue;
1376 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001377
1378 /* The only error we allow is that the file descriptor does not
1379 exist, in which case we dup /dev/null to it. */
1380 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001381 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001382 return_value = -1;
1383 continue;
1384 }
1385
1386 /* Try dupping /dev/null to this stdio file descriptor and
1387 repeat if there is a signal. Note that any errors in closing
1388 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001389 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001390 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001391 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001392 return_value = -1;
1393 continue;
1394 }
1395 }
1396
1397 /* If /dev/null is not one of the stdio file descriptors, close it. */
1398 if (dev_null > 2) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001399 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001400 status = TEMP_FAILURE_RETRY(close(dev_null));
1401 if (status == -1) {
1402 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001403 return_value = -1;
1404 }
1405 }
1406
1407 return return_value;
1408}
1409
Elliott Hughes124fae92012-10-31 14:20:03 -07001410static bool soinfo_link_image(soinfo* si) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001411 /* "base" might wrap around UINT32_MAX. */
Elliott Hughesc6200592013-09-30 18:43:46 -07001412 Elf_Addr base = si->load_bias;
1413 const Elf_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001414 int phnum = si->phnum;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001415 bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001416
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001417 /* We can't debug anything until the linker is relocated */
1418 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001419 INFO("[ linking %s ]", si->name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001420 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001421 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001422
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001423 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001424 size_t dynamic_count;
Elliott Hughesc6200592013-09-30 18:43:46 -07001425 Elf_Word dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001426 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001427 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001428 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001429 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001430 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001431 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001432 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001433 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001434 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001435 DEBUG("dynamic = %p", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001436 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001437 }
1438
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001439#if defined(__arm__)
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001440 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1441 &si->ARM_exidx, &si->ARM_exidx_count);
1442#endif
1443
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001444 // Extract useful information from dynamic section.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001445 uint32_t needed_count = 0;
Elliott Hughesc6200592013-09-30 18:43:46 -07001446 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001447 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1448 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1449 switch (d->d_tag) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001450 case DT_HASH:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001451 si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0];
1452 si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1];
1453 si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8);
1454 si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001455 break;
1456 case DT_STRTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001457 si->strtab = (const char *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001458 break;
1459 case DT_SYMTAB:
Elliott Hughesc6200592013-09-30 18:43:46 -07001460 si->symtab = (Elf_Sym *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001461 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001462#if !defined(__LP64__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001463 case DT_PLTREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001464 if (d->d_un.d_val != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001465 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1466 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001467 }
1468 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001469#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001470 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001471#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001472 si->plt_rela = (Elf_Rela*) (base + d->d_un.d_ptr);
1473#else
Elliott Hughesc6200592013-09-30 18:43:46 -07001474 si->plt_rel = (Elf_Rel*) (base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001475#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001476 break;
1477 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001478#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001479 si->plt_rela_count = d->d_un.d_val / sizeof(Elf_Rela);
1480#else
Elliott Hughesc6200592013-09-30 18:43:46 -07001481 si->plt_rel_count = d->d_un.d_val / sizeof(Elf_Rel);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001482#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001483 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001484#if !defined(__LP64__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001485 case DT_PLTGOT:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001486 // Used by 32-bit MIPS.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001487 si->plt_got = (unsigned *)(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001488 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001489#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001490 case DT_DEBUG:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001491 // Set the DT_DEBUG entry to the address of _r_debug for GDB
Chris Dearmancf239052013-01-11 15:32:20 -08001492 // if the dynamic table is writable
Elliott Hughes99c32052013-01-14 09:56:21 -08001493 if ((dynamic_flags & PF_W) != 0) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001494 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
Elliott Hughes99c32052013-01-14 09:56:21 -08001495 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001496 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001497#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001498 case DT_RELA:
1499 si->rela = (Elf_Rela*) (base + d->d_un.d_ptr);
1500 break;
1501 case DT_RELASZ:
1502 si->rela_count = d->d_un.d_val / sizeof(Elf_Rela);
1503 break;
1504 case DT_REL:
1505 DL_ERR("unsupported DT_REL in \"%s\"", si->name);
1506 return false;
1507 case DT_RELSZ:
1508 DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name);
1509 return false;
1510#else
1511 case DT_REL:
1512 si->rel = (Elf_Rel*) (base + d->d_un.d_ptr);
1513 break;
1514 case DT_RELSZ:
1515 si->rel_count = d->d_un.d_val / sizeof(Elf_Rel);
1516 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001517 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001518 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1519 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001520#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001521 case DT_INIT:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001522 si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001523 DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001524 break;
1525 case DT_FINI:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001526 si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001527 DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001528 break;
1529 case DT_INIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001530 si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001531 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001532 break;
1533 case DT_INIT_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001534 si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001535 break;
1536 case DT_FINI_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001537 si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001538 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001539 break;
1540 case DT_FINI_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001541 si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001542 break;
1543 case DT_PREINIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001544 si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001545 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001546 break;
1547 case DT_PREINIT_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001548 si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001549 break;
1550 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07001551#if defined(__LP64__)
1552 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1553 return false;
1554#else
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001555 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001556 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07001557#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001558 case DT_SYMBOLIC:
1559 si->has_DT_SYMBOLIC = true;
1560 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001561 case DT_NEEDED:
1562 ++needed_count;
1563 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001564 case DT_FLAGS:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001565 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001566#if defined(__LP64__)
1567 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1568 return false;
1569#else
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001570 si->has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07001571#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001572 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001573 if (d->d_un.d_val & DF_SYMBOLIC) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001574 si->has_DT_SYMBOLIC = true;
1575 }
1576 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001577#if defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001578 case DT_STRSZ:
1579 case DT_SYMENT:
1580 case DT_RELENT:
1581 break;
1582 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001583 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001584 {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001585 r_debug** dp = (r_debug**) d->d_un.d_ptr;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001586 *dp = &_r_debug;
1587 }
1588 break;
1589 case DT_MIPS_RLD_VERSION:
1590 case DT_MIPS_FLAGS:
1591 case DT_MIPS_BASE_ADDRESS:
1592 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001593 break;
1594
1595 case DT_MIPS_SYMTABNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001596 si->mips_symtabno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001597 break;
1598
1599 case DT_MIPS_LOCAL_GOTNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001600 si->mips_local_gotno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001601 break;
1602
1603 case DT_MIPS_GOTSYM:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001604 si->mips_gotsym = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001605 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001606#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001607
1608 default:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001609 DEBUG("Unused DT entry: type %p arg %p",
1610 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001611 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001612 }
1613 }
1614
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001615 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
1616 reinterpret_cast<void*>(si->base), si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001617
Elliott Hughes124fae92012-10-31 14:20:03 -07001618 // Sanity checks.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001619 if (relocating_linker && needed_count != 0) {
1620 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1621 return false;
1622 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001623 if (si->nbucket == 0) {
1624 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1625 return false;
1626 }
1627 if (si->strtab == 0) {
1628 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1629 return false;
1630 }
1631 if (si->symtab == 0) {
1632 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1633 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001634 }
1635
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001636 // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001637 if (si->flags & FLAG_EXE) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001638 memset(gLdPreloads, 0, sizeof(gLdPreloads));
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001639 size_t preload_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001640 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1641 soinfo* lsi = find_library(gLdPreloadNames[i]);
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001642 if (lsi != NULL) {
1643 gLdPreloads[preload_count++] = lsi;
1644 } else {
1645 // As with glibc, failure to load an LD_PRELOAD library is just a warning.
1646 DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
1647 gLdPreloadNames[i], si->name, linker_get_error_buffer());
Matt Fischer4fd42c12009-12-31 12:09:10 -06001648 }
Matt Fischer4fd42c12009-12-31 12:09:10 -06001649 }
1650 }
1651
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001652 soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*));
1653 soinfo** pneeded = needed;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001654
Elliott Hughesc6200592013-09-30 18:43:46 -07001655 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001656 if (d->d_tag == DT_NEEDED) {
1657 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001658 DEBUG("%s needs %s", si->name, library_name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001659 soinfo* lsi = find_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001660 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001661 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001662 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001663 library_name, si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001664 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001665 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001666 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001667 }
1668 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001669 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001670
Elliott Hughese4d792a2013-10-28 14:19:05 -07001671#if !defined(__LP64__)
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001672 if (si->has_text_relocations) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001673 // Make segments writable to allow text relocations to work properly. We will later call
1674 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
Nick Kralevich3d4470c2013-10-22 12:06:36 -07001675 DL_WARN("%s has text relocations. This is wasting memory and prevents "
1676 "security hardening. Please fix.", si->name);
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001677 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1678 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1679 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001680 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001681 }
1682 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07001683#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001684
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001685#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001686 if (si->plt_rela != NULL) {
1687 DEBUG("[ relocating %s plt ]\n", si->name );
1688 if (soinfo_relocate_a(si, si->plt_rela, si->plt_rela_count, needed)) {
1689 return false;
1690 }
1691 }
1692 if (si->rela != NULL) {
1693 DEBUG("[ relocating %s ]\n", si->name );
1694 if (soinfo_relocate_a(si, si->rela, si->rela_count, needed)) {
1695 return false;
1696 }
1697 }
1698#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001699 if (si->plt_rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001700 DEBUG("[ relocating %s plt ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001701 if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001702 return false;
1703 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001704 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001705 if (si->rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001706 DEBUG("[ relocating %s ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001707 if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001708 return false;
1709 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001710 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001711#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001712
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001713#if defined(__mips__)
Brian Carlstrom87c35852013-08-20 21:05:44 -07001714 if (!mips_relocate_got(si, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001715 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001716 }
1717#endif
1718
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001719 si->flags |= FLAG_LINKED;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001720 DEBUG("[ finished linking %s ]", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001721
Elliott Hughese4d792a2013-10-28 14:19:05 -07001722#if !defined(__LP64__)
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001723 if (si->has_text_relocations) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001724 // All relocations are done, we can protect our segments back to read-only.
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001725 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1726 DL_ERR("can't protect segments for \"%s\": %s",
1727 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001728 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001729 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001730 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07001731#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001732
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001733 /* We can also turn on GNU RELRO protection */
1734 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001735 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1736 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001737 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001738 }
1739
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001740 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001741 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001742}
1743
Nick Kralevich468319c2011-11-11 15:53:17 -08001744/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001745 * This function add vdso to internal dso list.
1746 * It helps to stack unwinding through signal handlers.
1747 * Also, it makes bionic more like glibc.
1748 */
1749static void add_vdso(KernelArgumentBlock& args UNUSED) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001750#if defined(AT_SYSINFO_EHDR)
Elliott Hughesc6200592013-09-30 18:43:46 -07001751 Elf_Ehdr* ehdr_vdso = reinterpret_cast<Elf_Ehdr*>(args.getauxval(AT_SYSINFO_EHDR));
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001752
1753 soinfo* si = soinfo_alloc("[vdso]");
Elliott Hughesc6200592013-09-30 18:43:46 -07001754 si->phdr = reinterpret_cast<Elf_Phdr*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001755 si->phnum = ehdr_vdso->e_phnum;
1756 si->link_map.l_name = si->name;
1757 for (size_t i = 0; i < si->phnum; ++i) {
1758 if (si->phdr[i].p_type == PT_LOAD) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001759 si->link_map.l_addr = reinterpret_cast<Elf_Addr>(ehdr_vdso) - si->phdr[i].p_vaddr;
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001760 break;
1761 }
1762 }
1763#endif
1764}
1765
1766/*
Nick Kralevich468319c2011-11-11 15:53:17 -08001767 * This code is called after the linker has linked itself and
1768 * fixed it's own GOT. It is safe to make references to externs
1769 * and other non-local data at this point.
1770 */
Elliott Hughesc6200592013-09-30 18:43:46 -07001771static Elf_Addr __linker_init_post_relocation(KernelArgumentBlock& args, Elf_Addr linker_base) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001772 /* NOTE: we store the args pointer on a special location
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001773 * of the temporary TLS area in order to pass it to
1774 * the C Library's runtime initializer.
1775 *
1776 * The initializer must clear the slot and reset the TLS
1777 * to point to a different location to ensure that no other
1778 * shared library constructor can access it.
1779 */
Elliott Hughesd3920b32013-02-07 18:39:34 -08001780 __libc_init_tls(args);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001781
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001782#if TIMING
1783 struct timeval t0, t1;
1784 gettimeofday(&t0, 0);
1785#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001786
Elliott Hughes18a206c2012-10-29 17:37:13 -07001787 // Initialize environment functions, and get to the ELF aux vectors table.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001788 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001789
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07001790 // If this is a setuid/setgid program, close the security hole described in
1791 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1792 if (get_AT_SECURE()) {
1793 nullify_closed_stdio();
1794 }
1795
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001796 debuggerd_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001797
Elliott Hughes18a206c2012-10-29 17:37:13 -07001798 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001799 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1800 if (LD_DEBUG != NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001801 gLdDebugVerbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001802 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001803
Elliott Hughes18a206c2012-10-29 17:37:13 -07001804 // Normally, these are cleaned by linker_env_init, but the test
1805 // doesn't cost us anything.
1806 const char* ldpath_env = NULL;
1807 const char* ldpreload_env = NULL;
1808 if (!get_AT_SECURE()) {
1809 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1810 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001811 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001812
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001813 INFO("[ android linker & debugger ]");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001814
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001815 soinfo* si = soinfo_alloc(args.argv[0]);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001816 if (si == NULL) {
1817 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001818 }
1819
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001820 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001821 si->flags |= FLAG_EXE;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001822 link_map_t* map = &(si->link_map);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001823
1824 map->l_addr = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001825 map->l_name = args.argv[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001826 map->l_prev = NULL;
1827 map->l_next = NULL;
1828
1829 _r_debug.r_map = map;
1830 r_debug_tail = map;
1831
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001832 /* gdb expects the linker to be in the debug shared object list.
1833 * Without this, gdb has trouble locating the linker's ".text"
1834 * and ".plt" sections. Gdb could also potentially use this to
1835 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1836 * Don't use soinfo_alloc(), because the linker shouldn't
1837 * be on the soinfo list.
Ben Cheng06f0e742012-08-10 16:07:02 -07001838 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001839 {
1840 static soinfo linker_soinfo;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001841#if defined(__LP64__)
Pavel Chupin1a57f9f2013-02-06 19:21:46 +04001842 strlcpy(linker_soinfo.name, "/system/bin/linker64", sizeof(linker_soinfo.name));
1843#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001844 strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
Pavel Chupin1a57f9f2013-02-06 19:21:46 +04001845#endif
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001846 linker_soinfo.flags = 0;
1847 linker_soinfo.base = linker_base;
1848
1849 /*
1850 * Set the dynamic field in the link map otherwise gdb will complain with
1851 * the following:
1852 * warning: .dynamic section for "/system/bin/linker" is not at the
1853 * expected address (wrong library or version mismatch?)
1854 */
Elliott Hughesc6200592013-09-30 18:43:46 -07001855 Elf_Ehdr *elf_hdr = (Elf_Ehdr *) linker_base;
1856 Elf_Phdr *phdr = (Elf_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001857 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1858 &linker_soinfo.dynamic, NULL, NULL);
1859 insert_soinfo_into_debug_map(&linker_soinfo);
1860 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001861
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001862 // Extract information passed from the kernel.
Elliott Hughesc6200592013-09-30 18:43:46 -07001863 si->phdr = reinterpret_cast<Elf_Phdr*>(args.getauxval(AT_PHDR));
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001864 si->phnum = args.getauxval(AT_PHNUM);
1865 si->entry = args.getauxval(AT_ENTRY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001866
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001867 /* Compute the value of si->base. We can't rely on the fact that
1868 * the first entry is the PHDR because this will not be true
1869 * for certain executables (e.g. some in the NDK unit test suite)
1870 */
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001871 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001872 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001873 si->load_bias = 0;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001874 for (size_t i = 0; i < si->phnum; ++i) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001875 if (si->phdr[i].p_type == PT_PHDR) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001876 si->load_bias = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_vaddr;
1877 si->base = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_offset;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001878 break;
1879 }
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001880 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001881 si->dynamic = NULL;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001882 si->ref_count = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001883
Elliott Hughes46882792012-08-03 16:49:39 -07001884 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1885 parse_LD_LIBRARY_PATH(ldpath_env);
1886 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001887
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001888 somain = si;
1889
Elliott Hughes124fae92012-10-31 14:20:03 -07001890 if (!soinfo_link_image(si)) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001891 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
Elliott Hughes18a206c2012-10-29 17:37:13 -07001892 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001893 }
1894
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001895 add_vdso(args);
1896
Elliott Hughesd23736e2012-11-01 15:16:56 -07001897 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001898
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001899 for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
1900 gLdPreloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08001901 }
1902
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001903 /* After the link_image, the si->load_bias is initialized.
1904 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1905 * We need to update this value for so exe here. So Unwind_Backtrace
1906 * for some arch like x86 could work correctly within so exe.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08001907 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08001908 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001909 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001910
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001911#if TIMING
1912 gettimeofday(&t1,NULL);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001913 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001914 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1915 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1916 ));
1917#endif
1918#if STATS
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001919 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07001920 linker_stats.count[kRelocAbsolute],
1921 linker_stats.count[kRelocRelative],
1922 linker_stats.count[kRelocCopy],
1923 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001924#endif
1925#if COUNT_PAGES
1926 {
1927 unsigned n;
1928 unsigned i;
1929 unsigned count = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001930 for (n = 0; n < 4096; n++) {
1931 if (bitmask[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001932 unsigned x = bitmask[n];
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001933 for (i = 0; i < 8; i++) {
1934 if (x & 1) {
1935 count++;
1936 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001937 x >>= 1;
1938 }
1939 }
1940 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001941 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001942 }
1943#endif
1944
1945#if TIMING || STATS || COUNT_PAGES
1946 fflush(stdout);
1947#endif
1948
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001949 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001950 return si->entry;
1951}
Nick Kralevich468319c2011-11-11 15:53:17 -08001952
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001953/* Compute the load-bias of an existing executable. This shall only
1954 * be used to compute the load bias of an executable or shared library
1955 * that was loaded by the kernel itself.
1956 *
1957 * Input:
1958 * elf -> address of ELF header, assumed to be at the start of the file.
1959 * Return:
1960 * load bias, i.e. add the value of any p_vaddr in the file to get
1961 * the corresponding address in memory.
1962 */
Elliott Hughesc6200592013-09-30 18:43:46 -07001963static Elf_Addr get_elf_exec_load_bias(const Elf_Ehdr* elf) {
1964 Elf_Addr offset = elf->e_phoff;
1965 const Elf_Phdr* phdr_table = (const Elf_Phdr*)((char*)elf + offset);
1966 const Elf_Phdr* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001967
Elliott Hughesc6200592013-09-30 18:43:46 -07001968 for (const Elf_Phdr* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001969 if (phdr->p_type == PT_LOAD) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001970 return reinterpret_cast<Elf_Addr>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001971 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001972 }
1973 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001974}
1975
Nick Kralevich468319c2011-11-11 15:53:17 -08001976/*
1977 * This is the entry point for the linker, called from begin.S. This
1978 * method is responsible for fixing the linker's own relocations, and
1979 * then calling __linker_init_post_relocation().
1980 *
1981 * Because this method is called before the linker has fixed it's own
1982 * relocations, any attempt to reference an extern variable, extern
1983 * function, or other GOT reference will generate a segfault.
1984 */
Elliott Hughesc6200592013-09-30 18:43:46 -07001985extern "C" Elf_Addr __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001986 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08001987
Elliott Hughesc6200592013-09-30 18:43:46 -07001988 Elf_Addr linker_addr = args.getauxval(AT_BASE);
Nick Kralevich468319c2011-11-11 15:53:17 -08001989
Elliott Hughesc6200592013-09-30 18:43:46 -07001990 Elf_Ehdr* elf_hdr = reinterpret_cast<Elf_Ehdr*>(linker_addr);
1991 Elf_Phdr* phdr = (Elf_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08001992
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001993 soinfo linker_so;
1994 memset(&linker_so, 0, sizeof(soinfo));
Nick Kralevich468319c2011-11-11 15:53:17 -08001995
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001996 linker_so.base = linker_addr;
1997 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
1998 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001999 linker_so.dynamic = NULL;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002000 linker_so.phdr = phdr;
2001 linker_so.phnum = elf_hdr->e_phnum;
2002 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07002003
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002004 if (!soinfo_link_image(&linker_so)) {
2005 // It would be nice to print an error message, but if the linker
2006 // can't link itself, there's no guarantee that we'll be able to
2007 // call write() (because it involves a GOT reference).
2008 //
2009 // This situation should never occur unless the linker itself
2010 // is corrupt.
2011 exit(EXIT_FAILURE);
2012 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002013
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002014 // We have successfully fixed our own relocations. It's safe to run
2015 // the main part of the linker now.
Elliott Hughes0d787c12013-04-04 13:46:46 -07002016 args.abort_message_ptr = &gAbortMessage;
Elliott Hughesc6200592013-09-30 18:43:46 -07002017 Elf_Addr start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002018
2019 set_soinfo_pool_protection(PROT_READ);
2020
2021 // Return the address that the calling assembly stub should jump to.
2022 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002023}