blob: 4d2e72f31a9c063d44c50700fd4cadb92eda76ac [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>
44#include <private/logd.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070045#include <private/ScopedPthreadMutexLocker.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046
47#include "linker.h"
48#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010049#include "linker_environ.h"
David 'Digit' Turner5c734642010-01-20 12:36:51 -080050#include "linker_format.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); \
149 write(2, msg, sizeof(msg)); \
150 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 Hughes3b297c42012-10-11 16:08:51 -0700162 format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), fmt, ##x); \
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700163 ERROR(fmt "\n", ##x); \
Dima Zavin2e855792009-05-20 18:28:09 -0700164 } while(0)
165
Elliott Hughes5419b942012-10-16 15:54:46 -0700166const char* linker_get_error() {
167 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700168}
169
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170/*
171 * This function is an empty stub where GDB locates a breakpoint to get notified
172 * about linker activity.
173 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700174extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
Elliott Hughesbedfe382012-08-14 14:07:59 -0700176static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800177 RT_CONSISTENT, 0};
Elliott Hughesbedfe382012-08-14 14:07:59 -0700178static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179
Elliott Hughes3b297c42012-10-11 16:08:51 -0700180static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181
Elliott Hughesbedfe382012-08-14 14:07:59 -0700182static void insert_soinfo_into_debug_map(soinfo * info) {
183 // Copy the necessary fields into the debug structure.
184 link_map* map = &(info->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185 map->l_addr = info->base;
186 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800187 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188
189 /* Stick the new library at the end of the list.
190 * gdb tends to care more about libc than it does
191 * about leaf libraries, and ordering it this way
192 * reduces the back-and-forth over the wire.
193 */
194 if (r_debug_tail) {
195 r_debug_tail->l_next = map;
196 map->l_prev = r_debug_tail;
197 map->l_next = 0;
198 } else {
199 _r_debug.r_map = map;
200 map->l_prev = 0;
201 map->l_next = 0;
202 }
203 r_debug_tail = map;
204}
205
Elliott Hughesbedfe382012-08-14 14:07:59 -0700206static void remove_soinfo_from_debug_map(soinfo* info) {
207 link_map* map = &(info->linkmap);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700208
Elliott Hughesbedfe382012-08-14 14:07:59 -0700209 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700210 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700211 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700212
Elliott Hughesbedfe382012-08-14 14:07:59 -0700213 if (map->l_prev) {
214 map->l_prev->l_next = map->l_next;
215 }
216 if (map->l_next) {
217 map->l_next->l_prev = map->l_prev;
218 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700219}
220
Elliott Hughesbedfe382012-08-14 14:07:59 -0700221static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222 if (info->flags & FLAG_EXE) {
223 // GDB already knows about the main executable
224 return;
225 }
226
Elliott Hughes3b297c42012-10-11 16:08:51 -0700227 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228
229 _r_debug.r_state = RT_ADD;
230 rtld_db_dlactivity();
231
232 insert_soinfo_into_debug_map(info);
233
234 _r_debug.r_state = RT_CONSISTENT;
235 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700236}
237
Elliott Hughesbedfe382012-08-14 14:07:59 -0700238static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700239 if (info->flags & FLAG_EXE) {
240 // GDB already knows about the main executable
241 return;
242 }
243
Elliott Hughes3b297c42012-10-11 16:08:51 -0700244 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700245
246 _r_debug.r_state = RT_DELETE;
247 rtld_db_dlactivity();
248
249 remove_soinfo_from_debug_map(info);
250
251 _r_debug.r_state = RT_CONSISTENT;
252 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800253}
254
Elliott Hughes18a206c2012-10-29 17:37:13 -0700255void notify_gdb_of_libraries() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800256 _r_debug.r_state = RT_ADD;
257 rtld_db_dlactivity();
258 _r_debug.r_state = RT_CONSISTENT;
259 rtld_db_dlactivity();
260}
261
Magnus Malmbornba98d922012-09-12 13:00:55 +0200262static bool ensure_free_list_non_empty() {
263 if (gSoInfoFreeList != NULL) {
264 return true;
265 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800266
Magnus Malmbornba98d922012-09-12 13:00:55 +0200267 // Allocate a new pool.
268 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
269 PROT_READ|PROT_WRITE,
270 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
271 if (pool == MAP_FAILED) {
272 return false;
273 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274
Magnus Malmbornba98d922012-09-12 13:00:55 +0200275 // Add the pool to our list of pools.
276 pool->next = gSoInfoPools;
277 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800278
Magnus Malmbornba98d922012-09-12 13:00:55 +0200279 // Chain the entries in the new pool onto the free list.
280 gSoInfoFreeList = &pool->info[0];
281 soinfo* next = NULL;
282 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
283 pool->info[i].next = next;
284 next = &pool->info[i];
285 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286
Magnus Malmbornba98d922012-09-12 13:00:55 +0200287 return true;
288}
289
Elliott Hughesd23736e2012-11-01 15:16:56 -0700290static void set_soinfo_pool_protection(int protection) {
291 for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
292 if (mprotect(p, sizeof(*p), protection) == -1) {
293 abort(); // Can't happen.
294 }
295 }
296}
297
Magnus Malmbornba98d922012-09-12 13:00:55 +0200298static soinfo* soinfo_alloc(const char* name) {
299 if (strlen(name) >= SOINFO_NAME_LEN) {
300 DL_ERR("library name \"%s\" too long", name);
301 return NULL;
302 }
303
304 if (!ensure_free_list_non_empty()) {
305 DL_ERR("out of memory when loading \"%s\"", name);
306 return NULL;
307 }
308
309 // Take the head element off the free list.
310 soinfo* si = gSoInfoFreeList;
311 gSoInfoFreeList = gSoInfoFreeList->next;
312
313 // Initialize the new element.
314 memset(si, 0, sizeof(soinfo));
315 strlcpy(si->name, name, sizeof(si->name));
316 sonext->next = si;
317 sonext = si;
318
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700319 TRACE("name %s: allocated soinfo @ %p\n", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200320 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800321}
322
Elliott Hughes46882792012-08-03 16:49:39 -0700323static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324{
Elliott Hughes46882792012-08-03 16:49:39 -0700325 if (si == NULL) {
326 return;
327 }
328
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800329 soinfo *prev = NULL, *trav;
330
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700331 TRACE("name %s: freeing soinfo @ %p\n", si->name, si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800332
333 for(trav = solist; trav != NULL; trav = trav->next){
334 if (trav == si)
335 break;
336 prev = trav;
337 }
338 if (trav == NULL) {
339 /* si was not ni solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700340 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800341 return;
342 }
343
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100344 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800345 always the static libdl_info.
346 */
347 prev->next = si->next;
348 if (si == sonext) sonext = prev;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200349 si->next = gSoInfoFreeList;
350 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800351}
352
Elliott Hughes46882792012-08-03 16:49:39 -0700353#ifdef ANDROID_ARM_LINKER
354
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355/* For a given PC, find the .so that it belongs to.
356 * Returns the base address of the .ARM.exidx section
357 * for that .so, and the number of 8-byte entries
358 * in that section (via *pcount).
359 *
360 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
361 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700362 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800363 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
365{
366 soinfo *si;
367 unsigned addr = (unsigned)pc;
368
Nick Kralevich468319c2011-11-11 15:53:17 -0800369 for (si = solist; si != 0; si = si->next){
370 if ((addr >= si->base) && (addr < (si->base + si->size))) {
371 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900372 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800373 }
374 }
375 *pcount = 0;
376 return NULL;
377}
Elliott Hughes46882792012-08-03 16:49:39 -0700378
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700379#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
Elliott Hughes46882792012-08-03 16:49:39 -0700380
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800381/* Here, we only have to provide a callback to iterate across all the
382 * loaded libraries. gcc_eh does the rest. */
383int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700384dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385 void *data)
386{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800387 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700388 for (soinfo* si = solist; si != NULL; si = si->next) {
389 dl_phdr_info dl_info;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800390 dl_info.dlpi_addr = si->linkmap.l_addr;
391 dl_info.dlpi_name = si->linkmap.l_name;
392 dl_info.dlpi_phdr = si->phdr;
393 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700394 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
395 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700397 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398 }
399 return rv;
400}
Elliott Hughes46882792012-08-03 16:49:39 -0700401
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402#endif
403
David 'Digit' Turner16084162012-06-12 16:25:37 +0200404static Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405{
406 Elf32_Sym *s;
407 Elf32_Sym *symtab = si->symtab;
408 const char *strtab = si->strtab;
409 unsigned n;
410
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700411 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800412 name, si->name, si->base, hash, hash % si->nbucket);
413 n = hash % si->nbucket;
414
415 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
416 s = symtab + n;
417 if(strcmp(strtab + s->st_name, name)) continue;
418
Doug Kwane8238072009-10-26 12:05:23 -0700419 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800420 switch(ELF32_ST_BIND(s->st_info)){
421 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700422 case STB_WEAK:
Robin Burchell439fa8e2012-07-05 09:21:07 +0200423 if(s->st_shndx == SHN_UNDEF)
424 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800425
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700426 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800427 name, si->name, s->st_value, s->st_size);
428 return s;
429 }
430 }
431
Doug Kwan94304352009-10-23 18:11:40 -0700432 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433}
434
435static unsigned elfhash(const char *_name)
436{
437 const unsigned char *name = (const unsigned char *) _name;
438 unsigned h = 0, g;
439
440 while(*name) {
441 h = (h << 4) + *name++;
442 g = h & 0xf0000000;
443 h ^= g;
444 h ^= g >> 24;
445 }
446 return h;
447}
448
449static Elf32_Sym *
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200450soinfo_do_lookup(soinfo *si, const char *name, soinfo **lsi,
451 soinfo *needed[])
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700452{
Doug Kwan94304352009-10-23 18:11:40 -0700453 unsigned elf_hash = elfhash(name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700454 Elf32_Sym *s = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600455 int i;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700456
Pavel Chupinc77c4342012-10-31 13:55:51 +0400457 if (si != NULL && somain != NULL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200458
459 /*
Pavel Chupinc77c4342012-10-31 13:55:51 +0400460 * Local scope is executable scope. Just start looking into it right away
461 * for the shortcut.
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200462 */
463
Pavel Chupinc77c4342012-10-31 13:55:51 +0400464 if (si == somain) {
465 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200466 if (s != NULL) {
Pavel Chupinc77c4342012-10-31 13:55:51 +0400467 *lsi = si;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200468 goto done;
469 }
Pavel Chupinc77c4342012-10-31 13:55:51 +0400470 } else {
471 /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200472
Pavel Chupinc77c4342012-10-31 13:55:51 +0400473 /*
474 * If this object was built with symbolic relocations disabled, the
475 * first place to look to resolve external references is the main
476 * executable.
477 */
Nick Kralevich468319c2011-11-11 15:53:17 -0800478
Pavel Chupinc77c4342012-10-31 13:55:51 +0400479 if (!si->has_DT_SYMBOLIC) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700480 DEBUG("%s: looking up %s in executable %s\n",
481 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400482 s = soinfo_elf_lookup(somain, elf_hash, name);
483 if (s != NULL) {
484 *lsi = somain;
485 goto done;
486 }
487 }
488
489 /* Look for symbols in the local scope (the object who is
490 * searching). This happens with C++ templates on i386 for some
491 * reason.
492 *
493 * Notes on weak symbols:
494 * The ELF specs are ambiguous about treatment of weak definitions in
495 * dynamic linking. Some systems return the first definition found
496 * and some the first non-weak definition. This is system dependent.
497 * Here we return the first definition found for simplicity. */
498
499 s = soinfo_elf_lookup(si, elf_hash, name);
500 if (s != NULL) {
501 *lsi = si;
502 goto done;
503 }
504
505 /*
506 * If this object was built with -Bsymbolic and symbol is not found
507 * in the local scope, try to find the symbol in the main executable.
508 */
509
510 if (si->has_DT_SYMBOLIC) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700511 DEBUG("%s: looking up %s in executable %s after local scope\n",
512 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400513 s = soinfo_elf_lookup(somain, elf_hash, name);
514 if (s != NULL) {
515 *lsi = somain;
516 goto done;
517 }
518 }
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200519 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700520 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700521
Matt Fischer4fd42c12009-12-31 12:09:10 -0600522 /* Next, look for it in the preloads list */
523 for(i = 0; preloads[i] != NULL; i++) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200524 s = soinfo_elf_lookup(preloads[i], elf_hash, name);
525 if(s != NULL) {
526 *lsi = preloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600527 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200528 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600529 }
530
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200531 for(i = 0; needed[i] != NULL; i++) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700532 DEBUG("%s: looking up %s in %s\n",
533 si->name, name, needed[i]->name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200534 s = soinfo_elf_lookup(needed[i], elf_hash, name);
535 if (s != NULL) {
536 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200537 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200538 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700539 }
540
541done:
542 if(s != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700543 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = 0x%08x, "
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200544 "found in %s, base = 0x%08x, load bias = 0x%08x\n",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700545 si->name, name, s->st_value,
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200546 (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700547 return s;
548 }
549
Doug Kwan94304352009-10-23 18:11:40 -0700550 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700551}
552
553/* This is used by dl_sym(). It performs symbol lookup only within the
554 specified soinfo object and not in any of its dependencies.
555 */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200556Elf32_Sym *soinfo_lookup(soinfo *si, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800557{
David 'Digit' Turner16084162012-06-12 16:25:37 +0200558 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800559}
560
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700561/* This is used by dl_sym(). It performs a global symbol lookup.
562 */
Matt Fischer1698d9e2009-12-31 12:17:56 -0600563Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800564{
Doug Kwan94304352009-10-23 18:11:40 -0700565 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800566 Elf32_Sym *s = NULL;
567 soinfo *si;
568
Matt Fischer1698d9e2009-12-31 12:17:56 -0600569 if(start == NULL) {
570 start = solist;
571 }
572
573 for(si = start; (s == NULL) && (si != NULL); si = si->next)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800574 {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700575 if(si->flags & FLAG_ERROR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800576 continue;
David 'Digit' Turner16084162012-06-12 16:25:37 +0200577 s = soinfo_elf_lookup(si, elf_hash, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800578 if (s != NULL) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700579 *found = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800580 break;
581 }
582 }
583
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700584 if(s != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700585 TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, si->base = 0x%08x\n",
586 name, s->st_value, si->base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800587 return s;
588 }
589
Doug Kwan94304352009-10-23 18:11:40 -0700590 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800591}
592
Mathias Agopianbda5da02011-09-27 22:30:19 -0700593soinfo *find_containing_library(const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600594{
595 soinfo *si;
596
597 for(si = solist; si != NULL; si = si->next)
598 {
599 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
600 return si;
601 }
602 }
603
604 return NULL;
605}
606
David 'Digit' Turner16084162012-06-12 16:25:37 +0200607Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600608{
609 unsigned int i;
610 unsigned soaddr = (unsigned)addr - si->base;
611
612 /* Search the library's symbol table for any defined symbol which
613 * contains this address */
614 for(i=0; i<si->nchain; i++) {
615 Elf32_Sym *sym = &si->symtab[i];
616
617 if(sym->st_shndx != SHN_UNDEF &&
618 soaddr >= sym->st_value &&
619 soaddr < sym->st_value + sym->st_size) {
620 return sym;
621 }
622 }
623
624 return NULL;
625}
626
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800627#if 0
628static void dump(soinfo *si)
629{
630 Elf32_Sym *s = si->symtab;
631 unsigned n;
632
633 for(n = 0; n < si->nchain; n++) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700634 TRACE("%04d> %08x: %02x %04x %08x %08x %s\n", n, s,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800635 s->st_info, s->st_shndx, s->st_value, s->st_size,
636 si->strtab + s->st_name);
637 s++;
638 }
639}
640#endif
641
Elliott Hughes124fae92012-10-31 14:20:03 -0700642static int open_library_on_path(const char* name, const char* const paths[]) {
643 char buf[512];
644 for (size_t i = 0; paths[i] != NULL; ++i) {
645 int n = format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
646 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
647 WARN("Ignoring very long library path: %s/%s\n", paths[i], name);
648 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800649 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700650 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
651 if (fd != -1) {
652 return fd;
653 }
654 }
655 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800656}
657
Elliott Hughes124fae92012-10-31 14:20:03 -0700658static int open_library(const char* name) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700659 TRACE("[ opening %s ]\n", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800660
Elliott Hughes124fae92012-10-31 14:20:03 -0700661 // If the name contains a slash, we should attempt to open it directly and not search the paths.
662 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700663 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
664 if (fd != -1) {
665 return fd;
666 }
667 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700668 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800669
Elliott Hughes124fae92012-10-31 14:20:03 -0700670 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
671 int fd = open_library_on_path(name, gLdPaths);
672 if (fd == -1) {
673 fd = open_library_on_path(name, gSoPaths);
674 }
675 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800676}
677
Elliott Hughes46882792012-08-03 16:49:39 -0700678// Returns 'true' if the library is prelinked or on failure so we error out
679// either way. We no longer support prelinking.
680static bool is_prelinked(int fd, const char* name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800681{
Elliott Hughes46882792012-08-03 16:49:39 -0700682 struct prelink_info_t {
683 long mmap_addr;
684 char tag[4]; // "PRE ".
685 };
686
Elliott Hughesbedfe382012-08-14 14:07:59 -0700687 off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800688 if (sz < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700689 DL_ERR("lseek failed: %s", strerror(errno));
690 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800691 }
692
Elliott Hughesbedfe382012-08-14 14:07:59 -0700693 prelink_info_t info;
Elliott Hughes8dfc0732012-07-27 15:30:51 -0700694 int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
695 if (rc != sizeof(info)) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700696 DL_ERR("could not read prelink_info_t structure for \"%s\": %s", name, strerror(errno));
Elliott Hughes46882792012-08-03 16:49:39 -0700697 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800698 }
699
Elliott Hughes46882792012-08-03 16:49:39 -0700700 if (memcmp(info.tag, "PRE ", 4) == 0) {
701 DL_ERR("prelinked libraries no longer supported: %s", name);
702 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800703 }
Elliott Hughes46882792012-08-03 16:49:39 -0700704 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800705}
706
David 'Digit' Turner16084162012-06-12 16:25:37 +0200707/* verify_elf_header
708 * Verifies the content of an ELF header.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800709 *
710 * Args:
711 *
712 * Returns:
713 * 0 on success
714 * -1 if no valid ELF object is found @ base.
715 */
716static int
David 'Digit' Turner16084162012-06-12 16:25:37 +0200717verify_elf_header(const Elf32_Ehdr* hdr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800718{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800719 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
720 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
721 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
722 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700723 if (hdr->e_type != ET_DYN) return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800724
725 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800726#ifdef ANDROID_ARM_LINKER
727 if (hdr->e_machine != EM_ARM) return -1;
728#elif defined(ANDROID_X86_LINKER)
729 if (hdr->e_machine != EM_386) return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700730#elif defined(ANDROID_MIPS_LINKER)
731 if (hdr->e_machine != EM_MIPS) return -1;
Zhenghua Wang897815a2011-10-19 00:29:14 +0800732#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800733 return 0;
734}
735
Elliott Hughes46882792012-08-03 16:49:39 -0700736struct scoped_fd {
737 ~scoped_fd() {
738 if (fd != -1) {
739 close(fd);
740 }
741 }
742 int fd;
743};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800744
Elliott Hughes46882792012-08-03 16:49:39 -0700745struct soinfo_ptr {
746 soinfo_ptr(const char* name) {
747 const char* bname = strrchr(name, '/');
748 ptr = soinfo_alloc(bname ? bname + 1 : name);
749 }
750 ~soinfo_ptr() {
751 soinfo_free(ptr);
752 }
753 soinfo* release() {
754 soinfo* result = ptr;
755 ptr = NULL;
756 return result;
757 }
758 soinfo* ptr;
759};
760
761// TODO: rewrite linker_phdr.h to use a class, then lose this.
762struct phdr_ptr {
763 phdr_ptr() : phdr_mmap(NULL) {}
764 ~phdr_ptr() {
765 if (phdr_mmap != NULL) {
766 phdr_table_unload(phdr_mmap, phdr_size);
767 }
768 }
769 void* phdr_mmap;
770 Elf32_Addr phdr_size;
771};
772
Elliott Hughes124fae92012-10-31 14:20:03 -0700773static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700774 // Open the file.
775 scoped_fd fd;
776 fd.fd = open_library(name);
777 if (fd.fd == -1) {
778 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800779 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700780 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800781
Elliott Hughes46882792012-08-03 16:49:39 -0700782 // Read the ELF header.
783 Elf32_Ehdr header[1];
784 int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200785 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700786 DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
787 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200788 }
789 if (ret != (int)sizeof(header)) {
Elliott Hughes46882792012-08-03 16:49:39 -0700790 DL_ERR("too small to be an ELF executable: %s", name);
791 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200792 }
793 if (verify_elf_header(header) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700794 DL_ERR("not a valid ELF executable: %s", name);
795 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800796 }
797
Elliott Hughes46882792012-08-03 16:49:39 -0700798 // Read the program header table.
799 const Elf32_Phdr* phdr_table;
800 phdr_ptr phdr_holder;
801 ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
802 &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200803 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700804 DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
805 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200806 }
Elliott Hughes46882792012-08-03 16:49:39 -0700807 size_t phdr_count = header->e_phnum;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200808
Elliott Hughes46882792012-08-03 16:49:39 -0700809 // Get the load extents.
810 Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700811 TRACE("[ '%s' wants sz=0x%08x ]\n", name, ext_sz);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200812 if (ext_sz == 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700813 DL_ERR("no loadable segments in file: %s", name);
814 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800815 }
816
Elliott Hughes46882792012-08-03 16:49:39 -0700817 // We no longer support pre-linked libraries.
818 if (is_prelinked(fd.fd, name)) {
819 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200820 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200821
Elliott Hughes46882792012-08-03 16:49:39 -0700822 // Reserve address space for all loadable segments.
823 void* load_start = NULL;
824 Elf32_Addr load_size = 0;
825 Elf32_Addr load_bias = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200826 ret = phdr_table_reserve_memory(phdr_table,
827 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200828 &load_start,
829 &load_size,
830 &load_bias);
831 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700832 DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
833 ext_sz, name, strerror(errno));
834 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200835 }
836
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700837 TRACE("[ allocated memory for %s @ %p (0x%08x) ]\n", name, load_start, load_size);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200838
839 /* Map all the segments in our address space with default protections */
840 ret = phdr_table_load_segments(phdr_table,
841 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200842 load_bias,
Elliott Hughes46882792012-08-03 16:49:39 -0700843 fd.fd);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200844 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700845 DL_ERR("can't map loadable segments for \"%s\": %s",
846 name, strerror(errno));
847 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200848 }
849
Elliott Hughes46882792012-08-03 16:49:39 -0700850 soinfo_ptr si(name);
851 if (si.ptr == NULL) {
852 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800853 }
854
Elliott Hughes46882792012-08-03 16:49:39 -0700855 si.ptr->base = (Elf32_Addr) load_start;
856 si.ptr->size = load_size;
857 si.ptr->load_bias = load_bias;
858 si.ptr->flags = 0;
859 si.ptr->entry = 0;
860 si.ptr->dynamic = (unsigned *)-1;
861 si.ptr->phnum = phdr_count;
862 si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
863 if (si.ptr->phdr == NULL) {
864 DL_ERR("can't find loaded PHDR for \"%s\"", name);
865 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200866 }
Elliott Hughes46882792012-08-03 16:49:39 -0700867
868 return si.release();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800869}
870
Elliott Hughesd23736e2012-11-01 15:16:56 -0700871static soinfo* init_library(soinfo* si) {
872 // At this point we know that whatever is loaded @ base is a valid ELF
873 // shared library whose segments are properly mapped in.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700874 TRACE("[ init_library base=0x%08x sz=0x%08x name='%s') ]\n",
875 si->base, si->size, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800876
Elliott Hughesd23736e2012-11-01 15:16:56 -0700877 if (!soinfo_link_image(si)) {
878 munmap((void *)si->base, si->size);
879 return NULL;
880 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800881
Elliott Hughesd23736e2012-11-01 15:16:56 -0700882 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800883}
884
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200885static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800886{
887 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700888 const char *bname;
889
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200890 // TODO: don't use basename only for determining libraries
891 // http://code.google.com/p/android/issues/detail?id=6670
892
893 bname = strrchr(name, '/');
894 bname = bname ? bname + 1 : name;
895
896 for(si = solist; si != NULL; si = si->next){
897 if(!strcmp(bname, si->name)) {
898 return si;
899 }
900 }
901 return NULL;
902}
903
Elliott Hughesd23736e2012-11-01 15:16:56 -0700904static soinfo* find_library_internal(const char* name) {
905 if (name == NULL) {
906 return somain;
907 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200908
Elliott Hughesd23736e2012-11-01 15:16:56 -0700909 soinfo* si = find_loaded_library(name);
910 if (si != NULL) {
911 if (si->flags & FLAG_ERROR) {
912 DL_ERR("\"%s\" failed to load previously", name);
913 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800914 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700915 if (si->flags & FLAG_LINKED) {
916 return si;
917 }
918 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
919 return NULL;
920 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800921
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700922 TRACE("[ '%s' has not been loaded yet. Locating...]\n", name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700923 si = load_library(name);
924 if (si != NULL) {
925 si = init_library(si);
926 }
927
928 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800929}
930
Elliott Hughesd23736e2012-11-01 15:16:56 -0700931static soinfo* find_library(const char* name) {
932 soinfo* si = find_library_internal(name);
933 if (si != NULL) {
934 si->refcount++;
935 }
936 return si;
937}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700938
Elliott Hughesd23736e2012-11-01 15:16:56 -0700939static int soinfo_unload(soinfo* si) {
940 if (si->refcount == 1) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700941 TRACE("unloading '%s'\n", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700942 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800943
Elliott Hughesd23736e2012-11-01 15:16:56 -0700944 for (unsigned* d = si->dynamic; *d; d += 2) {
945 if (d[0] == DT_NEEDED) {
946 soinfo* lsi = find_loaded_library(si->strtab + d[1]);
947 if (lsi != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700948 TRACE("%s needs to unload %s\n", si->name, lsi->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700949 soinfo_unload(lsi);
950 } else {
951 // TODO: should we return -1 in this case?
952 DL_ERR("\"%s\": could not unload dependent library", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800953 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700954 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800955 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700956
957 munmap(reinterpret_cast<void*>(si->base), si->size);
958 notify_gdb_of_unload(si);
959 soinfo_free(si);
960 si->refcount = 0;
961 } else {
962 si->refcount--;
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700963 PRINT("not unloading '%s', decrementing refcount to %d\n", si->name, si->refcount);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700964 }
965 return 0;
966}
967
968soinfo* do_dlopen(const char* name) {
969 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
970 soinfo* si = find_library(name);
971 if (si != NULL) {
972 si->CallConstructors();
973 }
974 set_soinfo_pool_protection(PROT_READ);
975 return si;
976}
977
978int do_dlclose(soinfo* si) {
979 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
980 int result = soinfo_unload(si);
981 set_soinfo_pool_protection(PROT_READ);
982 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800983}
984
985/* TODO: don't use unsigned for addrs below. It works, but is not
986 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
987 * long.
988 */
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200989static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count,
990 soinfo *needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800991{
992 Elf32_Sym *symtab = si->symtab;
993 const char *strtab = si->strtab;
994 Elf32_Sym *s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800995 Elf32_Rel *start = rel;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200996 soinfo *lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800997
Elliott Hughes46882792012-08-03 16:49:39 -0700998 for (size_t idx = 0; idx < count; ++idx, ++rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800999 unsigned type = ELF32_R_TYPE(rel->r_info);
1000 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001001 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001002 unsigned sym_addr = 0;
1003 char *sym_name = NULL;
1004
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001005 DEBUG("Processing '%s' relocation at index %d\n", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001006 if (type == 0) { // R_*_NONE
1007 continue;
1008 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001009 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001010 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001011 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Doug Kwane8238072009-10-26 12:05:23 -07001012 if(s == NULL) {
1013 /* We only allow an undefined symbol if this is a weak
1014 reference.. */
1015 s = &symtab[sym];
1016 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -07001017 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -07001018 return -1;
1019 }
1020
1021 /* IHI0044C AAELF 4.5.1.1:
1022
1023 Libraries are not searched to resolve weak references.
1024 It is not an error for a weak reference to remain
1025 unsatisfied.
1026
1027 During linking, the value of an undefined weak reference is:
1028 - Zero if the relocation type is absolute
1029 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -07001030 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -07001031 type is base-relative.
1032 */
1033
1034 switch (type) {
1035#if defined(ANDROID_ARM_LINKER)
1036 case R_ARM_JUMP_SLOT:
1037 case R_ARM_GLOB_DAT:
1038 case R_ARM_ABS32:
1039 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -07001040#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001041 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001042 case R_386_GLOB_DAT:
1043 case R_386_32:
1044 case R_386_RELATIVE: /* Dont' care. */
1045#endif /* ANDROID_*_LINKER */
1046 /* sym_addr was initialized to be zero above or relocation
1047 code below does not care about value of sym_addr.
1048 No need to do anything. */
1049 break;
1050
1051#if defined(ANDROID_X86_LINKER)
1052 case R_386_PC32:
1053 sym_addr = reloc;
1054 break;
1055#endif /* ANDROID_X86_LINKER */
1056
1057#if defined(ANDROID_ARM_LINKER)
1058 case R_ARM_COPY:
1059 /* Fall through. Can't really copy if weak symbol is
1060 not found in run-time. */
1061#endif /* ANDROID_ARM_LINKER */
1062 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001063 DL_ERR("unknown weak reloc type %d @ %p (%d)",
1064 type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -07001065 return -1;
1066 }
1067 } else {
1068 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001069#if 0
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001070 if((base == 0) && (si->base != 0)){
1071 /* linking from libraries to main image is bad */
Elliott Hughes46882792012-08-03 16:49:39 -07001072 DL_ERR("cannot locate \"%s\"...",
1073 strtab + symtab[sym].st_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001074 return -1;
1075 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001076#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001077 sym_addr = (unsigned)(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001078 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001079 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001080 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001081 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001082 }
1083
1084/* TODO: This is ugly. Split up the relocations by arch into
1085 * different files.
1086 */
1087 switch(type){
1088#if defined(ANDROID_ARM_LINKER)
1089 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001090 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001091 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001092 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 -08001093 *((unsigned*)reloc) = sym_addr;
1094 break;
1095 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001096 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001097 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001098 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 -08001099 *((unsigned*)reloc) = sym_addr;
1100 break;
1101 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001102 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001103 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001104 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001105 *((unsigned*)reloc) += sym_addr;
1106 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001107 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001108 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001109 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001110 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s\n",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001111 reloc, sym_addr, rel->r_offset, sym_name);
1112 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1113 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001114#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001115 case R_386_JMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001116 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001117 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001118 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 -08001119 *((unsigned*)reloc) = sym_addr;
1120 break;
1121 case R_386_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001122 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001123 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001124 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 -08001125 *((unsigned*)reloc) = sym_addr;
1126 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001127#elif defined(ANDROID_MIPS_LINKER)
1128 case R_MIPS_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001129 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001130 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001131 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001132 *((unsigned*)reloc) = sym_addr;
1133 break;
1134 case R_MIPS_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001135 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001136 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001137 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s\n",
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001138 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1139 if (s) {
1140 *((unsigned*)reloc) += sym_addr;
1141 } else {
1142 *((unsigned*)reloc) += si->base;
1143 }
1144 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001145#endif /* ANDROID_*_LINKER */
1146
1147#if defined(ANDROID_ARM_LINKER)
1148 case R_ARM_RELATIVE:
1149#elif defined(ANDROID_X86_LINKER)
1150 case R_386_RELATIVE:
1151#endif /* ANDROID_*_LINKER */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001152 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001153 MARK(rel->r_offset);
Elliott Hughes46882792012-08-03 16:49:39 -07001154 if (sym) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001155 DL_ERR("odd RELATIVE form...");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001156 return -1;
1157 }
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001158 TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x\n", reloc, si->base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001159 *((unsigned*)reloc) += si->base;
1160 break;
1161
1162#if defined(ANDROID_X86_LINKER)
1163 case R_386_32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001164 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001165 MARK(rel->r_offset);
1166
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001167 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 -08001168 *((unsigned *)reloc) += (unsigned)sym_addr;
1169 break;
1170
1171 case R_386_PC32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001172 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001173 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001174 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s\n",
1175 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001176 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1177 break;
1178#endif /* ANDROID_X86_LINKER */
1179
1180#ifdef ANDROID_ARM_LINKER
1181 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001182 if ((si->flags & FLAG_EXE) == 0) {
1183 /*
1184 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1185 *
1186 * Section 4.7.1.10 "Dynamic relocations"
1187 * R_ARM_COPY may only appear in executable objects where e_type is
1188 * set to ET_EXEC.
1189 *
1190 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1191 * We should explicitly disallow ET_DYN executables from having
1192 * R_ARM_COPY relocations.
1193 */
1194 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1195 return -1;
1196 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001197 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001198 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001199 TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s\n", reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001200 if (reloc == sym_addr) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001201 Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1202
1203 if (src == NULL) {
1204 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1205 return -1;
1206 }
1207 if (lsi->has_DT_SYMBOLIC) {
1208 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1209 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1210 return -1;
1211 }
1212 if (s->st_size < src->st_size) {
1213 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1214 si->name, s->st_size, src->st_size);
1215 return -1;
1216 }
1217 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1218 } else {
1219 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001220 return -1;
1221 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001222 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001223#endif /* ANDROID_ARM_LINKER */
1224
1225 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001226 DL_ERR("unknown reloc type %d @ %p (%d)",
1227 type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001228 return -1;
1229 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001230 }
1231 return 0;
1232}
1233
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001234#ifdef ANDROID_MIPS_LINKER
Elliott Hughesbedfe382012-08-14 14:07:59 -07001235static int mips_relocate_got(soinfo* si, soinfo* needed[]) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001236 unsigned *got;
1237 unsigned local_gotno, gotsym, symtabno;
1238 Elf32_Sym *symtab, *sym;
1239 unsigned g;
1240
1241 got = si->plt_got;
1242 local_gotno = si->mips_local_gotno;
1243 gotsym = si->mips_gotsym;
1244 symtabno = si->mips_symtabno;
1245 symtab = si->symtab;
1246
1247 /*
1248 * got[0] is address of lazy resolver function
1249 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001250 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001251 * (should be _rtld_bind_start)
1252 * FIXME: maybe this should be in a separate routine
1253 */
1254
1255 if ((si->flags & FLAG_LINKER) == 0) {
1256 g = 0;
1257 got[g++] = 0xdeadbeef;
1258 if (got[g] & 0x80000000) {
1259 got[g++] = 0xdeadfeed;
1260 }
1261 /*
1262 * Relocate the local GOT entries need to be relocated
1263 */
1264 for (; g < local_gotno; g++) {
1265 got[g] += si->load_bias;
1266 }
1267 }
1268
1269 /* Now for the global GOT entries */
1270 sym = symtab + gotsym;
1271 got = si->plt_got + local_gotno;
1272 for (g = gotsym; g < symtabno; g++, sym++, got++) {
1273 const char *sym_name;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001274 Elf32_Sym *s;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001275 soinfo *lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001276
1277 /* This is an undefined reference... try to locate it */
1278 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001279 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001280 if (s == NULL) {
1281 /* We only allow an undefined symbol if this is a weak
1282 reference.. */
1283 s = &symtab[g];
1284 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001285 DL_ERR("cannot locate \"%s\"...", sym_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001286 return -1;
1287 }
1288 *got = 0;
1289 }
1290 else {
1291 /* FIXME: is this sufficient?
1292 * For reference see NetBSD link loader
1293 * 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
1294 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001295 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001296 }
1297 }
1298 return 0;
1299}
1300#endif
1301
David 'Digit' Turner82156792009-05-18 14:37:41 +02001302/* Please read the "Initialization and Termination functions" functions.
1303 * of the linker design note in bionic/linker/README.TXT to understand
1304 * what the following code is doing.
1305 *
1306 * The important things to remember are:
1307 *
1308 * DT_PREINIT_ARRAY must be called first for executables, and should
1309 * not appear in shared libraries.
1310 *
1311 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1312 *
1313 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1314 *
1315 * DT_FINI_ARRAY must be parsed in reverse order.
1316 */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001317void soinfo::CallArray(const char* array_name UNUSED, unsigned* array, int count, bool reverse) {
1318 if (array == NULL) {
1319 return;
1320 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001321
Elliott Hughesd23736e2012-11-01 15:16:56 -07001322 int step = 1;
1323 if (reverse) {
1324 array += (count-1);
1325 step = -1;
1326 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001327
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001328 TRACE("[ Calling %s @ %p [%d] for '%s' ]\n", array_name, array, count, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001329
1330 for (int n = count; n > 0; n--) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001331 TRACE("[ Looking at %s[%d] *%p == 0x%08x ]\n", array_name, n, array, *array);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001332 void (*func)() = (void (*)()) *array;
1333 array += step;
1334 if (((int) func == 0) || ((int) func == -1)) {
1335 continue;
David 'Digit' Turner82156792009-05-18 14:37:41 +02001336 }
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001337 TRACE("[ Calling func @ %p ]\n", func);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001338 func();
1339 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001340
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001341 TRACE("[ Done calling %s for '%s' ]\n", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001342}
1343
Elliott Hughesd23736e2012-11-01 15:16:56 -07001344void soinfo::CallFunction(const char* function_name UNUSED, void (*function)()) {
1345 if (function == NULL) {
1346 return;
1347 }
1348
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001349 TRACE("[ Calling %s @ %p for '%s' ]\n", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001350 function();
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001351 TRACE("[ Done calling %s for '%s' ]\n", function_name, name);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001352}
1353
Elliott Hughesd23736e2012-11-01 15:16:56 -07001354void soinfo::CallPreInitConstructors() {
1355 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1356}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001357
Elliott Hughesd23736e2012-11-01 15:16:56 -07001358void soinfo::CallConstructors() {
1359 if (constructors_called) {
1360 return;
1361 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001362
Elliott Hughesd23736e2012-11-01 15:16:56 -07001363 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1364 // protect against recursive constructor calls. One simple example of constructor recursion
1365 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1366 // 1. The program depends on libc, so libc's constructor is called here.
1367 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1368 // 3. dlopen() calls the constructors on the newly created
1369 // soinfo for libc_malloc_debug_leak.so.
1370 // 4. The debug .so depends on libc, so CallConstructors is
1371 // called again with the libc soinfo. If it doesn't trigger the early-
1372 // out above, the libc constructor will be called again (recursively!).
1373 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001374
Elliott Hughesd23736e2012-11-01 15:16:56 -07001375 if (!(flags & FLAG_EXE) && preinit_array) {
1376 DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array);
1377 return;
1378 }
1379
1380 if (dynamic) {
1381 for (unsigned* d = dynamic; *d; d += 2) {
1382 if (d[0] == DT_NEEDED) {
1383 soinfo* lsi = find_loaded_library(strtab + d[1]);
1384 if (lsi == NULL) {
1385 DL_ERR("\"%s\": could not initialize dependent library", name);
1386 } else {
1387 lsi->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001388 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001389 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001390 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001391 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001392
Elliott Hughesd23736e2012-11-01 15:16:56 -07001393 CallFunction("DT_INIT", init_func);
1394 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001395}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001396
Elliott Hughesd23736e2012-11-01 15:16:56 -07001397void soinfo::CallDestructors() {
1398 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
1399 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001400}
1401
1402/* Force any of the closed stdin, stdout and stderr to be associated with
1403 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001404static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001405 int dev_null, i, status;
1406 int return_value = 0;
1407
David 'Digit' Turner16084162012-06-12 16:25:37 +02001408 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001409 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001410 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001411 return -1;
1412 }
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001413 TRACE("[ Opened /dev/null file-descriptor=%d]\n", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001414
1415 /* If any of the stdio file descriptors is valid and not associated
1416 with /dev/null, dup /dev/null to it. */
1417 for (i = 0; i < 3; i++) {
1418 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001419 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001420 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001421 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001422
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001423 TRACE("[ Nullifying stdio file descriptor %d]\n", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001424 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001425
Elliott Hughes46882792012-08-03 16:49:39 -07001426 /* If file is opened, we are good. */
1427 if (status != -1) {
1428 continue;
1429 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001430
1431 /* The only error we allow is that the file descriptor does not
1432 exist, in which case we dup /dev/null to it. */
1433 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001434 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001435 return_value = -1;
1436 continue;
1437 }
1438
1439 /* Try dupping /dev/null to this stdio file descriptor and
1440 repeat if there is a signal. Note that any errors in closing
1441 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001442 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001443 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001444 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001445 return_value = -1;
1446 continue;
1447 }
1448 }
1449
1450 /* If /dev/null is not one of the stdio file descriptors, close it. */
1451 if (dev_null > 2) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001452 TRACE("[ Closing /dev/null file-descriptor=%d]\n", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001453 status = TEMP_FAILURE_RETRY(close(dev_null));
1454 if (status == -1) {
1455 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001456 return_value = -1;
1457 }
1458 }
1459
1460 return return_value;
1461}
1462
Elliott Hughes124fae92012-10-31 14:20:03 -07001463static bool soinfo_link_image(soinfo* si) {
1464 si->flags |= FLAG_ERROR;
1465
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001466 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001467 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001468 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001469 int phnum = si->phnum;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001470 int relocating_linker = (si->flags & FLAG_LINKER) != 0;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001471 soinfo **needed, **pneeded;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001472
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001473 /* We can't debug anything until the linker is relocated */
1474 if (!relocating_linker) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001475 INFO("[ linking %s ]\n", si->name);
1476 DEBUG("si->base = 0x%08x si->flags = 0x%08x\n", si->base, si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001477 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001478
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001479 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001480 size_t dynamic_count;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001481 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
1482 &dynamic_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001483 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001484 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001485 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001486 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001487 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001488 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001489 if (!relocating_linker) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001490 DEBUG("dynamic = %p\n", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001491 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001492 }
1493
1494#ifdef ANDROID_ARM_LINKER
1495 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1496 &si->ARM_exidx, &si->ARM_exidx_count);
1497#endif
1498
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001499 /* extract useful information from dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001500 for (unsigned* d = si->dynamic; *d; ++d) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001501 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 -08001502 switch(*d++){
1503 case DT_HASH:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001504 si->nbucket = ((unsigned *) (base + *d))[0];
1505 si->nchain = ((unsigned *) (base + *d))[1];
1506 si->bucket = (unsigned *) (base + *d + 8);
1507 si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001508 break;
1509 case DT_STRTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001510 si->strtab = (const char *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001511 break;
1512 case DT_SYMTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001513 si->symtab = (Elf32_Sym *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001514 break;
1515 case DT_PLTREL:
1516 if(*d != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001517 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1518 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001519 }
1520 break;
1521 case DT_JMPREL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001522 si->plt_rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001523 break;
1524 case DT_PLTRELSZ:
1525 si->plt_rel_count = *d / 8;
1526 break;
1527 case DT_REL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001528 si->rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001529 break;
1530 case DT_RELSZ:
1531 si->rel_count = *d / 8;
1532 break;
1533 case DT_PLTGOT:
1534 /* Save this in case we decide to do lazy binding. We don't yet. */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001535 si->plt_got = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001536 break;
1537 case DT_DEBUG:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001538#if !defined(ANDROID_MIPS_LINKER)
Elliott Hughesbedfe382012-08-14 14:07:59 -07001539 // Set the DT_DEBUG entry to the address of _r_debug for GDB
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001540 *d = (int) &_r_debug;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001541#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001542 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001543 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001544 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1545 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001546 case DT_INIT:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001547 si->init_func = (void (*)(void))(base + *d);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001548 DEBUG("%s constructors (init func) found at %p\n", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001549 break;
1550 case DT_FINI:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001551 si->fini_func = (void (*)(void))(base + *d);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001552 DEBUG("%s destructors (fini func) found at %p\n", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001553 break;
1554 case DT_INIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001555 si->init_array = (unsigned *)(base + *d);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001556 DEBUG("%s constructors (init_array) found at %p\n", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001557 break;
1558 case DT_INIT_ARRAYSZ:
1559 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1560 break;
1561 case DT_FINI_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001562 si->fini_array = (unsigned *)(base + *d);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001563 DEBUG("%s destructors (fini_array) found at %p\n", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001564 break;
1565 case DT_FINI_ARRAYSZ:
1566 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1567 break;
1568 case DT_PREINIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001569 si->preinit_array = (unsigned *)(base + *d);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001570 DEBUG("%s constructors (preinit_array) found at %p\n", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001571 break;
1572 case DT_PREINIT_ARRAYSZ:
1573 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1574 break;
1575 case DT_TEXTREL:
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001576 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001577 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001578 case DT_SYMBOLIC:
1579 si->has_DT_SYMBOLIC = true;
1580 break;
1581#if defined(DT_FLAGS)
1582 case DT_FLAGS:
1583 if (*d & DF_TEXTREL) {
1584 si->has_text_relocations = true;
1585 }
1586 if (*d & DF_SYMBOLIC) {
1587 si->has_DT_SYMBOLIC = true;
1588 }
1589 break;
1590#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001591#if defined(ANDROID_MIPS_LINKER)
1592 case DT_NEEDED:
1593 case DT_STRSZ:
1594 case DT_SYMENT:
1595 case DT_RELENT:
1596 break;
1597 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001598 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001599 {
Elliott Hughesbedfe382012-08-14 14:07:59 -07001600 r_debug** dp = (r_debug**) *d;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001601 *dp = &_r_debug;
1602 }
1603 break;
1604 case DT_MIPS_RLD_VERSION:
1605 case DT_MIPS_FLAGS:
1606 case DT_MIPS_BASE_ADDRESS:
1607 case DT_MIPS_UNREFEXTNO:
1608 case DT_MIPS_RWPLT:
1609 break;
1610
1611 case DT_MIPS_PLTGOT:
1612#if 0
1613 /* not yet... */
1614 si->mips_pltgot = (unsigned *)(si->base + *d);
1615#endif
1616 break;
1617
1618 case DT_MIPS_SYMTABNO:
1619 si->mips_symtabno = *d;
1620 break;
1621
1622 case DT_MIPS_LOCAL_GOTNO:
1623 si->mips_local_gotno = *d;
1624 break;
1625
1626 case DT_MIPS_GOTSYM:
1627 si->mips_gotsym = *d;
1628 break;
1629
1630 default:
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001631 DEBUG("Unused DT entry: type 0x%08x arg 0x%08x\n", d[-1], d[0]);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001632 break;
1633#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001634 }
1635 }
1636
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001637 DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
1638 si->base, si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001639
Elliott Hughes124fae92012-10-31 14:20:03 -07001640 // Sanity checks.
1641 if (si->nbucket == 0) {
1642 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1643 return false;
1644 }
1645 if (si->strtab == 0) {
1646 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1647 return false;
1648 }
1649 if (si->symtab == 0) {
1650 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1651 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652 }
1653
Matt Fischer4fd42c12009-12-31 12:09:10 -06001654 /* if this is the main executable, then load all of the preloads now */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001655 if (si->flags & FLAG_EXE) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001656 memset(preloads, 0, sizeof(preloads));
Elliott Hughesd23736e2012-11-01 15:16:56 -07001657 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1658 soinfo* lsi = find_library(gLdPreloadNames[i]);
1659 if (lsi == NULL) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001660 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001661 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Elliott Hughes124fae92012-10-31 14:20:03 -07001662 gLdPreloadNames[i], si->name, tmp_err_buf);
1663 return false;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001664 }
Matt Fischer4fd42c12009-12-31 12:09:10 -06001665 preloads[i] = lsi;
1666 }
1667 }
1668
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001669 /* dynamic_count is an upper bound for the number of needed libs */
1670 pneeded = needed = (soinfo**) alloca((1 + dynamic_count) * sizeof(soinfo*));
1671
Elliott Hughes124fae92012-10-31 14:20:03 -07001672 for (unsigned* d = si->dynamic; *d; d += 2) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001673 if (d[0] == DT_NEEDED) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001674 DEBUG("%s needs %s\n", si->name, si->strtab + d[1]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001675 soinfo* lsi = find_library(si->strtab + d[1]);
1676 if (lsi == NULL) {
Dima Zavin03531952009-05-29 17:30:25 -07001677 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001678 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1679 si->strtab + d[1], si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001680 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001681 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001682 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001683 }
1684 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001685 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001686
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001687 if (si->has_text_relocations) {
1688 /* Unprotect the segments, i.e. make them writable, to allow
1689 * text relocations to work properly. We will later call
1690 * phdr_table_protect_segments() after all of them are applied
1691 * and all constructors are run.
1692 */
1693 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1694 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1695 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001696 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001697 }
1698 }
1699
Elliott Hughes124fae92012-10-31 14:20:03 -07001700 if (si->plt_rel) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001701 DEBUG("[ relocating %s plt ]\n", si->name );
Elliott Hughes124fae92012-10-31 14:20:03 -07001702 if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1703 return false;
1704 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001705 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001706 if (si->rel) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001707 DEBUG("[ relocating %s ]\n", si->name );
Elliott Hughes124fae92012-10-31 14:20:03 -07001708 if(soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1709 return false;
1710 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001711 }
1712
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001713#ifdef ANDROID_MIPS_LINKER
Elliott Hughes124fae92012-10-31 14:20:03 -07001714 if (mips_relocate_got(si, needed)) {
1715 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001716 }
1717#endif
1718
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001719 si->flags |= FLAG_LINKED;
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001720 DEBUG("[ finished linking %s ]\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001721
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001722 if (si->has_text_relocations) {
1723 /* All relocations are done, we can protect our segments back to
1724 * read-only. */
1725 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1726 DL_ERR("can't protect segments for \"%s\": %s",
1727 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001728 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001729 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001730 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001731
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001732 /* We can also turn on GNU RELRO protection */
1733 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001734 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1735 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001736 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001737 }
1738
Elliott Hughes124fae92012-10-31 14:20:03 -07001739 // If this is a setuid/setgid program, close the security hole described in
1740 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
Elliott Hughes18a206c2012-10-29 17:37:13 -07001741 if (get_AT_SECURE()) {
Elliott Hughes46882792012-08-03 16:49:39 -07001742 nullify_closed_stdio();
1743 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001744 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001745 si->flags &= ~FLAG_ERROR;
1746 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001747}
1748
Elliott Hughes46882792012-08-03 16:49:39 -07001749static void parse_path(const char* path, const char* delimiters,
1750 const char** array, char* buf, size_t buf_size, size_t max_count)
David Bartleybc3a5c22009-06-02 18:27:28 -07001751{
Elliott Hughes46882792012-08-03 16:49:39 -07001752 if (path == NULL) {
1753 return;
David Bartleybc3a5c22009-06-02 18:27:28 -07001754 }
1755
Elliott Hughes46882792012-08-03 16:49:39 -07001756 size_t len = strlcpy(buf, path, buf_size);
David Bartleybc3a5c22009-06-02 18:27:28 -07001757
Elliott Hughes46882792012-08-03 16:49:39 -07001758 size_t i = 0;
1759 char* buf_p = buf;
1760 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
1761 if (*array[i] != '\0') {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001762 ++i;
1763 }
1764 }
1765
Elliott Hughes46882792012-08-03 16:49:39 -07001766 // Forget the last path if we had to truncate; this occurs if the 2nd to
1767 // last char isn't '\0' (i.e. wasn't originally a delimiter).
1768 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
1769 array[i - 1] = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001770 } else {
Elliott Hughes46882792012-08-03 16:49:39 -07001771 array[i] = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001772 }
1773}
1774
Elliott Hughes46882792012-08-03 16:49:39 -07001775static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001776 parse_path(path, ":", gLdPaths,
1777 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
Elliott Hughes46882792012-08-03 16:49:39 -07001778}
1779
1780static void parse_LD_PRELOAD(const char* path) {
1781 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes124fae92012-10-31 14:20:03 -07001782 parse_path(path, " :", gLdPreloadNames,
1783 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
Elliott Hughes46882792012-08-03 16:49:39 -07001784}
1785
Nick Kralevich468319c2011-11-11 15:53:17 -08001786/*
1787 * This code is called after the linker has linked itself and
1788 * fixed it's own GOT. It is safe to make references to externs
1789 * and other non-local data at this point.
1790 */
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001791static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001792{
1793 static soinfo linker_soinfo;
1794
1795 int argc = (int) *elfdata;
1796 char **argv = (char**) (elfdata + 1);
Stephen Smalleybb440552012-01-20 10:59:15 -08001797 unsigned *vecs = (unsigned*) (argv + argc + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001798
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001799 /* NOTE: we store the elfdata pointer on a special location
1800 * of the temporary TLS area in order to pass it to
1801 * the C Library's runtime initializer.
1802 *
1803 * The initializer must clear the slot and reset the TLS
1804 * to point to a different location to ensure that no other
1805 * shared library constructor can access it.
1806 */
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001807 __libc_init_tls(elfdata);
1808
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001809#if TIMING
1810 struct timeval t0, t1;
1811 gettimeofday(&t0, 0);
1812#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001813
Elliott Hughes18a206c2012-10-29 17:37:13 -07001814 // Initialize environment functions, and get to the ELF aux vectors table.
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001815 vecs = linker_env_init(vecs);
1816
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001817 debugger_init();
1818
Elliott Hughes18a206c2012-10-29 17:37:13 -07001819 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001820 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1821 if (LD_DEBUG != NULL) {
1822 debug_verbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001823 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001824
Elliott Hughes18a206c2012-10-29 17:37:13 -07001825 // Normally, these are cleaned by linker_env_init, but the test
1826 // doesn't cost us anything.
1827 const char* ldpath_env = NULL;
1828 const char* ldpreload_env = NULL;
1829 if (!get_AT_SECURE()) {
1830 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1831 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001832 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001833
1834 INFO("[ android linker & debugger ]\n");
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001835 DEBUG("elfdata @ 0x%08x\n", (unsigned)elfdata);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001836
Elliott Hughes18a206c2012-10-29 17:37:13 -07001837 soinfo* si = soinfo_alloc(argv[0]);
1838 if (si == NULL) {
1839 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001840 }
1841
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001842 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001843 si->flags |= FLAG_EXE;
Elliott Hughesbedfe382012-08-14 14:07:59 -07001844 link_map* map = &(si->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001845
1846 map->l_addr = 0;
1847 map->l_name = argv[0];
1848 map->l_prev = NULL;
1849 map->l_next = NULL;
1850
1851 _r_debug.r_map = map;
1852 r_debug_tail = map;
1853
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001854 /* gdb expects the linker to be in the debug shared object list.
1855 * Without this, gdb has trouble locating the linker's ".text"
1856 * and ".plt" sections. Gdb could also potentially use this to
1857 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1858 * Don't use soinfo_alloc(), because the linker shouldn't
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001859 * be on the soinfo list.
1860 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001861 strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001862 linker_soinfo.flags = 0;
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001863 linker_soinfo.base = linker_base;
Ben Cheng06f0e742012-08-10 16:07:02 -07001864 /*
1865 * Set the dynamic field in the link map otherwise gdb will complain with
1866 * the following:
1867 * warning: .dynamic section for "/system/bin/linker" is not at the
1868 * expected address (wrong library or version mismatch?)
1869 */
1870 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
1871 Elf32_Phdr *phdr =
1872 (Elf32_Phdr *)((unsigned char *) linker_base + elf_hdr->e_phoff);
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001873 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1874 &linker_soinfo.dynamic, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001875 insert_soinfo_into_debug_map(&linker_soinfo);
1876
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001877 /* extract information passed from the kernel */
Elliott Hughes18a206c2012-10-29 17:37:13 -07001878 while (vecs[0] != 0){
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001879 switch(vecs[0]){
1880 case AT_PHDR:
1881 si->phdr = (Elf32_Phdr*) vecs[1];
1882 break;
1883 case AT_PHNUM:
1884 si->phnum = (int) vecs[1];
1885 break;
1886 case AT_ENTRY:
1887 si->entry = vecs[1];
1888 break;
1889 }
1890 vecs += 2;
1891 }
1892
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001893 /* Compute the value of si->base. We can't rely on the fact that
1894 * the first entry is the PHDR because this will not be true
1895 * for certain executables (e.g. some in the NDK unit test suite)
1896 */
1897 int nn;
1898 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001899 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001900 si->load_bias = 0;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001901 for ( nn = 0; nn < si->phnum; nn++ ) {
1902 if (si->phdr[nn].p_type == PT_PHDR) {
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001903 si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
1904 si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001905 break;
1906 }
1907 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001908 si->dynamic = (unsigned *)-1;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001909 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001910
Elliott Hughes46882792012-08-03 16:49:39 -07001911 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1912 parse_LD_LIBRARY_PATH(ldpath_env);
1913 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001914
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001915 somain = si;
1916
Elliott Hughes124fae92012-10-31 14:20:03 -07001917 if (!soinfo_link_image(si)) {
Dima Zavin2e855792009-05-20 18:28:09 -07001918 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
1919 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
1920 write(2, errmsg, sizeof(errmsg));
Elliott Hughes18a206c2012-10-29 17:37:13 -07001921 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001922 }
1923
Elliott Hughesd23736e2012-11-01 15:16:56 -07001924 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001925
Elliott Hughes18a206c2012-10-29 17:37:13 -07001926 for (size_t i = 0; preloads[i] != NULL; ++i) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001927 preloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08001928 }
1929
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08001930 /*After the link_image, the si->base is initialized.
1931 *For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1932 *We need to update this value for so exe here. So Unwind_Backtrace
1933 *for some arch like x86 could work correctly within so exe.
1934 */
1935 map->l_addr = si->base;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001936 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001937
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001938#if TIMING
1939 gettimeofday(&t1,NULL);
1940 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
1941 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1942 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1943 ));
1944#endif
1945#if STATS
1946 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07001947 linker_stats.count[kRelocAbsolute],
1948 linker_stats.count[kRelocRelative],
1949 linker_stats.count[kRelocCopy],
1950 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001951#endif
1952#if COUNT_PAGES
1953 {
1954 unsigned n;
1955 unsigned i;
1956 unsigned count = 0;
1957 for(n = 0; n < 4096; n++){
1958 if(bitmask[n]){
1959 unsigned x = bitmask[n];
1960 for(i = 0; i < 8; i++){
1961 if(x & 1) count++;
1962 x >>= 1;
1963 }
1964 }
1965 }
1966 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
1967 }
1968#endif
1969
1970#if TIMING || STATS || COUNT_PAGES
1971 fflush(stdout);
1972#endif
1973
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001974 TRACE("[ Ready to execute '%s' @ 0x%08x ]\n", si->name, si->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001975 return si->entry;
1976}
Nick Kralevich468319c2011-11-11 15:53:17 -08001977
1978/*
1979 * Find the value of AT_BASE passed to us by the kernel. This is the load
1980 * location of the linker.
1981 */
1982static unsigned find_linker_base(unsigned **elfdata) {
1983 int argc = (int) *elfdata;
1984 char **argv = (char**) (elfdata + 1);
1985 unsigned *vecs = (unsigned*) (argv + argc + 1);
1986 while (vecs[0] != 0) {
1987 vecs++;
1988 }
1989
1990 /* The end of the environment block is marked by two NULL pointers */
1991 vecs++;
1992
1993 while(vecs[0]) {
1994 if (vecs[0] == AT_BASE) {
1995 return vecs[1];
1996 }
1997 vecs += 2;
1998 }
1999
2000 return 0; // should never happen
2001}
2002
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002003/* Compute the load-bias of an existing executable. This shall only
2004 * be used to compute the load bias of an executable or shared library
2005 * that was loaded by the kernel itself.
2006 *
2007 * Input:
2008 * elf -> address of ELF header, assumed to be at the start of the file.
2009 * Return:
2010 * load bias, i.e. add the value of any p_vaddr in the file to get
2011 * the corresponding address in memory.
2012 */
2013static Elf32_Addr
2014get_elf_exec_load_bias(const Elf32_Ehdr* elf)
2015{
2016 Elf32_Addr offset = elf->e_phoff;
2017 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
2018 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
2019 const Elf32_Phdr* phdr;
2020
2021 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
2022 if (phdr->p_type == PT_LOAD) {
2023 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
2024 }
2025 }
2026 return 0;
2027}
2028
Nick Kralevich468319c2011-11-11 15:53:17 -08002029/*
2030 * This is the entry point for the linker, called from begin.S. This
2031 * method is responsible for fixing the linker's own relocations, and
2032 * then calling __linker_init_post_relocation().
2033 *
2034 * Because this method is called before the linker has fixed it's own
2035 * relocations, any attempt to reference an extern variable, extern
2036 * function, or other GOT reference will generate a segfault.
2037 */
Elliott Hughes46882792012-08-03 16:49:39 -07002038extern "C" unsigned __linker_init(unsigned **elfdata) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002039 unsigned linker_addr = find_linker_base(elfdata);
2040 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2041 Elf32_Phdr *phdr =
2042 (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2043
2044 soinfo linker_so;
2045 memset(&linker_so, 0, sizeof(soinfo));
2046
2047 linker_so.base = linker_addr;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002048 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002049 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Nick Kralevich468319c2011-11-11 15:53:17 -08002050 linker_so.dynamic = (unsigned *) -1;
2051 linker_so.phdr = phdr;
2052 linker_so.phnum = elf_hdr->e_phnum;
2053 linker_so.flags |= FLAG_LINKER;
Nick Kralevich468319c2011-11-11 15:53:17 -08002054
Elliott Hughes124fae92012-10-31 14:20:03 -07002055 if (!soinfo_link_image(&linker_so)) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002056 // It would be nice to print an error message, but if the linker
2057 // can't link itself, there's no guarantee that we'll be able to
2058 // call write() (because it involves a GOT reference).
2059 //
2060 // This situation should never occur unless the linker itself
2061 // is corrupt.
Elliott Hughes18a206c2012-10-29 17:37:13 -07002062 exit(EXIT_FAILURE);
Nick Kralevich468319c2011-11-11 15:53:17 -08002063 }
2064
2065 // We have successfully fixed our own relocations. It's safe to run
2066 // the main part of the linker now.
Elliott Hughes5419b942012-10-16 15:54:46 -07002067 unsigned start_address = __linker_init_post_relocation(elfdata, linker_addr);
2068
Elliott Hughesd23736e2012-11-01 15:16:56 -07002069 set_soinfo_pool_protection(PROT_READ);
2070
Elliott Hughes5419b942012-10-16 15:54:46 -07002071 // Return the address that the calling assembly stub should jump to.
2072 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002073}