blob: 502035bd53e18d3ba7d6a30eeb66baf530ebb27f [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 Hughes1e980b62013-01-17 18:36:06 -080044#include <private/debug_format.h>
Elliott Hughes46882792012-08-03 16:49:39 -070045#include <private/logd.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070046#include <private/ScopedPthreadMutexLocker.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047
48#include "linker.h"
49#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010050#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020051#include "linker_phdr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
David Bartleybc3a5c22009-06-02 18:27:28 -070053/* Assume average path length of 64 and max 8 paths */
54#define LDPATH_BUFSIZE 512
55#define LDPATH_MAX 8
56
Matt Fischer4fd42c12009-12-31 12:09:10 -060057#define LDPRELOAD_BUFSIZE 512
58#define LDPRELOAD_MAX 8
59
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
61 *
62 * Do NOT use malloc() and friends or pthread_*() code here.
63 * Don't use printf() either; it's caused mysterious memory
64 * corruption in the past.
65 * The linker runs before we bring up libc and it's easiest
66 * to make sure it does not depend on any complex libc features
67 *
68 * open issues / todo:
69 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070 * - are we doing everything we should for ARM_COPY relocations?
71 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072 * - after linking, set as much stuff as possible to READONLY
73 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070074 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075
Elliott Hughes124fae92012-10-31 14:20:03 -070076static bool soinfo_link_image(soinfo* si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077
Magnus Malmbornba98d922012-09-12 13:00:55 +020078// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
79// maps, each a single page in size. The pages are broken up into as many struct soinfo
80// objects as will fit, and they're all threaded together on a free list.
81#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
82struct soinfo_pool_t {
83 soinfo_pool_t* next;
84 soinfo info[SOINFO_PER_POOL];
85};
86static struct soinfo_pool_t* gSoInfoPools = NULL;
87static soinfo* gSoInfoFreeList = NULL;
88
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089static soinfo *solist = &libdl_info;
90static soinfo *sonext = &libdl_info;
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070091static soinfo *somain; /* main process, always the one after libdl_info */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092
Elliott Hughes124fae92012-10-31 14:20:03 -070093static const char* const gSoPaths[] = {
94 "/vendor/lib",
95 "/system/lib",
96 NULL
97};
David Bartleybc3a5c22009-06-02 18:27:28 -070098
Elliott Hughes124fae92012-10-31 14:20:03 -070099static char gLdPathsBuffer[LDPATH_BUFSIZE];
100static const char* gLdPaths[LDPATH_MAX + 1];
101
102static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
103static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600104
105static soinfo *preloads[LDPRELOAD_MAX + 1];
106
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700107static int debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108
Elliott Hughesbedfe382012-08-14 14:07:59 -0700109enum RelocationKind {
110 kRelocAbsolute = 0,
111 kRelocRelative,
112 kRelocCopy,
113 kRelocSymbol,
114 kRelocMax
115};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100116
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700118struct linker_stats_t {
119 int count[kRelocMax];
120};
121
122static linker_stats_t linker_stats;
123
124static void count_relocation(RelocationKind kind) {
125 ++linker_stats.count[kind];
126}
127#else
128static void count_relocation(RelocationKind) {
129}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130#endif
131
132#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700133static unsigned bitmask[4096];
134#define MARK(offset) \
135 do { \
136 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
137 } while(0)
138#else
139#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800140#endif
141
Elliott Hughes46882792012-08-03 16:49:39 -0700142// You shouldn't try to call memory-allocating functions in the dynamic linker.
143// Guard against the most obvious ones.
144#define DISALLOW_ALLOCATION(return_type, name, ...) \
145 return_type name __VA_ARGS__ \
146 { \
147 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
148 __libc_android_log_write(ANDROID_LOG_FATAL, "linker", msg); \
Chris Dearman20a24402012-10-31 05:39:27 -0700149 write(2, msg, strlen(msg)); \
Elliott Hughes46882792012-08-03 16:49:39 -0700150 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700151 }
Elliott Hughes46882792012-08-03 16:49:39 -0700152#define UNUSED __attribute__((unused))
153DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
154DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
155DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
156DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
Dima Zavin2e855792009-05-20 18:28:09 -0700157
Dima Zavin03531952009-05-29 17:30:25 -0700158static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700159static char __linker_dl_err_buf[768];
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700160#define DL_ERR(fmt, x...) \
161 do { \
Elliott Hughes1e980b62013-01-17 18:36:06 -0800162 __libc_format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), fmt, ##x); \
Elliott Hughes9c94fc92012-11-05 09:11:43 -0800163 /* If LD_DEBUG is set high enough, send every dlerror(3) message to the log. */ \
164 DEBUG(fmt "\n", ##x); \
Dima Zavin2e855792009-05-20 18:28:09 -0700165 } while(0)
166
Elliott Hughes5419b942012-10-16 15:54:46 -0700167const char* linker_get_error() {
168 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700169}
170
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800171/*
172 * This function is an empty stub where GDB locates a breakpoint to get notified
173 * about linker activity.
174 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700175extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176
Elliott Hughesbedfe382012-08-14 14:07:59 -0700177static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178 RT_CONSISTENT, 0};
Elliott Hughesbedfe382012-08-14 14:07:59 -0700179static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180
Elliott Hughes3b297c42012-10-11 16:08:51 -0700181static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182
Elliott Hughesbedfe382012-08-14 14:07:59 -0700183static void insert_soinfo_into_debug_map(soinfo * info) {
184 // Copy the necessary fields into the debug structure.
185 link_map* map = &(info->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186 map->l_addr = info->base;
187 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800188 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189
190 /* Stick the new library at the end of the list.
191 * gdb tends to care more about libc than it does
192 * about leaf libraries, and ordering it this way
193 * reduces the back-and-forth over the wire.
194 */
195 if (r_debug_tail) {
196 r_debug_tail->l_next = map;
197 map->l_prev = r_debug_tail;
198 map->l_next = 0;
199 } else {
200 _r_debug.r_map = map;
201 map->l_prev = 0;
202 map->l_next = 0;
203 }
204 r_debug_tail = map;
205}
206
Elliott Hughesbedfe382012-08-14 14:07:59 -0700207static void remove_soinfo_from_debug_map(soinfo* info) {
208 link_map* map = &(info->linkmap);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700209
Elliott Hughesbedfe382012-08-14 14:07:59 -0700210 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700211 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700212 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700213
Elliott Hughesbedfe382012-08-14 14:07:59 -0700214 if (map->l_prev) {
215 map->l_prev->l_next = map->l_next;
216 }
217 if (map->l_next) {
218 map->l_next->l_prev = map->l_prev;
219 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700220}
221
Elliott Hughesbedfe382012-08-14 14:07:59 -0700222static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223 if (info->flags & FLAG_EXE) {
224 // GDB already knows about the main executable
225 return;
226 }
227
Elliott Hughes3b297c42012-10-11 16:08:51 -0700228 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229
230 _r_debug.r_state = RT_ADD;
231 rtld_db_dlactivity();
232
233 insert_soinfo_into_debug_map(info);
234
235 _r_debug.r_state = RT_CONSISTENT;
236 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700237}
238
Elliott Hughesbedfe382012-08-14 14:07:59 -0700239static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700240 if (info->flags & FLAG_EXE) {
241 // GDB already knows about the main executable
242 return;
243 }
244
Elliott Hughes3b297c42012-10-11 16:08:51 -0700245 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700246
247 _r_debug.r_state = RT_DELETE;
248 rtld_db_dlactivity();
249
250 remove_soinfo_from_debug_map(info);
251
252 _r_debug.r_state = RT_CONSISTENT;
253 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800254}
255
Elliott Hughes18a206c2012-10-29 17:37:13 -0700256void notify_gdb_of_libraries() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800257 _r_debug.r_state = RT_ADD;
258 rtld_db_dlactivity();
259 _r_debug.r_state = RT_CONSISTENT;
260 rtld_db_dlactivity();
261}
262
Magnus Malmbornba98d922012-09-12 13:00:55 +0200263static bool ensure_free_list_non_empty() {
264 if (gSoInfoFreeList != NULL) {
265 return true;
266 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800267
Magnus Malmbornba98d922012-09-12 13:00:55 +0200268 // Allocate a new pool.
269 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
270 PROT_READ|PROT_WRITE,
271 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
272 if (pool == MAP_FAILED) {
273 return false;
274 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800275
Magnus Malmbornba98d922012-09-12 13:00:55 +0200276 // Add the pool to our list of pools.
277 pool->next = gSoInfoPools;
278 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800279
Magnus Malmbornba98d922012-09-12 13:00:55 +0200280 // Chain the entries in the new pool onto the free list.
281 gSoInfoFreeList = &pool->info[0];
282 soinfo* next = NULL;
283 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
284 pool->info[i].next = next;
285 next = &pool->info[i];
286 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800287
Magnus Malmbornba98d922012-09-12 13:00:55 +0200288 return true;
289}
290
Elliott Hughesd23736e2012-11-01 15:16:56 -0700291static void set_soinfo_pool_protection(int protection) {
292 for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
293 if (mprotect(p, sizeof(*p), protection) == -1) {
294 abort(); // Can't happen.
295 }
296 }
297}
298
Magnus Malmbornba98d922012-09-12 13:00:55 +0200299static soinfo* soinfo_alloc(const char* name) {
300 if (strlen(name) >= SOINFO_NAME_LEN) {
301 DL_ERR("library name \"%s\" too long", name);
302 return NULL;
303 }
304
305 if (!ensure_free_list_non_empty()) {
306 DL_ERR("out of memory when loading \"%s\"", name);
307 return NULL;
308 }
309
310 // Take the head element off the free list.
311 soinfo* si = gSoInfoFreeList;
312 gSoInfoFreeList = gSoInfoFreeList->next;
313
314 // Initialize the new element.
315 memset(si, 0, sizeof(soinfo));
316 strlcpy(si->name, name, sizeof(si->name));
317 sonext->next = si;
318 sonext = si;
319
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700320 TRACE("name %s: allocated soinfo @ %p\n", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200321 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800322}
323
Elliott Hughes46882792012-08-03 16:49:39 -0700324static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800325{
Elliott Hughes46882792012-08-03 16:49:39 -0700326 if (si == NULL) {
327 return;
328 }
329
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800330 soinfo *prev = NULL, *trav;
331
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700332 TRACE("name %s: freeing soinfo @ %p\n", si->name, si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333
334 for(trav = solist; trav != NULL; trav = trav->next){
335 if (trav == si)
336 break;
337 prev = trav;
338 }
339 if (trav == NULL) {
340 /* si was not ni solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700341 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800342 return;
343 }
344
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100345 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800346 always the static libdl_info.
347 */
348 prev->next = si->next;
349 if (si == sonext) sonext = prev;
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
Nick Kralevich468319c2011-11-11 15:53:17 -0800407 for (si = solist; si != 0; si = si->next){
408 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
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700417#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
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;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428 dl_info.dlpi_addr = si->linkmap.l_addr;
429 dl_info.dlpi_name = si->linkmap.l_name;
430 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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800440#endif
441
David 'Digit' Turner16084162012-06-12 16:25:37 +0200442static Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800443{
444 Elf32_Sym *s;
445 Elf32_Sym *symtab = si->symtab;
446 const char *strtab = si->strtab;
447 unsigned n;
448
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700449 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800450 name, si->name, si->base, hash, hash % si->nbucket);
451 n = hash % si->nbucket;
452
453 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
454 s = symtab + n;
455 if(strcmp(strtab + s->st_name, name)) continue;
456
Doug Kwane8238072009-10-26 12:05:23 -0700457 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800458 switch(ELF32_ST_BIND(s->st_info)){
459 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700460 case STB_WEAK:
Robin Burchell439fa8e2012-07-05 09:21:07 +0200461 if(s->st_shndx == SHN_UNDEF)
462 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800463
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700464 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800465 name, si->name, s->st_value, s->st_size);
466 return s;
467 }
468 }
469
Doug Kwan94304352009-10-23 18:11:40 -0700470 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800471}
472
473static unsigned elfhash(const char *_name)
474{
475 const unsigned char *name = (const unsigned char *) _name;
476 unsigned h = 0, g;
477
478 while(*name) {
479 h = (h << 4) + *name++;
480 g = h & 0xf0000000;
481 h ^= g;
482 h ^= g >> 24;
483 }
484 return h;
485}
486
487static Elf32_Sym *
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200488soinfo_do_lookup(soinfo *si, const char *name, soinfo **lsi,
489 soinfo *needed[])
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700490{
Doug Kwan94304352009-10-23 18:11:40 -0700491 unsigned elf_hash = elfhash(name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700492 Elf32_Sym *s = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600493 int i;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700494
Pavel Chupinc77c4342012-10-31 13:55:51 +0400495 if (si != NULL && somain != NULL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200496
497 /*
Pavel Chupinc77c4342012-10-31 13:55:51 +0400498 * Local scope is executable scope. Just start looking into it right away
499 * for the shortcut.
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200500 */
501
Pavel Chupinc77c4342012-10-31 13:55:51 +0400502 if (si == somain) {
503 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200504 if (s != NULL) {
Pavel Chupinc77c4342012-10-31 13:55:51 +0400505 *lsi = si;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200506 goto done;
507 }
Pavel Chupinc77c4342012-10-31 13:55:51 +0400508 } else {
509 /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200510
Pavel Chupinc77c4342012-10-31 13:55:51 +0400511 /*
512 * If this object was built with symbolic relocations disabled, the
513 * first place to look to resolve external references is the main
514 * executable.
515 */
Nick Kralevich468319c2011-11-11 15:53:17 -0800516
Pavel Chupinc77c4342012-10-31 13:55:51 +0400517 if (!si->has_DT_SYMBOLIC) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700518 DEBUG("%s: looking up %s in executable %s\n",
519 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400520 s = soinfo_elf_lookup(somain, elf_hash, name);
521 if (s != NULL) {
522 *lsi = somain;
523 goto done;
524 }
525 }
526
527 /* Look for symbols in the local scope (the object who is
528 * searching). This happens with C++ templates on i386 for some
529 * reason.
530 *
531 * Notes on weak symbols:
532 * The ELF specs are ambiguous about treatment of weak definitions in
533 * dynamic linking. Some systems return the first definition found
534 * and some the first non-weak definition. This is system dependent.
535 * Here we return the first definition found for simplicity. */
536
537 s = soinfo_elf_lookup(si, elf_hash, name);
538 if (s != NULL) {
539 *lsi = si;
540 goto done;
541 }
542
543 /*
544 * If this object was built with -Bsymbolic and symbol is not found
545 * in the local scope, try to find the symbol in the main executable.
546 */
547
548 if (si->has_DT_SYMBOLIC) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700549 DEBUG("%s: looking up %s in executable %s after local scope\n",
550 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400551 s = soinfo_elf_lookup(somain, elf_hash, name);
552 if (s != NULL) {
553 *lsi = somain;
554 goto done;
555 }
556 }
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200557 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700558 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700559
Matt Fischer4fd42c12009-12-31 12:09:10 -0600560 /* Next, look for it in the preloads list */
561 for(i = 0; preloads[i] != NULL; i++) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200562 s = soinfo_elf_lookup(preloads[i], elf_hash, name);
563 if(s != NULL) {
564 *lsi = preloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600565 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200566 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600567 }
568
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200569 for(i = 0; needed[i] != NULL; i++) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700570 DEBUG("%s: looking up %s in %s\n",
571 si->name, name, needed[i]->name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200572 s = soinfo_elf_lookup(needed[i], elf_hash, name);
573 if (s != NULL) {
574 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200575 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200576 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700577 }
578
579done:
580 if(s != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700581 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = 0x%08x, "
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200582 "found in %s, base = 0x%08x, load bias = 0x%08x\n",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700583 si->name, name, s->st_value,
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200584 (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700585 return s;
586 }
587
Doug Kwan94304352009-10-23 18:11:40 -0700588 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700589}
590
591/* This is used by dl_sym(). It performs symbol lookup only within the
592 specified soinfo object and not in any of its dependencies.
593 */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200594Elf32_Sym *soinfo_lookup(soinfo *si, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800595{
David 'Digit' Turner16084162012-06-12 16:25:37 +0200596 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800597}
598
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700599/* This is used by dl_sym(). It performs a global symbol lookup.
600 */
Elliott Hughescade4c32012-12-20 14:42:14 -0800601Elf32_Sym* lookup(const char* name, soinfo** found, soinfo* start) {
602 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
608 Elf32_Sym* s = NULL;
609 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) {
618 TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, found->base = 0x%08x\n",
619 name, s->st_value, (*found)->base);
620 }
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
Mathias Agopianbda5da02011-09-27 22:30:19 -0700625soinfo *find_containing_library(const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600626{
627 soinfo *si;
628
629 for(si = solist; si != NULL; si = si->next)
630 {
631 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
632 return si;
633 }
634 }
635
636 return NULL;
637}
638
David 'Digit' Turner16084162012-06-12 16:25:37 +0200639Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600640{
641 unsigned int i;
642 unsigned soaddr = (unsigned)addr - si->base;
643
644 /* Search the library's symbol table for any defined symbol which
645 * contains this address */
646 for(i=0; i<si->nchain; i++) {
647 Elf32_Sym *sym = &si->symtab[i];
648
649 if(sym->st_shndx != SHN_UNDEF &&
650 soaddr >= sym->st_value &&
651 soaddr < sym->st_value + sym->st_size) {
652 return sym;
653 }
654 }
655
656 return NULL;
657}
658
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800659#if 0
660static void dump(soinfo *si)
661{
662 Elf32_Sym *s = si->symtab;
663 unsigned n;
664
665 for(n = 0; n < si->nchain; n++) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700666 TRACE("%04d> %08x: %02x %04x %08x %08x %s\n", n, s,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800667 s->st_info, s->st_shndx, s->st_value, s->st_size,
668 si->strtab + s->st_name);
669 s++;
670 }
671}
672#endif
673
Elliott Hughes124fae92012-10-31 14:20:03 -0700674static int open_library_on_path(const char* name, const char* const paths[]) {
675 char buf[512];
676 for (size_t i = 0; paths[i] != NULL; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800677 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700678 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughes9c94fc92012-11-05 09:11:43 -0800679 PRINT("Warning: ignoring very long library path: %s/%s\n", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700680 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800681 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700682 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
683 if (fd != -1) {
684 return fd;
685 }
686 }
687 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800688}
689
Elliott Hughes124fae92012-10-31 14:20:03 -0700690static int open_library(const char* name) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700691 TRACE("[ opening %s ]\n", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800692
Elliott Hughes124fae92012-10-31 14:20:03 -0700693 // If the name contains a slash, we should attempt to open it directly and not search the paths.
694 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700695 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
696 if (fd != -1) {
697 return fd;
698 }
699 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700700 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800701
Elliott Hughes124fae92012-10-31 14:20:03 -0700702 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
703 int fd = open_library_on_path(name, gLdPaths);
704 if (fd == -1) {
705 fd = open_library_on_path(name, gSoPaths);
706 }
707 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800708}
709
Elliott Hughes46882792012-08-03 16:49:39 -0700710// Returns 'true' if the library is prelinked or on failure so we error out
711// either way. We no longer support prelinking.
712static bool is_prelinked(int fd, const char* name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800713{
Elliott Hughes46882792012-08-03 16:49:39 -0700714 struct prelink_info_t {
715 long mmap_addr;
716 char tag[4]; // "PRE ".
717 };
718
Elliott Hughesbedfe382012-08-14 14:07:59 -0700719 off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800720 if (sz < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700721 DL_ERR("lseek failed: %s", strerror(errno));
722 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800723 }
724
Elliott Hughesbedfe382012-08-14 14:07:59 -0700725 prelink_info_t info;
Elliott Hughes8dfc0732012-07-27 15:30:51 -0700726 int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
727 if (rc != sizeof(info)) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700728 DL_ERR("could not read prelink_info_t structure for \"%s\": %s", name, strerror(errno));
Elliott Hughes46882792012-08-03 16:49:39 -0700729 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800730 }
731
Elliott Hughes46882792012-08-03 16:49:39 -0700732 if (memcmp(info.tag, "PRE ", 4) == 0) {
733 DL_ERR("prelinked libraries no longer supported: %s", name);
734 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800735 }
Elliott Hughes46882792012-08-03 16:49:39 -0700736 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800737}
738
David 'Digit' Turner16084162012-06-12 16:25:37 +0200739/* verify_elf_header
740 * Verifies the content of an ELF header.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800741 *
742 * Args:
743 *
744 * Returns:
745 * 0 on success
746 * -1 if no valid ELF object is found @ base.
747 */
748static int
David 'Digit' Turner16084162012-06-12 16:25:37 +0200749verify_elf_header(const Elf32_Ehdr* hdr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800750{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800751 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
752 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
753 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
754 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700755 if (hdr->e_type != ET_DYN) return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800756
757 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800758#ifdef ANDROID_ARM_LINKER
759 if (hdr->e_machine != EM_ARM) return -1;
760#elif defined(ANDROID_X86_LINKER)
761 if (hdr->e_machine != EM_386) return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700762#elif defined(ANDROID_MIPS_LINKER)
763 if (hdr->e_machine != EM_MIPS) return -1;
Zhenghua Wang897815a2011-10-19 00:29:14 +0800764#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800765 return 0;
766}
767
Elliott Hughes46882792012-08-03 16:49:39 -0700768struct scoped_fd {
769 ~scoped_fd() {
770 if (fd != -1) {
771 close(fd);
772 }
773 }
774 int fd;
775};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800776
Elliott Hughes46882792012-08-03 16:49:39 -0700777struct soinfo_ptr {
778 soinfo_ptr(const char* name) {
779 const char* bname = strrchr(name, '/');
780 ptr = soinfo_alloc(bname ? bname + 1 : name);
781 }
782 ~soinfo_ptr() {
783 soinfo_free(ptr);
784 }
785 soinfo* release() {
786 soinfo* result = ptr;
787 ptr = NULL;
788 return result;
789 }
790 soinfo* ptr;
791};
792
793// TODO: rewrite linker_phdr.h to use a class, then lose this.
794struct phdr_ptr {
795 phdr_ptr() : phdr_mmap(NULL) {}
796 ~phdr_ptr() {
797 if (phdr_mmap != NULL) {
798 phdr_table_unload(phdr_mmap, phdr_size);
799 }
800 }
801 void* phdr_mmap;
802 Elf32_Addr phdr_size;
803};
804
Elliott Hughes124fae92012-10-31 14:20:03 -0700805static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700806 // Open the file.
807 scoped_fd fd;
808 fd.fd = open_library(name);
809 if (fd.fd == -1) {
810 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800811 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700812 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800813
Elliott Hughes46882792012-08-03 16:49:39 -0700814 // Read the ELF header.
815 Elf32_Ehdr header[1];
816 int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200817 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700818 DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
819 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200820 }
821 if (ret != (int)sizeof(header)) {
Elliott Hughes46882792012-08-03 16:49:39 -0700822 DL_ERR("too small to be an ELF executable: %s", name);
823 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200824 }
825 if (verify_elf_header(header) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700826 DL_ERR("not a valid ELF executable: %s", name);
827 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800828 }
829
Elliott Hughes46882792012-08-03 16:49:39 -0700830 // Read the program header table.
831 const Elf32_Phdr* phdr_table;
832 phdr_ptr phdr_holder;
833 ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
834 &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200835 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700836 DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
837 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200838 }
Elliott Hughes46882792012-08-03 16:49:39 -0700839 size_t phdr_count = header->e_phnum;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200840
Elliott Hughes46882792012-08-03 16:49:39 -0700841 // Get the load extents.
842 Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700843 TRACE("[ '%s' wants sz=0x%08x ]\n", name, ext_sz);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200844 if (ext_sz == 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700845 DL_ERR("no loadable segments in file: %s", name);
846 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800847 }
848
Elliott Hughes46882792012-08-03 16:49:39 -0700849 // We no longer support pre-linked libraries.
850 if (is_prelinked(fd.fd, name)) {
851 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200852 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200853
Elliott Hughes46882792012-08-03 16:49:39 -0700854 // Reserve address space for all loadable segments.
855 void* load_start = NULL;
856 Elf32_Addr load_size = 0;
857 Elf32_Addr load_bias = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200858 ret = phdr_table_reserve_memory(phdr_table,
859 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200860 &load_start,
861 &load_size,
862 &load_bias);
863 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700864 DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
865 ext_sz, name, strerror(errno));
866 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200867 }
868
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700869 TRACE("[ allocated memory for %s @ %p (0x%08x) ]\n", name, load_start, load_size);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200870
871 /* Map all the segments in our address space with default protections */
872 ret = phdr_table_load_segments(phdr_table,
873 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200874 load_bias,
Elliott Hughes46882792012-08-03 16:49:39 -0700875 fd.fd);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200876 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700877 DL_ERR("can't map loadable segments for \"%s\": %s",
878 name, strerror(errno));
879 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200880 }
881
Elliott Hughes46882792012-08-03 16:49:39 -0700882 soinfo_ptr si(name);
883 if (si.ptr == NULL) {
884 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800885 }
886
Elliott Hughes46882792012-08-03 16:49:39 -0700887 si.ptr->base = (Elf32_Addr) load_start;
888 si.ptr->size = load_size;
889 si.ptr->load_bias = load_bias;
890 si.ptr->flags = 0;
891 si.ptr->entry = 0;
892 si.ptr->dynamic = (unsigned *)-1;
893 si.ptr->phnum = phdr_count;
894 si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
895 if (si.ptr->phdr == NULL) {
896 DL_ERR("can't find loaded PHDR for \"%s\"", name);
897 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200898 }
Elliott Hughes46882792012-08-03 16:49:39 -0700899
900 return si.release();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800901}
902
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200903static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800904{
905 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700906 const char *bname;
907
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200908 // TODO: don't use basename only for determining libraries
909 // http://code.google.com/p/android/issues/detail?id=6670
910
911 bname = strrchr(name, '/');
912 bname = bname ? bname + 1 : name;
913
914 for(si = solist; si != NULL; si = si->next){
915 if(!strcmp(bname, si->name)) {
916 return si;
917 }
918 }
919 return NULL;
920}
921
Elliott Hughesd23736e2012-11-01 15:16:56 -0700922static soinfo* find_library_internal(const char* name) {
923 if (name == NULL) {
924 return somain;
925 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200926
Elliott Hughesd23736e2012-11-01 15:16:56 -0700927 soinfo* si = find_loaded_library(name);
928 if (si != NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700929 if (si->flags & FLAG_LINKED) {
930 return si;
931 }
932 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
933 return NULL;
934 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800935
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700936 TRACE("[ '%s' has not been loaded yet. Locating...]\n", name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700937 si = load_library(name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800938 if (si == NULL) {
939 return NULL;
940 }
941
942 // At this point we know that whatever is loaded @ base is a valid ELF
943 // shared library whose segments are properly mapped in.
944 TRACE("[ init_library base=0x%08x sz=0x%08x name='%s') ]\n",
945 si->base, si->size, si->name);
946
947 if (!soinfo_link_image(si)) {
948 munmap(reinterpret_cast<void*>(si->base), si->size);
949 soinfo_free(si);
950 return NULL;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700951 }
952
953 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800954}
955
Elliott Hughesd23736e2012-11-01 15:16:56 -0700956static soinfo* find_library(const char* name) {
957 soinfo* si = find_library_internal(name);
958 if (si != NULL) {
959 si->refcount++;
960 }
961 return si;
962}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700963
Elliott Hughesd23736e2012-11-01 15:16:56 -0700964static int soinfo_unload(soinfo* si) {
965 if (si->refcount == 1) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700966 TRACE("unloading '%s'\n", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700967 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800968
Elliott Hughesd23736e2012-11-01 15:16:56 -0700969 for (unsigned* d = si->dynamic; *d; d += 2) {
970 if (d[0] == DT_NEEDED) {
971 soinfo* lsi = find_loaded_library(si->strtab + d[1]);
972 if (lsi != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700973 TRACE("%s needs to unload %s\n", si->name, lsi->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700974 soinfo_unload(lsi);
975 } else {
976 // TODO: should we return -1 in this case?
977 DL_ERR("\"%s\": could not unload dependent library", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800978 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700979 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800980 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700981
982 munmap(reinterpret_cast<void*>(si->base), si->size);
983 notify_gdb_of_unload(si);
984 soinfo_free(si);
985 si->refcount = 0;
986 } else {
987 si->refcount--;
Elliott Hughes9c94fc92012-11-05 09:11:43 -0800988 TRACE("not unloading '%s', decrementing refcount to %d\n", si->name, si->refcount);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700989 }
990 return 0;
991}
992
Elliott Hughescade4c32012-12-20 14:42:14 -0800993void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
994 if (!get_AT_SECURE()) {
995 parse_LD_LIBRARY_PATH(ld_library_path);
996 }
997}
998
Elliott Hughese66190d2012-12-18 15:57:55 -0800999soinfo* do_dlopen(const char* name, int flags) {
1000 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
1001 DL_ERR("invalid flags to dlopen: %x", flags);
1002 return NULL;
1003 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001004 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
1005 soinfo* si = find_library(name);
1006 if (si != NULL) {
1007 si->CallConstructors();
1008 }
1009 set_soinfo_pool_protection(PROT_READ);
1010 return si;
1011}
1012
1013int do_dlclose(soinfo* si) {
1014 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
1015 int result = soinfo_unload(si);
1016 set_soinfo_pool_protection(PROT_READ);
1017 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001018}
1019
1020/* TODO: don't use unsigned for addrs below. It works, but is not
1021 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1022 * long.
1023 */
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001024static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count,
1025 soinfo *needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001026{
1027 Elf32_Sym *symtab = si->symtab;
1028 const char *strtab = si->strtab;
1029 Elf32_Sym *s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001030 Elf32_Rel *start = rel;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001031 soinfo *lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001032
Elliott Hughes46882792012-08-03 16:49:39 -07001033 for (size_t idx = 0; idx < count; ++idx, ++rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001034 unsigned type = ELF32_R_TYPE(rel->r_info);
1035 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001036 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001037 unsigned sym_addr = 0;
1038 char *sym_name = NULL;
1039
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001040 DEBUG("Processing '%s' relocation at index %d\n", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001041 if (type == 0) { // R_*_NONE
1042 continue;
1043 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001044 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001045 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001046 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Doug Kwane8238072009-10-26 12:05:23 -07001047 if(s == NULL) {
1048 /* We only allow an undefined symbol if this is a weak
1049 reference.. */
1050 s = &symtab[sym];
1051 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -07001052 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -07001053 return -1;
1054 }
1055
1056 /* IHI0044C AAELF 4.5.1.1:
1057
1058 Libraries are not searched to resolve weak references.
1059 It is not an error for a weak reference to remain
1060 unsatisfied.
1061
1062 During linking, the value of an undefined weak reference is:
1063 - Zero if the relocation type is absolute
1064 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -07001065 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -07001066 type is base-relative.
1067 */
1068
1069 switch (type) {
1070#if defined(ANDROID_ARM_LINKER)
1071 case R_ARM_JUMP_SLOT:
1072 case R_ARM_GLOB_DAT:
1073 case R_ARM_ABS32:
1074 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -07001075#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001076 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001077 case R_386_GLOB_DAT:
1078 case R_386_32:
1079 case R_386_RELATIVE: /* Dont' care. */
1080#endif /* ANDROID_*_LINKER */
1081 /* sym_addr was initialized to be zero above or relocation
1082 code below does not care about value of sym_addr.
1083 No need to do anything. */
1084 break;
1085
1086#if defined(ANDROID_X86_LINKER)
1087 case R_386_PC32:
1088 sym_addr = reloc;
1089 break;
1090#endif /* ANDROID_X86_LINKER */
1091
1092#if defined(ANDROID_ARM_LINKER)
1093 case R_ARM_COPY:
1094 /* Fall through. Can't really copy if weak symbol is
1095 not found in run-time. */
1096#endif /* ANDROID_ARM_LINKER */
1097 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001098 DL_ERR("unknown weak reloc type %d @ %p (%d)",
1099 type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -07001100 return -1;
1101 }
1102 } else {
1103 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001104#if 0
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001105 if((base == 0) && (si->base != 0)){
1106 /* linking from libraries to main image is bad */
Elliott Hughes46882792012-08-03 16:49:39 -07001107 DL_ERR("cannot locate \"%s\"...",
1108 strtab + symtab[sym].st_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001109 return -1;
1110 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001111#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001112 sym_addr = (unsigned)(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001113 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001114 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001115 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001116 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001117 }
1118
1119/* TODO: This is ugly. Split up the relocations by arch into
1120 * different files.
1121 */
1122 switch(type){
1123#if defined(ANDROID_ARM_LINKER)
1124 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001125 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001126 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001127 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001128 *((unsigned*)reloc) = sym_addr;
1129 break;
1130 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001131 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001132 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001133 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001134 *((unsigned*)reloc) = sym_addr;
1135 break;
1136 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001137 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001138 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001139 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001140 *((unsigned*)reloc) += sym_addr;
1141 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001142 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001143 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001144 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001145 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s\n",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001146 reloc, sym_addr, rel->r_offset, sym_name);
1147 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1148 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001149#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001150 case R_386_JMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001151 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001152 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001153 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001154 *((unsigned*)reloc) = sym_addr;
1155 break;
1156 case R_386_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001157 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001158 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001159 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001160 *((unsigned*)reloc) = sym_addr;
1161 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001162#elif defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001163 case R_MIPS_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001164 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001165 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001166 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s\n",
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001167 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1168 if (s) {
1169 *((unsigned*)reloc) += sym_addr;
1170 } else {
1171 *((unsigned*)reloc) += si->base;
1172 }
1173 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001174#endif /* ANDROID_*_LINKER */
1175
1176#if defined(ANDROID_ARM_LINKER)
1177 case R_ARM_RELATIVE:
1178#elif defined(ANDROID_X86_LINKER)
1179 case R_386_RELATIVE:
1180#endif /* ANDROID_*_LINKER */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001181 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001182 MARK(rel->r_offset);
Elliott Hughes46882792012-08-03 16:49:39 -07001183 if (sym) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001184 DL_ERR("odd RELATIVE form...");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001185 return -1;
1186 }
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001187 TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x\n", reloc, si->base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001188 *((unsigned*)reloc) += si->base;
1189 break;
1190
1191#if defined(ANDROID_X86_LINKER)
1192 case R_386_32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001193 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001194 MARK(rel->r_offset);
1195
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001196 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001197 *((unsigned *)reloc) += (unsigned)sym_addr;
1198 break;
1199
1200 case R_386_PC32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001201 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001202 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001203 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s\n",
1204 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001205 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1206 break;
1207#endif /* ANDROID_X86_LINKER */
1208
1209#ifdef ANDROID_ARM_LINKER
1210 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001211 if ((si->flags & FLAG_EXE) == 0) {
1212 /*
1213 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1214 *
1215 * Section 4.7.1.10 "Dynamic relocations"
1216 * R_ARM_COPY may only appear in executable objects where e_type is
1217 * set to ET_EXEC.
1218 *
1219 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1220 * We should explicitly disallow ET_DYN executables from having
1221 * R_ARM_COPY relocations.
1222 */
1223 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1224 return -1;
1225 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001226 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001227 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001228 TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s\n", reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001229 if (reloc == sym_addr) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001230 Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1231
1232 if (src == NULL) {
1233 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1234 return -1;
1235 }
1236 if (lsi->has_DT_SYMBOLIC) {
1237 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1238 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1239 return -1;
1240 }
1241 if (s->st_size < src->st_size) {
1242 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1243 si->name, s->st_size, src->st_size);
1244 return -1;
1245 }
1246 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1247 } else {
1248 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001249 return -1;
1250 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001251 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001252#endif /* ANDROID_ARM_LINKER */
1253
1254 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001255 DL_ERR("unknown reloc type %d @ %p (%d)",
1256 type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001257 return -1;
1258 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001259 }
1260 return 0;
1261}
1262
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001263#ifdef ANDROID_MIPS_LINKER
Elliott Hughesbedfe382012-08-14 14:07:59 -07001264static int mips_relocate_got(soinfo* si, soinfo* needed[]) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001265 unsigned *got;
1266 unsigned local_gotno, gotsym, symtabno;
1267 Elf32_Sym *symtab, *sym;
1268 unsigned g;
1269
1270 got = si->plt_got;
1271 local_gotno = si->mips_local_gotno;
1272 gotsym = si->mips_gotsym;
1273 symtabno = si->mips_symtabno;
1274 symtab = si->symtab;
1275
1276 /*
1277 * got[0] is address of lazy resolver function
1278 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001279 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001280 * (should be _rtld_bind_start)
1281 * FIXME: maybe this should be in a separate routine
1282 */
1283
1284 if ((si->flags & FLAG_LINKER) == 0) {
1285 g = 0;
1286 got[g++] = 0xdeadbeef;
1287 if (got[g] & 0x80000000) {
1288 got[g++] = 0xdeadfeed;
1289 }
1290 /*
1291 * Relocate the local GOT entries need to be relocated
1292 */
1293 for (; g < local_gotno; g++) {
1294 got[g] += si->load_bias;
1295 }
1296 }
1297
1298 /* Now for the global GOT entries */
1299 sym = symtab + gotsym;
1300 got = si->plt_got + local_gotno;
1301 for (g = gotsym; g < symtabno; g++, sym++, got++) {
1302 const char *sym_name;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001303 Elf32_Sym *s;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001304 soinfo *lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001305
1306 /* This is an undefined reference... try to locate it */
1307 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001308 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001309 if (s == NULL) {
1310 /* We only allow an undefined symbol if this is a weak
1311 reference.. */
1312 s = &symtab[g];
1313 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001314 DL_ERR("cannot locate \"%s\"...", sym_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001315 return -1;
1316 }
1317 *got = 0;
1318 }
1319 else {
1320 /* FIXME: is this sufficient?
1321 * For reference see NetBSD link loader
1322 * 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
1323 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001324 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001325 }
1326 }
1327 return 0;
1328}
1329#endif
1330
David 'Digit' Turner82156792009-05-18 14:37:41 +02001331/* Please read the "Initialization and Termination functions" functions.
1332 * of the linker design note in bionic/linker/README.TXT to understand
1333 * what the following code is doing.
1334 *
1335 * The important things to remember are:
1336 *
1337 * DT_PREINIT_ARRAY must be called first for executables, and should
1338 * not appear in shared libraries.
1339 *
1340 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1341 *
1342 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1343 *
1344 * DT_FINI_ARRAY must be parsed in reverse order.
1345 */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001346void soinfo::CallArray(const char* array_name UNUSED, unsigned* array, int count, bool reverse) {
1347 if (array == NULL) {
1348 return;
1349 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001350
Elliott Hughesd23736e2012-11-01 15:16:56 -07001351 int step = 1;
1352 if (reverse) {
1353 array += (count-1);
1354 step = -1;
1355 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001356
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001357 TRACE("[ Calling %s @ %p [%d] for '%s' ]\n", array_name, array, count, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001358
1359 for (int n = count; n > 0; n--) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001360 TRACE("[ Looking at %s[%d] *%p == 0x%08x ]\n", array_name, n, array, *array);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001361 void (*func)() = (void (*)()) *array;
1362 array += step;
Elliott Hughesdb492b32013-01-03 15:44:03 -08001363 CallFunction("function", func);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001364 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001365
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001366 TRACE("[ Done calling %s for '%s' ]\n", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001367}
1368
Elliott Hughesd23736e2012-11-01 15:16:56 -07001369void soinfo::CallFunction(const char* function_name UNUSED, void (*function)()) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001370 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001371 return;
1372 }
1373
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001374 TRACE("[ Calling %s @ %p for '%s' ]\n", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001375 function();
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001376 TRACE("[ Done calling %s for '%s' ]\n", function_name, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001377
1378 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1379 // are still writable. This happens with our debug malloc (see http://b/7941716).
1380 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001381}
1382
Elliott Hughesd23736e2012-11-01 15:16:56 -07001383void soinfo::CallPreInitConstructors() {
1384 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1385}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001386
Elliott Hughesd23736e2012-11-01 15:16:56 -07001387void soinfo::CallConstructors() {
1388 if (constructors_called) {
1389 return;
1390 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001391
Elliott Hughesd23736e2012-11-01 15:16:56 -07001392 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1393 // protect against recursive constructor calls. One simple example of constructor recursion
1394 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1395 // 1. The program depends on libc, so libc's constructor is called here.
1396 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1397 // 3. dlopen() calls the constructors on the newly created
1398 // soinfo for libc_malloc_debug_leak.so.
1399 // 4. The debug .so depends on libc, so CallConstructors is
1400 // called again with the libc soinfo. If it doesn't trigger the early-
1401 // out above, the libc constructor will be called again (recursively!).
1402 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001403
Elliott Hughesd23736e2012-11-01 15:16:56 -07001404 if (!(flags & FLAG_EXE) && preinit_array) {
1405 DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array);
1406 return;
1407 }
1408
1409 if (dynamic) {
1410 for (unsigned* d = dynamic; *d; d += 2) {
1411 if (d[0] == DT_NEEDED) {
1412 soinfo* lsi = find_loaded_library(strtab + d[1]);
1413 if (lsi == NULL) {
1414 DL_ERR("\"%s\": could not initialize dependent library", name);
1415 } else {
1416 lsi->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001417 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001418 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001419 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001420 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001421
Elliott Hughesd23736e2012-11-01 15:16:56 -07001422 CallFunction("DT_INIT", init_func);
1423 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001424}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001425
Elliott Hughesd23736e2012-11-01 15:16:56 -07001426void soinfo::CallDestructors() {
1427 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
1428 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001429}
1430
1431/* Force any of the closed stdin, stdout and stderr to be associated with
1432 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001433static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001434 int dev_null, i, status;
1435 int return_value = 0;
1436
David 'Digit' Turner16084162012-06-12 16:25:37 +02001437 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001438 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001439 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001440 return -1;
1441 }
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001442 TRACE("[ Opened /dev/null file-descriptor=%d]\n", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001443
1444 /* If any of the stdio file descriptors is valid and not associated
1445 with /dev/null, dup /dev/null to it. */
1446 for (i = 0; i < 3; i++) {
1447 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001448 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001449 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001450 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001451
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001452 TRACE("[ Nullifying stdio file descriptor %d]\n", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001453 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001454
Elliott Hughes46882792012-08-03 16:49:39 -07001455 /* If file is opened, we are good. */
1456 if (status != -1) {
1457 continue;
1458 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001459
1460 /* The only error we allow is that the file descriptor does not
1461 exist, in which case we dup /dev/null to it. */
1462 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001463 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001464 return_value = -1;
1465 continue;
1466 }
1467
1468 /* Try dupping /dev/null to this stdio file descriptor and
1469 repeat if there is a signal. Note that any errors in closing
1470 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001471 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001472 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001473 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001474 return_value = -1;
1475 continue;
1476 }
1477 }
1478
1479 /* If /dev/null is not one of the stdio file descriptors, close it. */
1480 if (dev_null > 2) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001481 TRACE("[ Closing /dev/null file-descriptor=%d]\n", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001482 status = TEMP_FAILURE_RETRY(close(dev_null));
1483 if (status == -1) {
1484 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001485 return_value = -1;
1486 }
1487 }
1488
1489 return return_value;
1490}
1491
Elliott Hughes124fae92012-10-31 14:20:03 -07001492static bool soinfo_link_image(soinfo* si) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001493 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001494 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001495 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001496 int phnum = si->phnum;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001497 int relocating_linker = (si->flags & FLAG_LINKER) != 0;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001498 soinfo **needed, **pneeded;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001499
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001500 /* We can't debug anything until the linker is relocated */
1501 if (!relocating_linker) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001502 INFO("[ linking %s ]\n", si->name);
1503 DEBUG("si->base = 0x%08x si->flags = 0x%08x\n", si->base, si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001504 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001505
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001506 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001507 size_t dynamic_count;
Chris Dearmancf239052013-01-11 15:32:20 -08001508 Elf32_Word dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001509 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001510 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001511 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001512 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001513 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001514 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001515 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001516 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001517 if (!relocating_linker) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001518 DEBUG("dynamic = %p\n", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001519 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001520 }
1521
1522#ifdef ANDROID_ARM_LINKER
1523 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1524 &si->ARM_exidx, &si->ARM_exidx_count);
1525#endif
1526
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001527 /* extract useful information from dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001528 for (unsigned* d = si->dynamic; *d; ++d) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001529 DEBUG("d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", d, d[0], d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001530 switch(*d++){
1531 case DT_HASH:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001532 si->nbucket = ((unsigned *) (base + *d))[0];
1533 si->nchain = ((unsigned *) (base + *d))[1];
1534 si->bucket = (unsigned *) (base + *d + 8);
1535 si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001536 break;
1537 case DT_STRTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001538 si->strtab = (const char *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001539 break;
1540 case DT_SYMTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001541 si->symtab = (Elf32_Sym *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001542 break;
1543 case DT_PLTREL:
1544 if(*d != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001545 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1546 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001547 }
1548 break;
1549 case DT_JMPREL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001550 si->plt_rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001551 break;
1552 case DT_PLTRELSZ:
1553 si->plt_rel_count = *d / 8;
1554 break;
1555 case DT_REL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001556 si->rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001557 break;
1558 case DT_RELSZ:
1559 si->rel_count = *d / 8;
1560 break;
1561 case DT_PLTGOT:
1562 /* Save this in case we decide to do lazy binding. We don't yet. */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001563 si->plt_got = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001564 break;
1565 case DT_DEBUG:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001566 // Set the DT_DEBUG entry to the address of _r_debug for GDB
Chris Dearmancf239052013-01-11 15:32:20 -08001567 // if the dynamic table is writable
Elliott Hughes99c32052013-01-14 09:56:21 -08001568 if ((dynamic_flags & PF_W) != 0) {
Chris Dearmancf239052013-01-11 15:32:20 -08001569 *d = (int) &_r_debug;
Elliott Hughes99c32052013-01-14 09:56:21 -08001570 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001571 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001572 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001573 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1574 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001575 case DT_INIT:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001576 si->init_func = (void (*)(void))(base + *d);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001577 DEBUG("%s constructors (init func) found at %p\n", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001578 break;
1579 case DT_FINI:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001580 si->fini_func = (void (*)(void))(base + *d);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001581 DEBUG("%s destructors (fini func) found at %p\n", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001582 break;
1583 case DT_INIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001584 si->init_array = (unsigned *)(base + *d);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001585 DEBUG("%s constructors (init_array) found at %p\n", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001586 break;
1587 case DT_INIT_ARRAYSZ:
1588 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1589 break;
1590 case DT_FINI_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001591 si->fini_array = (unsigned *)(base + *d);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001592 DEBUG("%s destructors (fini_array) found at %p\n", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001593 break;
1594 case DT_FINI_ARRAYSZ:
1595 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1596 break;
1597 case DT_PREINIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001598 si->preinit_array = (unsigned *)(base + *d);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001599 DEBUG("%s constructors (preinit_array) found at %p\n", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001600 break;
1601 case DT_PREINIT_ARRAYSZ:
1602 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1603 break;
1604 case DT_TEXTREL:
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001605 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001606 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001607 case DT_SYMBOLIC:
1608 si->has_DT_SYMBOLIC = true;
1609 break;
1610#if defined(DT_FLAGS)
1611 case DT_FLAGS:
1612 if (*d & DF_TEXTREL) {
1613 si->has_text_relocations = true;
1614 }
1615 if (*d & DF_SYMBOLIC) {
1616 si->has_DT_SYMBOLIC = true;
1617 }
1618 break;
1619#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001620#if defined(ANDROID_MIPS_LINKER)
1621 case DT_NEEDED:
1622 case DT_STRSZ:
1623 case DT_SYMENT:
1624 case DT_RELENT:
1625 break;
1626 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001627 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001628 {
Elliott Hughesbedfe382012-08-14 14:07:59 -07001629 r_debug** dp = (r_debug**) *d;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001630 *dp = &_r_debug;
1631 }
1632 break;
1633 case DT_MIPS_RLD_VERSION:
1634 case DT_MIPS_FLAGS:
1635 case DT_MIPS_BASE_ADDRESS:
1636 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001637 break;
1638
1639 case DT_MIPS_SYMTABNO:
1640 si->mips_symtabno = *d;
1641 break;
1642
1643 case DT_MIPS_LOCAL_GOTNO:
1644 si->mips_local_gotno = *d;
1645 break;
1646
1647 case DT_MIPS_GOTSYM:
1648 si->mips_gotsym = *d;
1649 break;
1650
1651 default:
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001652 DEBUG("Unused DT entry: type 0x%08x arg 0x%08x\n", d[-1], d[0]);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001653 break;
1654#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001655 }
1656 }
1657
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001658 DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
1659 si->base, si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001660
Elliott Hughes124fae92012-10-31 14:20:03 -07001661 // Sanity checks.
1662 if (si->nbucket == 0) {
1663 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1664 return false;
1665 }
1666 if (si->strtab == 0) {
1667 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1668 return false;
1669 }
1670 if (si->symtab == 0) {
1671 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1672 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001673 }
1674
Matt Fischer4fd42c12009-12-31 12:09:10 -06001675 /* if this is the main executable, then load all of the preloads now */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001676 if (si->flags & FLAG_EXE) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001677 memset(preloads, 0, sizeof(preloads));
Elliott Hughesd23736e2012-11-01 15:16:56 -07001678 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1679 soinfo* lsi = find_library(gLdPreloadNames[i]);
1680 if (lsi == NULL) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001681 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001682 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Elliott Hughes124fae92012-10-31 14:20:03 -07001683 gLdPreloadNames[i], si->name, tmp_err_buf);
1684 return false;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001685 }
Matt Fischer4fd42c12009-12-31 12:09:10 -06001686 preloads[i] = lsi;
1687 }
1688 }
1689
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001690 /* dynamic_count is an upper bound for the number of needed libs */
1691 pneeded = needed = (soinfo**) alloca((1 + dynamic_count) * sizeof(soinfo*));
1692
Elliott Hughes124fae92012-10-31 14:20:03 -07001693 for (unsigned* d = si->dynamic; *d; d += 2) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001694 if (d[0] == DT_NEEDED) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001695 DEBUG("%s needs %s\n", si->name, si->strtab + d[1]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001696 soinfo* lsi = find_library(si->strtab + d[1]);
1697 if (lsi == NULL) {
Dima Zavin03531952009-05-29 17:30:25 -07001698 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001699 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1700 si->strtab + d[1], si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001701 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001702 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001703 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001704 }
1705 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001706 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001707
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001708 if (si->has_text_relocations) {
1709 /* Unprotect the segments, i.e. make them writable, to allow
1710 * text relocations to work properly. We will later call
1711 * phdr_table_protect_segments() after all of them are applied
1712 * and all constructors are run.
1713 */
1714 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1715 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1716 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001717 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001718 }
1719 }
1720
Elliott Hughes124fae92012-10-31 14:20:03 -07001721 if (si->plt_rel) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001722 DEBUG("[ relocating %s plt ]\n", si->name );
Elliott Hughes124fae92012-10-31 14:20:03 -07001723 if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1724 return false;
1725 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001726 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001727 if (si->rel) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001728 DEBUG("[ relocating %s ]\n", si->name );
Elliott Hughes124fae92012-10-31 14:20:03 -07001729 if(soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1730 return false;
1731 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001732 }
1733
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001734#ifdef ANDROID_MIPS_LINKER
Elliott Hughes124fae92012-10-31 14:20:03 -07001735 if (mips_relocate_got(si, needed)) {
1736 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001737 }
1738#endif
1739
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001740 si->flags |= FLAG_LINKED;
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001741 DEBUG("[ finished linking %s ]\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001742
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001743 if (si->has_text_relocations) {
1744 /* All relocations are done, we can protect our segments back to
1745 * read-only. */
1746 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1747 DL_ERR("can't protect segments for \"%s\": %s",
1748 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001749 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001750 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001751 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001752
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001753 /* We can also turn on GNU RELRO protection */
1754 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001755 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1756 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001757 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001758 }
1759
Elliott Hughes124fae92012-10-31 14:20:03 -07001760 // If this is a setuid/setgid program, close the security hole described in
1761 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
Elliott Hughes18a206c2012-10-29 17:37:13 -07001762 if (get_AT_SECURE()) {
Elliott Hughes46882792012-08-03 16:49:39 -07001763 nullify_closed_stdio();
1764 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001765 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001766 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001767}
1768
Nick Kralevich468319c2011-11-11 15:53:17 -08001769/*
1770 * This code is called after the linker has linked itself and
1771 * fixed it's own GOT. It is safe to make references to externs
1772 * and other non-local data at this point.
1773 */
Nick Kralevich36bd3712013-01-16 13:13:22 -08001774static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001775{
1776 static soinfo linker_soinfo;
1777
1778 int argc = (int) *elfdata;
1779 char **argv = (char**) (elfdata + 1);
Stephen Smalleybb440552012-01-20 10:59:15 -08001780 unsigned *vecs = (unsigned*) (argv + argc + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001781
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001782 /* NOTE: we store the elfdata pointer on a special location
1783 * of the temporary TLS area in order to pass it to
1784 * the C Library's runtime initializer.
1785 *
1786 * The initializer must clear the slot and reset the TLS
1787 * to point to a different location to ensure that no other
1788 * shared library constructor can access it.
1789 */
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001790 __libc_init_tls(elfdata);
1791
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001792#if TIMING
1793 struct timeval t0, t1;
1794 gettimeofday(&t0, 0);
1795#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001796
Elliott Hughes18a206c2012-10-29 17:37:13 -07001797 // Initialize environment functions, and get to the ELF aux vectors table.
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001798 vecs = linker_env_init(vecs);
1799
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001800 debugger_init();
1801
Elliott Hughes18a206c2012-10-29 17:37:13 -07001802 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001803 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1804 if (LD_DEBUG != NULL) {
1805 debug_verbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001806 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001807
Elliott Hughes18a206c2012-10-29 17:37:13 -07001808 // Normally, these are cleaned by linker_env_init, but the test
1809 // doesn't cost us anything.
1810 const char* ldpath_env = NULL;
1811 const char* ldpreload_env = NULL;
1812 if (!get_AT_SECURE()) {
1813 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1814 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001815 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001816
1817 INFO("[ android linker & debugger ]\n");
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001818 DEBUG("elfdata @ 0x%08x\n", (unsigned)elfdata);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001819
Elliott Hughes18a206c2012-10-29 17:37:13 -07001820 soinfo* si = soinfo_alloc(argv[0]);
1821 if (si == NULL) {
1822 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001823 }
1824
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001825 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001826 si->flags |= FLAG_EXE;
Elliott Hughesbedfe382012-08-14 14:07:59 -07001827 link_map* map = &(si->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001828
1829 map->l_addr = 0;
1830 map->l_name = argv[0];
1831 map->l_prev = NULL;
1832 map->l_next = NULL;
1833
1834 _r_debug.r_map = map;
1835 r_debug_tail = map;
1836
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001837 /* gdb expects the linker to be in the debug shared object list.
1838 * Without this, gdb has trouble locating the linker's ".text"
1839 * and ".plt" sections. Gdb could also potentially use this to
1840 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1841 * Don't use soinfo_alloc(), because the linker shouldn't
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001842 * be on the soinfo list.
1843 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001844 strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001845 linker_soinfo.flags = 0;
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001846 linker_soinfo.base = linker_base;
Ben Cheng06f0e742012-08-10 16:07:02 -07001847 /*
1848 * Set the dynamic field in the link map otherwise gdb will complain with
1849 * the following:
1850 * warning: .dynamic section for "/system/bin/linker" is not at the
1851 * expected address (wrong library or version mismatch?)
1852 */
1853 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
1854 Elf32_Phdr *phdr =
1855 (Elf32_Phdr *)((unsigned char *) linker_base + elf_hdr->e_phoff);
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001856 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Chris Dearmancf239052013-01-11 15:32:20 -08001857 &linker_soinfo.dynamic, NULL, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001858 insert_soinfo_into_debug_map(&linker_soinfo);
1859
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001860 /* extract information passed from the kernel */
Elliott Hughes18a206c2012-10-29 17:37:13 -07001861 while (vecs[0] != 0){
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001862 switch(vecs[0]){
1863 case AT_PHDR:
1864 si->phdr = (Elf32_Phdr*) vecs[1];
1865 break;
1866 case AT_PHNUM:
1867 si->phnum = (int) vecs[1];
1868 break;
1869 case AT_ENTRY:
1870 si->entry = vecs[1];
1871 break;
1872 }
1873 vecs += 2;
1874 }
1875
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001876 /* Compute the value of si->base. We can't rely on the fact that
1877 * the first entry is the PHDR because this will not be true
1878 * for certain executables (e.g. some in the NDK unit test suite)
1879 */
1880 int nn;
1881 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001882 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001883 si->load_bias = 0;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001884 for ( nn = 0; nn < si->phnum; nn++ ) {
1885 if (si->phdr[nn].p_type == PT_PHDR) {
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001886 si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
1887 si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001888 break;
1889 }
1890 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001891 si->dynamic = (unsigned *)-1;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001892 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001893
Elliott Hughes46882792012-08-03 16:49:39 -07001894 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1895 parse_LD_LIBRARY_PATH(ldpath_env);
1896 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001897
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001898 somain = si;
1899
Elliott Hughes124fae92012-10-31 14:20:03 -07001900 if (!soinfo_link_image(si)) {
Chris Dearman20a24402012-10-31 05:39:27 -07001901 const char* msg = "CANNOT LINK EXECUTABLE\n";
Dima Zavin2e855792009-05-20 18:28:09 -07001902 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
Chris Dearman20a24402012-10-31 05:39:27 -07001903 write(2, msg, strlen(msg));
Elliott Hughes18a206c2012-10-29 17:37:13 -07001904 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001905 }
1906
Elliott Hughesd23736e2012-11-01 15:16:56 -07001907 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001908
Elliott Hughes18a206c2012-10-29 17:37:13 -07001909 for (size_t i = 0; preloads[i] != NULL; ++i) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001910 preloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08001911 }
1912
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08001913 /*After the link_image, the si->load_bias is initialized.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08001914 *For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1915 *We need to update this value for so exe here. So Unwind_Backtrace
1916 *for some arch like x86 could work correctly within so exe.
1917 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08001918 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001919 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001920
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001921#if TIMING
1922 gettimeofday(&t1,NULL);
1923 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
1924 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1925 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1926 ));
1927#endif
1928#if STATS
1929 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07001930 linker_stats.count[kRelocAbsolute],
1931 linker_stats.count[kRelocRelative],
1932 linker_stats.count[kRelocCopy],
1933 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001934#endif
1935#if COUNT_PAGES
1936 {
1937 unsigned n;
1938 unsigned i;
1939 unsigned count = 0;
1940 for(n = 0; n < 4096; n++){
1941 if(bitmask[n]){
1942 unsigned x = bitmask[n];
1943 for(i = 0; i < 8; i++){
1944 if(x & 1) count++;
1945 x >>= 1;
1946 }
1947 }
1948 }
1949 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
1950 }
1951#endif
1952
1953#if TIMING || STATS || COUNT_PAGES
1954 fflush(stdout);
1955#endif
1956
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001957 TRACE("[ Ready to execute '%s' @ 0x%08x ]\n", si->name, si->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001958 return si->entry;
1959}
Nick Kralevich468319c2011-11-11 15:53:17 -08001960
1961/*
1962 * Find the value of AT_BASE passed to us by the kernel. This is the load
1963 * location of the linker.
1964 */
Nick Kralevich36bd3712013-01-16 13:13:22 -08001965static unsigned find_linker_base(unsigned **elfdata) {
Nick Kralevich468319c2011-11-11 15:53:17 -08001966 int argc = (int) *elfdata;
1967 char **argv = (char**) (elfdata + 1);
1968 unsigned *vecs = (unsigned*) (argv + argc + 1);
1969 while (vecs[0] != 0) {
1970 vecs++;
1971 }
1972
1973 /* The end of the environment block is marked by two NULL pointers */
1974 vecs++;
1975
1976 while(vecs[0]) {
1977 if (vecs[0] == AT_BASE) {
1978 return vecs[1];
1979 }
1980 vecs += 2;
1981 }
1982
1983 return 0; // should never happen
1984}
1985
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001986/* Compute the load-bias of an existing executable. This shall only
1987 * be used to compute the load bias of an executable or shared library
1988 * that was loaded by the kernel itself.
1989 *
1990 * Input:
1991 * elf -> address of ELF header, assumed to be at the start of the file.
1992 * Return:
1993 * load bias, i.e. add the value of any p_vaddr in the file to get
1994 * the corresponding address in memory.
1995 */
1996static Elf32_Addr
1997get_elf_exec_load_bias(const Elf32_Ehdr* elf)
1998{
1999 Elf32_Addr offset = elf->e_phoff;
2000 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
2001 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
2002 const Elf32_Phdr* phdr;
2003
2004 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
2005 if (phdr->p_type == PT_LOAD) {
2006 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
2007 }
2008 }
2009 return 0;
2010}
2011
Nick Kralevich468319c2011-11-11 15:53:17 -08002012/*
2013 * This is the entry point for the linker, called from begin.S. This
2014 * method is responsible for fixing the linker's own relocations, and
2015 * then calling __linker_init_post_relocation().
2016 *
2017 * Because this method is called before the linker has fixed it's own
2018 * relocations, any attempt to reference an extern variable, extern
2019 * function, or other GOT reference will generate a segfault.
2020 */
Nick Kralevich36bd3712013-01-16 13:13:22 -08002021extern "C" unsigned __linker_init(unsigned **elfdata) {
2022 unsigned linker_addr = find_linker_base(elfdata);
Nick Kralevich468319c2011-11-11 15:53:17 -08002023 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2024 Elf32_Phdr *phdr =
2025 (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2026
2027 soinfo linker_so;
2028 memset(&linker_so, 0, sizeof(soinfo));
2029
2030 linker_so.base = linker_addr;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002031 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002032 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Nick Kralevich468319c2011-11-11 15:53:17 -08002033 linker_so.dynamic = (unsigned *) -1;
2034 linker_so.phdr = phdr;
2035 linker_so.phnum = elf_hdr->e_phnum;
2036 linker_so.flags |= FLAG_LINKER;
Nick Kralevich468319c2011-11-11 15:53:17 -08002037
Elliott Hughes124fae92012-10-31 14:20:03 -07002038 if (!soinfo_link_image(&linker_so)) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002039 // It would be nice to print an error message, but if the linker
2040 // can't link itself, there's no guarantee that we'll be able to
2041 // call write() (because it involves a GOT reference).
2042 //
2043 // This situation should never occur unless the linker itself
2044 // is corrupt.
Elliott Hughes18a206c2012-10-29 17:37:13 -07002045 exit(EXIT_FAILURE);
Nick Kralevich468319c2011-11-11 15:53:17 -08002046 }
2047
2048 // We have successfully fixed our own relocations. It's safe to run
2049 // the main part of the linker now.
Elliott Hughes5419b942012-10-16 15:54:46 -07002050 unsigned start_address = __linker_init_post_relocation(elfdata, linker_addr);
2051
Elliott Hughesd23736e2012-11-01 15:16:56 -07002052 set_soinfo_pool_protection(PROT_READ);
2053
Elliott Hughes5419b942012-10-16 15:54:46 -07002054 // Return the address that the calling assembly stub should jump to.
2055 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002056}