blob: 40f29a237dca037b01b37baf259786f87a94a35f [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
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -0700107#if LINKER_DEBUG
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108int debug_verbosity;
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -0700109#endif
110
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111static int pid;
112
Elliott Hughesbedfe382012-08-14 14:07:59 -0700113enum RelocationKind {
114 kRelocAbsolute = 0,
115 kRelocRelative,
116 kRelocCopy,
117 kRelocSymbol,
118 kRelocMax
119};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100120
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700122struct linker_stats_t {
123 int count[kRelocMax];
124};
125
126static linker_stats_t linker_stats;
127
128static void count_relocation(RelocationKind kind) {
129 ++linker_stats.count[kind];
130}
131#else
132static void count_relocation(RelocationKind) {
133}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134#endif
135
136#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700137static unsigned bitmask[4096];
138#define MARK(offset) \
139 do { \
140 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
141 } while(0)
142#else
143#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144#endif
145
Elliott Hughes46882792012-08-03 16:49:39 -0700146// You shouldn't try to call memory-allocating functions in the dynamic linker.
147// Guard against the most obvious ones.
148#define DISALLOW_ALLOCATION(return_type, name, ...) \
149 return_type name __VA_ARGS__ \
150 { \
151 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
152 __libc_android_log_write(ANDROID_LOG_FATAL, "linker", msg); \
153 write(2, msg, sizeof(msg)); \
154 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700155 }
Elliott Hughes46882792012-08-03 16:49:39 -0700156#define UNUSED __attribute__((unused))
157DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
158DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
159DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
160DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
Dima Zavin2e855792009-05-20 18:28:09 -0700161
Dima Zavin03531952009-05-29 17:30:25 -0700162static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700163static char __linker_dl_err_buf[768];
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700164#define DL_ERR(fmt, x...) \
165 do { \
Elliott Hughes3b297c42012-10-11 16:08:51 -0700166 format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), fmt, ##x); \
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700167 ERROR(fmt "\n", ##x); \
Dima Zavin2e855792009-05-20 18:28:09 -0700168 } while(0)
169
Elliott Hughes5419b942012-10-16 15:54:46 -0700170const char* linker_get_error() {
171 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700172}
173
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174/*
175 * This function is an empty stub where GDB locates a breakpoint to get notified
176 * about linker activity.
177 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700178extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179
Elliott Hughesbedfe382012-08-14 14:07:59 -0700180static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181 RT_CONSISTENT, 0};
Elliott Hughesbedfe382012-08-14 14:07:59 -0700182static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183
Elliott Hughes3b297c42012-10-11 16:08:51 -0700184static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185
Elliott Hughesbedfe382012-08-14 14:07:59 -0700186static void insert_soinfo_into_debug_map(soinfo * info) {
187 // Copy the necessary fields into the debug structure.
188 link_map* map = &(info->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189 map->l_addr = info->base;
190 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800191 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192
193 /* Stick the new library at the end of the list.
194 * gdb tends to care more about libc than it does
195 * about leaf libraries, and ordering it this way
196 * reduces the back-and-forth over the wire.
197 */
198 if (r_debug_tail) {
199 r_debug_tail->l_next = map;
200 map->l_prev = r_debug_tail;
201 map->l_next = 0;
202 } else {
203 _r_debug.r_map = map;
204 map->l_prev = 0;
205 map->l_next = 0;
206 }
207 r_debug_tail = map;
208}
209
Elliott Hughesbedfe382012-08-14 14:07:59 -0700210static void remove_soinfo_from_debug_map(soinfo* info) {
211 link_map* map = &(info->linkmap);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700212
Elliott Hughesbedfe382012-08-14 14:07:59 -0700213 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700214 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700215 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700216
Elliott Hughesbedfe382012-08-14 14:07:59 -0700217 if (map->l_prev) {
218 map->l_prev->l_next = map->l_next;
219 }
220 if (map->l_next) {
221 map->l_next->l_prev = map->l_prev;
222 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700223}
224
Elliott Hughesbedfe382012-08-14 14:07:59 -0700225static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226 if (info->flags & FLAG_EXE) {
227 // GDB already knows about the main executable
228 return;
229 }
230
Elliott Hughes3b297c42012-10-11 16:08:51 -0700231 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800232
233 _r_debug.r_state = RT_ADD;
234 rtld_db_dlactivity();
235
236 insert_soinfo_into_debug_map(info);
237
238 _r_debug.r_state = RT_CONSISTENT;
239 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700240}
241
Elliott Hughesbedfe382012-08-14 14:07:59 -0700242static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700243 if (info->flags & FLAG_EXE) {
244 // GDB already knows about the main executable
245 return;
246 }
247
Elliott Hughes3b297c42012-10-11 16:08:51 -0700248 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700249
250 _r_debug.r_state = RT_DELETE;
251 rtld_db_dlactivity();
252
253 remove_soinfo_from_debug_map(info);
254
255 _r_debug.r_state = RT_CONSISTENT;
256 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800257}
258
Elliott Hughes18a206c2012-10-29 17:37:13 -0700259void notify_gdb_of_libraries() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260 _r_debug.r_state = RT_ADD;
261 rtld_db_dlactivity();
262 _r_debug.r_state = RT_CONSISTENT;
263 rtld_db_dlactivity();
264}
265
Magnus Malmbornba98d922012-09-12 13:00:55 +0200266static bool ensure_free_list_non_empty() {
267 if (gSoInfoFreeList != NULL) {
268 return true;
269 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800270
Magnus Malmbornba98d922012-09-12 13:00:55 +0200271 // Allocate a new pool.
272 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
273 PROT_READ|PROT_WRITE,
274 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
275 if (pool == MAP_FAILED) {
276 return false;
277 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800278
Magnus Malmbornba98d922012-09-12 13:00:55 +0200279 // Add the pool to our list of pools.
280 pool->next = gSoInfoPools;
281 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282
Magnus Malmbornba98d922012-09-12 13:00:55 +0200283 // Chain the entries in the new pool onto the free list.
284 gSoInfoFreeList = &pool->info[0];
285 soinfo* next = NULL;
286 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
287 pool->info[i].next = next;
288 next = &pool->info[i];
289 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800290
Magnus Malmbornba98d922012-09-12 13:00:55 +0200291 return true;
292}
293
Elliott Hughesd23736e2012-11-01 15:16:56 -0700294static void set_soinfo_pool_protection(int protection) {
295 for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
296 if (mprotect(p, sizeof(*p), protection) == -1) {
297 abort(); // Can't happen.
298 }
299 }
300}
301
Magnus Malmbornba98d922012-09-12 13:00:55 +0200302static soinfo* soinfo_alloc(const char* name) {
303 if (strlen(name) >= SOINFO_NAME_LEN) {
304 DL_ERR("library name \"%s\" too long", name);
305 return NULL;
306 }
307
308 if (!ensure_free_list_non_empty()) {
309 DL_ERR("out of memory when loading \"%s\"", name);
310 return NULL;
311 }
312
313 // Take the head element off the free list.
314 soinfo* si = gSoInfoFreeList;
315 gSoInfoFreeList = gSoInfoFreeList->next;
316
317 // Initialize the new element.
318 memset(si, 0, sizeof(soinfo));
319 strlcpy(si->name, name, sizeof(si->name));
320 sonext->next = si;
321 sonext = si;
322
323 TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
324 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800325}
326
Elliott Hughes46882792012-08-03 16:49:39 -0700327static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800328{
Elliott Hughes46882792012-08-03 16:49:39 -0700329 if (si == NULL) {
330 return;
331 }
332
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333 soinfo *prev = NULL, *trav;
334
335 TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
336
337 for(trav = solist; trav != NULL; trav = trav->next){
338 if (trav == si)
339 break;
340 prev = trav;
341 }
342 if (trav == NULL) {
343 /* si was not ni solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700344 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800345 return;
346 }
347
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100348 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800349 always the static libdl_info.
350 */
351 prev->next = si->next;
352 if (si == sonext) sonext = prev;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200353 si->next = gSoInfoFreeList;
354 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355}
356
Elliott Hughes46882792012-08-03 16:49:39 -0700357#ifdef ANDROID_ARM_LINKER
358
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800359/* For a given PC, find the .so that it belongs to.
360 * Returns the base address of the .ARM.exidx section
361 * for that .so, and the number of 8-byte entries
362 * in that section (via *pcount).
363 *
364 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
365 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700366 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800367 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800368_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
369{
370 soinfo *si;
371 unsigned addr = (unsigned)pc;
372
Nick Kralevich468319c2011-11-11 15:53:17 -0800373 for (si = solist; si != 0; si = si->next){
374 if ((addr >= si->base) && (addr < (si->base + si->size))) {
375 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900376 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800377 }
378 }
379 *pcount = 0;
380 return NULL;
381}
Elliott Hughes46882792012-08-03 16:49:39 -0700382
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700383#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
Elliott Hughes46882792012-08-03 16:49:39 -0700384
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385/* Here, we only have to provide a callback to iterate across all the
386 * loaded libraries. gcc_eh does the rest. */
387int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700388dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800389 void *data)
390{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800391 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700392 for (soinfo* si = solist; si != NULL; si = si->next) {
393 dl_phdr_info dl_info;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800394 dl_info.dlpi_addr = si->linkmap.l_addr;
395 dl_info.dlpi_name = si->linkmap.l_name;
396 dl_info.dlpi_phdr = si->phdr;
397 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700398 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
399 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800400 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700401 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402 }
403 return rv;
404}
Elliott Hughes46882792012-08-03 16:49:39 -0700405
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406#endif
407
David 'Digit' Turner16084162012-06-12 16:25:37 +0200408static Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800409{
410 Elf32_Sym *s;
411 Elf32_Sym *symtab = si->symtab;
412 const char *strtab = si->strtab;
413 unsigned n;
414
415 TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
416 name, si->name, si->base, hash, hash % si->nbucket);
417 n = hash % si->nbucket;
418
419 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
420 s = symtab + n;
421 if(strcmp(strtab + s->st_name, name)) continue;
422
Doug Kwane8238072009-10-26 12:05:23 -0700423 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800424 switch(ELF32_ST_BIND(s->st_info)){
425 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700426 case STB_WEAK:
Robin Burchell439fa8e2012-07-05 09:21:07 +0200427 if(s->st_shndx == SHN_UNDEF)
428 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800429
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800430 TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
431 name, si->name, s->st_value, s->st_size);
432 return s;
433 }
434 }
435
Doug Kwan94304352009-10-23 18:11:40 -0700436 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800437}
438
439static unsigned elfhash(const char *_name)
440{
441 const unsigned char *name = (const unsigned char *) _name;
442 unsigned h = 0, g;
443
444 while(*name) {
445 h = (h << 4) + *name++;
446 g = h & 0xf0000000;
447 h ^= g;
448 h ^= g >> 24;
449 }
450 return h;
451}
452
453static Elf32_Sym *
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200454soinfo_do_lookup(soinfo *si, const char *name, soinfo **lsi,
455 soinfo *needed[])
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700456{
Doug Kwan94304352009-10-23 18:11:40 -0700457 unsigned elf_hash = elfhash(name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700458 Elf32_Sym *s = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600459 int i;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700460
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200461 if (si != NULL) {
462
463 /*
464 * If this object was built with symbolic relocations disabled, the
465 * first place to look to resolve external references is the main
466 * executable.
467 */
468
469 if (!si->has_DT_SYMBOLIC) {
470 DEBUG("%5d %s: looking up %s in executable %s\n",
471 pid, si->name, name, somain->name);
472 s = soinfo_elf_lookup(somain, elf_hash, name);
473 if (s != NULL) {
474 *lsi = somain;
475 goto done;
476 }
477 }
478
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700479 /* Look for symbols in the local scope (the object who is
480 * searching). This happens with C++ templates on i386 for some
481 * reason.
482 *
483 * Notes on weak symbols:
484 * The ELF specs are ambiguous about treatment of weak definitions in
485 * dynamic linking. Some systems return the first definition found
486 * and some the first non-weak definition. This is system dependent.
487 * Here we return the first definition found for simplicity. */
Nick Kralevich468319c2011-11-11 15:53:17 -0800488
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700489 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200490 if (s != NULL) {
491 *lsi = si;
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700492 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200493 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700494 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700495
Matt Fischer4fd42c12009-12-31 12:09:10 -0600496 /* Next, look for it in the preloads list */
497 for(i = 0; preloads[i] != NULL; i++) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200498 s = soinfo_elf_lookup(preloads[i], elf_hash, name);
499 if(s != NULL) {
500 *lsi = preloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600501 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200502 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600503 }
504
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200505 for(i = 0; needed[i] != NULL; i++) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200506 DEBUG("%5d %s: looking up %s in %s\n",
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200507 pid, si->name, name, needed[i]->name);
508 s = soinfo_elf_lookup(needed[i], elf_hash, name);
509 if (s != NULL) {
510 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200511 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200512 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700513 }
514
515done:
516 if(s != NULL) {
517 TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200518 "found in %s, base = 0x%08x, load bias = 0x%08x\n",
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900519 pid, si->name, name, s->st_value,
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200520 (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700521 return s;
522 }
523
Doug Kwan94304352009-10-23 18:11:40 -0700524 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700525}
526
527/* This is used by dl_sym(). It performs symbol lookup only within the
528 specified soinfo object and not in any of its dependencies.
529 */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200530Elf32_Sym *soinfo_lookup(soinfo *si, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800531{
David 'Digit' Turner16084162012-06-12 16:25:37 +0200532 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800533}
534
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700535/* This is used by dl_sym(). It performs a global symbol lookup.
536 */
Matt Fischer1698d9e2009-12-31 12:17:56 -0600537Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800538{
Doug Kwan94304352009-10-23 18:11:40 -0700539 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800540 Elf32_Sym *s = NULL;
541 soinfo *si;
542
Matt Fischer1698d9e2009-12-31 12:17:56 -0600543 if(start == NULL) {
544 start = solist;
545 }
546
547 for(si = start; (s == NULL) && (si != NULL); si = si->next)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800548 {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700549 if(si->flags & FLAG_ERROR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800550 continue;
David 'Digit' Turner16084162012-06-12 16:25:37 +0200551 s = soinfo_elf_lookup(si, elf_hash, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800552 if (s != NULL) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700553 *found = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800554 break;
555 }
556 }
557
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700558 if(s != NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800559 TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
560 "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
561 return s;
562 }
563
Doug Kwan94304352009-10-23 18:11:40 -0700564 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800565}
566
Mathias Agopianbda5da02011-09-27 22:30:19 -0700567soinfo *find_containing_library(const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600568{
569 soinfo *si;
570
571 for(si = solist; si != NULL; si = si->next)
572 {
573 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
574 return si;
575 }
576 }
577
578 return NULL;
579}
580
David 'Digit' Turner16084162012-06-12 16:25:37 +0200581Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600582{
583 unsigned int i;
584 unsigned soaddr = (unsigned)addr - si->base;
585
586 /* Search the library's symbol table for any defined symbol which
587 * contains this address */
588 for(i=0; i<si->nchain; i++) {
589 Elf32_Sym *sym = &si->symtab[i];
590
591 if(sym->st_shndx != SHN_UNDEF &&
592 soaddr >= sym->st_value &&
593 soaddr < sym->st_value + sym->st_size) {
594 return sym;
595 }
596 }
597
598 return NULL;
599}
600
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800601#if 0
602static void dump(soinfo *si)
603{
604 Elf32_Sym *s = si->symtab;
605 unsigned n;
606
607 for(n = 0; n < si->nchain; n++) {
608 TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
609 s->st_info, s->st_shndx, s->st_value, s->st_size,
610 si->strtab + s->st_name);
611 s++;
612 }
613}
614#endif
615
Elliott Hughes124fae92012-10-31 14:20:03 -0700616static int open_library_on_path(const char* name, const char* const paths[]) {
617 char buf[512];
618 for (size_t i = 0; paths[i] != NULL; ++i) {
619 int n = format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
620 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
621 WARN("Ignoring very long library path: %s/%s\n", paths[i], name);
622 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800623 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700624 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
625 if (fd != -1) {
626 return fd;
627 }
628 }
629 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630}
631
Elliott Hughes124fae92012-10-31 14:20:03 -0700632static int open_library(const char* name) {
633 TRACE("[ %5d opening %s ]\n", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800634
Elliott Hughes124fae92012-10-31 14:20:03 -0700635 // If the name contains a slash, we should attempt to open it directly and not search the paths.
636 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700637 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
638 if (fd != -1) {
639 return fd;
640 }
641 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700642 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800643
Elliott Hughes124fae92012-10-31 14:20:03 -0700644 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
645 int fd = open_library_on_path(name, gLdPaths);
646 if (fd == -1) {
647 fd = open_library_on_path(name, gSoPaths);
648 }
649 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800650}
651
Elliott Hughes46882792012-08-03 16:49:39 -0700652// Returns 'true' if the library is prelinked or on failure so we error out
653// either way. We no longer support prelinking.
654static bool is_prelinked(int fd, const char* name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800655{
Elliott Hughes46882792012-08-03 16:49:39 -0700656 struct prelink_info_t {
657 long mmap_addr;
658 char tag[4]; // "PRE ".
659 };
660
Elliott Hughesbedfe382012-08-14 14:07:59 -0700661 off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800662 if (sz < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700663 DL_ERR("lseek failed: %s", strerror(errno));
664 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800665 }
666
Elliott Hughesbedfe382012-08-14 14:07:59 -0700667 prelink_info_t info;
Elliott Hughes8dfc0732012-07-27 15:30:51 -0700668 int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
669 if (rc != sizeof(info)) {
Elliott Hughes46882792012-08-03 16:49:39 -0700670 DL_ERR("could not read prelink_info_t structure for \"%s\":", name, strerror(errno));
671 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800672 }
673
Elliott Hughes46882792012-08-03 16:49:39 -0700674 if (memcmp(info.tag, "PRE ", 4) == 0) {
675 DL_ERR("prelinked libraries no longer supported: %s", name);
676 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800677 }
Elliott Hughes46882792012-08-03 16:49:39 -0700678 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800679}
680
David 'Digit' Turner16084162012-06-12 16:25:37 +0200681/* verify_elf_header
682 * Verifies the content of an ELF header.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800683 *
684 * Args:
685 *
686 * Returns:
687 * 0 on success
688 * -1 if no valid ELF object is found @ base.
689 */
690static int
David 'Digit' Turner16084162012-06-12 16:25:37 +0200691verify_elf_header(const Elf32_Ehdr* hdr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800692{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800693 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
694 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
695 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
696 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700697 if (hdr->e_type != ET_DYN) return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800698
699 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800700#ifdef ANDROID_ARM_LINKER
701 if (hdr->e_machine != EM_ARM) return -1;
702#elif defined(ANDROID_X86_LINKER)
703 if (hdr->e_machine != EM_386) return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700704#elif defined(ANDROID_MIPS_LINKER)
705 if (hdr->e_machine != EM_MIPS) return -1;
Zhenghua Wang897815a2011-10-19 00:29:14 +0800706#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800707 return 0;
708}
709
Elliott Hughes46882792012-08-03 16:49:39 -0700710struct scoped_fd {
711 ~scoped_fd() {
712 if (fd != -1) {
713 close(fd);
714 }
715 }
716 int fd;
717};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800718
Elliott Hughes46882792012-08-03 16:49:39 -0700719struct soinfo_ptr {
720 soinfo_ptr(const char* name) {
721 const char* bname = strrchr(name, '/');
722 ptr = soinfo_alloc(bname ? bname + 1 : name);
723 }
724 ~soinfo_ptr() {
725 soinfo_free(ptr);
726 }
727 soinfo* release() {
728 soinfo* result = ptr;
729 ptr = NULL;
730 return result;
731 }
732 soinfo* ptr;
733};
734
735// TODO: rewrite linker_phdr.h to use a class, then lose this.
736struct phdr_ptr {
737 phdr_ptr() : phdr_mmap(NULL) {}
738 ~phdr_ptr() {
739 if (phdr_mmap != NULL) {
740 phdr_table_unload(phdr_mmap, phdr_size);
741 }
742 }
743 void* phdr_mmap;
744 Elf32_Addr phdr_size;
745};
746
Elliott Hughes124fae92012-10-31 14:20:03 -0700747static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700748 // Open the file.
749 scoped_fd fd;
750 fd.fd = open_library(name);
751 if (fd.fd == -1) {
752 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800753 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700754 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800755
Elliott Hughes46882792012-08-03 16:49:39 -0700756 // Read the ELF header.
757 Elf32_Ehdr header[1];
758 int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200759 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700760 DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
761 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200762 }
763 if (ret != (int)sizeof(header)) {
Elliott Hughes46882792012-08-03 16:49:39 -0700764 DL_ERR("too small to be an ELF executable: %s", name);
765 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200766 }
767 if (verify_elf_header(header) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700768 DL_ERR("not a valid ELF executable: %s", name);
769 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800770 }
771
Elliott Hughes46882792012-08-03 16:49:39 -0700772 // Read the program header table.
773 const Elf32_Phdr* phdr_table;
774 phdr_ptr phdr_holder;
775 ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
776 &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200777 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700778 DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
779 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200780 }
Elliott Hughes46882792012-08-03 16:49:39 -0700781 size_t phdr_count = header->e_phnum;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200782
Elliott Hughes46882792012-08-03 16:49:39 -0700783 // Get the load extents.
784 Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
785 TRACE("[ %5d - '%s' wants sz=0x%08x ]\n", pid, name, ext_sz);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200786 if (ext_sz == 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700787 DL_ERR("no loadable segments in file: %s", name);
788 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800789 }
790
Elliott Hughes46882792012-08-03 16:49:39 -0700791 // We no longer support pre-linked libraries.
792 if (is_prelinked(fd.fd, name)) {
793 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200794 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200795
Elliott Hughes46882792012-08-03 16:49:39 -0700796 // Reserve address space for all loadable segments.
797 void* load_start = NULL;
798 Elf32_Addr load_size = 0;
799 Elf32_Addr load_bias = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200800 ret = phdr_table_reserve_memory(phdr_table,
801 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200802 &load_start,
803 &load_size,
804 &load_bias);
805 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700806 DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
807 ext_sz, name, strerror(errno));
808 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200809 }
810
811 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
812 pid, name, load_start, load_size);
813
814 /* Map all the segments in our address space with default protections */
815 ret = phdr_table_load_segments(phdr_table,
816 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200817 load_bias,
Elliott Hughes46882792012-08-03 16:49:39 -0700818 fd.fd);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200819 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700820 DL_ERR("can't map loadable segments for \"%s\": %s",
821 name, strerror(errno));
822 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200823 }
824
Elliott Hughes46882792012-08-03 16:49:39 -0700825 soinfo_ptr si(name);
826 if (si.ptr == NULL) {
827 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800828 }
829
Elliott Hughes46882792012-08-03 16:49:39 -0700830 si.ptr->base = (Elf32_Addr) load_start;
831 si.ptr->size = load_size;
832 si.ptr->load_bias = load_bias;
833 si.ptr->flags = 0;
834 si.ptr->entry = 0;
835 si.ptr->dynamic = (unsigned *)-1;
836 si.ptr->phnum = phdr_count;
837 si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
838 if (si.ptr->phdr == NULL) {
839 DL_ERR("can't find loaded PHDR for \"%s\"", name);
840 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200841 }
Elliott Hughes46882792012-08-03 16:49:39 -0700842
843 return si.release();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800844}
845
Elliott Hughesd23736e2012-11-01 15:16:56 -0700846static soinfo* init_library(soinfo* si) {
847 // At this point we know that whatever is loaded @ base is a valid ELF
848 // shared library whose segments are properly mapped in.
849 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
850 pid, si->base, si->size, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800851
Elliott Hughesd23736e2012-11-01 15:16:56 -0700852 if (!soinfo_link_image(si)) {
853 munmap((void *)si->base, si->size);
854 return NULL;
855 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800856
Elliott Hughesd23736e2012-11-01 15:16:56 -0700857 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800858}
859
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200860static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800861{
862 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700863 const char *bname;
864
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200865 // TODO: don't use basename only for determining libraries
866 // http://code.google.com/p/android/issues/detail?id=6670
867
868 bname = strrchr(name, '/');
869 bname = bname ? bname + 1 : name;
870
871 for(si = solist; si != NULL; si = si->next){
872 if(!strcmp(bname, si->name)) {
873 return si;
874 }
875 }
876 return NULL;
877}
878
Elliott Hughesd23736e2012-11-01 15:16:56 -0700879static soinfo* find_library_internal(const char* name) {
880 if (name == NULL) {
881 return somain;
882 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200883
Elliott Hughesd23736e2012-11-01 15:16:56 -0700884 soinfo* si = find_loaded_library(name);
885 if (si != NULL) {
886 if (si->flags & FLAG_ERROR) {
887 DL_ERR("\"%s\" failed to load previously", name);
888 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800889 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700890 if (si->flags & FLAG_LINKED) {
891 return si;
892 }
893 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
894 return NULL;
895 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800896
Elliott Hughesd23736e2012-11-01 15:16:56 -0700897 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
898 si = load_library(name);
899 if (si != NULL) {
900 si = init_library(si);
901 }
902
903 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800904}
905
Elliott Hughesd23736e2012-11-01 15:16:56 -0700906static soinfo* find_library(const char* name) {
907 soinfo* si = find_library_internal(name);
908 if (si != NULL) {
909 si->refcount++;
910 }
911 return si;
912}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700913
Elliott Hughesd23736e2012-11-01 15:16:56 -0700914static int soinfo_unload(soinfo* si) {
915 if (si->refcount == 1) {
916 TRACE("%5d unloading '%s'\n", pid, si->name);
917 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800918
Elliott Hughesd23736e2012-11-01 15:16:56 -0700919 for (unsigned* d = si->dynamic; *d; d += 2) {
920 if (d[0] == DT_NEEDED) {
921 soinfo* lsi = find_loaded_library(si->strtab + d[1]);
922 if (lsi != NULL) {
923 TRACE("%5d %s needs to unload %s\n", pid, si->name, lsi->name);
924 soinfo_unload(lsi);
925 } else {
926 // TODO: should we return -1 in this case?
927 DL_ERR("\"%s\": could not unload dependent library", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800928 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700929 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800930 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700931
932 munmap(reinterpret_cast<void*>(si->base), si->size);
933 notify_gdb_of_unload(si);
934 soinfo_free(si);
935 si->refcount = 0;
936 } else {
937 si->refcount--;
938 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
939 pid, si->name, si->refcount);
940 }
941 return 0;
942}
943
944soinfo* do_dlopen(const char* name) {
945 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
946 soinfo* si = find_library(name);
947 if (si != NULL) {
948 si->CallConstructors();
949 }
950 set_soinfo_pool_protection(PROT_READ);
951 return si;
952}
953
954int do_dlclose(soinfo* si) {
955 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
956 int result = soinfo_unload(si);
957 set_soinfo_pool_protection(PROT_READ);
958 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800959}
960
961/* TODO: don't use unsigned for addrs below. It works, but is not
962 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
963 * long.
964 */
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200965static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count,
966 soinfo *needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800967{
968 Elf32_Sym *symtab = si->symtab;
969 const char *strtab = si->strtab;
970 Elf32_Sym *s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800971 Elf32_Rel *start = rel;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200972 soinfo *lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800973
Elliott Hughes46882792012-08-03 16:49:39 -0700974 for (size_t idx = 0; idx < count; ++idx, ++rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800975 unsigned type = ELF32_R_TYPE(rel->r_info);
976 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200977 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800978 unsigned sym_addr = 0;
979 char *sym_name = NULL;
980
981 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
982 si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700983 if (type == 0) { // R_*_NONE
984 continue;
985 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800986 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -0700987 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200988 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Doug Kwane8238072009-10-26 12:05:23 -0700989 if(s == NULL) {
990 /* We only allow an undefined symbol if this is a weak
991 reference.. */
992 s = &symtab[sym];
993 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700994 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -0700995 return -1;
996 }
997
998 /* IHI0044C AAELF 4.5.1.1:
999
1000 Libraries are not searched to resolve weak references.
1001 It is not an error for a weak reference to remain
1002 unsatisfied.
1003
1004 During linking, the value of an undefined weak reference is:
1005 - Zero if the relocation type is absolute
1006 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -07001007 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -07001008 type is base-relative.
1009 */
1010
1011 switch (type) {
1012#if defined(ANDROID_ARM_LINKER)
1013 case R_ARM_JUMP_SLOT:
1014 case R_ARM_GLOB_DAT:
1015 case R_ARM_ABS32:
1016 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -07001017#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001018 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001019 case R_386_GLOB_DAT:
1020 case R_386_32:
1021 case R_386_RELATIVE: /* Dont' care. */
1022#endif /* ANDROID_*_LINKER */
1023 /* sym_addr was initialized to be zero above or relocation
1024 code below does not care about value of sym_addr.
1025 No need to do anything. */
1026 break;
1027
1028#if defined(ANDROID_X86_LINKER)
1029 case R_386_PC32:
1030 sym_addr = reloc;
1031 break;
1032#endif /* ANDROID_X86_LINKER */
1033
1034#if defined(ANDROID_ARM_LINKER)
1035 case R_ARM_COPY:
1036 /* Fall through. Can't really copy if weak symbol is
1037 not found in run-time. */
1038#endif /* ANDROID_ARM_LINKER */
1039 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001040 DL_ERR("unknown weak reloc type %d @ %p (%d)",
1041 type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -07001042 return -1;
1043 }
1044 } else {
1045 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001046#if 0
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001047 if((base == 0) && (si->base != 0)){
1048 /* linking from libraries to main image is bad */
Elliott Hughes46882792012-08-03 16:49:39 -07001049 DL_ERR("cannot locate \"%s\"...",
1050 strtab + symtab[sym].st_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001051 return -1;
1052 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001053#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001054 sym_addr = (unsigned)(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001055 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001056 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001057 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001058 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001059 }
1060
1061/* TODO: This is ugly. Split up the relocations by arch into
1062 * different files.
1063 */
1064 switch(type){
1065#if defined(ANDROID_ARM_LINKER)
1066 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001067 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001068 MARK(rel->r_offset);
1069 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1070 reloc, sym_addr, sym_name);
1071 *((unsigned*)reloc) = sym_addr;
1072 break;
1073 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001074 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001075 MARK(rel->r_offset);
1076 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1077 reloc, sym_addr, sym_name);
1078 *((unsigned*)reloc) = sym_addr;
1079 break;
1080 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001081 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001082 MARK(rel->r_offset);
1083 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1084 reloc, sym_addr, sym_name);
1085 *((unsigned*)reloc) += sym_addr;
1086 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001087 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001088 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001089 MARK(rel->r_offset);
1090 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1091 reloc, sym_addr, rel->r_offset, sym_name);
1092 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1093 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001094#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001095 case R_386_JMP_SLOT:
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);
1098 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1099 reloc, sym_addr, sym_name);
1100 *((unsigned*)reloc) = sym_addr;
1101 break;
1102 case R_386_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001103 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001104 MARK(rel->r_offset);
1105 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1106 reloc, sym_addr, sym_name);
1107 *((unsigned*)reloc) = sym_addr;
1108 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001109#elif defined(ANDROID_MIPS_LINKER)
1110 case R_MIPS_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001111 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001112 MARK(rel->r_offset);
1113 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1114 reloc, sym_addr, sym_name);
1115 *((unsigned*)reloc) = sym_addr;
1116 break;
1117 case R_MIPS_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001118 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001119 MARK(rel->r_offset);
1120 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x %s\n", pid,
1121 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1122 if (s) {
1123 *((unsigned*)reloc) += sym_addr;
1124 } else {
1125 *((unsigned*)reloc) += si->base;
1126 }
1127 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001128#endif /* ANDROID_*_LINKER */
1129
1130#if defined(ANDROID_ARM_LINKER)
1131 case R_ARM_RELATIVE:
1132#elif defined(ANDROID_X86_LINKER)
1133 case R_386_RELATIVE:
1134#endif /* ANDROID_*_LINKER */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001135 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001136 MARK(rel->r_offset);
Elliott Hughes46882792012-08-03 16:49:39 -07001137 if (sym) {
1138 DL_ERR("odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001139 return -1;
1140 }
1141 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1142 reloc, si->base);
1143 *((unsigned*)reloc) += si->base;
1144 break;
1145
1146#if defined(ANDROID_X86_LINKER)
1147 case R_386_32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001148 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001149 MARK(rel->r_offset);
1150
1151 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1152 reloc, sym_addr, sym_name);
1153 *((unsigned *)reloc) += (unsigned)sym_addr;
1154 break;
1155
1156 case R_386_PC32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001157 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001158 MARK(rel->r_offset);
1159 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1160 "+%08x (%08x - %08x) %s\n", pid, reloc,
1161 (sym_addr - reloc), sym_addr, reloc, sym_name);
1162 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1163 break;
1164#endif /* ANDROID_X86_LINKER */
1165
1166#ifdef ANDROID_ARM_LINKER
1167 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001168 if ((si->flags & FLAG_EXE) == 0) {
1169 /*
1170 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1171 *
1172 * Section 4.7.1.10 "Dynamic relocations"
1173 * R_ARM_COPY may only appear in executable objects where e_type is
1174 * set to ET_EXEC.
1175 *
1176 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1177 * We should explicitly disallow ET_DYN executables from having
1178 * R_ARM_COPY relocations.
1179 */
1180 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1181 return -1;
1182 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001183 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001184 MARK(rel->r_offset);
1185 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1186 reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001187 if (reloc == sym_addr) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001188 Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1189
1190 if (src == NULL) {
1191 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1192 return -1;
1193 }
1194 if (lsi->has_DT_SYMBOLIC) {
1195 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1196 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1197 return -1;
1198 }
1199 if (s->st_size < src->st_size) {
1200 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1201 si->name, s->st_size, src->st_size);
1202 return -1;
1203 }
1204 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1205 } else {
1206 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001207 return -1;
1208 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001209 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001210#endif /* ANDROID_ARM_LINKER */
1211
1212 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001213 DL_ERR("unknown reloc type %d @ %p (%d)",
1214 type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001215 return -1;
1216 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001217 }
1218 return 0;
1219}
1220
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001221#ifdef ANDROID_MIPS_LINKER
Elliott Hughesbedfe382012-08-14 14:07:59 -07001222static int mips_relocate_got(soinfo* si, soinfo* needed[]) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001223 unsigned *got;
1224 unsigned local_gotno, gotsym, symtabno;
1225 Elf32_Sym *symtab, *sym;
1226 unsigned g;
1227
1228 got = si->plt_got;
1229 local_gotno = si->mips_local_gotno;
1230 gotsym = si->mips_gotsym;
1231 symtabno = si->mips_symtabno;
1232 symtab = si->symtab;
1233
1234 /*
1235 * got[0] is address of lazy resolver function
1236 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001237 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001238 * (should be _rtld_bind_start)
1239 * FIXME: maybe this should be in a separate routine
1240 */
1241
1242 if ((si->flags & FLAG_LINKER) == 0) {
1243 g = 0;
1244 got[g++] = 0xdeadbeef;
1245 if (got[g] & 0x80000000) {
1246 got[g++] = 0xdeadfeed;
1247 }
1248 /*
1249 * Relocate the local GOT entries need to be relocated
1250 */
1251 for (; g < local_gotno; g++) {
1252 got[g] += si->load_bias;
1253 }
1254 }
1255
1256 /* Now for the global GOT entries */
1257 sym = symtab + gotsym;
1258 got = si->plt_got + local_gotno;
1259 for (g = gotsym; g < symtabno; g++, sym++, got++) {
1260 const char *sym_name;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001261 Elf32_Sym *s;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001262 soinfo *lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001263
1264 /* This is an undefined reference... try to locate it */
1265 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001266 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001267 if (s == NULL) {
1268 /* We only allow an undefined symbol if this is a weak
1269 reference.. */
1270 s = &symtab[g];
1271 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001272 DL_ERR("cannot locate \"%s\"...", sym_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001273 return -1;
1274 }
1275 *got = 0;
1276 }
1277 else {
1278 /* FIXME: is this sufficient?
1279 * For reference see NetBSD link loader
1280 * 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
1281 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001282 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001283 }
1284 }
1285 return 0;
1286}
1287#endif
1288
David 'Digit' Turner82156792009-05-18 14:37:41 +02001289/* Please read the "Initialization and Termination functions" functions.
1290 * of the linker design note in bionic/linker/README.TXT to understand
1291 * what the following code is doing.
1292 *
1293 * The important things to remember are:
1294 *
1295 * DT_PREINIT_ARRAY must be called first for executables, and should
1296 * not appear in shared libraries.
1297 *
1298 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1299 *
1300 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1301 *
1302 * DT_FINI_ARRAY must be parsed in reverse order.
1303 */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001304void soinfo::CallArray(const char* array_name UNUSED, unsigned* array, int count, bool reverse) {
1305 if (array == NULL) {
1306 return;
1307 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001308
Elliott Hughesd23736e2012-11-01 15:16:56 -07001309 int step = 1;
1310 if (reverse) {
1311 array += (count-1);
1312 step = -1;
1313 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001314
Elliott Hughesd23736e2012-11-01 15:16:56 -07001315 TRACE("[ %5d Calling %s @ %p [%d] for '%s' ]\n", pid, array_name, array, count, name);
1316
1317 for (int n = count; n > 0; n--) {
1318 TRACE("[ %5d Looking at %s[%d] *%p == 0x%08x ]\n", pid, array_name, n, array, *array);
1319 void (*func)() = (void (*)()) *array;
1320 array += step;
1321 if (((int) func == 0) || ((int) func == -1)) {
1322 continue;
David 'Digit' Turner82156792009-05-18 14:37:41 +02001323 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001324 TRACE("[ %5d Calling func @ %p ]\n", pid, func);
1325 func();
1326 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001327
Elliott Hughesd23736e2012-11-01 15:16:56 -07001328 TRACE("[ %5d Done calling %s for '%s' ]\n", pid, array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001329}
1330
Elliott Hughesd23736e2012-11-01 15:16:56 -07001331void soinfo::CallFunction(const char* function_name UNUSED, void (*function)()) {
1332 if (function == NULL) {
1333 return;
1334 }
1335
1336 TRACE("[ %5d Calling %s @ %p for '%s' ]\n", pid, function_name, function, name);
1337 function();
1338 TRACE("[ %5d Done calling %s for '%s' ]\n", pid, function_name, name);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001339}
1340
Elliott Hughesd23736e2012-11-01 15:16:56 -07001341void soinfo::CallPreInitConstructors() {
1342 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1343}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001344
Elliott Hughesd23736e2012-11-01 15:16:56 -07001345void soinfo::CallConstructors() {
1346 if (constructors_called) {
1347 return;
1348 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001349
Elliott Hughesd23736e2012-11-01 15:16:56 -07001350 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1351 // protect against recursive constructor calls. One simple example of constructor recursion
1352 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1353 // 1. The program depends on libc, so libc's constructor is called here.
1354 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1355 // 3. dlopen() calls the constructors on the newly created
1356 // soinfo for libc_malloc_debug_leak.so.
1357 // 4. The debug .so depends on libc, so CallConstructors is
1358 // called again with the libc soinfo. If it doesn't trigger the early-
1359 // out above, the libc constructor will be called again (recursively!).
1360 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001361
Elliott Hughesd23736e2012-11-01 15:16:56 -07001362 if (!(flags & FLAG_EXE) && preinit_array) {
1363 DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array);
1364 return;
1365 }
1366
1367 if (dynamic) {
1368 for (unsigned* d = dynamic; *d; d += 2) {
1369 if (d[0] == DT_NEEDED) {
1370 soinfo* lsi = find_loaded_library(strtab + d[1]);
1371 if (lsi == NULL) {
1372 DL_ERR("\"%s\": could not initialize dependent library", name);
1373 } else {
1374 lsi->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001375 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001376 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001377 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001378 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001379
Elliott Hughesd23736e2012-11-01 15:16:56 -07001380 CallFunction("DT_INIT", init_func);
1381 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001382}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001383
Elliott Hughesd23736e2012-11-01 15:16:56 -07001384void soinfo::CallDestructors() {
1385 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
1386 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001387}
1388
1389/* Force any of the closed stdin, stdout and stderr to be associated with
1390 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001391static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001392 int dev_null, i, status;
1393 int return_value = 0;
1394
David 'Digit' Turner16084162012-06-12 16:25:37 +02001395 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001396 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001397 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001398 return -1;
1399 }
1400 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1401
1402 /* If any of the stdio file descriptors is valid and not associated
1403 with /dev/null, dup /dev/null to it. */
1404 for (i = 0; i < 3; i++) {
1405 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001406 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001407 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001408 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001409
1410 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
Elliott Hughes46882792012-08-03 16:49:39 -07001411 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001412
Elliott Hughes46882792012-08-03 16:49:39 -07001413 /* If file is opened, we are good. */
1414 if (status != -1) {
1415 continue;
1416 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001417
1418 /* The only error we allow is that the file descriptor does not
1419 exist, in which case we dup /dev/null to it. */
1420 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001421 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001422 return_value = -1;
1423 continue;
1424 }
1425
1426 /* Try dupping /dev/null to this stdio file descriptor and
1427 repeat if there is a signal. Note that any errors in closing
1428 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001429 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001430 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001431 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001432 return_value = -1;
1433 continue;
1434 }
1435 }
1436
1437 /* If /dev/null is not one of the stdio file descriptors, close it. */
1438 if (dev_null > 2) {
1439 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001440 status = TEMP_FAILURE_RETRY(close(dev_null));
1441 if (status == -1) {
1442 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001443 return_value = -1;
1444 }
1445 }
1446
1447 return return_value;
1448}
1449
Elliott Hughes124fae92012-10-31 14:20:03 -07001450static bool soinfo_link_image(soinfo* si) {
1451 si->flags |= FLAG_ERROR;
1452
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001453 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001454 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001455 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001456 int phnum = si->phnum;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001457 int relocating_linker = (si->flags & FLAG_LINKER) != 0;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001458 soinfo **needed, **pneeded;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001459
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001460 /* We can't debug anything until the linker is relocated */
1461 if (!relocating_linker) {
1462 INFO("[ %5d linking %s ]\n", pid, si->name);
1463 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1464 si->base, si->flags);
1465 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001466
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001467 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001468 size_t dynamic_count;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001469 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
1470 &dynamic_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001471 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001472 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001473 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001474 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001475 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001476 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001477 if (!relocating_linker) {
1478 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1479 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001480 }
1481
1482#ifdef ANDROID_ARM_LINKER
1483 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1484 &si->ARM_exidx, &si->ARM_exidx_count);
1485#endif
1486
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001487 /* extract useful information from dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001488 for (unsigned* d = si->dynamic; *d; ++d) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001489 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1490 switch(*d++){
1491 case DT_HASH:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001492 si->nbucket = ((unsigned *) (base + *d))[0];
1493 si->nchain = ((unsigned *) (base + *d))[1];
1494 si->bucket = (unsigned *) (base + *d + 8);
1495 si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001496 break;
1497 case DT_STRTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001498 si->strtab = (const char *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001499 break;
1500 case DT_SYMTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001501 si->symtab = (Elf32_Sym *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001502 break;
1503 case DT_PLTREL:
1504 if(*d != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001505 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1506 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001507 }
1508 break;
1509 case DT_JMPREL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001510 si->plt_rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001511 break;
1512 case DT_PLTRELSZ:
1513 si->plt_rel_count = *d / 8;
1514 break;
1515 case DT_REL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001516 si->rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001517 break;
1518 case DT_RELSZ:
1519 si->rel_count = *d / 8;
1520 break;
1521 case DT_PLTGOT:
1522 /* Save this in case we decide to do lazy binding. We don't yet. */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001523 si->plt_got = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001524 break;
1525 case DT_DEBUG:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001526#if !defined(ANDROID_MIPS_LINKER)
Elliott Hughesbedfe382012-08-14 14:07:59 -07001527 // Set the DT_DEBUG entry to the address of _r_debug for GDB
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001528 *d = (int) &_r_debug;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001529#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001530 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001531 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001532 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1533 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001534 case DT_INIT:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001535 si->init_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001536 DEBUG("%5d %s constructors (init func) found at %p\n",
1537 pid, si->name, si->init_func);
1538 break;
1539 case DT_FINI:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001540 si->fini_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001541 DEBUG("%5d %s destructors (fini func) found at %p\n",
1542 pid, si->name, si->fini_func);
1543 break;
1544 case DT_INIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001545 si->init_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001546 DEBUG("%5d %s constructors (init_array) found at %p\n",
1547 pid, si->name, si->init_array);
1548 break;
1549 case DT_INIT_ARRAYSZ:
1550 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1551 break;
1552 case DT_FINI_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001553 si->fini_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001554 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1555 pid, si->name, si->fini_array);
1556 break;
1557 case DT_FINI_ARRAYSZ:
1558 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1559 break;
1560 case DT_PREINIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001561 si->preinit_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001562 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1563 pid, si->name, si->preinit_array);
1564 break;
1565 case DT_PREINIT_ARRAYSZ:
1566 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1567 break;
1568 case DT_TEXTREL:
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001569 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001570 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001571 case DT_SYMBOLIC:
1572 si->has_DT_SYMBOLIC = true;
1573 break;
1574#if defined(DT_FLAGS)
1575 case DT_FLAGS:
1576 if (*d & DF_TEXTREL) {
1577 si->has_text_relocations = true;
1578 }
1579 if (*d & DF_SYMBOLIC) {
1580 si->has_DT_SYMBOLIC = true;
1581 }
1582 break;
1583#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001584#if defined(ANDROID_MIPS_LINKER)
1585 case DT_NEEDED:
1586 case DT_STRSZ:
1587 case DT_SYMENT:
1588 case DT_RELENT:
1589 break;
1590 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001591 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001592 {
Elliott Hughesbedfe382012-08-14 14:07:59 -07001593 r_debug** dp = (r_debug**) *d;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001594 *dp = &_r_debug;
1595 }
1596 break;
1597 case DT_MIPS_RLD_VERSION:
1598 case DT_MIPS_FLAGS:
1599 case DT_MIPS_BASE_ADDRESS:
1600 case DT_MIPS_UNREFEXTNO:
1601 case DT_MIPS_RWPLT:
1602 break;
1603
1604 case DT_MIPS_PLTGOT:
1605#if 0
1606 /* not yet... */
1607 si->mips_pltgot = (unsigned *)(si->base + *d);
1608#endif
1609 break;
1610
1611 case DT_MIPS_SYMTABNO:
1612 si->mips_symtabno = *d;
1613 break;
1614
1615 case DT_MIPS_LOCAL_GOTNO:
1616 si->mips_local_gotno = *d;
1617 break;
1618
1619 case DT_MIPS_GOTSYM:
1620 si->mips_gotsym = *d;
1621 break;
1622
1623 default:
1624 DEBUG("%5d Unused DT entry: type 0x%08x arg 0x%08x\n",
1625 pid, d[-1], d[0]);
1626 break;
1627#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001628 }
1629 }
1630
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001631 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001632 pid, si->base, si->strtab, si->symtab);
1633
Elliott Hughes124fae92012-10-31 14:20:03 -07001634 // Sanity checks.
1635 if (si->nbucket == 0) {
1636 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1637 return false;
1638 }
1639 if (si->strtab == 0) {
1640 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1641 return false;
1642 }
1643 if (si->symtab == 0) {
1644 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1645 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001646 }
1647
Matt Fischer4fd42c12009-12-31 12:09:10 -06001648 /* if this is the main executable, then load all of the preloads now */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001649 if (si->flags & FLAG_EXE) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001650 memset(preloads, 0, sizeof(preloads));
Elliott Hughesd23736e2012-11-01 15:16:56 -07001651 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1652 soinfo* lsi = find_library(gLdPreloadNames[i]);
1653 if (lsi == NULL) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001654 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001655 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Elliott Hughes124fae92012-10-31 14:20:03 -07001656 gLdPreloadNames[i], si->name, tmp_err_buf);
1657 return false;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001658 }
Matt Fischer4fd42c12009-12-31 12:09:10 -06001659 preloads[i] = lsi;
1660 }
1661 }
1662
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001663 /* dynamic_count is an upper bound for the number of needed libs */
1664 pneeded = needed = (soinfo**) alloca((1 + dynamic_count) * sizeof(soinfo*));
1665
Elliott Hughes124fae92012-10-31 14:20:03 -07001666 for (unsigned* d = si->dynamic; *d; d += 2) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001667 if (d[0] == DT_NEEDED) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001668 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001669 soinfo* lsi = find_library(si->strtab + d[1]);
1670 if (lsi == NULL) {
Dima Zavin03531952009-05-29 17:30:25 -07001671 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001672 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1673 si->strtab + d[1], si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001674 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001675 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001676 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001677 }
1678 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001679 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001680
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001681 if (si->has_text_relocations) {
1682 /* Unprotect the segments, i.e. make them writable, to allow
1683 * text relocations to work properly. We will later call
1684 * phdr_table_protect_segments() after all of them are applied
1685 * and all constructors are run.
1686 */
1687 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1688 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1689 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001690 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001691 }
1692 }
1693
Elliott Hughes124fae92012-10-31 14:20:03 -07001694 if (si->plt_rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001695 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
Elliott Hughes124fae92012-10-31 14:20:03 -07001696 if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1697 return false;
1698 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001699 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001700 if (si->rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001701 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
Elliott Hughes124fae92012-10-31 14:20:03 -07001702 if(soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1703 return false;
1704 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001705 }
1706
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001707#ifdef ANDROID_MIPS_LINKER
Elliott Hughes124fae92012-10-31 14:20:03 -07001708 if (mips_relocate_got(si, needed)) {
1709 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001710 }
1711#endif
1712
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001713 si->flags |= FLAG_LINKED;
1714 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1715
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001716 if (si->has_text_relocations) {
1717 /* All relocations are done, we can protect our segments back to
1718 * read-only. */
1719 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1720 DL_ERR("can't protect segments for \"%s\": %s",
1721 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001722 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001723 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001724 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001725
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001726 /* We can also turn on GNU RELRO protection */
1727 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001728 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1729 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001730 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001731 }
1732
Elliott Hughes124fae92012-10-31 14:20:03 -07001733 // If this is a setuid/setgid program, close the security hole described in
1734 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
Elliott Hughes18a206c2012-10-29 17:37:13 -07001735 if (get_AT_SECURE()) {
Elliott Hughes46882792012-08-03 16:49:39 -07001736 nullify_closed_stdio();
1737 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001738 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001739 si->flags &= ~FLAG_ERROR;
1740 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001741}
1742
Elliott Hughes46882792012-08-03 16:49:39 -07001743static void parse_path(const char* path, const char* delimiters,
1744 const char** array, char* buf, size_t buf_size, size_t max_count)
David Bartleybc3a5c22009-06-02 18:27:28 -07001745{
Elliott Hughes46882792012-08-03 16:49:39 -07001746 if (path == NULL) {
1747 return;
David Bartleybc3a5c22009-06-02 18:27:28 -07001748 }
1749
Elliott Hughes46882792012-08-03 16:49:39 -07001750 size_t len = strlcpy(buf, path, buf_size);
David Bartleybc3a5c22009-06-02 18:27:28 -07001751
Elliott Hughes46882792012-08-03 16:49:39 -07001752 size_t i = 0;
1753 char* buf_p = buf;
1754 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
1755 if (*array[i] != '\0') {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001756 ++i;
1757 }
1758 }
1759
Elliott Hughes46882792012-08-03 16:49:39 -07001760 // Forget the last path if we had to truncate; this occurs if the 2nd to
1761 // last char isn't '\0' (i.e. wasn't originally a delimiter).
1762 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
1763 array[i - 1] = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001764 } else {
Elliott Hughes46882792012-08-03 16:49:39 -07001765 array[i] = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001766 }
1767}
1768
Elliott Hughes46882792012-08-03 16:49:39 -07001769static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001770 parse_path(path, ":", gLdPaths,
1771 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
Elliott Hughes46882792012-08-03 16:49:39 -07001772}
1773
1774static void parse_LD_PRELOAD(const char* path) {
1775 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes124fae92012-10-31 14:20:03 -07001776 parse_path(path, " :", gLdPreloadNames,
1777 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
Elliott Hughes46882792012-08-03 16:49:39 -07001778}
1779
Nick Kralevich468319c2011-11-11 15:53:17 -08001780/*
1781 * This code is called after the linker has linked itself and
1782 * fixed it's own GOT. It is safe to make references to externs
1783 * and other non-local data at this point.
1784 */
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001785static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001786{
1787 static soinfo linker_soinfo;
1788
1789 int argc = (int) *elfdata;
1790 char **argv = (char**) (elfdata + 1);
Stephen Smalleybb440552012-01-20 10:59:15 -08001791 unsigned *vecs = (unsigned*) (argv + argc + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001792
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001793 /* NOTE: we store the elfdata pointer on a special location
1794 * of the temporary TLS area in order to pass it to
1795 * the C Library's runtime initializer.
1796 *
1797 * The initializer must clear the slot and reset the TLS
1798 * to point to a different location to ensure that no other
1799 * shared library constructor can access it.
1800 */
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001801 __libc_init_tls(elfdata);
1802
1803 pid = getpid();
1804
1805#if TIMING
1806 struct timeval t0, t1;
1807 gettimeofday(&t0, 0);
1808#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001809
Elliott Hughes18a206c2012-10-29 17:37:13 -07001810 // Initialize environment functions, and get to the ELF aux vectors table.
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001811 vecs = linker_env_init(vecs);
1812
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001813 debugger_init();
1814
Elliott Hughes18a206c2012-10-29 17:37:13 -07001815 // Get a few environment variables.
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07001816#if LINKER_DEBUG
Elliott Hughes18a206c2012-10-29 17:37:13 -07001817 {
1818 const char* env = linker_env_get("LD_DEBUG");
1819 if (env != NULL) {
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001820 debug_verbosity = atoi(env);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001821 }
1822 }
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07001823#endif
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");
1835 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
1836
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
1974 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
1975 si->entry);
1976 return si->entry;
1977}
Nick Kralevich468319c2011-11-11 15:53:17 -08001978
1979/*
1980 * Find the value of AT_BASE passed to us by the kernel. This is the load
1981 * location of the linker.
1982 */
1983static unsigned find_linker_base(unsigned **elfdata) {
1984 int argc = (int) *elfdata;
1985 char **argv = (char**) (elfdata + 1);
1986 unsigned *vecs = (unsigned*) (argv + argc + 1);
1987 while (vecs[0] != 0) {
1988 vecs++;
1989 }
1990
1991 /* The end of the environment block is marked by two NULL pointers */
1992 vecs++;
1993
1994 while(vecs[0]) {
1995 if (vecs[0] == AT_BASE) {
1996 return vecs[1];
1997 }
1998 vecs += 2;
1999 }
2000
2001 return 0; // should never happen
2002}
2003
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002004/* Compute the load-bias of an existing executable. This shall only
2005 * be used to compute the load bias of an executable or shared library
2006 * that was loaded by the kernel itself.
2007 *
2008 * Input:
2009 * elf -> address of ELF header, assumed to be at the start of the file.
2010 * Return:
2011 * load bias, i.e. add the value of any p_vaddr in the file to get
2012 * the corresponding address in memory.
2013 */
2014static Elf32_Addr
2015get_elf_exec_load_bias(const Elf32_Ehdr* elf)
2016{
2017 Elf32_Addr offset = elf->e_phoff;
2018 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
2019 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
2020 const Elf32_Phdr* phdr;
2021
2022 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
2023 if (phdr->p_type == PT_LOAD) {
2024 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
2025 }
2026 }
2027 return 0;
2028}
2029
Nick Kralevich468319c2011-11-11 15:53:17 -08002030/*
2031 * This is the entry point for the linker, called from begin.S. This
2032 * method is responsible for fixing the linker's own relocations, and
2033 * then calling __linker_init_post_relocation().
2034 *
2035 * Because this method is called before the linker has fixed it's own
2036 * relocations, any attempt to reference an extern variable, extern
2037 * function, or other GOT reference will generate a segfault.
2038 */
Elliott Hughes46882792012-08-03 16:49:39 -07002039extern "C" unsigned __linker_init(unsigned **elfdata) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002040 unsigned linker_addr = find_linker_base(elfdata);
2041 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2042 Elf32_Phdr *phdr =
2043 (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2044
2045 soinfo linker_so;
2046 memset(&linker_so, 0, sizeof(soinfo));
2047
2048 linker_so.base = linker_addr;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002049 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002050 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Nick Kralevich468319c2011-11-11 15:53:17 -08002051 linker_so.dynamic = (unsigned *) -1;
2052 linker_so.phdr = phdr;
2053 linker_so.phnum = elf_hdr->e_phnum;
2054 linker_so.flags |= FLAG_LINKER;
Nick Kralevich468319c2011-11-11 15:53:17 -08002055
Elliott Hughes124fae92012-10-31 14:20:03 -07002056 if (!soinfo_link_image(&linker_so)) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002057 // It would be nice to print an error message, but if the linker
2058 // can't link itself, there's no guarantee that we'll be able to
2059 // call write() (because it involves a GOT reference).
2060 //
2061 // This situation should never occur unless the linker itself
2062 // is corrupt.
Elliott Hughes18a206c2012-10-29 17:37:13 -07002063 exit(EXIT_FAILURE);
Nick Kralevich468319c2011-11-11 15:53:17 -08002064 }
2065
2066 // We have successfully fixed our own relocations. It's safe to run
2067 // the main part of the linker now.
Elliott Hughes5419b942012-10-16 15:54:46 -07002068 unsigned start_address = __linker_init_post_relocation(elfdata, linker_addr);
2069
Elliott Hughesd23736e2012-11-01 15:16:56 -07002070 set_soinfo_pool_protection(PROT_READ);
2071
Elliott Hughes5419b942012-10-16 15:54:46 -07002072 // Return the address that the calling assembly stub should jump to.
2073 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002074}