blob: f7f81250c292078c49e6c68b2f4a9ae16731472b [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>
34#include <stdbool.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038#include <sys/atomics.h>
Elliott Hughes46882792012-08-03 16:49:39 -070039#include <sys/mman.h>
40#include <sys/stat.h>
41#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042
Elliott Hughes46882792012-08-03 16:49:39 -070043// Private C library headers.
44#include <private/bionic_tls.h>
45#include <private/logd.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070046#include <private/ScopedPthreadMutexLocker.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047
48#include "linker.h"
49#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010050#include "linker_environ.h"
David 'Digit' Turner5c734642010-01-20 12:36:51 -080051#include "linker_format.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020052#include "linker_phdr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
David Bartleybc3a5c22009-06-02 18:27:28 -070054/* Assume average path length of 64 and max 8 paths */
55#define LDPATH_BUFSIZE 512
56#define LDPATH_MAX 8
57
Matt Fischer4fd42c12009-12-31 12:09:10 -060058#define LDPRELOAD_BUFSIZE 512
59#define LDPRELOAD_MAX 8
60
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
62 *
63 * Do NOT use malloc() and friends or pthread_*() code here.
64 * Don't use printf() either; it's caused mysterious memory
65 * corruption in the past.
66 * The linker runs before we bring up libc and it's easiest
67 * to make sure it does not depend on any complex libc features
68 *
69 * open issues / todo:
70 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071 * - are we doing everything we should for ARM_COPY relocations?
72 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073 * - after linking, set as much stuff as possible to READONLY
74 * and NOEXEC
75 * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
76 * headers provide versions that are negative...
Elliott Hughes46882792012-08-03 16:49:39 -070077 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
79
Elliott Hughes124fae92012-10-31 14:20:03 -070080static bool soinfo_link_image(soinfo* si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
Magnus Malmbornba98d922012-09-12 13:00:55 +020082// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
83// maps, each a single page in size. The pages are broken up into as many struct soinfo
84// objects as will fit, and they're all threaded together on a free list.
85#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
86struct soinfo_pool_t {
87 soinfo_pool_t* next;
88 soinfo info[SOINFO_PER_POOL];
89};
90static struct soinfo_pool_t* gSoInfoPools = NULL;
91static soinfo* gSoInfoFreeList = NULL;
92
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093static soinfo *solist = &libdl_info;
94static soinfo *sonext = &libdl_info;
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070095static soinfo *somain; /* main process, always the one after libdl_info */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096
Elliott Hughes124fae92012-10-31 14:20:03 -070097static const char* const gSoPaths[] = {
98 "/vendor/lib",
99 "/system/lib",
100 NULL
101};
David Bartleybc3a5c22009-06-02 18:27:28 -0700102
Elliott Hughes124fae92012-10-31 14:20:03 -0700103static char gLdPathsBuffer[LDPATH_BUFSIZE];
104static const char* gLdPaths[LDPATH_MAX + 1];
105
106static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
107static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600108
109static soinfo *preloads[LDPRELOAD_MAX + 1];
110
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -0700111#if LINKER_DEBUG
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112int debug_verbosity;
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -0700113#endif
114
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115static int pid;
116
Elliott Hughesbedfe382012-08-14 14:07:59 -0700117enum RelocationKind {
118 kRelocAbsolute = 0,
119 kRelocRelative,
120 kRelocCopy,
121 kRelocSymbol,
122 kRelocMax
123};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100124
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700126struct linker_stats_t {
127 int count[kRelocMax];
128};
129
130static linker_stats_t linker_stats;
131
132static void count_relocation(RelocationKind kind) {
133 ++linker_stats.count[kind];
134}
135#else
136static void count_relocation(RelocationKind) {
137}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138#endif
139
140#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700141static unsigned bitmask[4096];
142#define MARK(offset) \
143 do { \
144 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
145 } while(0)
146#else
147#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148#endif
149
Elliott Hughes46882792012-08-03 16:49:39 -0700150// You shouldn't try to call memory-allocating functions in the dynamic linker.
151// Guard against the most obvious ones.
152#define DISALLOW_ALLOCATION(return_type, name, ...) \
153 return_type name __VA_ARGS__ \
154 { \
155 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
156 __libc_android_log_write(ANDROID_LOG_FATAL, "linker", msg); \
157 write(2, msg, sizeof(msg)); \
158 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700159 }
Elliott Hughes46882792012-08-03 16:49:39 -0700160#define UNUSED __attribute__((unused))
161DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
162DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
163DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
164DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
Dima Zavin2e855792009-05-20 18:28:09 -0700165
Dima Zavin03531952009-05-29 17:30:25 -0700166static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700167static char __linker_dl_err_buf[768];
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700168#define DL_ERR(fmt, x...) \
169 do { \
Elliott Hughes3b297c42012-10-11 16:08:51 -0700170 format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), fmt, ##x); \
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700171 ERROR(fmt "\n", ##x); \
Dima Zavin2e855792009-05-20 18:28:09 -0700172 } while(0)
173
Elliott Hughes5419b942012-10-16 15:54:46 -0700174const char* linker_get_error() {
175 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700176}
177
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178/*
179 * This function is an empty stub where GDB locates a breakpoint to get notified
180 * about linker activity.
181 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700182extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183
Elliott Hughesbedfe382012-08-14 14:07:59 -0700184static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185 RT_CONSISTENT, 0};
Elliott Hughesbedfe382012-08-14 14:07:59 -0700186static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800187
Elliott Hughes3b297c42012-10-11 16:08:51 -0700188static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189
Elliott Hughesbedfe382012-08-14 14:07:59 -0700190static void insert_soinfo_into_debug_map(soinfo * info) {
191 // Copy the necessary fields into the debug structure.
192 link_map* map = &(info->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193 map->l_addr = info->base;
194 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800195 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196
197 /* Stick the new library at the end of the list.
198 * gdb tends to care more about libc than it does
199 * about leaf libraries, and ordering it this way
200 * reduces the back-and-forth over the wire.
201 */
202 if (r_debug_tail) {
203 r_debug_tail->l_next = map;
204 map->l_prev = r_debug_tail;
205 map->l_next = 0;
206 } else {
207 _r_debug.r_map = map;
208 map->l_prev = 0;
209 map->l_next = 0;
210 }
211 r_debug_tail = map;
212}
213
Elliott Hughesbedfe382012-08-14 14:07:59 -0700214static void remove_soinfo_from_debug_map(soinfo* info) {
215 link_map* map = &(info->linkmap);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700216
Elliott Hughesbedfe382012-08-14 14:07:59 -0700217 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700218 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700219 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700220
Elliott Hughesbedfe382012-08-14 14:07:59 -0700221 if (map->l_prev) {
222 map->l_prev->l_next = map->l_next;
223 }
224 if (map->l_next) {
225 map->l_next->l_prev = map->l_prev;
226 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700227}
228
Elliott Hughesbedfe382012-08-14 14:07:59 -0700229static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230 if (info->flags & FLAG_EXE) {
231 // GDB already knows about the main executable
232 return;
233 }
234
Elliott Hughes3b297c42012-10-11 16:08:51 -0700235 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236
237 _r_debug.r_state = RT_ADD;
238 rtld_db_dlactivity();
239
240 insert_soinfo_into_debug_map(info);
241
242 _r_debug.r_state = RT_CONSISTENT;
243 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700244}
245
Elliott Hughesbedfe382012-08-14 14:07:59 -0700246static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700247 if (info->flags & FLAG_EXE) {
248 // GDB already knows about the main executable
249 return;
250 }
251
Elliott Hughes3b297c42012-10-11 16:08:51 -0700252 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700253
254 _r_debug.r_state = RT_DELETE;
255 rtld_db_dlactivity();
256
257 remove_soinfo_from_debug_map(info);
258
259 _r_debug.r_state = RT_CONSISTENT;
260 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800261}
262
Elliott Hughes18a206c2012-10-29 17:37:13 -0700263void notify_gdb_of_libraries() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264 _r_debug.r_state = RT_ADD;
265 rtld_db_dlactivity();
266 _r_debug.r_state = RT_CONSISTENT;
267 rtld_db_dlactivity();
268}
269
Magnus Malmbornba98d922012-09-12 13:00:55 +0200270static bool ensure_free_list_non_empty() {
271 if (gSoInfoFreeList != NULL) {
272 return true;
273 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274
Magnus Malmbornba98d922012-09-12 13:00:55 +0200275 // Allocate a new pool.
276 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
277 PROT_READ|PROT_WRITE,
278 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
279 if (pool == MAP_FAILED) {
280 return false;
281 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282
Magnus Malmbornba98d922012-09-12 13:00:55 +0200283 // Add the pool to our list of pools.
284 pool->next = gSoInfoPools;
285 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286
Magnus Malmbornba98d922012-09-12 13:00:55 +0200287 // Chain the entries in the new pool onto the free list.
288 gSoInfoFreeList = &pool->info[0];
289 soinfo* next = NULL;
290 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
291 pool->info[i].next = next;
292 next = &pool->info[i];
293 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800294
Magnus Malmbornba98d922012-09-12 13:00:55 +0200295 return true;
296}
297
298static soinfo* soinfo_alloc(const char* name) {
299 if (strlen(name) >= SOINFO_NAME_LEN) {
300 DL_ERR("library name \"%s\" too long", name);
301 return NULL;
302 }
303
304 if (!ensure_free_list_non_empty()) {
305 DL_ERR("out of memory when loading \"%s\"", name);
306 return NULL;
307 }
308
309 // Take the head element off the free list.
310 soinfo* si = gSoInfoFreeList;
311 gSoInfoFreeList = gSoInfoFreeList->next;
312
313 // Initialize the new element.
314 memset(si, 0, sizeof(soinfo));
315 strlcpy(si->name, name, sizeof(si->name));
316 sonext->next = si;
317 sonext = si;
318
319 TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
320 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800321}
322
Elliott Hughes46882792012-08-03 16:49:39 -0700323static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324{
Elliott Hughes46882792012-08-03 16:49:39 -0700325 if (si == NULL) {
326 return;
327 }
328
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800329 soinfo *prev = NULL, *trav;
330
331 TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
332
333 for(trav = solist; trav != NULL; trav = trav->next){
334 if (trav == si)
335 break;
336 prev = trav;
337 }
338 if (trav == NULL) {
339 /* si was not ni solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700340 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800341 return;
342 }
343
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100344 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800345 always the static libdl_info.
346 */
347 prev->next = si->next;
348 if (si == sonext) sonext = prev;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200349 si->next = gSoInfoFreeList;
350 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800351}
352
Elliott Hughes46882792012-08-03 16:49:39 -0700353#ifdef ANDROID_ARM_LINKER
354
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355/* For a given PC, find the .so that it belongs to.
356 * Returns the base address of the .ARM.exidx section
357 * for that .so, and the number of 8-byte entries
358 * in that section (via *pcount).
359 *
360 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
361 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700362 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800363 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
365{
366 soinfo *si;
367 unsigned addr = (unsigned)pc;
368
Nick Kralevich468319c2011-11-11 15:53:17 -0800369 for (si = solist; si != 0; si = si->next){
370 if ((addr >= si->base) && (addr < (si->base + si->size))) {
371 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900372 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800373 }
374 }
375 *pcount = 0;
376 return NULL;
377}
Elliott Hughes46882792012-08-03 16:49:39 -0700378
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700379#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
Elliott Hughes46882792012-08-03 16:49:39 -0700380
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800381/* Here, we only have to provide a callback to iterate across all the
382 * loaded libraries. gcc_eh does the rest. */
383int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700384dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385 void *data)
386{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800387 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700388 for (soinfo* si = solist; si != NULL; si = si->next) {
389 dl_phdr_info dl_info;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800390 dl_info.dlpi_addr = si->linkmap.l_addr;
391 dl_info.dlpi_name = si->linkmap.l_name;
392 dl_info.dlpi_phdr = si->phdr;
393 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700394 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
395 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700397 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398 }
399 return rv;
400}
Elliott Hughes46882792012-08-03 16:49:39 -0700401
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402#endif
403
David 'Digit' Turner16084162012-06-12 16:25:37 +0200404static Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405{
406 Elf32_Sym *s;
407 Elf32_Sym *symtab = si->symtab;
408 const char *strtab = si->strtab;
409 unsigned n;
410
411 TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
412 name, si->name, si->base, hash, hash % si->nbucket);
413 n = hash % si->nbucket;
414
415 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
416 s = symtab + n;
417 if(strcmp(strtab + s->st_name, name)) continue;
418
Doug Kwane8238072009-10-26 12:05:23 -0700419 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800420 switch(ELF32_ST_BIND(s->st_info)){
421 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700422 case STB_WEAK:
Robin Burchell439fa8e2012-07-05 09:21:07 +0200423 if(s->st_shndx == SHN_UNDEF)
424 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800425
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800426 TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
427 name, si->name, s->st_value, s->st_size);
428 return s;
429 }
430 }
431
Doug Kwan94304352009-10-23 18:11:40 -0700432 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433}
434
435static unsigned elfhash(const char *_name)
436{
437 const unsigned char *name = (const unsigned char *) _name;
438 unsigned h = 0, g;
439
440 while(*name) {
441 h = (h << 4) + *name++;
442 g = h & 0xf0000000;
443 h ^= g;
444 h ^= g >> 24;
445 }
446 return h;
447}
448
449static Elf32_Sym *
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200450soinfo_do_lookup(soinfo *si, const char *name, soinfo **lsi,
451 soinfo *needed[])
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700452{
Doug Kwan94304352009-10-23 18:11:40 -0700453 unsigned elf_hash = elfhash(name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700454 Elf32_Sym *s = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600455 int i;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700456
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200457 if (si != NULL) {
458
459 /*
460 * If this object was built with symbolic relocations disabled, the
461 * first place to look to resolve external references is the main
462 * executable.
463 */
464
465 if (!si->has_DT_SYMBOLIC) {
466 DEBUG("%5d %s: looking up %s in executable %s\n",
467 pid, si->name, name, somain->name);
468 s = soinfo_elf_lookup(somain, elf_hash, name);
469 if (s != NULL) {
470 *lsi = somain;
471 goto done;
472 }
473 }
474
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700475 /* Look for symbols in the local scope (the object who is
476 * searching). This happens with C++ templates on i386 for some
477 * reason.
478 *
479 * Notes on weak symbols:
480 * The ELF specs are ambiguous about treatment of weak definitions in
481 * dynamic linking. Some systems return the first definition found
482 * and some the first non-weak definition. This is system dependent.
483 * Here we return the first definition found for simplicity. */
Nick Kralevich468319c2011-11-11 15:53:17 -0800484
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700485 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200486 if (s != NULL) {
487 *lsi = si;
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700488 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200489 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700490 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700491
Matt Fischer4fd42c12009-12-31 12:09:10 -0600492 /* Next, look for it in the preloads list */
493 for(i = 0; preloads[i] != NULL; i++) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200494 s = soinfo_elf_lookup(preloads[i], elf_hash, name);
495 if(s != NULL) {
496 *lsi = preloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600497 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200498 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600499 }
500
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200501 for(i = 0; needed[i] != NULL; i++) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200502 DEBUG("%5d %s: looking up %s in %s\n",
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200503 pid, si->name, name, needed[i]->name);
504 s = soinfo_elf_lookup(needed[i], elf_hash, name);
505 if (s != NULL) {
506 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200507 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200508 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700509 }
510
511done:
512 if(s != NULL) {
513 TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200514 "found in %s, base = 0x%08x, load bias = 0x%08x\n",
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900515 pid, si->name, name, s->st_value,
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200516 (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700517 return s;
518 }
519
Doug Kwan94304352009-10-23 18:11:40 -0700520 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700521}
522
523/* This is used by dl_sym(). It performs symbol lookup only within the
524 specified soinfo object and not in any of its dependencies.
525 */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200526Elf32_Sym *soinfo_lookup(soinfo *si, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800527{
David 'Digit' Turner16084162012-06-12 16:25:37 +0200528 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800529}
530
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700531/* This is used by dl_sym(). It performs a global symbol lookup.
532 */
Matt Fischer1698d9e2009-12-31 12:17:56 -0600533Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534{
Doug Kwan94304352009-10-23 18:11:40 -0700535 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800536 Elf32_Sym *s = NULL;
537 soinfo *si;
538
Matt Fischer1698d9e2009-12-31 12:17:56 -0600539 if(start == NULL) {
540 start = solist;
541 }
542
543 for(si = start; (s == NULL) && (si != NULL); si = si->next)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800544 {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700545 if(si->flags & FLAG_ERROR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800546 continue;
David 'Digit' Turner16084162012-06-12 16:25:37 +0200547 s = soinfo_elf_lookup(si, elf_hash, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800548 if (s != NULL) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700549 *found = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800550 break;
551 }
552 }
553
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700554 if(s != NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800555 TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
556 "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
557 return s;
558 }
559
Doug Kwan94304352009-10-23 18:11:40 -0700560 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800561}
562
Mathias Agopianbda5da02011-09-27 22:30:19 -0700563soinfo *find_containing_library(const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600564{
565 soinfo *si;
566
567 for(si = solist; si != NULL; si = si->next)
568 {
569 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
570 return si;
571 }
572 }
573
574 return NULL;
575}
576
David 'Digit' Turner16084162012-06-12 16:25:37 +0200577Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600578{
579 unsigned int i;
580 unsigned soaddr = (unsigned)addr - si->base;
581
582 /* Search the library's symbol table for any defined symbol which
583 * contains this address */
584 for(i=0; i<si->nchain; i++) {
585 Elf32_Sym *sym = &si->symtab[i];
586
587 if(sym->st_shndx != SHN_UNDEF &&
588 soaddr >= sym->st_value &&
589 soaddr < sym->st_value + sym->st_size) {
590 return sym;
591 }
592 }
593
594 return NULL;
595}
596
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800597#if 0
598static void dump(soinfo *si)
599{
600 Elf32_Sym *s = si->symtab;
601 unsigned n;
602
603 for(n = 0; n < si->nchain; n++) {
604 TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
605 s->st_info, s->st_shndx, s->st_value, s->st_size,
606 si->strtab + s->st_name);
607 s++;
608 }
609}
610#endif
611
Elliott Hughes124fae92012-10-31 14:20:03 -0700612static int open_library_on_path(const char* name, const char* const paths[]) {
613 char buf[512];
614 for (size_t i = 0; paths[i] != NULL; ++i) {
615 int n = format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
616 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
617 WARN("Ignoring very long library path: %s/%s\n", paths[i], name);
618 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800619 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700620 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
621 if (fd != -1) {
622 return fd;
623 }
624 }
625 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800626}
627
Elliott Hughes124fae92012-10-31 14:20:03 -0700628static int open_library(const char* name) {
629 TRACE("[ %5d opening %s ]\n", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630
Elliott Hughes124fae92012-10-31 14:20:03 -0700631 // If the name contains a slash, we should attempt to open it directly and not search the paths.
632 if (strchr(name, '/') != NULL) {
633 return TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
634 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800635
Elliott Hughes124fae92012-10-31 14:20:03 -0700636 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
637 int fd = open_library_on_path(name, gLdPaths);
638 if (fd == -1) {
639 fd = open_library_on_path(name, gSoPaths);
640 }
641 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800642}
643
Elliott Hughes46882792012-08-03 16:49:39 -0700644// Returns 'true' if the library is prelinked or on failure so we error out
645// either way. We no longer support prelinking.
646static bool is_prelinked(int fd, const char* name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800647{
Elliott Hughes46882792012-08-03 16:49:39 -0700648 struct prelink_info_t {
649 long mmap_addr;
650 char tag[4]; // "PRE ".
651 };
652
Elliott Hughesbedfe382012-08-14 14:07:59 -0700653 off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800654 if (sz < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700655 DL_ERR("lseek failed: %s", strerror(errno));
656 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657 }
658
Elliott Hughesbedfe382012-08-14 14:07:59 -0700659 prelink_info_t info;
Elliott Hughes8dfc0732012-07-27 15:30:51 -0700660 int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
661 if (rc != sizeof(info)) {
Elliott Hughes46882792012-08-03 16:49:39 -0700662 DL_ERR("could not read prelink_info_t structure for \"%s\":", name, strerror(errno));
663 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800664 }
665
Elliott Hughes46882792012-08-03 16:49:39 -0700666 if (memcmp(info.tag, "PRE ", 4) == 0) {
667 DL_ERR("prelinked libraries no longer supported: %s", name);
668 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800669 }
Elliott Hughes46882792012-08-03 16:49:39 -0700670 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800671}
672
David 'Digit' Turner16084162012-06-12 16:25:37 +0200673/* verify_elf_header
674 * Verifies the content of an ELF header.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800675 *
676 * Args:
677 *
678 * Returns:
679 * 0 on success
680 * -1 if no valid ELF object is found @ base.
681 */
682static int
David 'Digit' Turner16084162012-06-12 16:25:37 +0200683verify_elf_header(const Elf32_Ehdr* hdr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800684{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800685 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
686 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
687 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
688 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700689 if (hdr->e_type != ET_DYN) return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800690
691 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800692#ifdef ANDROID_ARM_LINKER
693 if (hdr->e_machine != EM_ARM) return -1;
694#elif defined(ANDROID_X86_LINKER)
695 if (hdr->e_machine != EM_386) return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700696#elif defined(ANDROID_MIPS_LINKER)
697 if (hdr->e_machine != EM_MIPS) return -1;
Zhenghua Wang897815a2011-10-19 00:29:14 +0800698#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800699 return 0;
700}
701
Elliott Hughes46882792012-08-03 16:49:39 -0700702struct scoped_fd {
703 ~scoped_fd() {
704 if (fd != -1) {
705 close(fd);
706 }
707 }
708 int fd;
709};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800710
Elliott Hughes46882792012-08-03 16:49:39 -0700711struct soinfo_ptr {
712 soinfo_ptr(const char* name) {
713 const char* bname = strrchr(name, '/');
714 ptr = soinfo_alloc(bname ? bname + 1 : name);
715 }
716 ~soinfo_ptr() {
717 soinfo_free(ptr);
718 }
719 soinfo* release() {
720 soinfo* result = ptr;
721 ptr = NULL;
722 return result;
723 }
724 soinfo* ptr;
725};
726
727// TODO: rewrite linker_phdr.h to use a class, then lose this.
728struct phdr_ptr {
729 phdr_ptr() : phdr_mmap(NULL) {}
730 ~phdr_ptr() {
731 if (phdr_mmap != NULL) {
732 phdr_table_unload(phdr_mmap, phdr_size);
733 }
734 }
735 void* phdr_mmap;
736 Elf32_Addr phdr_size;
737};
738
Elliott Hughes124fae92012-10-31 14:20:03 -0700739static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700740 // Open the file.
741 scoped_fd fd;
742 fd.fd = open_library(name);
743 if (fd.fd == -1) {
744 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800745 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700746 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800747
Elliott Hughes46882792012-08-03 16:49:39 -0700748 // Read the ELF header.
749 Elf32_Ehdr header[1];
750 int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200751 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700752 DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
753 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200754 }
755 if (ret != (int)sizeof(header)) {
Elliott Hughes46882792012-08-03 16:49:39 -0700756 DL_ERR("too small to be an ELF executable: %s", name);
757 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200758 }
759 if (verify_elf_header(header) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700760 DL_ERR("not a valid ELF executable: %s", name);
761 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800762 }
763
Elliott Hughes46882792012-08-03 16:49:39 -0700764 // Read the program header table.
765 const Elf32_Phdr* phdr_table;
766 phdr_ptr phdr_holder;
767 ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
768 &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200769 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700770 DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
771 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200772 }
Elliott Hughes46882792012-08-03 16:49:39 -0700773 size_t phdr_count = header->e_phnum;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200774
Elliott Hughes46882792012-08-03 16:49:39 -0700775 // Get the load extents.
776 Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
777 TRACE("[ %5d - '%s' wants sz=0x%08x ]\n", pid, name, ext_sz);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200778 if (ext_sz == 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700779 DL_ERR("no loadable segments in file: %s", name);
780 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800781 }
782
Elliott Hughes46882792012-08-03 16:49:39 -0700783 // We no longer support pre-linked libraries.
784 if (is_prelinked(fd.fd, name)) {
785 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200786 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200787
Elliott Hughes46882792012-08-03 16:49:39 -0700788 // Reserve address space for all loadable segments.
789 void* load_start = NULL;
790 Elf32_Addr load_size = 0;
791 Elf32_Addr load_bias = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200792 ret = phdr_table_reserve_memory(phdr_table,
793 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200794 &load_start,
795 &load_size,
796 &load_bias);
797 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700798 DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
799 ext_sz, name, strerror(errno));
800 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200801 }
802
803 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
804 pid, name, load_start, load_size);
805
806 /* Map all the segments in our address space with default protections */
807 ret = phdr_table_load_segments(phdr_table,
808 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200809 load_bias,
Elliott Hughes46882792012-08-03 16:49:39 -0700810 fd.fd);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200811 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700812 DL_ERR("can't map loadable segments for \"%s\": %s",
813 name, strerror(errno));
814 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200815 }
816
Elliott Hughes46882792012-08-03 16:49:39 -0700817 soinfo_ptr si(name);
818 if (si.ptr == NULL) {
819 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800820 }
821
Elliott Hughes46882792012-08-03 16:49:39 -0700822 si.ptr->base = (Elf32_Addr) load_start;
823 si.ptr->size = load_size;
824 si.ptr->load_bias = load_bias;
825 si.ptr->flags = 0;
826 si.ptr->entry = 0;
827 si.ptr->dynamic = (unsigned *)-1;
828 si.ptr->phnum = phdr_count;
829 si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
830 if (si.ptr->phdr == NULL) {
831 DL_ERR("can't find loaded PHDR for \"%s\"", name);
832 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200833 }
Elliott Hughes46882792012-08-03 16:49:39 -0700834
835 return si.release();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800836}
837
838static soinfo *
839init_library(soinfo *si)
840{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800841 /* At this point we know that whatever is loaded @ base is a valid ELF
842 * shared library whose segments are properly mapped in. */
843 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
844 pid, si->base, si->size, si->name);
845
Elliott Hughes124fae92012-10-31 14:20:03 -0700846 if (!soinfo_link_image(si)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800847 munmap((void *)si->base, si->size);
848 return NULL;
849 }
850
851 return si;
852}
853
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200854static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800855{
856 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700857 const char *bname;
858
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200859 // TODO: don't use basename only for determining libraries
860 // http://code.google.com/p/android/issues/detail?id=6670
861
862 bname = strrchr(name, '/');
863 bname = bname ? bname + 1 : name;
864
865 for(si = solist; si != NULL; si = si->next){
866 if(!strcmp(bname, si->name)) {
867 return si;
868 }
869 }
870 return NULL;
871}
872
873soinfo *find_library(const char *name)
874{
875 soinfo *si;
876
David 'Digit' Turner67748092010-07-21 16:18:21 -0700877 if (name == NULL)
878 return somain;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700879
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200880 si = find_loaded_library(name);
881 if (si != NULL) {
882 if(si->flags & FLAG_ERROR) {
883 DL_ERR("\"%s\" failed to load previously", name);
Dima Zavin2e855792009-05-20 18:28:09 -0700884 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800885 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200886 if(si->flags & FLAG_LINKED) return si;
887 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
888 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800889 }
890
891 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
892 si = load_library(name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700893 if (si == NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800894 return NULL;
895 return init_library(si);
896}
897
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800898static void call_destructors(soinfo *si);
Elliott Hughesbedfe382012-08-14 14:07:59 -0700899
900int soinfo_unload(soinfo* si) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800901 if (si->refcount == 1) {
902 TRACE("%5d unloading '%s'\n", pid, si->name);
903 call_destructors(si);
904
Elliott Hughesbedfe382012-08-14 14:07:59 -0700905 for (unsigned* d = si->dynamic; *d; d += 2) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800906 if(d[0] == DT_NEEDED){
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200907 soinfo *lsi = find_loaded_library(si->strtab + d[1]);
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200908 if (lsi) {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700909 TRACE("%5d %s needs to unload %s\n", pid,
910 si->name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200911 soinfo_unload(lsi);
Elliott Hughesbedfe382012-08-14 14:07:59 -0700912 } else {
913 // TODO: should we return -1 in this case?
Elliott Hughes46882792012-08-03 16:49:39 -0700914 DL_ERR("\"%s\": could not unload dependent library",
915 si->name);
Elliott Hughesbedfe382012-08-14 14:07:59 -0700916 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800917 }
918 }
919
920 munmap((char *)si->base, si->size);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700921 notify_gdb_of_unload(si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200922 soinfo_free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800923 si->refcount = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700924 } else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800925 si->refcount--;
926 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
927 pid, si->name, si->refcount);
928 }
Elliott Hughesbedfe382012-08-14 14:07:59 -0700929 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800930}
931
932/* TODO: don't use unsigned for addrs below. It works, but is not
933 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
934 * long.
935 */
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200936static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count,
937 soinfo *needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800938{
939 Elf32_Sym *symtab = si->symtab;
940 const char *strtab = si->strtab;
941 Elf32_Sym *s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800942 Elf32_Rel *start = rel;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200943 soinfo *lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800944
Elliott Hughes46882792012-08-03 16:49:39 -0700945 for (size_t idx = 0; idx < count; ++idx, ++rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800946 unsigned type = ELF32_R_TYPE(rel->r_info);
947 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200948 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800949 unsigned sym_addr = 0;
950 char *sym_name = NULL;
951
952 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
953 si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700954 if (type == 0) { // R_*_NONE
955 continue;
956 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800957 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -0700958 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200959 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Doug Kwane8238072009-10-26 12:05:23 -0700960 if(s == NULL) {
961 /* We only allow an undefined symbol if this is a weak
962 reference.. */
963 s = &symtab[sym];
964 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700965 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -0700966 return -1;
967 }
968
969 /* IHI0044C AAELF 4.5.1.1:
970
971 Libraries are not searched to resolve weak references.
972 It is not an error for a weak reference to remain
973 unsatisfied.
974
975 During linking, the value of an undefined weak reference is:
976 - Zero if the relocation type is absolute
977 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -0700978 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -0700979 type is base-relative.
980 */
981
982 switch (type) {
983#if defined(ANDROID_ARM_LINKER)
984 case R_ARM_JUMP_SLOT:
985 case R_ARM_GLOB_DAT:
986 case R_ARM_ABS32:
987 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -0700988#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700989 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -0700990 case R_386_GLOB_DAT:
991 case R_386_32:
992 case R_386_RELATIVE: /* Dont' care. */
993#endif /* ANDROID_*_LINKER */
994 /* sym_addr was initialized to be zero above or relocation
995 code below does not care about value of sym_addr.
996 No need to do anything. */
997 break;
998
999#if defined(ANDROID_X86_LINKER)
1000 case R_386_PC32:
1001 sym_addr = reloc;
1002 break;
1003#endif /* ANDROID_X86_LINKER */
1004
1005#if defined(ANDROID_ARM_LINKER)
1006 case R_ARM_COPY:
1007 /* Fall through. Can't really copy if weak symbol is
1008 not found in run-time. */
1009#endif /* ANDROID_ARM_LINKER */
1010 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001011 DL_ERR("unknown weak reloc type %d @ %p (%d)",
1012 type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -07001013 return -1;
1014 }
1015 } else {
1016 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001017#if 0
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001018 if((base == 0) && (si->base != 0)){
1019 /* linking from libraries to main image is bad */
Elliott Hughes46882792012-08-03 16:49:39 -07001020 DL_ERR("cannot locate \"%s\"...",
1021 strtab + symtab[sym].st_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001022 return -1;
1023 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001024#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001025 sym_addr = (unsigned)(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001026 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001027 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001028 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001029 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001030 }
1031
1032/* TODO: This is ugly. Split up the relocations by arch into
1033 * different files.
1034 */
1035 switch(type){
1036#if defined(ANDROID_ARM_LINKER)
1037 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001038 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001039 MARK(rel->r_offset);
1040 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1041 reloc, sym_addr, sym_name);
1042 *((unsigned*)reloc) = sym_addr;
1043 break;
1044 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001045 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001046 MARK(rel->r_offset);
1047 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1048 reloc, sym_addr, sym_name);
1049 *((unsigned*)reloc) = sym_addr;
1050 break;
1051 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001052 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001053 MARK(rel->r_offset);
1054 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1055 reloc, sym_addr, sym_name);
1056 *((unsigned*)reloc) += sym_addr;
1057 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001058 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001059 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001060 MARK(rel->r_offset);
1061 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1062 reloc, sym_addr, rel->r_offset, sym_name);
1063 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1064 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001065#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001066 case R_386_JMP_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_386_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;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001080#elif defined(ANDROID_MIPS_LINKER)
1081 case R_MIPS_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001082 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001083 MARK(rel->r_offset);
1084 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1085 reloc, sym_addr, sym_name);
1086 *((unsigned*)reloc) = sym_addr;
1087 break;
1088 case R_MIPS_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001089 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001090 MARK(rel->r_offset);
1091 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x %s\n", pid,
1092 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1093 if (s) {
1094 *((unsigned*)reloc) += sym_addr;
1095 } else {
1096 *((unsigned*)reloc) += si->base;
1097 }
1098 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001099#endif /* ANDROID_*_LINKER */
1100
1101#if defined(ANDROID_ARM_LINKER)
1102 case R_ARM_RELATIVE:
1103#elif defined(ANDROID_X86_LINKER)
1104 case R_386_RELATIVE:
1105#endif /* ANDROID_*_LINKER */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001106 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001107 MARK(rel->r_offset);
Elliott Hughes46882792012-08-03 16:49:39 -07001108 if (sym) {
1109 DL_ERR("odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001110 return -1;
1111 }
1112 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1113 reloc, si->base);
1114 *((unsigned*)reloc) += si->base;
1115 break;
1116
1117#if defined(ANDROID_X86_LINKER)
1118 case R_386_32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001119 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001120 MARK(rel->r_offset);
1121
1122 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1123 reloc, sym_addr, sym_name);
1124 *((unsigned *)reloc) += (unsigned)sym_addr;
1125 break;
1126
1127 case R_386_PC32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001128 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001129 MARK(rel->r_offset);
1130 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1131 "+%08x (%08x - %08x) %s\n", pid, reloc,
1132 (sym_addr - reloc), sym_addr, reloc, sym_name);
1133 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1134 break;
1135#endif /* ANDROID_X86_LINKER */
1136
1137#ifdef ANDROID_ARM_LINKER
1138 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001139 if ((si->flags & FLAG_EXE) == 0) {
1140 /*
1141 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1142 *
1143 * Section 4.7.1.10 "Dynamic relocations"
1144 * R_ARM_COPY may only appear in executable objects where e_type is
1145 * set to ET_EXEC.
1146 *
1147 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1148 * We should explicitly disallow ET_DYN executables from having
1149 * R_ARM_COPY relocations.
1150 */
1151 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1152 return -1;
1153 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001154 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001155 MARK(rel->r_offset);
1156 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1157 reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001158 if (reloc == sym_addr) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001159 Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1160
1161 if (src == NULL) {
1162 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1163 return -1;
1164 }
1165 if (lsi->has_DT_SYMBOLIC) {
1166 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1167 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1168 return -1;
1169 }
1170 if (s->st_size < src->st_size) {
1171 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1172 si->name, s->st_size, src->st_size);
1173 return -1;
1174 }
1175 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1176 } else {
1177 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001178 return -1;
1179 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001180 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001181#endif /* ANDROID_ARM_LINKER */
1182
1183 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001184 DL_ERR("unknown reloc type %d @ %p (%d)",
1185 type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001186 return -1;
1187 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001188 }
1189 return 0;
1190}
1191
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001192#ifdef ANDROID_MIPS_LINKER
Elliott Hughesbedfe382012-08-14 14:07:59 -07001193static int mips_relocate_got(soinfo* si, soinfo* needed[]) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001194 unsigned *got;
1195 unsigned local_gotno, gotsym, symtabno;
1196 Elf32_Sym *symtab, *sym;
1197 unsigned g;
1198
1199 got = si->plt_got;
1200 local_gotno = si->mips_local_gotno;
1201 gotsym = si->mips_gotsym;
1202 symtabno = si->mips_symtabno;
1203 symtab = si->symtab;
1204
1205 /*
1206 * got[0] is address of lazy resolver function
1207 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001208 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001209 * (should be _rtld_bind_start)
1210 * FIXME: maybe this should be in a separate routine
1211 */
1212
1213 if ((si->flags & FLAG_LINKER) == 0) {
1214 g = 0;
1215 got[g++] = 0xdeadbeef;
1216 if (got[g] & 0x80000000) {
1217 got[g++] = 0xdeadfeed;
1218 }
1219 /*
1220 * Relocate the local GOT entries need to be relocated
1221 */
1222 for (; g < local_gotno; g++) {
1223 got[g] += si->load_bias;
1224 }
1225 }
1226
1227 /* Now for the global GOT entries */
1228 sym = symtab + gotsym;
1229 got = si->plt_got + local_gotno;
1230 for (g = gotsym; g < symtabno; g++, sym++, got++) {
1231 const char *sym_name;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001232 Elf32_Sym *s;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001233 soinfo *lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001234
1235 /* This is an undefined reference... try to locate it */
1236 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001237 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001238 if (s == NULL) {
1239 /* We only allow an undefined symbol if this is a weak
1240 reference.. */
1241 s = &symtab[g];
1242 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001243 DL_ERR("cannot locate \"%s\"...", sym_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001244 return -1;
1245 }
1246 *got = 0;
1247 }
1248 else {
1249 /* FIXME: is this sufficient?
1250 * For reference see NetBSD link loader
1251 * http://cvsweb.netbsd.org/bsdweb.cgi/src/libexec/ld.elf_so/arch/mips/mips_reloc.c?rev=1.53&content-type=text/x-cvsweb-markup
1252 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001253 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001254 }
1255 }
1256 return 0;
1257}
1258#endif
1259
David 'Digit' Turner82156792009-05-18 14:37:41 +02001260/* Please read the "Initialization and Termination functions" functions.
1261 * of the linker design note in bionic/linker/README.TXT to understand
1262 * what the following code is doing.
1263 *
1264 * The important things to remember are:
1265 *
1266 * DT_PREINIT_ARRAY must be called first for executables, and should
1267 * not appear in shared libraries.
1268 *
1269 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1270 *
1271 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1272 *
1273 * DT_FINI_ARRAY must be parsed in reverse order.
1274 */
1275
1276static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001277{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001278 int n, inc = 1;
1279
1280 if (reverse) {
1281 ctor += (count-1);
1282 inc = -1;
1283 }
1284
1285 for(n = count; n > 0; n--) {
1286 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1287 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001288 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001289 void (*func)() = (void (*)()) *ctor;
1290 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001291 if(((int) func == 0) || ((int) func == -1)) continue;
1292 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1293 func();
1294 }
1295}
1296
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001297static void soinfo_call_preinit_constructors(soinfo *si)
1298{
1299 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1300 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1301 si->name);
1302 call_array(si->preinit_array, si->preinit_array_count, 0);
1303 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1304}
1305
David 'Digit' Turner16084162012-06-12 16:25:37 +02001306void soinfo_call_constructors(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001307{
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001308 if (si->constructors_called)
1309 return;
1310
Jesse Hallf5d16932012-01-30 15:39:57 -08001311 // Set this before actually calling the constructors, otherwise it doesn't
1312 // protect against recursive constructor calls. One simple example of
1313 // constructor recursion is the libc debug malloc, which is implemented in
1314 // libc_malloc_debug_leak.so:
1315 // 1. The program depends on libc, so libc's constructor is called here.
1316 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001317 // 3. dlopen() calls soinfo_call_constructors() with the newly created
Jesse Hallf5d16932012-01-30 15:39:57 -08001318 // soinfo for libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001319 // 4. The debug so depends on libc, so soinfo_call_constructors() is
Jesse Hallf5d16932012-01-30 15:39:57 -08001320 // called again with the libc soinfo. If it doesn't trigger the early-
1321 // out above, the libc constructor will be called again (recursively!).
1322 si->constructors_called = 1;
1323
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001324 if (!(si->flags & FLAG_EXE) && si->preinit_array) {
1325 DL_ERR("shared library \"%s\" has a preinit_array table @ 0x%08x. "
1326 "This is INVALID.", si->name, (unsigned) si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001327 }
1328
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001329 if (si->dynamic) {
1330 unsigned *d;
1331 for(d = si->dynamic; *d; d += 2) {
1332 if(d[0] == DT_NEEDED){
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001333 soinfo* lsi = find_loaded_library(si->strtab + d[1]);
1334 if (!lsi) {
1335 DL_ERR("\"%s\": could not initialize dependent library",
1336 si->name);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001337 } else {
David 'Digit' Turner16084162012-06-12 16:25:37 +02001338 soinfo_call_constructors(lsi);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001339 }
1340 }
1341 }
1342 }
1343
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001344 if (si->init_func) {
1345 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1346 (unsigned)si->init_func, si->name);
1347 si->init_func();
1348 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1349 }
1350
1351 if (si->init_array) {
1352 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1353 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001354 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001355 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1356 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001357
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001358}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001359
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001360static void call_destructors(soinfo *si)
1361{
1362 if (si->fini_array) {
1363 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1364 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001365 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001366 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1367 }
1368
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001369 if (si->fini_func) {
1370 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1371 (unsigned)si->fini_func, si->name);
1372 si->fini_func();
1373 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1374 }
1375}
1376
1377/* Force any of the closed stdin, stdout and stderr to be associated with
1378 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001379static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001380 int dev_null, i, status;
1381 int return_value = 0;
1382
David 'Digit' Turner16084162012-06-12 16:25:37 +02001383 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001384 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001385 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001386 return -1;
1387 }
1388 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1389
1390 /* If any of the stdio file descriptors is valid and not associated
1391 with /dev/null, dup /dev/null to it. */
1392 for (i = 0; i < 3; i++) {
1393 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001394 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001395 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001396 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001397
1398 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
Elliott Hughes46882792012-08-03 16:49:39 -07001399 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001400
Elliott Hughes46882792012-08-03 16:49:39 -07001401 /* If file is opened, we are good. */
1402 if (status != -1) {
1403 continue;
1404 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001405
1406 /* The only error we allow is that the file descriptor does not
1407 exist, in which case we dup /dev/null to it. */
1408 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001409 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001410 return_value = -1;
1411 continue;
1412 }
1413
1414 /* Try dupping /dev/null to this stdio file descriptor and
1415 repeat if there is a signal. Note that any errors in closing
1416 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001417 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001418 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001419 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001420 return_value = -1;
1421 continue;
1422 }
1423 }
1424
1425 /* If /dev/null is not one of the stdio file descriptors, close it. */
1426 if (dev_null > 2) {
1427 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001428 status = TEMP_FAILURE_RETRY(close(dev_null));
1429 if (status == -1) {
1430 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001431 return_value = -1;
1432 }
1433 }
1434
1435 return return_value;
1436}
1437
Elliott Hughes124fae92012-10-31 14:20:03 -07001438static bool soinfo_link_image(soinfo* si) {
1439 si->flags |= FLAG_ERROR;
1440
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001441 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001442 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001443 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001444 int phnum = si->phnum;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001445 int relocating_linker = (si->flags & FLAG_LINKER) != 0;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001446 soinfo **needed, **pneeded;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001447
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001448 /* We can't debug anything until the linker is relocated */
1449 if (!relocating_linker) {
1450 INFO("[ %5d linking %s ]\n", pid, si->name);
1451 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1452 si->base, si->flags);
1453 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001454
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001455 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001456 size_t dynamic_count;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001457 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
1458 &dynamic_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001459 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001460 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001461 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001462 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001463 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001464 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001465 if (!relocating_linker) {
1466 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1467 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001468 }
1469
1470#ifdef ANDROID_ARM_LINKER
1471 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1472 &si->ARM_exidx, &si->ARM_exidx_count);
1473#endif
1474
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001475 /* extract useful information from dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001476 for (unsigned* d = si->dynamic; *d; ++d) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001477 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1478 switch(*d++){
1479 case DT_HASH:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001480 si->nbucket = ((unsigned *) (base + *d))[0];
1481 si->nchain = ((unsigned *) (base + *d))[1];
1482 si->bucket = (unsigned *) (base + *d + 8);
1483 si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001484 break;
1485 case DT_STRTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001486 si->strtab = (const char *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001487 break;
1488 case DT_SYMTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001489 si->symtab = (Elf32_Sym *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001490 break;
1491 case DT_PLTREL:
1492 if(*d != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001493 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1494 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001495 }
1496 break;
1497 case DT_JMPREL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001498 si->plt_rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001499 break;
1500 case DT_PLTRELSZ:
1501 si->plt_rel_count = *d / 8;
1502 break;
1503 case DT_REL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001504 si->rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001505 break;
1506 case DT_RELSZ:
1507 si->rel_count = *d / 8;
1508 break;
1509 case DT_PLTGOT:
1510 /* Save this in case we decide to do lazy binding. We don't yet. */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001511 si->plt_got = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001512 break;
1513 case DT_DEBUG:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001514#if !defined(ANDROID_MIPS_LINKER)
Elliott Hughesbedfe382012-08-14 14:07:59 -07001515 // Set the DT_DEBUG entry to the address of _r_debug for GDB
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001516 *d = (int) &_r_debug;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001517#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001518 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001519 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001520 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1521 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001522 case DT_INIT:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001523 si->init_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001524 DEBUG("%5d %s constructors (init func) found at %p\n",
1525 pid, si->name, si->init_func);
1526 break;
1527 case DT_FINI:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001528 si->fini_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001529 DEBUG("%5d %s destructors (fini func) found at %p\n",
1530 pid, si->name, si->fini_func);
1531 break;
1532 case DT_INIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001533 si->init_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001534 DEBUG("%5d %s constructors (init_array) found at %p\n",
1535 pid, si->name, si->init_array);
1536 break;
1537 case DT_INIT_ARRAYSZ:
1538 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1539 break;
1540 case DT_FINI_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001541 si->fini_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001542 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1543 pid, si->name, si->fini_array);
1544 break;
1545 case DT_FINI_ARRAYSZ:
1546 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1547 break;
1548 case DT_PREINIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001549 si->preinit_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001550 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1551 pid, si->name, si->preinit_array);
1552 break;
1553 case DT_PREINIT_ARRAYSZ:
1554 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1555 break;
1556 case DT_TEXTREL:
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001557 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001558 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001559 case DT_SYMBOLIC:
1560 si->has_DT_SYMBOLIC = true;
1561 break;
1562#if defined(DT_FLAGS)
1563 case DT_FLAGS:
1564 if (*d & DF_TEXTREL) {
1565 si->has_text_relocations = true;
1566 }
1567 if (*d & DF_SYMBOLIC) {
1568 si->has_DT_SYMBOLIC = true;
1569 }
1570 break;
1571#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001572#if defined(ANDROID_MIPS_LINKER)
1573 case DT_NEEDED:
1574 case DT_STRSZ:
1575 case DT_SYMENT:
1576 case DT_RELENT:
1577 break;
1578 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001579 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001580 {
Elliott Hughesbedfe382012-08-14 14:07:59 -07001581 r_debug** dp = (r_debug**) *d;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001582 *dp = &_r_debug;
1583 }
1584 break;
1585 case DT_MIPS_RLD_VERSION:
1586 case DT_MIPS_FLAGS:
1587 case DT_MIPS_BASE_ADDRESS:
1588 case DT_MIPS_UNREFEXTNO:
1589 case DT_MIPS_RWPLT:
1590 break;
1591
1592 case DT_MIPS_PLTGOT:
1593#if 0
1594 /* not yet... */
1595 si->mips_pltgot = (unsigned *)(si->base + *d);
1596#endif
1597 break;
1598
1599 case DT_MIPS_SYMTABNO:
1600 si->mips_symtabno = *d;
1601 break;
1602
1603 case DT_MIPS_LOCAL_GOTNO:
1604 si->mips_local_gotno = *d;
1605 break;
1606
1607 case DT_MIPS_GOTSYM:
1608 si->mips_gotsym = *d;
1609 break;
1610
1611 default:
1612 DEBUG("%5d Unused DT entry: type 0x%08x arg 0x%08x\n",
1613 pid, d[-1], d[0]);
1614 break;
1615#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001616 }
1617 }
1618
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001619 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001620 pid, si->base, si->strtab, si->symtab);
1621
Elliott Hughes124fae92012-10-31 14:20:03 -07001622 // Sanity checks.
1623 if (si->nbucket == 0) {
1624 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1625 return false;
1626 }
1627 if (si->strtab == 0) {
1628 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1629 return false;
1630 }
1631 if (si->symtab == 0) {
1632 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1633 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001634 }
1635
Matt Fischer4fd42c12009-12-31 12:09:10 -06001636 /* if this is the main executable, then load all of the preloads now */
1637 if(si->flags & FLAG_EXE) {
1638 int i;
1639 memset(preloads, 0, sizeof(preloads));
Elliott Hughes124fae92012-10-31 14:20:03 -07001640 for(i = 0; gLdPreloadNames[i] != NULL; i++) {
1641 soinfo *lsi = find_library(gLdPreloadNames[i]);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001642 if(lsi == 0) {
1643 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001644 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Elliott Hughes124fae92012-10-31 14:20:03 -07001645 gLdPreloadNames[i], si->name, tmp_err_buf);
1646 return false;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001647 }
1648 lsi->refcount++;
1649 preloads[i] = lsi;
1650 }
1651 }
1652
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001653 /* dynamic_count is an upper bound for the number of needed libs */
1654 pneeded = needed = (soinfo**) alloca((1 + dynamic_count) * sizeof(soinfo*));
1655
Elliott Hughes124fae92012-10-31 14:20:03 -07001656 for (unsigned* d = si->dynamic; *d; d += 2) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001657 if(d[0] == DT_NEEDED){
1658 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001659 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001660 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001661 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001662 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1663 si->strtab + d[1], si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001664 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001665 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001666 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001667 lsi->refcount++;
1668 }
1669 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001670 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001671
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001672 if (si->has_text_relocations) {
1673 /* Unprotect the segments, i.e. make them writable, to allow
1674 * text relocations to work properly. We will later call
1675 * phdr_table_protect_segments() after all of them are applied
1676 * and all constructors are run.
1677 */
1678 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1679 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1680 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001681 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001682 }
1683 }
1684
Elliott Hughes124fae92012-10-31 14:20:03 -07001685 if (si->plt_rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001686 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
Elliott Hughes124fae92012-10-31 14:20:03 -07001687 if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1688 return false;
1689 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001690 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001691 if (si->rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001692 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
Elliott Hughes124fae92012-10-31 14:20:03 -07001693 if(soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1694 return false;
1695 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001696 }
1697
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001698#ifdef ANDROID_MIPS_LINKER
Elliott Hughes124fae92012-10-31 14:20:03 -07001699 if (mips_relocate_got(si, needed)) {
1700 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001701 }
1702#endif
1703
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001704 si->flags |= FLAG_LINKED;
1705 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1706
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001707 if (si->has_text_relocations) {
1708 /* All relocations are done, we can protect our segments back to
1709 * read-only. */
1710 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1711 DL_ERR("can't protect segments for \"%s\": %s",
1712 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001713 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001714 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001715 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001716
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001717 /* We can also turn on GNU RELRO protection */
1718 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001719 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1720 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001721 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001722 }
1723
Elliott Hughes124fae92012-10-31 14:20:03 -07001724 // If this is a setuid/setgid program, close the security hole described in
1725 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
Elliott Hughes18a206c2012-10-29 17:37:13 -07001726 if (get_AT_SECURE()) {
Elliott Hughes46882792012-08-03 16:49:39 -07001727 nullify_closed_stdio();
1728 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001729 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001730 si->flags &= ~FLAG_ERROR;
1731 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001732}
1733
Elliott Hughes46882792012-08-03 16:49:39 -07001734static void parse_path(const char* path, const char* delimiters,
1735 const char** array, char* buf, size_t buf_size, size_t max_count)
David Bartleybc3a5c22009-06-02 18:27:28 -07001736{
Elliott Hughes46882792012-08-03 16:49:39 -07001737 if (path == NULL) {
1738 return;
David Bartleybc3a5c22009-06-02 18:27:28 -07001739 }
1740
Elliott Hughes46882792012-08-03 16:49:39 -07001741 size_t len = strlcpy(buf, path, buf_size);
David Bartleybc3a5c22009-06-02 18:27:28 -07001742
Elliott Hughes46882792012-08-03 16:49:39 -07001743 size_t i = 0;
1744 char* buf_p = buf;
1745 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
1746 if (*array[i] != '\0') {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001747 ++i;
1748 }
1749 }
1750
Elliott Hughes46882792012-08-03 16:49:39 -07001751 // Forget the last path if we had to truncate; this occurs if the 2nd to
1752 // last char isn't '\0' (i.e. wasn't originally a delimiter).
1753 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
1754 array[i - 1] = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001755 } else {
Elliott Hughes46882792012-08-03 16:49:39 -07001756 array[i] = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001757 }
1758}
1759
Elliott Hughes46882792012-08-03 16:49:39 -07001760static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001761 parse_path(path, ":", gLdPaths,
1762 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
Elliott Hughes46882792012-08-03 16:49:39 -07001763}
1764
1765static void parse_LD_PRELOAD(const char* path) {
1766 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes124fae92012-10-31 14:20:03 -07001767 parse_path(path, " :", gLdPreloadNames,
1768 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
Elliott Hughes46882792012-08-03 16:49:39 -07001769}
1770
Nick Kralevich468319c2011-11-11 15:53:17 -08001771/*
1772 * This code is called after the linker has linked itself and
1773 * fixed it's own GOT. It is safe to make references to externs
1774 * and other non-local data at this point.
1775 */
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001776static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001777{
1778 static soinfo linker_soinfo;
1779
1780 int argc = (int) *elfdata;
1781 char **argv = (char**) (elfdata + 1);
Stephen Smalleybb440552012-01-20 10:59:15 -08001782 unsigned *vecs = (unsigned*) (argv + argc + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001783
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001784 /* NOTE: we store the elfdata pointer on a special location
1785 * of the temporary TLS area in order to pass it to
1786 * the C Library's runtime initializer.
1787 *
1788 * The initializer must clear the slot and reset the TLS
1789 * to point to a different location to ensure that no other
1790 * shared library constructor can access it.
1791 */
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001792 __libc_init_tls(elfdata);
1793
1794 pid = getpid();
1795
1796#if TIMING
1797 struct timeval t0, t1;
1798 gettimeofday(&t0, 0);
1799#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001800
Elliott Hughes18a206c2012-10-29 17:37:13 -07001801 // Initialize environment functions, and get to the ELF aux vectors table.
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001802 vecs = linker_env_init(vecs);
1803
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001804 debugger_init();
1805
Elliott Hughes18a206c2012-10-29 17:37:13 -07001806 // Get a few environment variables.
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07001807#if LINKER_DEBUG
Elliott Hughes18a206c2012-10-29 17:37:13 -07001808 {
1809 const char* env = linker_env_get("LD_DEBUG");
1810 if (env != NULL) {
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001811 debug_verbosity = atoi(env);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001812 }
1813 }
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07001814#endif
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001815
Elliott Hughes18a206c2012-10-29 17:37:13 -07001816 // Normally, these are cleaned by linker_env_init, but the test
1817 // doesn't cost us anything.
1818 const char* ldpath_env = NULL;
1819 const char* ldpreload_env = NULL;
1820 if (!get_AT_SECURE()) {
1821 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1822 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001823 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001824
1825 INFO("[ android linker & debugger ]\n");
1826 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
1827
Elliott Hughes18a206c2012-10-29 17:37:13 -07001828 soinfo* si = soinfo_alloc(argv[0]);
1829 if (si == NULL) {
1830 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001831 }
1832
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001833 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001834 si->flags |= FLAG_EXE;
Elliott Hughesbedfe382012-08-14 14:07:59 -07001835 link_map* map = &(si->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001836
1837 map->l_addr = 0;
1838 map->l_name = argv[0];
1839 map->l_prev = NULL;
1840 map->l_next = NULL;
1841
1842 _r_debug.r_map = map;
1843 r_debug_tail = map;
1844
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001845 /* gdb expects the linker to be in the debug shared object list.
1846 * Without this, gdb has trouble locating the linker's ".text"
1847 * and ".plt" sections. Gdb could also potentially use this to
1848 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1849 * Don't use soinfo_alloc(), because the linker shouldn't
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001850 * be on the soinfo list.
1851 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001852 strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001853 linker_soinfo.flags = 0;
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001854 linker_soinfo.base = linker_base;
Ben Cheng06f0e742012-08-10 16:07:02 -07001855 /*
1856 * Set the dynamic field in the link map otherwise gdb will complain with
1857 * the following:
1858 * warning: .dynamic section for "/system/bin/linker" is not at the
1859 * expected address (wrong library or version mismatch?)
1860 */
1861 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
1862 Elf32_Phdr *phdr =
1863 (Elf32_Phdr *)((unsigned char *) linker_base + elf_hdr->e_phoff);
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001864 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1865 &linker_soinfo.dynamic, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001866 insert_soinfo_into_debug_map(&linker_soinfo);
1867
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001868 /* extract information passed from the kernel */
Elliott Hughes18a206c2012-10-29 17:37:13 -07001869 while (vecs[0] != 0){
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001870 switch(vecs[0]){
1871 case AT_PHDR:
1872 si->phdr = (Elf32_Phdr*) vecs[1];
1873 break;
1874 case AT_PHNUM:
1875 si->phnum = (int) vecs[1];
1876 break;
1877 case AT_ENTRY:
1878 si->entry = vecs[1];
1879 break;
1880 }
1881 vecs += 2;
1882 }
1883
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001884 /* Compute the value of si->base. We can't rely on the fact that
1885 * the first entry is the PHDR because this will not be true
1886 * for certain executables (e.g. some in the NDK unit test suite)
1887 */
1888 int nn;
1889 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001890 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001891 si->load_bias = 0;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001892 for ( nn = 0; nn < si->phnum; nn++ ) {
1893 if (si->phdr[nn].p_type == PT_PHDR) {
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001894 si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
1895 si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001896 break;
1897 }
1898 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001899 si->dynamic = (unsigned *)-1;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001900 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001901
Elliott Hughes46882792012-08-03 16:49:39 -07001902 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1903 parse_LD_LIBRARY_PATH(ldpath_env);
1904 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001905
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001906 somain = si;
1907
Elliott Hughes124fae92012-10-31 14:20:03 -07001908 if (!soinfo_link_image(si)) {
Dima Zavin2e855792009-05-20 18:28:09 -07001909 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
1910 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
1911 write(2, errmsg, sizeof(errmsg));
Elliott Hughes18a206c2012-10-29 17:37:13 -07001912 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001913 }
1914
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001915 soinfo_call_preinit_constructors(si);
1916
Elliott Hughes18a206c2012-10-29 17:37:13 -07001917 for (size_t i = 0; preloads[i] != NULL; ++i) {
Kito Cheng326e85e2012-07-15 00:49:27 +08001918 soinfo_call_constructors(preloads[i]);
1919 }
1920
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08001921 /*After the link_image, the si->base is initialized.
1922 *For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1923 *We need to update this value for so exe here. So Unwind_Backtrace
1924 *for some arch like x86 could work correctly within so exe.
1925 */
1926 map->l_addr = si->base;
David 'Digit' Turner16084162012-06-12 16:25:37 +02001927 soinfo_call_constructors(si);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001928
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001929#if TIMING
1930 gettimeofday(&t1,NULL);
1931 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
1932 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1933 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1934 ));
1935#endif
1936#if STATS
1937 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07001938 linker_stats.count[kRelocAbsolute],
1939 linker_stats.count[kRelocRelative],
1940 linker_stats.count[kRelocCopy],
1941 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001942#endif
1943#if COUNT_PAGES
1944 {
1945 unsigned n;
1946 unsigned i;
1947 unsigned count = 0;
1948 for(n = 0; n < 4096; n++){
1949 if(bitmask[n]){
1950 unsigned x = bitmask[n];
1951 for(i = 0; i < 8; i++){
1952 if(x & 1) count++;
1953 x >>= 1;
1954 }
1955 }
1956 }
1957 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
1958 }
1959#endif
1960
1961#if TIMING || STATS || COUNT_PAGES
1962 fflush(stdout);
1963#endif
1964
1965 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
1966 si->entry);
1967 return si->entry;
1968}
Nick Kralevich468319c2011-11-11 15:53:17 -08001969
1970/*
1971 * Find the value of AT_BASE passed to us by the kernel. This is the load
1972 * location of the linker.
1973 */
1974static unsigned find_linker_base(unsigned **elfdata) {
1975 int argc = (int) *elfdata;
1976 char **argv = (char**) (elfdata + 1);
1977 unsigned *vecs = (unsigned*) (argv + argc + 1);
1978 while (vecs[0] != 0) {
1979 vecs++;
1980 }
1981
1982 /* The end of the environment block is marked by two NULL pointers */
1983 vecs++;
1984
1985 while(vecs[0]) {
1986 if (vecs[0] == AT_BASE) {
1987 return vecs[1];
1988 }
1989 vecs += 2;
1990 }
1991
1992 return 0; // should never happen
1993}
1994
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001995/* Compute the load-bias of an existing executable. This shall only
1996 * be used to compute the load bias of an executable or shared library
1997 * that was loaded by the kernel itself.
1998 *
1999 * Input:
2000 * elf -> address of ELF header, assumed to be at the start of the file.
2001 * Return:
2002 * load bias, i.e. add the value of any p_vaddr in the file to get
2003 * the corresponding address in memory.
2004 */
2005static Elf32_Addr
2006get_elf_exec_load_bias(const Elf32_Ehdr* elf)
2007{
2008 Elf32_Addr offset = elf->e_phoff;
2009 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
2010 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
2011 const Elf32_Phdr* phdr;
2012
2013 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
2014 if (phdr->p_type == PT_LOAD) {
2015 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
2016 }
2017 }
2018 return 0;
2019}
2020
Nick Kralevich468319c2011-11-11 15:53:17 -08002021/*
2022 * This is the entry point for the linker, called from begin.S. This
2023 * method is responsible for fixing the linker's own relocations, and
2024 * then calling __linker_init_post_relocation().
2025 *
2026 * Because this method is called before the linker has fixed it's own
2027 * relocations, any attempt to reference an extern variable, extern
2028 * function, or other GOT reference will generate a segfault.
2029 */
Elliott Hughes46882792012-08-03 16:49:39 -07002030extern "C" unsigned __linker_init(unsigned **elfdata) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002031 unsigned linker_addr = find_linker_base(elfdata);
2032 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2033 Elf32_Phdr *phdr =
2034 (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2035
2036 soinfo linker_so;
2037 memset(&linker_so, 0, sizeof(soinfo));
2038
2039 linker_so.base = linker_addr;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002040 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002041 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Nick Kralevich468319c2011-11-11 15:53:17 -08002042 linker_so.dynamic = (unsigned *) -1;
2043 linker_so.phdr = phdr;
2044 linker_so.phnum = elf_hdr->e_phnum;
2045 linker_so.flags |= FLAG_LINKER;
Nick Kralevich468319c2011-11-11 15:53:17 -08002046
Elliott Hughes124fae92012-10-31 14:20:03 -07002047 if (!soinfo_link_image(&linker_so)) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002048 // It would be nice to print an error message, but if the linker
2049 // can't link itself, there's no guarantee that we'll be able to
2050 // call write() (because it involves a GOT reference).
2051 //
2052 // This situation should never occur unless the linker itself
2053 // is corrupt.
Elliott Hughes18a206c2012-10-29 17:37:13 -07002054 exit(EXIT_FAILURE);
Nick Kralevich468319c2011-11-11 15:53:17 -08002055 }
2056
2057 // We have successfully fixed our own relocations. It's safe to run
2058 // the main part of the linker now.
Elliott Hughes5419b942012-10-16 15:54:46 -07002059 unsigned start_address = __linker_init_post_relocation(elfdata, linker_addr);
2060
2061 // Return the address that the calling assembly stub should jump to.
2062 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002063}