blob: d218590cfd587f97dcd0d9f65b99302ff7bc852a [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.
43#include <private/bionic_tls.h>
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080044#include <private/KernelArgumentBlock.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070045#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[] = {
93 "/vendor/lib",
94 "/system/lib",
95 NULL
96};
David Bartleybc3a5c22009-06-02 18:27:28 -070097
Elliott Hughes124fae92012-10-31 14:20:03 -070098static char gLdPathsBuffer[LDPATH_BUFSIZE];
99static const char* gLdPaths[LDPATH_MAX + 1];
100
101static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
102static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600103
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800104static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600105
Elliott Hughes650be4e2013-03-05 18:47:58 -0800106__LIBC_HIDDEN__ int gLdDebugVerbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107
Elliott Hughes0d787c12013-04-04 13:46:46 -0700108__LIBC_HIDDEN__ abort_msg_t* gAbortMessage = NULL; // For debuggerd.
109
Elliott Hughesbedfe382012-08-14 14:07:59 -0700110enum RelocationKind {
111 kRelocAbsolute = 0,
112 kRelocRelative,
113 kRelocCopy,
114 kRelocSymbol,
115 kRelocMax
116};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100117
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700119struct linker_stats_t {
120 int count[kRelocMax];
121};
122
123static linker_stats_t linker_stats;
124
125static void count_relocation(RelocationKind kind) {
126 ++linker_stats.count[kind];
127}
128#else
129static void count_relocation(RelocationKind) {
130}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131#endif
132
133#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700134static unsigned bitmask[4096];
135#define MARK(offset) \
136 do { \
137 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
138 } while(0)
139#else
140#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141#endif
142
Elliott Hughes46882792012-08-03 16:49:39 -0700143// You shouldn't try to call memory-allocating functions in the dynamic linker.
144// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700145#define DISALLOW_ALLOCATION(return_type, name, ...) \
146 return_type name __VA_ARGS__ \
147 { \
Elliott Hughes46882792012-08-03 16:49:39 -0700148 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700149 __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
150 write(2, msg, strlen(msg)); \
151 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700152 }
Elliott Hughes46882792012-08-03 16:49:39 -0700153#define UNUSED __attribute__((unused))
154DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
155DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
156DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
157DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
Dima Zavin2e855792009-05-20 18:28:09 -0700158
Dima Zavin03531952009-05-29 17:30:25 -0700159static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700160static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700161
Elliott Hughes650be4e2013-03-05 18:47:58 -0800162char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700163 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700164}
165
Elliott Hughes650be4e2013-03-05 18:47:58 -0800166size_t linker_get_error_buffer_size() {
167 return sizeof(__linker_dl_err_buf);
168}
169
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170/*
171 * This function is an empty stub where GDB locates a breakpoint to get notified
172 * about linker activity.
173 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700174extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
Elliott Hughes0d787c12013-04-04 13:46:46 -0700176static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity, RT_CONSISTENT, 0};
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700177static link_map_t* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178
Elliott Hughes3b297c42012-10-11 16:08:51 -0700179static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180
Elliott Hughesbedfe382012-08-14 14:07:59 -0700181static void insert_soinfo_into_debug_map(soinfo * info) {
182 // Copy the necessary fields into the debug structure.
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700183 link_map_t* map = &(info->link_map);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184 map->l_addr = info->base;
185 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800186 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800187
188 /* Stick the new library at the end of the list.
189 * gdb tends to care more about libc than it does
190 * about leaf libraries, and ordering it this way
191 * reduces the back-and-forth over the wire.
192 */
193 if (r_debug_tail) {
194 r_debug_tail->l_next = map;
195 map->l_prev = r_debug_tail;
196 map->l_next = 0;
197 } else {
198 _r_debug.r_map = map;
199 map->l_prev = 0;
200 map->l_next = 0;
201 }
202 r_debug_tail = map;
203}
204
Elliott Hughesbedfe382012-08-14 14:07:59 -0700205static void remove_soinfo_from_debug_map(soinfo* info) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700206 link_map_t* map = &(info->link_map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700207
Elliott Hughesbedfe382012-08-14 14:07:59 -0700208 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700209 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700210 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700211
Elliott Hughesbedfe382012-08-14 14:07:59 -0700212 if (map->l_prev) {
213 map->l_prev->l_next = map->l_next;
214 }
215 if (map->l_next) {
216 map->l_next->l_prev = map->l_prev;
217 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700218}
219
Elliott Hughesbedfe382012-08-14 14:07:59 -0700220static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221 if (info->flags & FLAG_EXE) {
222 // GDB already knows about the main executable
223 return;
224 }
225
Elliott Hughes3b297c42012-10-11 16:08:51 -0700226 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227
228 _r_debug.r_state = RT_ADD;
229 rtld_db_dlactivity();
230
231 insert_soinfo_into_debug_map(info);
232
233 _r_debug.r_state = RT_CONSISTENT;
234 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700235}
236
Elliott Hughesbedfe382012-08-14 14:07:59 -0700237static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700238 if (info->flags & FLAG_EXE) {
239 // GDB already knows about the main executable
240 return;
241 }
242
Elliott Hughes3b297c42012-10-11 16:08:51 -0700243 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700244
245 _r_debug.r_state = RT_DELETE;
246 rtld_db_dlactivity();
247
248 remove_soinfo_from_debug_map(info);
249
250 _r_debug.r_state = RT_CONSISTENT;
251 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800252}
253
Elliott Hughes18a206c2012-10-29 17:37:13 -0700254void notify_gdb_of_libraries() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255 _r_debug.r_state = RT_ADD;
256 rtld_db_dlactivity();
257 _r_debug.r_state = RT_CONSISTENT;
258 rtld_db_dlactivity();
259}
260
Magnus Malmbornba98d922012-09-12 13:00:55 +0200261static bool ensure_free_list_non_empty() {
262 if (gSoInfoFreeList != NULL) {
263 return true;
264 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800265
Magnus Malmbornba98d922012-09-12 13:00:55 +0200266 // Allocate a new pool.
267 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
268 PROT_READ|PROT_WRITE,
269 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
270 if (pool == MAP_FAILED) {
271 return false;
272 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273
Magnus Malmbornba98d922012-09-12 13:00:55 +0200274 // Add the pool to our list of pools.
275 pool->next = gSoInfoPools;
276 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277
Magnus Malmbornba98d922012-09-12 13:00:55 +0200278 // Chain the entries in the new pool onto the free list.
279 gSoInfoFreeList = &pool->info[0];
280 soinfo* next = NULL;
281 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
282 pool->info[i].next = next;
283 next = &pool->info[i];
284 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285
Magnus Malmbornba98d922012-09-12 13:00:55 +0200286 return true;
287}
288
Elliott Hughesd23736e2012-11-01 15:16:56 -0700289static void set_soinfo_pool_protection(int protection) {
290 for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
291 if (mprotect(p, sizeof(*p), protection) == -1) {
292 abort(); // Can't happen.
293 }
294 }
295}
296
Magnus Malmbornba98d922012-09-12 13:00:55 +0200297static soinfo* soinfo_alloc(const char* name) {
298 if (strlen(name) >= SOINFO_NAME_LEN) {
299 DL_ERR("library name \"%s\" too long", name);
300 return NULL;
301 }
302
303 if (!ensure_free_list_non_empty()) {
304 DL_ERR("out of memory when loading \"%s\"", name);
305 return NULL;
306 }
307
308 // Take the head element off the free list.
309 soinfo* si = gSoInfoFreeList;
310 gSoInfoFreeList = gSoInfoFreeList->next;
311
312 // Initialize the new element.
313 memset(si, 0, sizeof(soinfo));
314 strlcpy(si->name, name, sizeof(si->name));
315 sonext->next = si;
316 sonext = si;
317
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700318 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200319 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800320}
321
Elliott Hughes46882792012-08-03 16:49:39 -0700322static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800323{
Elliott Hughes46882792012-08-03 16:49:39 -0700324 if (si == NULL) {
325 return;
326 }
327
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800328 soinfo *prev = NULL, *trav;
329
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700330 TRACE("name %s: freeing soinfo @ %p", si->name, si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800331
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800332 for (trav = solist; trav != NULL; trav = trav->next) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333 if (trav == si)
334 break;
335 prev = trav;
336 }
337 if (trav == NULL) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800338 /* si was not in solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700339 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800340 return;
341 }
342
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100343 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800344 always the static libdl_info.
345 */
346 prev->next = si->next;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800347 if (si == sonext) {
348 sonext = prev;
349 }
Magnus Malmbornba98d922012-09-12 13:00:55 +0200350 si->next = gSoInfoFreeList;
351 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800352}
353
Elliott Hughescade4c32012-12-20 14:42:14 -0800354
355static void parse_path(const char* path, const char* delimiters,
356 const char** array, char* buf, size_t buf_size, size_t max_count) {
357 if (path == NULL) {
358 return;
359 }
360
361 size_t len = strlcpy(buf, path, buf_size);
362
363 size_t i = 0;
364 char* buf_p = buf;
365 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
366 if (*array[i] != '\0') {
367 ++i;
368 }
369 }
370
371 // Forget the last path if we had to truncate; this occurs if the 2nd to
372 // last char isn't '\0' (i.e. wasn't originally a delimiter).
373 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
374 array[i - 1] = NULL;
375 } else {
376 array[i] = NULL;
377 }
378}
379
380static void parse_LD_LIBRARY_PATH(const char* path) {
381 parse_path(path, ":", gLdPaths,
382 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
383}
384
385static void parse_LD_PRELOAD(const char* path) {
386 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
387 parse_path(path, " :", gLdPreloadNames,
388 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
389}
390
Elliott Hughes46882792012-08-03 16:49:39 -0700391#ifdef ANDROID_ARM_LINKER
392
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393/* For a given PC, find the .so that it belongs to.
394 * Returns the base address of the .ARM.exidx section
395 * for that .so, and the number of 8-byte entries
396 * in that section (via *pcount).
397 *
398 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
399 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700400 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800401 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
403{
404 soinfo *si;
405 unsigned addr = (unsigned)pc;
406
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700407 for (si = solist; si != 0; si = si->next) {
Nick Kralevich468319c2011-11-11 15:53:17 -0800408 if ((addr >= si->base) && (addr < (si->base + si->size))) {
409 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900410 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411 }
412 }
413 *pcount = 0;
414 return NULL;
415}
Elliott Hughes46882792012-08-03 16:49:39 -0700416
Christopher Ferris24053a42013-08-19 17:45:09 -0700417#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700418
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800419/* Here, we only have to provide a callback to iterate across all the
420 * loaded libraries. gcc_eh does the rest. */
421int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700422dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800423 void *data)
424{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800425 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700426 for (soinfo* si = solist; si != NULL; si = si->next) {
427 dl_phdr_info dl_info;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700428 dl_info.dlpi_addr = si->link_map.l_addr;
429 dl_info.dlpi_name = si->link_map.l_name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800430 dl_info.dlpi_phdr = si->phdr;
431 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700432 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
433 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800434 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700435 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800436 }
437 return rv;
438}
Elliott Hughes46882792012-08-03 16:49:39 -0700439
Elliott Hughesc6200592013-09-30 18:43:46 -0700440static Elf_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
441 Elf_Sym* symtab = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800442 const char* strtab = si->strtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800443
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700444 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
445 name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800446
Christopher Ferris6bec5b72013-06-03 17:27:49 -0700447 for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700448 Elf_Sym* s = symtab + n;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800449 if (strcmp(strtab + s->st_name, name)) continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800450
Doug Kwane8238072009-10-26 12:05:23 -0700451 /* only concern ourselves with global and weak symbol definitions */
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700452 switch (ELF_ST_BIND(s->st_info)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800453 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700454 case STB_WEAK:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800455 if (s->st_shndx == SHN_UNDEF) {
Robin Burchell439fa8e2012-07-05 09:21:07 +0200456 continue;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800457 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800458
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700459 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
460 name, si->name, reinterpret_cast<void*>(s->st_value),
461 static_cast<size_t>(s->st_size));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800462 return s;
463 }
464 }
465
Doug Kwan94304352009-10-23 18:11:40 -0700466 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800467}
468
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800469static unsigned elfhash(const char* _name) {
470 const unsigned char* name = (const unsigned char*) _name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800471 unsigned h = 0, g;
472
473 while(*name) {
474 h = (h << 4) + *name++;
475 g = h & 0xf0000000;
476 h ^= g;
477 h ^= g >> 24;
478 }
479 return h;
480}
481
Elliott Hughesc6200592013-09-30 18:43:46 -0700482static Elf_Sym* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
Doug Kwan94304352009-10-23 18:11:40 -0700483 unsigned elf_hash = elfhash(name);
Elliott Hughesc6200592013-09-30 18:43:46 -0700484 Elf_Sym* s = NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700485
Pavel Chupinc77c4342012-10-31 13:55:51 +0400486 if (si != NULL && somain != NULL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200487
488 /*
Pavel Chupinc77c4342012-10-31 13:55:51 +0400489 * Local scope is executable scope. Just start looking into it right away
490 * for the shortcut.
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200491 */
492
Pavel Chupinc77c4342012-10-31 13:55:51 +0400493 if (si == somain) {
494 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200495 if (s != NULL) {
Pavel Chupinc77c4342012-10-31 13:55:51 +0400496 *lsi = si;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200497 goto done;
498 }
Pavel Chupinc77c4342012-10-31 13:55:51 +0400499 } else {
500 /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200501
Pavel Chupinc77c4342012-10-31 13:55:51 +0400502 /*
503 * If this object was built with symbolic relocations disabled, the
504 * first place to look to resolve external references is the main
505 * executable.
506 */
Nick Kralevich468319c2011-11-11 15:53:17 -0800507
Pavel Chupinc77c4342012-10-31 13:55:51 +0400508 if (!si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700509 DEBUG("%s: looking up %s in executable %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700510 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400511 s = soinfo_elf_lookup(somain, elf_hash, name);
512 if (s != NULL) {
513 *lsi = somain;
514 goto done;
515 }
516 }
517
518 /* Look for symbols in the local scope (the object who is
519 * searching). This happens with C++ templates on i386 for some
520 * reason.
521 *
522 * Notes on weak symbols:
523 * The ELF specs are ambiguous about treatment of weak definitions in
524 * dynamic linking. Some systems return the first definition found
525 * and some the first non-weak definition. This is system dependent.
526 * Here we return the first definition found for simplicity. */
527
528 s = soinfo_elf_lookup(si, elf_hash, name);
529 if (s != NULL) {
530 *lsi = si;
531 goto done;
532 }
533
534 /*
535 * If this object was built with -Bsymbolic and symbol is not found
536 * in the local scope, try to find the symbol in the main executable.
537 */
538
539 if (si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700540 DEBUG("%s: looking up %s in executable %s after local scope",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700541 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400542 s = soinfo_elf_lookup(somain, elf_hash, name);
543 if (s != NULL) {
544 *lsi = somain;
545 goto done;
546 }
547 }
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200548 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700549 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700550
Matt Fischer4fd42c12009-12-31 12:09:10 -0600551 /* Next, look for it in the preloads list */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800552 for (int i = 0; gLdPreloads[i] != NULL; i++) {
553 s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
554 if (s != NULL) {
555 *lsi = gLdPreloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600556 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200557 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600558 }
559
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800560 for (int i = 0; needed[i] != NULL; i++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700561 DEBUG("%s: looking up %s in %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700562 si->name, name, needed[i]->name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200563 s = soinfo_elf_lookup(needed[i], elf_hash, name);
564 if (s != NULL) {
565 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200566 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200567 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700568 }
569
570done:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800571 if (s != NULL) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700572 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
573 "found in %s, base = %p, load bias = %p",
574 si->name, name, reinterpret_cast<void*>(s->st_value),
575 (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
576 reinterpret_cast<void*>((*lsi)->load_bias));
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700577 return s;
578 }
579
Doug Kwan94304352009-10-23 18:11:40 -0700580 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700581}
582
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800583/* This is used by dlsym(3). It performs symbol lookup only within the
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700584 specified soinfo object and not in any of its dependencies.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800585
586 TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
587 that it should do a breadth first search through the dependency
588 tree. This agrees with the ELF spec (aka System V Application
589 Binary Interface) where in Chapter 5 it discuss resolving "Shared
590 Object Dependencies" in breadth first search order.
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700591 */
Elliott Hughesc6200592013-09-30 18:43:46 -0700592Elf_Sym* dlsym_handle_lookup(soinfo* si, const char* name) {
David 'Digit' Turner16084162012-06-12 16:25:37 +0200593 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800594}
595
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800596/* This is used by dlsym(3) to performs a global symbol lookup. If the
597 start value is null (for RTLD_DEFAULT), the search starts at the
598 beginning of the global solist. Otherwise the search starts at the
599 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700600 */
Elliott Hughesc6200592013-09-30 18:43:46 -0700601Elf_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800602 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800603
Elliott Hughescade4c32012-12-20 14:42:14 -0800604 if (start == NULL) {
605 start = solist;
606 }
607
Elliott Hughesc6200592013-09-30 18:43:46 -0700608 Elf_Sym* s = NULL;
Elliott Hughescade4c32012-12-20 14:42:14 -0800609 for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
610 s = soinfo_elf_lookup(si, elf_hash, name);
611 if (s != NULL) {
612 *found = si;
613 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600614 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800615 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600616
Elliott Hughescade4c32012-12-20 14:42:14 -0800617 if (s != NULL) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700618 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
619 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800620 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800621
Elliott Hughescade4c32012-12-20 14:42:14 -0800622 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800623}
624
Kito Chengfa8c05d2013-03-12 14:58:06 +0800625soinfo* find_containing_library(const void* p) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700626 Elf_Addr address = reinterpret_cast<Elf_Addr>(p);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800627 for (soinfo* si = solist; si != NULL; si = si->next) {
628 if (address >= si->base && address - si->base < si->size) {
629 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600630 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800631 }
632 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600633}
634
Elliott Hughesc6200592013-09-30 18:43:46 -0700635Elf_Sym* dladdr_find_symbol(soinfo* si, const void* addr) {
636 Elf_Addr soaddr = reinterpret_cast<Elf_Addr>(addr) - si->base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600637
Kito Chengfa8c05d2013-03-12 14:58:06 +0800638 // Search the library's symbol table for any defined symbol which
639 // contains this address.
640 for (size_t i = 0; i < si->nchain; ++i) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700641 Elf_Sym* sym = &si->symtab[i];
Kito Chengfa8c05d2013-03-12 14:58:06 +0800642 if (sym->st_shndx != SHN_UNDEF &&
643 soaddr >= sym->st_value &&
644 soaddr < sym->st_value + sym->st_size) {
645 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600646 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800647 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600648
Kito Chengfa8c05d2013-03-12 14:58:06 +0800649 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600650}
651
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800652#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800653static void dump(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800654{
Elliott Hughesc6200592013-09-30 18:43:46 -0700655 Elf_Sym* s = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800656 for (unsigned n = 0; n < si->nchain; n++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700657 TRACE("%04d> %08x: %02x %04x %08x %08x %s", n, s,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800658 s->st_info, s->st_shndx, s->st_value, s->st_size,
659 si->strtab + s->st_name);
660 s++;
661 }
662}
663#endif
664
Elliott Hughes124fae92012-10-31 14:20:03 -0700665static int open_library_on_path(const char* name, const char* const paths[]) {
666 char buf[512];
667 for (size_t i = 0; paths[i] != NULL; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800668 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700669 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700670 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700671 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800672 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700673 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
674 if (fd != -1) {
675 return fd;
676 }
677 }
678 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800679}
680
Elliott Hughes124fae92012-10-31 14:20:03 -0700681static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700682 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800683
Elliott Hughes124fae92012-10-31 14:20:03 -0700684 // If the name contains a slash, we should attempt to open it directly and not search the paths.
685 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700686 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
687 if (fd != -1) {
688 return fd;
689 }
690 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700691 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800692
Elliott Hughes124fae92012-10-31 14:20:03 -0700693 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
694 int fd = open_library_on_path(name, gLdPaths);
695 if (fd == -1) {
696 fd = open_library_on_path(name, gSoPaths);
697 }
698 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800699}
700
Elliott Hughes124fae92012-10-31 14:20:03 -0700701static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700702 // Open the file.
Elliott Hughes650be4e2013-03-05 18:47:58 -0800703 int fd = open_library(name);
704 if (fd == -1) {
Elliott Hughes46882792012-08-03 16:49:39 -0700705 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800706 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700707 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800708
Elliott Hughes650be4e2013-03-05 18:47:58 -0800709 // Read the ELF header and load the segments.
710 ElfReader elf_reader(name, fd);
711 if (!elf_reader.Load()) {
Elliott Hughes46882792012-08-03 16:49:39 -0700712 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800713 }
714
Elliott Hughes650be4e2013-03-05 18:47:58 -0800715 const char* bname = strrchr(name, '/');
716 soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
717 if (si == NULL) {
Elliott Hughes46882792012-08-03 16:49:39 -0700718 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200719 }
Elliott Hughes650be4e2013-03-05 18:47:58 -0800720 si->base = elf_reader.load_start();
721 si->size = elf_reader.load_size();
722 si->load_bias = elf_reader.load_bias();
723 si->flags = 0;
724 si->entry = 0;
725 si->dynamic = NULL;
726 si->phnum = elf_reader.phdr_count();
727 si->phdr = elf_reader.loaded_phdr();
728 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800729}
730
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200731static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800732{
733 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700734 const char *bname;
735
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200736 // TODO: don't use basename only for determining libraries
737 // http://code.google.com/p/android/issues/detail?id=6670
738
739 bname = strrchr(name, '/');
740 bname = bname ? bname + 1 : name;
741
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800742 for (si = solist; si != NULL; si = si->next) {
743 if (!strcmp(bname, si->name)) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200744 return si;
745 }
746 }
747 return NULL;
748}
749
Elliott Hughesd23736e2012-11-01 15:16:56 -0700750static soinfo* find_library_internal(const char* name) {
751 if (name == NULL) {
752 return somain;
753 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200754
Elliott Hughesd23736e2012-11-01 15:16:56 -0700755 soinfo* si = find_loaded_library(name);
756 if (si != NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700757 if (si->flags & FLAG_LINKED) {
758 return si;
759 }
760 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
761 return NULL;
762 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800763
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700764 TRACE("[ '%s' has not been loaded yet. Locating...]", name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700765 si = load_library(name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800766 if (si == NULL) {
767 return NULL;
768 }
769
770 // At this point we know that whatever is loaded @ base is a valid ELF
771 // shared library whose segments are properly mapped in.
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700772 TRACE("[ init_library base=%p sz=0x%08x name='%s' ]",
773 reinterpret_cast<void*>(si->base), si->size, si->name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800774
775 if (!soinfo_link_image(si)) {
776 munmap(reinterpret_cast<void*>(si->base), si->size);
777 soinfo_free(si);
778 return NULL;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700779 }
780
781 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800782}
783
Elliott Hughesd23736e2012-11-01 15:16:56 -0700784static soinfo* find_library(const char* name) {
785 soinfo* si = find_library_internal(name);
786 if (si != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700787 si->ref_count++;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700788 }
789 return si;
790}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700791
Elliott Hughesd23736e2012-11-01 15:16:56 -0700792static int soinfo_unload(soinfo* si) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700793 if (si->ref_count == 1) {
794 TRACE("unloading '%s'", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700795 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800796
Elliott Hughesc6200592013-09-30 18:43:46 -0700797 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800798 if (d->d_tag == DT_NEEDED) {
799 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughes8147d3c2013-05-09 14:19:58 -0700800 TRACE("%s needs to unload %s", si->name, library_name);
801 soinfo_unload(find_loaded_library(library_name));
Elliott Hughesd23736e2012-11-01 15:16:56 -0700802 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800803 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700804
805 munmap(reinterpret_cast<void*>(si->base), si->size);
806 notify_gdb_of_unload(si);
807 soinfo_free(si);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700808 si->ref_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700809 } else {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700810 si->ref_count--;
Elliott Hughesc6200592013-09-30 18:43:46 -0700811 TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700812 }
813 return 0;
814}
815
Elliott Hughescade4c32012-12-20 14:42:14 -0800816void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
817 if (!get_AT_SECURE()) {
818 parse_LD_LIBRARY_PATH(ld_library_path);
819 }
820}
821
Elliott Hughese66190d2012-12-18 15:57:55 -0800822soinfo* do_dlopen(const char* name, int flags) {
823 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
824 DL_ERR("invalid flags to dlopen: %x", flags);
825 return NULL;
826 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700827 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
828 soinfo* si = find_library(name);
829 if (si != NULL) {
830 si->CallConstructors();
831 }
832 set_soinfo_pool_protection(PROT_READ);
833 return si;
834}
835
836int do_dlclose(soinfo* si) {
837 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
838 int result = soinfo_unload(si);
839 set_soinfo_pool_protection(PROT_READ);
840 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800841}
842
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700843#if defined(ANDROID_X86_64_LINKER)
844static int soinfo_relocate_a(soinfo* si, Elf_Rela* rela, unsigned count, soinfo* needed[]) {
845 Elf_Sym* symtab = si->symtab;
846 const char* strtab = si->strtab;
847 Elf_Sym* s;
848 Elf_Rela* start = rela;
849 soinfo* lsi;
850
851 for (size_t idx = 0; idx < count; ++idx, ++rela) {
852 unsigned type = ELF_R_TYPE(rela->r_info);
853 unsigned sym = ELF_R_SYM(rela->r_info);
854 Elf_Addr reloc = static_cast<Elf_Addr>(rela->r_offset + si->load_bias);
855 Elf_Addr sym_addr = 0;
856 char* sym_name = NULL;
857
858 DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
859 if (type == 0) { // R_*_NONE
860 continue;
861 }
862 if (sym != 0) {
863 sym_name = (char *)(strtab + symtab[sym].st_name);
864 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
865 if (s == NULL) {
866 // We only allow an undefined symbol if this is a weak reference...
867 s = &symtab[sym];
868 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
869 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
870 return -1;
871 }
872
873 /* IHI0044C AAELF 4.5.1.1:
874
875 Libraries are not searched to resolve weak references.
876 It is not an error for a weak reference to remain unsatisfied.
877
878 During linking, the value of an undefined weak reference is:
879 - Zero if the relocation type is absolute
880 - The address of the place if the relocation is pc-relative
881 - The address of nominal base address if the relocation
882 type is base-relative.
883 */
884
885 switch (type) {
886 case R_X86_64_JUMP_SLOT:
887 case R_X86_64_GLOB_DAT:
888 case R_X86_64_32:
889 case R_X86_64_RELATIVE:
890 // No need to do anything.
891 break;
892
893 case R_X86_64_PC32:
894 sym_addr = reloc;
895 break;
896
897 default:
898 DL_ERR("unknown weak reloc type %d @ %p (%d)", type, rela, (int) (rela - start));
899 return -1;
900 }
901 } else {
902 // We got a definition.
903 sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias);
904 }
905 count_relocation(kRelocSymbol);
906 } else {
907 s = NULL;
908 }
909
910 switch (type) {
911 case R_X86_64_JUMP_SLOT:
912 count_relocation(kRelocAbsolute);
913 MARK(rela->r_offset);
914 TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
915 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
916 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
917 break;
918 case R_X86_64_GLOB_DAT:
919 count_relocation(kRelocAbsolute);
920 MARK(rela->r_offset);
921 TRACE_TYPE(RELO, "RELO GLOB_DAT %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_RELATIVE:
926 count_relocation(kRelocRelative);
927 MARK(rela->r_offset);
928 if (sym) {
929 DL_ERR("odd RELATIVE form...");
930 return -1;
931 }
932 TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
933 static_cast<size_t>(si->base));
934 *reinterpret_cast<Elf_Addr*>(reloc) = si->base + rela->r_addend;
935 break;
936
937 case R_X86_64_32:
938 count_relocation(kRelocRelative);
939 MARK(rela->r_offset);
940 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
941 static_cast<size_t>(sym_addr), sym_name);
942 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
943 break;
944
945 case R_X86_64_PC32:
946 count_relocation(kRelocRelative);
947 MARK(rela->r_offset);
948 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
949 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
950 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
951 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend - reloc;
952 break;
953 default:
954 DL_ERR("unknown reloc type %d @ %p (%d)", type, rela, (int) (rela - start));
955 return -1;
956 }
957 }
958 return 0;
959}
960#else
Elliott Hughesc6200592013-09-30 18:43:46 -0700961static int soinfo_relocate(soinfo* si, Elf_Rel* rel, unsigned count,
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800962 soinfo* needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800963{
Elliott Hughesc6200592013-09-30 18:43:46 -0700964 Elf_Sym* symtab = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800965 const char* strtab = si->strtab;
Elliott Hughesc6200592013-09-30 18:43:46 -0700966 Elf_Sym* s;
967 Elf_Rel* start = rel;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800968 soinfo* lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800969
Elliott Hughes46882792012-08-03 16:49:39 -0700970 for (size_t idx = 0; idx < count; ++idx, ++rel) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700971 unsigned type = ELF_R_TYPE(rel->r_info);
972 // TODO: don't use unsigned for 'sym'. Use uint32_t or Elf_Addr instead.
973 unsigned sym = ELF_R_SYM(rel->r_info);
Elliott Hughesc6200592013-09-30 18:43:46 -0700974 Elf_Addr reloc = static_cast<Elf_Addr>(rel->r_offset + si->load_bias);
975 Elf_Addr sym_addr = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800976 char* sym_name = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800977
Elliott Hughesc6200592013-09-30 18:43:46 -0700978 DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700979 if (type == 0) { // R_*_NONE
980 continue;
981 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800982 if (sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -0700983 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200984 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800985 if (s == NULL) {
Doug Kwane8238072009-10-26 12:05:23 -0700986 /* We only allow an undefined symbol if this is a weak
987 reference.. */
988 s = &symtab[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700989 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700990 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -0700991 return -1;
992 }
993
994 /* IHI0044C AAELF 4.5.1.1:
995
996 Libraries are not searched to resolve weak references.
997 It is not an error for a weak reference to remain
998 unsatisfied.
999
1000 During linking, the value of an undefined weak reference is:
1001 - Zero if the relocation type is absolute
1002 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -07001003 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -07001004 type is base-relative.
1005 */
1006
1007 switch (type) {
1008#if defined(ANDROID_ARM_LINKER)
1009 case R_ARM_JUMP_SLOT:
1010 case R_ARM_GLOB_DAT:
1011 case R_ARM_ABS32:
1012 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -07001013#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001014 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001015 case R_386_GLOB_DAT:
1016 case R_386_32:
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001017 case R_386_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -07001018#endif /* ANDROID_*_LINKER */
1019 /* sym_addr was initialized to be zero above or relocation
1020 code below does not care about value of sym_addr.
1021 No need to do anything. */
1022 break;
1023
1024#if defined(ANDROID_X86_LINKER)
1025 case R_386_PC32:
1026 sym_addr = reloc;
1027 break;
1028#endif /* ANDROID_X86_LINKER */
1029
1030#if defined(ANDROID_ARM_LINKER)
1031 case R_ARM_COPY:
1032 /* Fall through. Can't really copy if weak symbol is
1033 not found in run-time. */
1034#endif /* ANDROID_ARM_LINKER */
1035 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001036 DL_ERR("unknown weak reloc type %d @ %p (%d)",
1037 type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -07001038 return -1;
1039 }
1040 } else {
1041 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001042#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001043 if ((base == 0) && (si->base != 0)) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001044 /* linking from libraries to main image is bad */
Elliott Hughes46882792012-08-03 16:49:39 -07001045 DL_ERR("cannot locate \"%s\"...",
1046 strtab + symtab[sym].st_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001047 return -1;
1048 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001049#endif
Elliott Hughesc6200592013-09-30 18:43:46 -07001050 sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001051 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001052 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001053 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001054 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001055 }
1056
1057/* TODO: This is ugly. Split up the relocations by arch into
1058 * different files.
1059 */
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001060 switch (type) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001061#if defined(ANDROID_ARM_LINKER)
1062 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#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001088 case R_386_JMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001089 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001090 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001091 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001092 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001093 break;
1094 case R_386_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001095 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001096 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001097 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001098 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001099 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001100#elif defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001101 case R_MIPS_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001102 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001103 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001104 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s",
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001105 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1106 if (s) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001107 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001108 } else {
Elliott Hughesc6200592013-09-30 18:43:46 -07001109 *reinterpret_cast<Elf_Addr*>(reloc) += si->base;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001110 }
1111 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001112#endif /* ANDROID_*_LINKER */
1113
1114#if defined(ANDROID_ARM_LINKER)
1115 case R_ARM_RELATIVE:
1116#elif defined(ANDROID_X86_LINKER)
1117 case R_386_RELATIVE:
1118#endif /* ANDROID_*_LINKER */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001119 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001120 MARK(rel->r_offset);
Elliott Hughes46882792012-08-03 16:49:39 -07001121 if (sym) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001122 DL_ERR("odd RELATIVE form...");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001123 return -1;
1124 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001125 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1126 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base));
Elliott Hughesc6200592013-09-30 18:43:46 -07001127 *reinterpret_cast<Elf_Addr*>(reloc) += si->base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001128 break;
1129
1130#if defined(ANDROID_X86_LINKER)
1131 case R_386_32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001132 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001133 MARK(rel->r_offset);
1134
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001135 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001136 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001137 break;
1138
1139 case R_386_PC32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001140 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001141 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001142 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001143 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001144 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr - reloc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001145 break;
1146#endif /* ANDROID_X86_LINKER */
1147
1148#ifdef ANDROID_ARM_LINKER
1149 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001150 if ((si->flags & FLAG_EXE) == 0) {
1151 /*
1152 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1153 *
1154 * Section 4.7.1.10 "Dynamic relocations"
1155 * R_ARM_COPY may only appear in executable objects where e_type is
1156 * set to ET_EXEC.
1157 *
1158 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1159 * We should explicitly disallow ET_DYN executables from having
1160 * R_ARM_COPY relocations.
1161 */
1162 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1163 return -1;
1164 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001165 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001166 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001167 TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s", reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001168 if (reloc == sym_addr) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001169 Elf_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001170
1171 if (src == NULL) {
1172 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1173 return -1;
1174 }
1175 if (lsi->has_DT_SYMBOLIC) {
1176 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1177 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1178 return -1;
1179 }
1180 if (s->st_size < src->st_size) {
1181 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1182 si->name, s->st_size, src->st_size);
1183 return -1;
1184 }
1185 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1186 } else {
1187 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001188 return -1;
1189 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001190 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001191#endif /* ANDROID_ARM_LINKER */
1192
1193 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001194 DL_ERR("unknown reloc type %d @ %p (%d)",
1195 type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001196 return -1;
1197 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001198 }
1199 return 0;
1200}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001201#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001202
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001203#ifdef ANDROID_MIPS_LINKER
Brian Carlstrom87c35852013-08-20 21:05:44 -07001204static bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
1205 unsigned* got = si->plt_got;
1206 if (got == NULL) {
1207 return true;
1208 }
1209 unsigned local_gotno = si->mips_local_gotno;
1210 unsigned gotsym = si->mips_gotsym;
1211 unsigned symtabno = si->mips_symtabno;
Elliott Hughesc6200592013-09-30 18:43:46 -07001212 Elf_Sym* symtab = si->symtab;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001213
1214 /*
1215 * got[0] is address of lazy resolver function
1216 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001217 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001218 * (should be _rtld_bind_start)
1219 * FIXME: maybe this should be in a separate routine
1220 */
1221
1222 if ((si->flags & FLAG_LINKER) == 0) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001223 size_t g = 0;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001224 got[g++] = 0xdeadbeef;
1225 if (got[g] & 0x80000000) {
1226 got[g++] = 0xdeadfeed;
1227 }
1228 /*
1229 * Relocate the local GOT entries need to be relocated
1230 */
1231 for (; g < local_gotno; g++) {
1232 got[g] += si->load_bias;
1233 }
1234 }
1235
1236 /* Now for the global GOT entries */
Elliott Hughesc6200592013-09-30 18:43:46 -07001237 Elf_Sym* sym = symtab + gotsym;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001238 got = si->plt_got + local_gotno;
Brian Carlstrom87c35852013-08-20 21:05:44 -07001239 for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001240 const char* sym_name;
Elliott Hughesc6200592013-09-30 18:43:46 -07001241 Elf_Sym* s;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001242 soinfo* lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001243
1244 /* This is an undefined reference... try to locate it */
1245 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001246 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001247 if (s == NULL) {
1248 /* We only allow an undefined symbol if this is a weak
1249 reference.. */
1250 s = &symtab[g];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001251 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001252 DL_ERR("cannot locate \"%s\"...", sym_name);
Brian Carlstrom87c35852013-08-20 21:05:44 -07001253 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001254 }
1255 *got = 0;
1256 }
1257 else {
1258 /* FIXME: is this sufficient?
1259 * For reference see NetBSD link loader
1260 * 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
1261 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001262 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001263 }
1264 }
Brian Carlstrom87c35852013-08-20 21:05:44 -07001265 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001266}
1267#endif
1268
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001269void soinfo::CallArray(const char* array_name UNUSED, linker_function_t* functions, size_t count, bool reverse) {
1270 if (functions == NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001271 return;
1272 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001273
Elliott Hughesc6200592013-09-30 18:43:46 -07001274 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001275
1276 int begin = reverse ? (count - 1) : 0;
1277 int end = reverse ? -1 : count;
1278 int step = reverse ? -1 : 1;
1279
1280 for (int i = begin; i != end; i += step) {
1281 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1282 CallFunction("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001283 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001284
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001285 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001286}
1287
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001288void soinfo::CallFunction(const char* function_name UNUSED, linker_function_t function) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001289 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001290 return;
1291 }
1292
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001293 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001294 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001295 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001296
1297 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1298 // are still writable. This happens with our debug malloc (see http://b/7941716).
1299 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001300}
1301
Elliott Hughesd23736e2012-11-01 15:16:56 -07001302void soinfo::CallPreInitConstructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001303 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1304 // but ignored in a shared library.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001305 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1306}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001307
Elliott Hughesd23736e2012-11-01 15:16:56 -07001308void soinfo::CallConstructors() {
1309 if (constructors_called) {
1310 return;
1311 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001312
Elliott Hughesd23736e2012-11-01 15:16:56 -07001313 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1314 // protect against recursive constructor calls. One simple example of constructor recursion
1315 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1316 // 1. The program depends on libc, so libc's constructor is called here.
1317 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1318 // 3. dlopen() calls the constructors on the newly created
1319 // soinfo for libc_malloc_debug_leak.so.
1320 // 4. The debug .so depends on libc, so CallConstructors is
1321 // called again with the libc soinfo. If it doesn't trigger the early-
1322 // out above, the libc constructor will be called again (recursively!).
1323 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001324
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001325 if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
1326 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001327 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001328 name, preinit_array_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001329 }
1330
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001331 if (dynamic != NULL) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001332 for (Elf_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001333 if (d->d_tag == DT_NEEDED) {
1334 const char* library_name = strtab + d->d_un.d_val;
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001335 TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
1336 find_loaded_library(library_name)->CallConstructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001337 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001338 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001339 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001340
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001341 TRACE("\"%s\": calling constructors", name);
1342
1343 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001344 CallFunction("DT_INIT", init_func);
1345 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001346}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001347
Elliott Hughesd23736e2012-11-01 15:16:56 -07001348void soinfo::CallDestructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001349 TRACE("\"%s\": calling destructors", name);
1350
1351 // DT_FINI_ARRAY must be parsed in reverse order.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001352 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001353
1354 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001355 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001356}
1357
1358/* Force any of the closed stdin, stdout and stderr to be associated with
1359 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001360static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001361 int dev_null, i, status;
1362 int return_value = 0;
1363
David 'Digit' Turner16084162012-06-12 16:25:37 +02001364 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001365 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001366 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001367 return -1;
1368 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001369 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001370
1371 /* If any of the stdio file descriptors is valid and not associated
1372 with /dev/null, dup /dev/null to it. */
1373 for (i = 0; i < 3; i++) {
1374 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001375 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001376 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001377 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001378
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001379 TRACE("[ Nullifying stdio file descriptor %d]", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001380 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001381
Elliott Hughes46882792012-08-03 16:49:39 -07001382 /* If file is opened, we are good. */
1383 if (status != -1) {
1384 continue;
1385 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001386
1387 /* The only error we allow is that the file descriptor does not
1388 exist, in which case we dup /dev/null to it. */
1389 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001390 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001391 return_value = -1;
1392 continue;
1393 }
1394
1395 /* Try dupping /dev/null to this stdio file descriptor and
1396 repeat if there is a signal. Note that any errors in closing
1397 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001398 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001399 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001400 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001401 return_value = -1;
1402 continue;
1403 }
1404 }
1405
1406 /* If /dev/null is not one of the stdio file descriptors, close it. */
1407 if (dev_null > 2) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001408 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001409 status = TEMP_FAILURE_RETRY(close(dev_null));
1410 if (status == -1) {
1411 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001412 return_value = -1;
1413 }
1414 }
1415
1416 return return_value;
1417}
1418
Elliott Hughes124fae92012-10-31 14:20:03 -07001419static bool soinfo_link_image(soinfo* si) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001420 /* "base" might wrap around UINT32_MAX. */
Elliott Hughesc6200592013-09-30 18:43:46 -07001421 Elf_Addr base = si->load_bias;
1422 const Elf_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001423 int phnum = si->phnum;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001424 bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001425
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001426 /* We can't debug anything until the linker is relocated */
1427 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001428 INFO("[ linking %s ]", si->name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001429 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001430 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001431
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001432 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001433 size_t dynamic_count;
Elliott Hughesc6200592013-09-30 18:43:46 -07001434 Elf_Word dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001435 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001436 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001437 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001438 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001439 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001440 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001441 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001442 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001443 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001444 DEBUG("dynamic = %p", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001445 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001446 }
1447
1448#ifdef ANDROID_ARM_LINKER
1449 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1450 &si->ARM_exidx, &si->ARM_exidx_count);
1451#endif
1452
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001453 // Extract useful information from dynamic section.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001454 uint32_t needed_count = 0;
Elliott Hughesc6200592013-09-30 18:43:46 -07001455 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001456 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1457 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1458 switch (d->d_tag) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001459 case DT_HASH:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001460 si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0];
1461 si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1];
1462 si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8);
1463 si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001464 break;
1465 case DT_STRTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001466 si->strtab = (const char *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001467 break;
1468 case DT_SYMTAB:
Elliott Hughesc6200592013-09-30 18:43:46 -07001469 si->symtab = (Elf_Sym *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001470 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001471#if !defined(ANDROID_X86_64_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001472 case DT_PLTREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001473 if (d->d_un.d_val != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001474 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1475 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001476 }
1477 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001478#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001479 case DT_JMPREL:
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001480#if defined(ANDROID_X86_64_LINKER)
1481 si->plt_rela = (Elf_Rela*) (base + d->d_un.d_ptr);
1482#else
Elliott Hughesc6200592013-09-30 18:43:46 -07001483 si->plt_rel = (Elf_Rel*) (base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001484#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001485 break;
1486 case DT_PLTRELSZ:
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001487#if defined(ANDROID_X86_64_LINKER)
1488 si->plt_rela_count = d->d_un.d_val / sizeof(Elf_Rela);
1489#else
Elliott Hughesc6200592013-09-30 18:43:46 -07001490 si->plt_rel_count = d->d_un.d_val / sizeof(Elf_Rel);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001491#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001492 break;
1493 case DT_PLTGOT:
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001494#if !defined(ANDROID_X86_64_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001495 /* Save this in case we decide to do lazy binding. We don't yet. */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001496 si->plt_got = (unsigned *)(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001497 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001498#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001499 case DT_DEBUG:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001500 // Set the DT_DEBUG entry to the address of _r_debug for GDB
Chris Dearmancf239052013-01-11 15:32:20 -08001501 // if the dynamic table is writable
Elliott Hughes99c32052013-01-14 09:56:21 -08001502 if ((dynamic_flags & PF_W) != 0) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001503 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
Elliott Hughes99c32052013-01-14 09:56:21 -08001504 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001505 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001506#if defined(ANDROID_X86_64_LINKER)
1507 case DT_RELA:
1508 si->rela = (Elf_Rela*) (base + d->d_un.d_ptr);
1509 break;
1510 case DT_RELASZ:
1511 si->rela_count = d->d_un.d_val / sizeof(Elf_Rela);
1512 break;
1513 case DT_REL:
1514 DL_ERR("unsupported DT_REL in \"%s\"", si->name);
1515 return false;
1516 case DT_RELSZ:
1517 DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name);
1518 return false;
1519#else
1520 case DT_REL:
1521 si->rel = (Elf_Rel*) (base + d->d_un.d_ptr);
1522 break;
1523 case DT_RELSZ:
1524 si->rel_count = d->d_un.d_val / sizeof(Elf_Rel);
1525 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001526 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001527 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1528 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001529#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001530 case DT_INIT:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001531 si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001532 DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001533 break;
1534 case DT_FINI:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001535 si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001536 DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001537 break;
1538 case DT_INIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001539 si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001540 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001541 break;
1542 case DT_INIT_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001543 si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001544 break;
1545 case DT_FINI_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001546 si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001547 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001548 break;
1549 case DT_FINI_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001550 si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001551 break;
1552 case DT_PREINIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001553 si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001554 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001555 break;
1556 case DT_PREINIT_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001557 si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001558 break;
1559 case DT_TEXTREL:
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001560 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001561 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001562 case DT_SYMBOLIC:
1563 si->has_DT_SYMBOLIC = true;
1564 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001565 case DT_NEEDED:
1566 ++needed_count;
1567 break;
1568#if defined DT_FLAGS
1569 // TODO: why is DT_FLAGS not defined?
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001570 case DT_FLAGS:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001571 if (d->d_un.d_val & DF_TEXTREL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001572 si->has_text_relocations = true;
1573 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001574 if (d->d_un.d_val & DF_SYMBOLIC) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001575 si->has_DT_SYMBOLIC = true;
1576 }
1577 break;
1578#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001579#if defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001580 case DT_STRSZ:
1581 case DT_SYMENT:
1582 case DT_RELENT:
1583 break;
1584 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001585 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001586 {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001587 r_debug** dp = (r_debug**) d->d_un.d_ptr;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001588 *dp = &_r_debug;
1589 }
1590 break;
1591 case DT_MIPS_RLD_VERSION:
1592 case DT_MIPS_FLAGS:
1593 case DT_MIPS_BASE_ADDRESS:
1594 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001595 break;
1596
1597 case DT_MIPS_SYMTABNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001598 si->mips_symtabno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001599 break;
1600
1601 case DT_MIPS_LOCAL_GOTNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001602 si->mips_local_gotno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001603 break;
1604
1605 case DT_MIPS_GOTSYM:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001606 si->mips_gotsym = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001607 break;
1608
1609 default:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001610 DEBUG("Unused DT entry: type 0x%08x arg 0x%08x", d->d_tag, d->d_un.d_val);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001611 break;
1612#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001613 }
1614 }
1615
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001616 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
1617 reinterpret_cast<void*>(si->base), si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001618
Elliott Hughes124fae92012-10-31 14:20:03 -07001619 // Sanity checks.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001620 if (relocating_linker && needed_count != 0) {
1621 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1622 return false;
1623 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001624 if (si->nbucket == 0) {
1625 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1626 return false;
1627 }
1628 if (si->strtab == 0) {
1629 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1630 return false;
1631 }
1632 if (si->symtab == 0) {
1633 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1634 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001635 }
1636
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001637 // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001638 if (si->flags & FLAG_EXE) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001639 memset(gLdPreloads, 0, sizeof(gLdPreloads));
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001640 size_t preload_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001641 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1642 soinfo* lsi = find_library(gLdPreloadNames[i]);
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001643 if (lsi != NULL) {
1644 gLdPreloads[preload_count++] = lsi;
1645 } else {
1646 // As with glibc, failure to load an LD_PRELOAD library is just a warning.
1647 DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
1648 gLdPreloadNames[i], si->name, linker_get_error_buffer());
Matt Fischer4fd42c12009-12-31 12:09:10 -06001649 }
Matt Fischer4fd42c12009-12-31 12:09:10 -06001650 }
1651 }
1652
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001653 soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*));
1654 soinfo** pneeded = needed;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001655
Elliott Hughesc6200592013-09-30 18:43:46 -07001656 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001657 if (d->d_tag == DT_NEEDED) {
1658 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001659 DEBUG("%s needs %s", si->name, library_name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001660 soinfo* lsi = find_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001661 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001662 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001663 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001664 library_name, si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001665 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001666 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001667 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001668 }
1669 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001670 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001671
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001672 if (si->has_text_relocations) {
1673 /* Unprotect the segments, i.e. make them writable, to allow
1674 * text relocations to work properly. We will later call
1675 * phdr_table_protect_segments() after all of them are applied
1676 * and all constructors are run.
1677 */
Nick Kralevichc9084422013-06-21 12:31:33 -07001678 DL_WARN("%s has text relocations. This is wasting memory and is "
1679 "a security risk. Please fix.", si->name);
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001680 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1681 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1682 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001683 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001684 }
1685 }
1686
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001687#if defined(ANDROID_X86_64_LINKER)
1688 if (si->plt_rela != NULL) {
1689 DEBUG("[ relocating %s plt ]\n", si->name );
1690 if (soinfo_relocate_a(si, si->plt_rela, si->plt_rela_count, needed)) {
1691 return false;
1692 }
1693 }
1694 if (si->rela != NULL) {
1695 DEBUG("[ relocating %s ]\n", si->name );
1696 if (soinfo_relocate_a(si, si->rela, si->rela_count, needed)) {
1697 return false;
1698 }
1699 }
1700#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001701 if (si->plt_rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001702 DEBUG("[ relocating %s plt ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001703 if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001704 return false;
1705 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001706 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001707 if (si->rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001708 DEBUG("[ relocating %s ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001709 if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001710 return false;
1711 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001712 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001713#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001714
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001715#ifdef ANDROID_MIPS_LINKER
Brian Carlstrom87c35852013-08-20 21:05:44 -07001716 if (!mips_relocate_got(si, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001717 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001718 }
1719#endif
1720
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001721 si->flags |= FLAG_LINKED;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001722 DEBUG("[ finished linking %s ]", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001723
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001724 if (si->has_text_relocations) {
1725 /* All relocations are done, we can protect our segments back to
1726 * read-only. */
1727 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1728 DL_ERR("can't protect segments for \"%s\": %s",
1729 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001730 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001731 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001732 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001733
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001734 /* We can also turn on GNU RELRO protection */
1735 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001736 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1737 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001738 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001739 }
1740
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001741 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001742 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001743}
1744
Nick Kralevich468319c2011-11-11 15:53:17 -08001745/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001746 * This function add vdso to internal dso list.
1747 * It helps to stack unwinding through signal handlers.
1748 * Also, it makes bionic more like glibc.
1749 */
1750static void add_vdso(KernelArgumentBlock& args UNUSED) {
1751#ifdef AT_SYSINFO_EHDR
Elliott Hughesc6200592013-09-30 18:43:46 -07001752 Elf_Ehdr* ehdr_vdso = reinterpret_cast<Elf_Ehdr*>(args.getauxval(AT_SYSINFO_EHDR));
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001753
1754 soinfo* si = soinfo_alloc("[vdso]");
Elliott Hughesc6200592013-09-30 18:43:46 -07001755 si->phdr = reinterpret_cast<Elf_Phdr*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001756 si->phnum = ehdr_vdso->e_phnum;
1757 si->link_map.l_name = si->name;
1758 for (size_t i = 0; i < si->phnum; ++i) {
1759 if (si->phdr[i].p_type == PT_LOAD) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001760 si->link_map.l_addr = reinterpret_cast<Elf_Addr>(ehdr_vdso) - si->phdr[i].p_vaddr;
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001761 break;
1762 }
1763 }
1764#endif
1765}
1766
1767/*
Nick Kralevich468319c2011-11-11 15:53:17 -08001768 * This code is called after the linker has linked itself and
1769 * fixed it's own GOT. It is safe to make references to externs
1770 * and other non-local data at this point.
1771 */
Elliott Hughesc6200592013-09-30 18:43:46 -07001772static Elf_Addr __linker_init_post_relocation(KernelArgumentBlock& args, Elf_Addr linker_base) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001773 /* NOTE: we store the args pointer on a special location
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001774 * of the temporary TLS area in order to pass it to
1775 * the C Library's runtime initializer.
1776 *
1777 * The initializer must clear the slot and reset the TLS
1778 * to point to a different location to ensure that no other
1779 * shared library constructor can access it.
1780 */
Elliott Hughesd3920b32013-02-07 18:39:34 -08001781 __libc_init_tls(args);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001782
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001783#if TIMING
1784 struct timeval t0, t1;
1785 gettimeofday(&t0, 0);
1786#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001787
Elliott Hughes18a206c2012-10-29 17:37:13 -07001788 // Initialize environment functions, and get to the ELF aux vectors table.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001789 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001790
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07001791 // If this is a setuid/setgid program, close the security hole described in
1792 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1793 if (get_AT_SECURE()) {
1794 nullify_closed_stdio();
1795 }
1796
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001797 debuggerd_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001798
Elliott Hughes18a206c2012-10-29 17:37:13 -07001799 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001800 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1801 if (LD_DEBUG != NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001802 gLdDebugVerbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001803 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001804
Elliott Hughes18a206c2012-10-29 17:37:13 -07001805 // Normally, these are cleaned by linker_env_init, but the test
1806 // doesn't cost us anything.
1807 const char* ldpath_env = NULL;
1808 const char* ldpreload_env = NULL;
1809 if (!get_AT_SECURE()) {
1810 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1811 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001812 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001813
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001814 INFO("[ android linker & debugger ]");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001815
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001816 soinfo* si = soinfo_alloc(args.argv[0]);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001817 if (si == NULL) {
1818 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001819 }
1820
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001821 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001822 si->flags |= FLAG_EXE;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001823 link_map_t* map = &(si->link_map);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001824
1825 map->l_addr = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001826 map->l_name = args.argv[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001827 map->l_prev = NULL;
1828 map->l_next = NULL;
1829
1830 _r_debug.r_map = map;
1831 r_debug_tail = map;
1832
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001833 /* gdb expects the linker to be in the debug shared object list.
1834 * Without this, gdb has trouble locating the linker's ".text"
1835 * and ".plt" sections. Gdb could also potentially use this to
1836 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1837 * Don't use soinfo_alloc(), because the linker shouldn't
1838 * be on the soinfo list.
Ben Cheng06f0e742012-08-10 16:07:02 -07001839 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001840 {
1841 static soinfo linker_soinfo;
1842 strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
1843 linker_soinfo.flags = 0;
1844 linker_soinfo.base = linker_base;
1845
1846 /*
1847 * Set the dynamic field in the link map otherwise gdb will complain with
1848 * the following:
1849 * warning: .dynamic section for "/system/bin/linker" is not at the
1850 * expected address (wrong library or version mismatch?)
1851 */
Elliott Hughesc6200592013-09-30 18:43:46 -07001852 Elf_Ehdr *elf_hdr = (Elf_Ehdr *) linker_base;
1853 Elf_Phdr *phdr = (Elf_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001854 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1855 &linker_soinfo.dynamic, NULL, NULL);
1856 insert_soinfo_into_debug_map(&linker_soinfo);
1857 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001858
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001859 // Extract information passed from the kernel.
Elliott Hughesc6200592013-09-30 18:43:46 -07001860 si->phdr = reinterpret_cast<Elf_Phdr*>(args.getauxval(AT_PHDR));
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001861 si->phnum = args.getauxval(AT_PHNUM);
1862 si->entry = args.getauxval(AT_ENTRY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001863
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001864 /* Compute the value of si->base. We can't rely on the fact that
1865 * the first entry is the PHDR because this will not be true
1866 * for certain executables (e.g. some in the NDK unit test suite)
1867 */
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001868 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001869 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001870 si->load_bias = 0;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001871 for (size_t i = 0; i < si->phnum; ++i) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001872 if (si->phdr[i].p_type == PT_PHDR) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001873 si->load_bias = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_vaddr;
1874 si->base = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_offset;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001875 break;
1876 }
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001877 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001878 si->dynamic = NULL;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001879 si->ref_count = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001880
Elliott Hughes46882792012-08-03 16:49:39 -07001881 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1882 parse_LD_LIBRARY_PATH(ldpath_env);
1883 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001884
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001885 somain = si;
1886
Elliott Hughes124fae92012-10-31 14:20:03 -07001887 if (!soinfo_link_image(si)) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001888 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
Elliott Hughes18a206c2012-10-29 17:37:13 -07001889 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001890 }
1891
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001892 add_vdso(args);
1893
Elliott Hughesd23736e2012-11-01 15:16:56 -07001894 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001895
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001896 for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
1897 gLdPreloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08001898 }
1899
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001900 /* After the link_image, the si->load_bias is initialized.
1901 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1902 * We need to update this value for so exe here. So Unwind_Backtrace
1903 * for some arch like x86 could work correctly within so exe.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08001904 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08001905 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001906 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001907
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001908#if TIMING
1909 gettimeofday(&t1,NULL);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001910 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001911 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1912 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1913 ));
1914#endif
1915#if STATS
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001916 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07001917 linker_stats.count[kRelocAbsolute],
1918 linker_stats.count[kRelocRelative],
1919 linker_stats.count[kRelocCopy],
1920 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001921#endif
1922#if COUNT_PAGES
1923 {
1924 unsigned n;
1925 unsigned i;
1926 unsigned count = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001927 for (n = 0; n < 4096; n++) {
1928 if (bitmask[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001929 unsigned x = bitmask[n];
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001930 for (i = 0; i < 8; i++) {
1931 if (x & 1) {
1932 count++;
1933 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001934 x >>= 1;
1935 }
1936 }
1937 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001938 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001939 }
1940#endif
1941
1942#if TIMING || STATS || COUNT_PAGES
1943 fflush(stdout);
1944#endif
1945
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001946 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001947 return si->entry;
1948}
Nick Kralevich468319c2011-11-11 15:53:17 -08001949
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001950/* Compute the load-bias of an existing executable. This shall only
1951 * be used to compute the load bias of an executable or shared library
1952 * that was loaded by the kernel itself.
1953 *
1954 * Input:
1955 * elf -> address of ELF header, assumed to be at the start of the file.
1956 * Return:
1957 * load bias, i.e. add the value of any p_vaddr in the file to get
1958 * the corresponding address in memory.
1959 */
Elliott Hughesc6200592013-09-30 18:43:46 -07001960static Elf_Addr get_elf_exec_load_bias(const Elf_Ehdr* elf) {
1961 Elf_Addr offset = elf->e_phoff;
1962 const Elf_Phdr* phdr_table = (const Elf_Phdr*)((char*)elf + offset);
1963 const Elf_Phdr* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001964
Elliott Hughesc6200592013-09-30 18:43:46 -07001965 for (const Elf_Phdr* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001966 if (phdr->p_type == PT_LOAD) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001967 return reinterpret_cast<Elf_Addr>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001968 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001969 }
1970 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001971}
1972
Nick Kralevich468319c2011-11-11 15:53:17 -08001973/*
1974 * This is the entry point for the linker, called from begin.S. This
1975 * method is responsible for fixing the linker's own relocations, and
1976 * then calling __linker_init_post_relocation().
1977 *
1978 * Because this method is called before the linker has fixed it's own
1979 * relocations, any attempt to reference an extern variable, extern
1980 * function, or other GOT reference will generate a segfault.
1981 */
Elliott Hughesc6200592013-09-30 18:43:46 -07001982extern "C" Elf_Addr __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001983 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08001984
Elliott Hughesc6200592013-09-30 18:43:46 -07001985 Elf_Addr linker_addr = args.getauxval(AT_BASE);
Nick Kralevich468319c2011-11-11 15:53:17 -08001986
Elliott Hughesc6200592013-09-30 18:43:46 -07001987 Elf_Ehdr* elf_hdr = reinterpret_cast<Elf_Ehdr*>(linker_addr);
1988 Elf_Phdr* phdr = (Elf_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08001989
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001990 soinfo linker_so;
1991 memset(&linker_so, 0, sizeof(soinfo));
Nick Kralevich468319c2011-11-11 15:53:17 -08001992
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001993 linker_so.base = linker_addr;
1994 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
1995 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001996 linker_so.dynamic = NULL;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001997 linker_so.phdr = phdr;
1998 linker_so.phnum = elf_hdr->e_phnum;
1999 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07002000
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002001 if (!soinfo_link_image(&linker_so)) {
2002 // It would be nice to print an error message, but if the linker
2003 // can't link itself, there's no guarantee that we'll be able to
2004 // call write() (because it involves a GOT reference).
2005 //
2006 // This situation should never occur unless the linker itself
2007 // is corrupt.
2008 exit(EXIT_FAILURE);
2009 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002010
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002011 // We have successfully fixed our own relocations. It's safe to run
2012 // the main part of the linker now.
Elliott Hughes0d787c12013-04-04 13:46:46 -07002013 args.abort_message_ptr = &gAbortMessage;
Elliott Hughesc6200592013-09-30 18:43:46 -07002014 Elf_Addr start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002015
2016 set_soinfo_pool_protection(PROT_READ);
2017
2018 // Return the address that the calling assembly stub should jump to.
2019 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002020}