blob: 8dde9410b94a90d0166abf6311913d024530d6da [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * 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
29#include <linux/auxvec.h>
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35#include <fcntl.h>
36#include <errno.h>
37#include <dlfcn.h>
38#include <sys/stat.h>
39
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -070040#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
42#include <sys/mman.h>
43
44#include <sys/atomics.h>
45
46/* special private C library header - see Android.mk */
47#include <bionic_tls.h>
48
49#include "linker.h"
50#include "linker_debug.h"
51
52#include "ba.h"
53
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070054#define ALLOW_SYMBOLS_FROM_MAIN 1
James Dongba52b302009-04-30 20:37:36 -070055#define SO_MAX 96
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056
David Bartleybc3a5c22009-06-02 18:27:28 -070057/* Assume average path length of 64 and max 8 paths */
58#define LDPATH_BUFSIZE 512
59#define LDPATH_MAX 8
60
Matt Fischer4fd42c12009-12-31 12:09:10 -060061#define LDPRELOAD_BUFSIZE 512
62#define LDPRELOAD_MAX 8
63
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
65 *
66 * Do NOT use malloc() and friends or pthread_*() code here.
67 * Don't use printf() either; it's caused mysterious memory
68 * corruption in the past.
69 * The linker runs before we bring up libc and it's easiest
70 * to make sure it does not depend on any complex libc features
71 *
72 * open issues / todo:
73 *
74 * - should we do anything special for STB_WEAK symbols?
75 * - are we doing everything we should for ARM_COPY relocations?
76 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077 * - after linking, set as much stuff as possible to READONLY
78 * and NOEXEC
79 * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
80 * headers provide versions that are negative...
81 * - allocate space for soinfo structs dynamically instead of
82 * having a hard limit (64)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083*/
84
85
86static int link_image(soinfo *si, unsigned wr_offset);
87
88static int socount = 0;
89static soinfo sopool[SO_MAX];
90static soinfo *freelist = NULL;
91static soinfo *solist = &libdl_info;
92static soinfo *sonext = &libdl_info;
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070093#if ALLOW_SYMBOLS_FROM_MAIN
94static soinfo *somain; /* main process, always the one after libdl_info */
95#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096
Iliyan Malchevaf7315a2009-10-16 17:50:42 -070097
98/* Set up for the buddy allocator managing the prelinked libraries. */
99static struct ba_bits ba_prelink_bitmap[(LIBLAST - LIBBASE) / LIBINC];
100static struct ba ba_prelink = {
101 .base = LIBBASE,
102 .size = LIBLAST - LIBBASE,
103 .min_alloc = LIBINC,
Iliyan Malchevbb9eede2009-10-19 14:25:17 -0700104 /* max_order will be determined automatically */
Iliyan Malchevaf7315a2009-10-16 17:50:42 -0700105 .bitmap = ba_prelink_bitmap,
106 .num_entries = sizeof(ba_prelink_bitmap)/sizeof(ba_prelink_bitmap[0]),
107};
108
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700109static inline int validate_soinfo(soinfo *si)
110{
111 return (si >= sopool && si < sopool + SO_MAX) ||
112 si == &libdl_info;
113}
114
David Bartleybc3a5c22009-06-02 18:27:28 -0700115static char ldpaths_buf[LDPATH_BUFSIZE];
116static const char *ldpaths[LDPATH_MAX + 1];
117
Matt Fischer4fd42c12009-12-31 12:09:10 -0600118static char ldpreloads_buf[LDPRELOAD_BUFSIZE];
119static const char *ldpreload_names[LDPRELOAD_MAX + 1];
120
121static soinfo *preloads[LDPRELOAD_MAX + 1];
122
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123int debug_verbosity;
124static int pid;
125
126#if STATS
127struct _link_stats linker_stats;
128#endif
129
130#if COUNT_PAGES
131unsigned bitmask[4096];
132#endif
133
134#ifndef PT_ARM_EXIDX
135#define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */
136#endif
137
Dima Zavin2e855792009-05-20 18:28:09 -0700138#define HOODLUM(name, ret, ...) \
139 ret name __VA_ARGS__ \
140 { \
141 char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \
142 write(2, errstr, sizeof(errstr)); \
143 abort(); \
144 }
145HOODLUM(malloc, void *, (size_t size));
146HOODLUM(free, void, (void *ptr));
147HOODLUM(realloc, void *, (void *ptr, size_t size));
148HOODLUM(calloc, void *, (size_t cnt, size_t size));
149
Dima Zavin03531952009-05-29 17:30:25 -0700150static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700151static char __linker_dl_err_buf[768];
152#define DL_ERR(fmt, x...) \
153 do { \
154 snprintf(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), \
155 "%s[%d]: " fmt, __func__, __LINE__, ##x); \
Erik Gillingd00d23a2009-07-22 17:06:11 -0700156 ERROR(fmt "\n", ##x); \
Dima Zavin2e855792009-05-20 18:28:09 -0700157 } while(0)
158
159const char *linker_get_error(void)
160{
161 return (const char *)&__linker_dl_err_buf[0];
162}
163
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164/*
165 * This function is an empty stub where GDB locates a breakpoint to get notified
166 * about linker activity.
167 */
168extern void __attribute__((noinline)) rtld_db_dlactivity(void);
169
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
171 RT_CONSISTENT, 0};
172static struct link_map *r_debug_tail = 0;
173
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700174static pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
176static void insert_soinfo_into_debug_map(soinfo * info)
177{
178 struct link_map * map;
179
180 /* Copy the necessary fields into the debug structure.
181 */
182 map = &(info->linkmap);
183 map->l_addr = info->base;
184 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800185 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186
187 /* Stick the new library at the end of the list.
188 * gdb tends to care more about libc than it does
189 * about leaf libraries, and ordering it this way
190 * reduces the back-and-forth over the wire.
191 */
192 if (r_debug_tail) {
193 r_debug_tail->l_next = map;
194 map->l_prev = r_debug_tail;
195 map->l_next = 0;
196 } else {
197 _r_debug.r_map = map;
198 map->l_prev = 0;
199 map->l_next = 0;
200 }
201 r_debug_tail = map;
202}
203
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700204static void remove_soinfo_from_debug_map(soinfo * info)
205{
206 struct link_map * map = &(info->linkmap);
207
208 if (r_debug_tail == map)
209 r_debug_tail = map->l_prev;
210
211 if (map->l_prev) map->l_prev->l_next = map->l_next;
212 if (map->l_next) map->l_next->l_prev = map->l_prev;
213}
214
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800215void notify_gdb_of_load(soinfo * info)
216{
217 if (info->flags & FLAG_EXE) {
218 // GDB already knows about the main executable
219 return;
220 }
221
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700222 pthread_mutex_lock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223
224 _r_debug.r_state = RT_ADD;
225 rtld_db_dlactivity();
226
227 insert_soinfo_into_debug_map(info);
228
229 _r_debug.r_state = RT_CONSISTENT;
230 rtld_db_dlactivity();
231
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700232 pthread_mutex_unlock(&_r_debug_lock);
233}
234
235void notify_gdb_of_unload(soinfo * info)
236{
237 if (info->flags & FLAG_EXE) {
238 // GDB already knows about the main executable
239 return;
240 }
241
242 pthread_mutex_lock(&_r_debug_lock);
243
244 _r_debug.r_state = RT_DELETE;
245 rtld_db_dlactivity();
246
247 remove_soinfo_from_debug_map(info);
248
249 _r_debug.r_state = RT_CONSISTENT;
250 rtld_db_dlactivity();
251
252 pthread_mutex_unlock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800253}
254
255void notify_gdb_of_libraries()
256{
257 _r_debug.r_state = RT_ADD;
258 rtld_db_dlactivity();
259 _r_debug.r_state = RT_CONSISTENT;
260 rtld_db_dlactivity();
261}
262
263static soinfo *alloc_info(const char *name)
264{
265 soinfo *si;
266
267 if(strlen(name) >= SOINFO_NAME_LEN) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700268 DL_ERR("%5d library name %s too long", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269 return 0;
270 }
271
272 /* The freelist is populated when we call free_info(), which in turn is
273 done only by dlclose(), which is not likely to be used.
274 */
275 if (!freelist) {
276 if(socount == SO_MAX) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700277 DL_ERR("%5d too many libraries when loading %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800278 return NULL;
279 }
280 freelist = sopool + socount++;
281 freelist->next = NULL;
282 }
283
284 si = freelist;
285 freelist = freelist->next;
286
287 /* Make sure we get a clean block of soinfo */
288 memset(si, 0, sizeof(soinfo));
289 strcpy((char*) si->name, name);
290 sonext->next = si;
291 si->ba_index = -1; /* by default, prelinked */
292 si->next = NULL;
293 si->refcount = 0;
294 sonext = si;
295
296 TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
297 return si;
298}
299
300static void free_info(soinfo *si)
301{
302 soinfo *prev = NULL, *trav;
303
304 TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
305
306 for(trav = solist; trav != NULL; trav = trav->next){
307 if (trav == si)
308 break;
309 prev = trav;
310 }
311 if (trav == NULL) {
312 /* si was not ni solist */
Erik Gillingd00d23a2009-07-22 17:06:11 -0700313 DL_ERR("%5d name %s is not in solist!", pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800314 return;
315 }
316
317 /* prev will never be NULL, because the first entry in solist is
318 always the static libdl_info.
319 */
320 prev->next = si->next;
321 if (si == sonext) sonext = prev;
322 si->next = freelist;
323 freelist = si;
324}
325
326#ifndef LINKER_TEXT_BASE
327#error "linker's makefile must define LINKER_TEXT_BASE"
328#endif
329#ifndef LINKER_AREA_SIZE
330#error "linker's makefile must define LINKER_AREA_SIZE"
331#endif
332#define LINKER_BASE ((LINKER_TEXT_BASE) & 0xfff00000)
333#define LINKER_TOP (LINKER_BASE + (LINKER_AREA_SIZE))
334
335const char *addr_to_name(unsigned addr)
336{
337 soinfo *si;
338
339 for(si = solist; si != 0; si = si->next){
340 if((addr >= si->base) && (addr < (si->base + si->size))) {
341 return si->name;
342 }
343 }
344
345 if((addr >= LINKER_BASE) && (addr < LINKER_TOP)){
346 return "linker";
347 }
348
349 return "";
350}
351
352/* For a given PC, find the .so that it belongs to.
353 * Returns the base address of the .ARM.exidx section
354 * for that .so, and the number of 8-byte entries
355 * in that section (via *pcount).
356 *
357 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
358 *
359 * This function is exposed via dlfcn.c and libdl.so.
360 */
361#ifdef ANDROID_ARM_LINKER
362_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
363{
364 soinfo *si;
365 unsigned addr = (unsigned)pc;
366
367 if ((addr < LINKER_BASE) || (addr >= LINKER_TOP)) {
368 for (si = solist; si != 0; si = si->next){
369 if ((addr >= si->base) && (addr < (si->base + si->size))) {
370 *pcount = si->ARM_exidx_count;
371 return (_Unwind_Ptr)(si->base + (unsigned long)si->ARM_exidx);
372 }
373 }
374 }
375 *pcount = 0;
376 return NULL;
377}
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900378#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_SH_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800379/* Here, we only have to provide a callback to iterate across all the
380 * loaded libraries. gcc_eh does the rest. */
381int
382dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
383 void *data)
384{
385 soinfo *si;
386 struct dl_phdr_info dl_info;
387 int rv = 0;
388
389 for (si = solist; si != NULL; si = si->next) {
390 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;
394 rv = cb(&dl_info, sizeof (struct dl_phdr_info), data);
395 if (rv != 0)
396 break;
397 }
398 return rv;
399}
400#endif
401
402static Elf32_Sym *_elf_lookup(soinfo *si, unsigned hash, const char *name)
403{
404 Elf32_Sym *s;
405 Elf32_Sym *symtab = si->symtab;
406 const char *strtab = si->strtab;
407 unsigned n;
408
409 TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
410 name, si->name, si->base, hash, hash % si->nbucket);
411 n = hash % si->nbucket;
412
413 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
414 s = symtab + n;
415 if(strcmp(strtab + s->st_name, name)) continue;
416
417 /* only concern ourselves with global symbols */
418 switch(ELF32_ST_BIND(s->st_info)){
419 case STB_GLOBAL:
420 /* no section == undefined */
421 if(s->st_shndx == 0) continue;
422
423 case STB_WEAK:
424 TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
425 name, si->name, s->st_value, s->st_size);
426 return s;
427 }
428 }
429
430 return 0;
431}
432
433static unsigned elfhash(const char *_name)
434{
435 const unsigned char *name = (const unsigned char *) _name;
436 unsigned h = 0, g;
437
438 while(*name) {
439 h = (h << 4) + *name++;
440 g = h & 0xf0000000;
441 h ^= g;
442 h ^= g >> 24;
443 }
444 return h;
445}
446
447static Elf32_Sym *
448_do_lookup_in_so(soinfo *si, const char *name, unsigned *elf_hash)
449{
450 if (*elf_hash == 0)
451 *elf_hash = elfhash(name);
452 return _elf_lookup (si, *elf_hash, name);
453}
454
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700455static Elf32_Sym *
456_do_lookup(soinfo *si, const char *name, unsigned *base)
457{
458 unsigned elf_hash = 0;
459 Elf32_Sym *s;
460 unsigned *d;
461 soinfo *lsi = si;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600462 int i;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700463
464 /* Look for symbols in the local scope first (the object who is
465 * searching). This happens with C++ templates on i386 for some
466 * reason. */
467 s = _do_lookup_in_so(si, name, &elf_hash);
468 if(s != NULL)
469 goto done;
470
Matt Fischer4fd42c12009-12-31 12:09:10 -0600471 /* Next, look for it in the preloads list */
472 for(i = 0; preloads[i] != NULL; i++) {
473 lsi = preloads[i];
474 s = _do_lookup_in_so(lsi, name, &elf_hash);
475 if(s != NULL)
476 goto done;
477 }
478
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700479 for(d = si->dynamic; *d; d += 2) {
480 if(d[0] == DT_NEEDED){
481 lsi = (soinfo *)d[1];
482 if (!validate_soinfo(lsi)) {
483 DL_ERR("%5d bad DT_NEEDED pointer in %s",
484 pid, si->name);
485 return 0;
486 }
487
488 DEBUG("%5d %s: looking up %s in %s\n",
489 pid, si->name, name, lsi->name);
490 s = _do_lookup_in_so(lsi, name, &elf_hash);
Min-su, Kim3cab22c2010-01-19 10:05:33 +0900491 if ((s != NULL) && (s->st_shndx != SHN_UNDEF))
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700492 goto done;
493 }
494 }
495
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -0700496#if ALLOW_SYMBOLS_FROM_MAIN
497 /* If we are resolving relocations while dlopen()ing a library, it's OK for
498 * the library to resolve a symbol that's defined in the executable itself,
499 * although this is rare and is generally a bad idea.
500 */
501 if (somain) {
502 lsi = somain;
503 DEBUG("%5d %s: looking up %s in executable %s\n",
504 pid, si->name, name, lsi->name);
505 s = _do_lookup_in_so(lsi, name, &elf_hash);
506 }
507#endif
508
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700509done:
510 if(s != NULL) {
511 TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
512 "found in %s, base = 0x%08x\n",
513 pid, si->name, name, s->st_value, lsi->name, lsi->base);
514 *base = lsi->base;
515 return s;
516 }
517
518 return 0;
519}
520
521/* This is used by dl_sym(). It performs symbol lookup only within the
522 specified soinfo object and not in any of its dependencies.
523 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800524Elf32_Sym *lookup_in_library(soinfo *si, const char *name)
525{
526 unsigned unused = 0;
527 return _do_lookup_in_so(si, name, &unused);
528}
529
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700530/* This is used by dl_sym(). It performs a global symbol lookup.
531 */
Matt Fischer1698d9e2009-12-31 12:17:56 -0600532Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800533{
534 unsigned elf_hash = 0;
535 Elf32_Sym *s = NULL;
536 soinfo *si;
537
Matt Fischer1698d9e2009-12-31 12:17:56 -0600538 if(start == NULL) {
539 start = solist;
540 }
541
542 for(si = start; (s == NULL) && (si != NULL); si = si->next)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800543 {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700544 if(si->flags & FLAG_ERROR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800545 continue;
546 s = _do_lookup_in_so(si, name, &elf_hash);
547 if (s != NULL) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700548 *found = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800549 break;
550 }
551 }
552
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700553 if(s != NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800554 TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
555 "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
556 return s;
557 }
558
559 return 0;
560}
561
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600562soinfo *find_containing_library(void *addr)
563{
564 soinfo *si;
565
566 for(si = solist; si != NULL; si = si->next)
567 {
568 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
569 return si;
570 }
571 }
572
573 return NULL;
574}
575
576Elf32_Sym *find_containing_symbol(void *addr, soinfo *si)
577{
578 unsigned int i;
579 unsigned soaddr = (unsigned)addr - si->base;
580
581 /* Search the library's symbol table for any defined symbol which
582 * contains this address */
583 for(i=0; i<si->nchain; i++) {
584 Elf32_Sym *sym = &si->symtab[i];
585
586 if(sym->st_shndx != SHN_UNDEF &&
587 soaddr >= sym->st_value &&
588 soaddr < sym->st_value + sym->st_size) {
589 return sym;
590 }
591 }
592
593 return NULL;
594}
595
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800596#if 0
597static void dump(soinfo *si)
598{
599 Elf32_Sym *s = si->symtab;
600 unsigned n;
601
602 for(n = 0; n < si->nchain; n++) {
603 TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
604 s->st_info, s->st_shndx, s->st_value, s->st_size,
605 si->strtab + s->st_name);
606 s++;
607 }
608}
609#endif
610
611static const char *sopaths[] = {
612 "/system/lib",
613 "/lib",
614 0
615};
616
617static int _open_lib(const char *name)
618{
619 int fd;
620 struct stat filestat;
621
622 if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
623 if ((fd = open(name, O_RDONLY)) >= 0)
624 return fd;
625 }
626
627 return -1;
628}
629
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630static int open_library(const char *name)
631{
632 int fd;
633 char buf[512];
634 const char **path;
David Bartleybc3a5c22009-06-02 18:27:28 -0700635 int n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800636
637 TRACE("[ %5d opening %s ]\n", pid, name);
638
639 if(name == 0) return -1;
640 if(strlen(name) > 256) return -1;
641
642 if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
643 return fd;
644
David Bartleybc3a5c22009-06-02 18:27:28 -0700645 for (path = ldpaths; *path; path++) {
646 n = snprintf(buf, sizeof(buf), "%s/%s", *path, name);
647 if (n < 0 || n >= (int)sizeof(buf)) {
648 WARN("Ignoring very long library path: %s/%s\n", *path, name);
649 continue;
650 }
651 if ((fd = _open_lib(buf)) >= 0)
652 return fd;
653 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800654 for (path = sopaths; *path; path++) {
David Bartleybc3a5c22009-06-02 18:27:28 -0700655 n = snprintf(buf, sizeof(buf), "%s/%s", *path, name);
656 if (n < 0 || n >= (int)sizeof(buf)) {
657 WARN("Ignoring very long library path: %s/%s\n", *path, name);
658 continue;
659 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800660 if ((fd = _open_lib(buf)) >= 0)
661 return fd;
662 }
663
664 return -1;
665}
666
667/* temporary space for holding the first page of the shared lib
668 * which contains the elf header (with the pht). */
669static unsigned char __header[PAGE_SIZE];
670
671typedef struct {
672 long mmap_addr;
673 char tag[4]; /* 'P', 'R', 'E', ' ' */
674} prelink_info_t;
675
676/* Returns the requested base address if the library is prelinked,
677 * and 0 otherwise. */
678static unsigned long
679is_prelinked(int fd, const char *name)
680{
681 off_t sz;
682 prelink_info_t info;
683
684 sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
685 if (sz < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700686 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800687 return 0;
688 }
689
690 if (read(fd, &info, sizeof(info)) != sizeof(info)) {
691 WARN("Could not read prelink_info_t structure for `%s`\n", name);
692 return 0;
693 }
694
695 if (strncmp(info.tag, "PRE ", 4)) {
696 WARN("`%s` is not a prelinked library\n", name);
697 return 0;
698 }
699
700 return (unsigned long)info.mmap_addr;
701}
702
703/* verify_elf_object
704 * Verifies if the object @ base is a valid ELF object
705 *
706 * Args:
707 *
708 * Returns:
709 * 0 on success
710 * -1 if no valid ELF object is found @ base.
711 */
712static int
713verify_elf_object(void *base, const char *name)
714{
715 Elf32_Ehdr *hdr = (Elf32_Ehdr *) base;
716
717 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
718 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
719 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
720 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
721
722 /* TODO: Should we verify anything else in the header? */
723
724 return 0;
725}
726
727
728/* get_lib_extents
729 * Retrieves the base (*base) address where the ELF object should be
730 * mapped and its overall memory size (*total_sz).
731 *
732 * Args:
733 * fd: Opened file descriptor for the library
734 * name: The name of the library
735 * _hdr: Pointer to the header page of the library
736 * total_sz: Total size of the memory that should be allocated for
737 * this library
738 *
739 * Returns:
740 * -1 if there was an error while trying to get the lib extents.
741 * The possible reasons are:
742 * - Could not determine if the library was prelinked.
743 * - The library provided is not a valid ELF object
744 * 0 if the library did not request a specific base offset (normal
745 * for non-prelinked libs)
746 * > 0 if the library requests a specific address to be mapped to.
747 * This indicates a pre-linked library.
748 */
749static unsigned
750get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz)
751{
752 unsigned req_base;
753 unsigned min_vaddr = 0xffffffff;
754 unsigned max_vaddr = 0;
755 unsigned char *_hdr = (unsigned char *)__hdr;
756 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)_hdr;
757 Elf32_Phdr *phdr;
758 int cnt;
759
760 TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name);
761 if (verify_elf_object(_hdr, name) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700762 DL_ERR("%5d - %s is not a valid ELF object", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800763 return (unsigned)-1;
764 }
765
766 req_base = (unsigned) is_prelinked(fd, name);
767 if (req_base == (unsigned)-1)
768 return -1;
769 else if (req_base != 0) {
770 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
771 pid, name, req_base);
772 } else {
773 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
774 }
775
776 phdr = (Elf32_Phdr *)(_hdr + ehdr->e_phoff);
777
778 /* find the min/max p_vaddrs from all the PT_LOAD segments so we can
779 * get the range. */
780 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
781 if (phdr->p_type == PT_LOAD) {
782 if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr)
783 max_vaddr = phdr->p_vaddr + phdr->p_memsz;
784 if (phdr->p_vaddr < min_vaddr)
785 min_vaddr = phdr->p_vaddr;
786 }
787 }
788
789 if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700790 DL_ERR("%5d - No loadable segments found in %s.", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800791 return (unsigned)-1;
792 }
793
794 /* truncate min_vaddr down to page boundary */
795 min_vaddr &= ~PAGE_MASK;
796
797 /* round max_vaddr up to the next page */
798 max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK;
799
800 *total_sz = (max_vaddr - min_vaddr);
801 return (unsigned)req_base;
802}
803
804/* alloc_mem_region
805 *
806 * This function reserves a chunk of memory to be used for mapping in
807 * the shared library. We reserve the entire memory region here, and
808 * then the rest of the linker will relocate the individual loadable
809 * segments into the correct locations within this memory range.
810 *
811 * Args:
812 * si->base: The requested base of the allocation. If 0, a sane one will be
813 * chosen in the range LIBBASE <= base < LIBLAST.
814 * si->size: The size of the allocation.
815 *
816 * Returns:
817 * -1 on failure, and 0 on success. On success, si->base will contain
818 * the virtual address at which the library will be mapped.
819 */
820
821static int reserve_mem_region(soinfo *si)
822{
823 void *base = mmap((void *)si->base, si->size, PROT_READ | PROT_EXEC,
824 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
825 if (base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700826 DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700827 "as requested, will try general pool: %d (%s)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800828 pid, (si->base ? "" : "non-"), si->name, si->base,
829 errno, strerror(errno));
830 return -1;
831 } else if (base != (void *)si->base) {
Dima Zavin2e855792009-05-20 18:28:09 -0700832 DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700833 "not at 0x%08x", pid, (si->base ? "" : "non-"),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800834 si->name, (unsigned)base, si->base);
835 munmap(base, si->size);
836 return -1;
837 }
838 return 0;
839}
840
841static int
842alloc_mem_region(soinfo *si)
843{
844 if (si->base) {
845 /* Attempt to mmap a prelinked library. */
846 si->ba_index = -1;
847 return reserve_mem_region(si);
848 }
849
850 /* This is not a prelinked library, so we attempt to allocate space
851 for it from the buddy allocator, which manages the area between
852 LIBBASE and LIBLAST.
853 */
Iliyan Malchevaf7315a2009-10-16 17:50:42 -0700854 si->ba_index = ba_allocate(&ba_prelink, si->size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800855 if(si->ba_index >= 0) {
Iliyan Malchevaf7315a2009-10-16 17:50:42 -0700856 si->base = ba_start_addr(&ba_prelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800857 PRINT("%5d mapping library '%s' at %08x (index %d) " \
858 "through buddy allocator.\n",
859 pid, si->name, si->base, si->ba_index);
860 if (reserve_mem_region(si) < 0) {
Iliyan Malchevaf7315a2009-10-16 17:50:42 -0700861 ba_free(&ba_prelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800862 si->ba_index = -1;
863 si->base = 0;
864 goto err;
865 }
866 return 0;
867 }
868
869err:
Erik Gillingd00d23a2009-07-22 17:06:11 -0700870 DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800871 pid, si->name);
872 return -1;
873}
874
875#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
876#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
877 MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
878 MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
879/* load_segments
880 *
881 * This function loads all the loadable (PT_LOAD) segments into memory
882 * at their appropriate memory offsets off the base address.
883 *
884 * Args:
885 * fd: Open file descriptor to the library to load.
886 * header: Pointer to a header page that contains the ELF header.
887 * This is needed since we haven't mapped in the real file yet.
888 * si: ptr to soinfo struct describing the shared object.
889 *
890 * Returns:
891 * 0 on success, -1 on failure.
892 */
893static int
894load_segments(int fd, void *header, soinfo *si)
895{
896 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
897 Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned char *)header + ehdr->e_phoff);
898 unsigned char *base = (unsigned char *)si->base;
899 int cnt;
900 unsigned len;
901 unsigned char *tmp;
902 unsigned char *pbase;
903 unsigned char *extra_base;
904 unsigned extra_len;
905 unsigned total_sz = 0;
906
907 si->wrprotect_start = 0xffffffff;
908 si->wrprotect_end = 0;
909
910 TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n",
911 pid, si->name, (unsigned)si->base);
912 /* Now go through all the PT_LOAD segments and map them into memory
913 * at the appropriate locations. */
914 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
915 if (phdr->p_type == PT_LOAD) {
916 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
917 /* we want to map in the segment on a page boundary */
918 tmp = base + (phdr->p_vaddr & (~PAGE_MASK));
919 /* add the # of bytes we masked off above to the total length. */
920 len = phdr->p_filesz + (phdr->p_vaddr & PAGE_MASK);
921
922 TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x "
923 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name,
924 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
925 pbase = mmap(tmp, len, PFLAGS_TO_PROT(phdr->p_flags),
926 MAP_PRIVATE | MAP_FIXED, fd,
927 phdr->p_offset & (~PAGE_MASK));
928 if (pbase == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700929 DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700930 "p_vaddr=0x%08x p_offset=0x%08x", pid, si->name,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800931 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
932 goto fail;
933 }
934
935 /* If 'len' didn't end on page boundary, and it's a writable
936 * segment, zero-fill the rest. */
937 if ((len & PAGE_MASK) && (phdr->p_flags & PF_W))
938 memset((void *)(pbase + len), 0, PAGE_SIZE - (len & PAGE_MASK));
939
940 /* Check to see if we need to extend the map for this segment to
941 * cover the diff between filesz and memsz (i.e. for bss).
942 *
943 * base _+---------------------+ page boundary
944 * . .
945 * | |
946 * . .
947 * pbase _+---------------------+ page boundary
948 * | |
949 * . .
950 * base + p_vaddr _| |
951 * . \ \ .
952 * . | filesz | .
953 * pbase + len _| / | |
954 * <0 pad> . . .
955 * extra_base _+------------|--------+ page boundary
956 * / . . .
957 * | . . .
958 * | +------------|--------+ page boundary
959 * extra_len-> | | | |
960 * | . | memsz .
961 * | . | .
962 * \ _| / |
963 * . .
964 * | |
965 * _+---------------------+ page boundary
966 */
967 tmp = (unsigned char *)(((unsigned)pbase + len + PAGE_SIZE - 1) &
968 (~PAGE_MASK));
969 if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) {
970 extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp;
971 TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x "
972 "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len);
973 /* map in the extra page(s) as anonymous into the range.
974 * This is probably not necessary as we already mapped in
975 * the entire region previously, but we just want to be
976 * sure. This will also set the right flags on the region
977 * (though we can probably accomplish the same thing with
978 * mprotect).
979 */
980 extra_base = mmap((void *)tmp, extra_len,
981 PFLAGS_TO_PROT(phdr->p_flags),
982 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
983 -1, 0);
984 if (extra_base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700985 DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x"
Erik Gillingd00d23a2009-07-22 17:06:11 -0700986 " (0x%08x) ]", pid, si->name, (unsigned)tmp,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800987 extra_len);
988 goto fail;
989 }
990 /* TODO: Check if we need to memset-0 this region.
991 * Anonymous mappings are zero-filled copy-on-writes, so we
992 * shouldn't need to. */
993 TRACE("[ %5d - Segment from '%s' extended @ 0x%08x "
994 "(0x%08x)\n", pid, si->name, (unsigned)extra_base,
995 extra_len);
996 }
997 /* set the len here to show the full extent of the segment we
998 * just loaded, mostly for debugging */
999 len = (((unsigned)base + phdr->p_vaddr + phdr->p_memsz +
1000 PAGE_SIZE - 1) & (~PAGE_MASK)) - (unsigned)pbase;
1001 TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x "
1002 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
1003 (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset);
1004 total_sz += len;
1005 /* Make the section writable just in case we'll have to write to
1006 * it during relocation (i.e. text segment). However, we will
1007 * remember what range of addresses should be write protected.
1008 *
1009 */
1010 if (!(phdr->p_flags & PF_W)) {
1011 if ((unsigned)pbase < si->wrprotect_start)
1012 si->wrprotect_start = (unsigned)pbase;
1013 if (((unsigned)pbase + len) > si->wrprotect_end)
1014 si->wrprotect_end = (unsigned)pbase + len;
1015 mprotect(pbase, len,
1016 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
1017 }
1018 } else if (phdr->p_type == PT_DYNAMIC) {
1019 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
1020 /* this segment contains the dynamic linking information */
1021 si->dynamic = (unsigned *)(base + phdr->p_vaddr);
1022 } else {
1023#ifdef ANDROID_ARM_LINKER
1024 if (phdr->p_type == PT_ARM_EXIDX) {
1025 DEBUG_DUMP_PHDR(phdr, "PT_ARM_EXIDX", pid);
1026 /* exidx entries (used for stack unwinding) are 8 bytes each.
1027 */
1028 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1029 si->ARM_exidx_count = phdr->p_memsz / 8;
1030 }
1031#endif
1032 }
1033
1034 }
1035
1036 /* Sanity check */
1037 if (total_sz > si->size) {
Dima Zavin2e855792009-05-20 18:28:09 -07001038 DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001039 "greater than what was allocated (0x%08x). THIS IS BAD!",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001040 pid, total_sz, si->name, si->size);
1041 goto fail;
1042 }
1043
1044 TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. "
1045 "Total memory footprint: 0x%08x bytes ]\n", pid, si->name,
1046 (unsigned)si->base, si->size);
1047 return 0;
1048
1049fail:
1050 /* We can just blindly unmap the entire region even though some things
1051 * were mapped in originally with anonymous and others could have been
1052 * been mapped in from the file before we failed. The kernel will unmap
1053 * all the pages in the range, irrespective of how they got there.
1054 */
1055 munmap((void *)si->base, si->size);
1056 si->flags |= FLAG_ERROR;
1057 return -1;
1058}
1059
1060/* TODO: Implement this to take care of the fact that Android ARM
1061 * ELF objects shove everything into a single loadable segment that has the
1062 * write bit set. wr_offset is then used to set non-(data|bss) pages to be
1063 * non-writable.
1064 */
1065#if 0
1066static unsigned
1067get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr)
1068{
1069 Elf32_Shdr *shdr_start;
1070 Elf32_Shdr *shdr;
1071 int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr);
1072 int cnt;
1073 unsigned wr_offset = 0xffffffff;
1074
1075 shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd,
1076 ehdr->e_shoff & (~PAGE_MASK));
1077 if (shdr_start == MAP_FAILED) {
1078 WARN("%5d - Could not read section header info from '%s'. Will not "
1079 "not be able to determine write-protect offset.\n", pid, name);
1080 return (unsigned)-1;
1081 }
1082
1083 for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) {
1084 if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) &&
1085 (shdr->sh_addr < wr_offset)) {
1086 wr_offset = shdr->sh_addr;
1087 }
1088 }
1089
1090 munmap(shdr_start, shdr_sz);
1091 return wr_offset;
1092}
1093#endif
1094
1095static soinfo *
1096load_library(const char *name)
1097{
1098 int fd = open_library(name);
1099 int cnt;
1100 unsigned ext_sz;
1101 unsigned req_base;
Erik Gillingfde86422009-07-28 20:28:19 -07001102 const char *bname;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001103 soinfo *si = NULL;
1104 Elf32_Ehdr *hdr;
1105
Dima Zavin2e855792009-05-20 18:28:09 -07001106 if(fd == -1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001107 DL_ERR("Library '%s' not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001108 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -07001109 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001110
1111 /* We have to read the ELF header to figure out what to do with this image
1112 */
1113 if (lseek(fd, 0, SEEK_SET) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001114 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001115 goto fail;
1116 }
1117
1118 if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001119 DL_ERR("read() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001120 goto fail;
1121 }
1122
1123 /* Parse the ELF header and get the size of the memory footprint for
1124 * the library */
1125 req_base = get_lib_extents(fd, name, &__header[0], &ext_sz);
1126 if (req_base == (unsigned)-1)
1127 goto fail;
1128 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
1129 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
1130
1131 /* Now configure the soinfo struct where we'll store all of our data
1132 * for the ELF object. If the loading fails, we waste the entry, but
1133 * same thing would happen if we failed during linking. Configuring the
1134 * soinfo struct here is a lot more convenient.
1135 */
Erik Gillingfde86422009-07-28 20:28:19 -07001136 bname = strrchr(name, '/');
1137 si = alloc_info(bname ? bname + 1 : name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001138 if (si == NULL)
1139 goto fail;
1140
1141 /* Carve out a chunk of memory where we will map in the individual
1142 * segments */
1143 si->base = req_base;
1144 si->size = ext_sz;
1145 si->flags = 0;
1146 si->entry = 0;
1147 si->dynamic = (unsigned *)-1;
1148 if (alloc_mem_region(si) < 0)
1149 goto fail;
1150
1151 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
1152 pid, name, (void *)si->base, (unsigned) ext_sz);
1153
1154 /* Now actually load the library's segments into right places in memory */
1155 if (load_segments(fd, &__header[0], si) < 0) {
1156 if (si->ba_index >= 0) {
Iliyan Malchevaf7315a2009-10-16 17:50:42 -07001157 ba_free(&ba_prelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001158 si->ba_index = -1;
1159 }
1160 goto fail;
1161 }
1162
1163 /* this might not be right. Technically, we don't even need this info
1164 * once we go through 'load_segments'. */
1165 hdr = (Elf32_Ehdr *)si->base;
1166 si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff);
1167 si->phnum = hdr->e_phnum;
1168 /**/
1169
1170 close(fd);
1171 return si;
1172
1173fail:
1174 if (si) free_info(si);
1175 close(fd);
1176 return NULL;
1177}
1178
1179static soinfo *
1180init_library(soinfo *si)
1181{
1182 unsigned wr_offset = 0xffffffff;
1183
1184 /* At this point we know that whatever is loaded @ base is a valid ELF
1185 * shared library whose segments are properly mapped in. */
1186 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
1187 pid, si->base, si->size, si->name);
1188
1189 if (si->base < LIBBASE || si->base >= LIBLAST)
1190 si->flags |= FLAG_PRELINKED;
1191
1192 if(link_image(si, wr_offset)) {
1193 /* We failed to link. However, we can only restore libbase
1194 ** if no additional libraries have moved it since we updated it.
1195 */
1196 munmap((void *)si->base, si->size);
1197 return NULL;
1198 }
1199
1200 return si;
1201}
1202
1203soinfo *find_library(const char *name)
1204{
1205 soinfo *si;
Erik Gillingfde86422009-07-28 20:28:19 -07001206 const char *bname = strrchr(name, '/');
1207 bname = bname ? bname + 1 : name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001208
1209 for(si = solist; si != 0; si = si->next){
Erik Gillingfde86422009-07-28 20:28:19 -07001210 if(!strcmp(bname, si->name)) {
Erik Gilling30eb4022009-08-13 16:05:30 -07001211 if(si->flags & FLAG_ERROR) {
1212 DL_ERR("%5d '%s' failed to load previously", pid, bname);
1213 return NULL;
1214 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001215 if(si->flags & FLAG_LINKED) return si;
Erik Gillingd00d23a2009-07-22 17:06:11 -07001216 DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name);
Dima Zavin2e855792009-05-20 18:28:09 -07001217 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001218 }
1219 }
1220
1221 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
1222 si = load_library(name);
1223 if(si == NULL)
1224 return NULL;
1225 return init_library(si);
1226}
1227
1228/* TODO:
1229 * notify gdb of unload
1230 * for non-prelinked libraries, find a way to decrement libbase
1231 */
1232static void call_destructors(soinfo *si);
1233unsigned unload_library(soinfo *si)
1234{
1235 unsigned *d;
1236 if (si->refcount == 1) {
1237 TRACE("%5d unloading '%s'\n", pid, si->name);
1238 call_destructors(si);
1239
1240 for(d = si->dynamic; *d; d += 2) {
1241 if(d[0] == DT_NEEDED){
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001242 soinfo *lsi = (soinfo *)d[1];
1243 d[1] = 0;
1244 if (validate_soinfo(lsi)) {
1245 TRACE("%5d %s needs to unload %s\n", pid,
1246 si->name, lsi->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001247 unload_library(lsi);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001248 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001249 else
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001250 DL_ERR("%5d %s: could not unload dependent library",
1251 pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001252 }
1253 }
1254
1255 munmap((char *)si->base, si->size);
1256 if (si->ba_index >= 0) {
1257 PRINT("%5d releasing library '%s' address space at %08x "\
1258 "through buddy allocator.\n",
1259 pid, si->name, si->base);
Iliyan Malchevaf7315a2009-10-16 17:50:42 -07001260 ba_free(&ba_prelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001261 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001262 notify_gdb_of_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001263 free_info(si);
1264 si->refcount = 0;
1265 }
1266 else {
1267 si->refcount--;
1268 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
1269 pid, si->name, si->refcount);
1270 }
1271 return si->refcount;
1272}
1273
1274/* TODO: don't use unsigned for addrs below. It works, but is not
1275 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1276 * long.
1277 */
1278static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count)
1279{
1280 Elf32_Sym *symtab = si->symtab;
1281 const char *strtab = si->strtab;
1282 Elf32_Sym *s;
1283 unsigned base;
1284 Elf32_Rel *start = rel;
1285 unsigned idx;
1286
1287 for (idx = 0; idx < count; ++idx) {
1288 unsigned type = ELF32_R_TYPE(rel->r_info);
1289 unsigned sym = ELF32_R_SYM(rel->r_info);
1290 unsigned reloc = (unsigned)(rel->r_offset + si->base);
1291 unsigned sym_addr = 0;
1292 char *sym_name = NULL;
1293
1294 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1295 si->name, idx);
1296 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001297 sym_name = (char *)(strtab + symtab[sym].st_name);
1298 s = _do_lookup(si, sym_name, &base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001299 if(s == 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001300 DL_ERR("%5d cannot locate '%s'...", pid, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001301 return -1;
1302 }
1303#if 0
1304 if((base == 0) && (si->base != 0)){
1305 /* linking from libraries to main image is bad */
Erik Gillingd00d23a2009-07-22 17:06:11 -07001306 DL_ERR("%5d cannot locate '%s'...",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001307 pid, strtab + symtab[sym].st_name);
1308 return -1;
1309 }
1310#endif
David 'Digit' Turner3c998762009-10-13 16:55:18 -07001311 // st_shndx==SHN_UNDEF means an undefined symbol.
1312 // st_value should be 0 then, except that the low bit of st_value is
1313 // used to indicate whether the symbol points to an ARM or thumb function,
1314 // and should be ignored in the following check.
1315 if ((s->st_shndx == SHN_UNDEF) && ((s->st_value & ~1) != 0)) {
1316 DL_ERR("%5d In '%s', symbol=%s shndx=%d && value=0x%08x. We do not "
1317 "handle this yet", pid, si->name, sym_name, s->st_shndx,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001318 s->st_value);
1319 return -1;
1320 }
1321 sym_addr = (unsigned)(s->st_value + base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001322 COUNT_RELOC(RELOC_SYMBOL);
1323 } else {
1324 s = 0;
1325 }
1326
1327/* TODO: This is ugly. Split up the relocations by arch into
1328 * different files.
1329 */
1330 switch(type){
1331#if defined(ANDROID_ARM_LINKER)
1332 case R_ARM_JUMP_SLOT:
1333 COUNT_RELOC(RELOC_ABSOLUTE);
1334 MARK(rel->r_offset);
1335 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1336 reloc, sym_addr, sym_name);
1337 *((unsigned*)reloc) = sym_addr;
1338 break;
1339 case R_ARM_GLOB_DAT:
1340 COUNT_RELOC(RELOC_ABSOLUTE);
1341 MARK(rel->r_offset);
1342 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1343 reloc, sym_addr, sym_name);
1344 *((unsigned*)reloc) = sym_addr;
1345 break;
1346 case R_ARM_ABS32:
1347 COUNT_RELOC(RELOC_ABSOLUTE);
1348 MARK(rel->r_offset);
1349 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1350 reloc, sym_addr, sym_name);
1351 *((unsigned*)reloc) += sym_addr;
1352 break;
David 'Digit' Turnerfe62de12009-12-02 10:54:53 -08001353 case R_ARM_REL32:
1354 COUNT_RELOC(RELOC_RELATIVE);
1355 MARK(rel->r_offset);
1356 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1357 reloc, sym_addr, rel->r_offset, sym_name);
1358 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1359 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001360#elif defined(ANDROID_X86_LINKER)
1361 case R_386_JUMP_SLOT:
1362 COUNT_RELOC(RELOC_ABSOLUTE);
1363 MARK(rel->r_offset);
1364 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1365 reloc, sym_addr, sym_name);
1366 *((unsigned*)reloc) = sym_addr;
1367 break;
1368 case R_386_GLOB_DAT:
1369 COUNT_RELOC(RELOC_ABSOLUTE);
1370 MARK(rel->r_offset);
1371 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1372 reloc, sym_addr, sym_name);
1373 *((unsigned*)reloc) = sym_addr;
1374 break;
1375#endif /* ANDROID_*_LINKER */
1376
1377#if defined(ANDROID_ARM_LINKER)
1378 case R_ARM_RELATIVE:
1379#elif defined(ANDROID_X86_LINKER)
1380 case R_386_RELATIVE:
1381#endif /* ANDROID_*_LINKER */
1382 COUNT_RELOC(RELOC_RELATIVE);
1383 MARK(rel->r_offset);
1384 if(sym){
Erik Gillingd00d23a2009-07-22 17:06:11 -07001385 DL_ERR("%5d odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001386 return -1;
1387 }
1388 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1389 reloc, si->base);
1390 *((unsigned*)reloc) += si->base;
1391 break;
1392
1393#if defined(ANDROID_X86_LINKER)
1394 case R_386_32:
1395 COUNT_RELOC(RELOC_RELATIVE);
1396 MARK(rel->r_offset);
1397
1398 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1399 reloc, sym_addr, sym_name);
1400 *((unsigned *)reloc) += (unsigned)sym_addr;
1401 break;
1402
1403 case R_386_PC32:
1404 COUNT_RELOC(RELOC_RELATIVE);
1405 MARK(rel->r_offset);
1406 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1407 "+%08x (%08x - %08x) %s\n", pid, reloc,
1408 (sym_addr - reloc), sym_addr, reloc, sym_name);
1409 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1410 break;
1411#endif /* ANDROID_X86_LINKER */
1412
1413#ifdef ANDROID_ARM_LINKER
1414 case R_ARM_COPY:
1415 COUNT_RELOC(RELOC_COPY);
1416 MARK(rel->r_offset);
1417 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1418 reloc, s->st_size, sym_addr, sym_name);
1419 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1420 break;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001421 case R_ARM_NONE:
1422 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001423#endif /* ANDROID_ARM_LINKER */
1424
1425 default:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001426 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001427 pid, type, rel, (int) (rel - start));
1428 return -1;
1429 }
1430 rel++;
1431 }
1432 return 0;
1433}
1434
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001435#if defined(ANDROID_SH_LINKER)
1436static int reloc_library_a(soinfo *si, Elf32_Rela *rela, unsigned count)
1437{
1438 Elf32_Sym *symtab = si->symtab;
1439 const char *strtab = si->strtab;
1440 Elf32_Sym *s;
1441 unsigned base;
1442 Elf32_Rela *start = rela;
1443 unsigned idx;
1444
1445 for (idx = 0; idx < count; ++idx) {
1446 unsigned type = ELF32_R_TYPE(rela->r_info);
1447 unsigned sym = ELF32_R_SYM(rela->r_info);
1448 unsigned reloc = (unsigned)(rela->r_offset + si->base);
1449 unsigned sym_addr = 0;
1450 char *sym_name = NULL;
1451
1452 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1453 si->name, idx);
1454 if(sym != 0) {
1455 sym_name = (char *)(strtab + symtab[sym].st_name);
1456 s = _do_lookup(si, sym_name, &base);
1457 if(s == 0) {
1458 DL_ERR("%5d cannot locate '%s'...", pid, sym_name);
1459 return -1;
1460 }
1461#if 0
1462 if((base == 0) && (si->base != 0)){
1463 /* linking from libraries to main image is bad */
1464 DL_ERR("%5d cannot locate '%s'...",
1465 pid, strtab + symtab[sym].st_name);
1466 return -1;
1467 }
1468#endif
1469 if ((s->st_shndx == SHN_UNDEF) && (s->st_value != 0)) {
1470 DL_ERR("%5d In '%s', shndx=%d && value=0x%08x. We do not "
1471 "handle this yet", pid, si->name, s->st_shndx,
1472 s->st_value);
1473 return -1;
1474 }
1475 sym_addr = (unsigned)(s->st_value + base);
1476 COUNT_RELOC(RELOC_SYMBOL);
1477 } else {
1478 s = 0;
1479 }
1480
1481/* TODO: This is ugly. Split up the relocations by arch into
1482 * different files.
1483 */
1484 switch(type){
1485 case R_SH_JUMP_SLOT:
1486 COUNT_RELOC(RELOC_ABSOLUTE);
1487 MARK(rela->r_offset);
1488 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1489 reloc, sym_addr, sym_name);
1490 *((unsigned*)reloc) = sym_addr;
1491 break;
1492 case R_SH_GLOB_DAT:
1493 COUNT_RELOC(RELOC_ABSOLUTE);
1494 MARK(rela->r_offset);
1495 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1496 reloc, sym_addr, sym_name);
1497 *((unsigned*)reloc) = sym_addr;
1498 break;
1499 case R_SH_DIR32:
1500 COUNT_RELOC(RELOC_ABSOLUTE);
1501 MARK(rela->r_offset);
1502 TRACE_TYPE(RELO, "%5d RELO DIR32 %08x <- %08x %s\n", pid,
1503 reloc, sym_addr, sym_name);
1504 *((unsigned*)reloc) += sym_addr;
1505 break;
1506 case R_SH_RELATIVE:
1507 COUNT_RELOC(RELOC_RELATIVE);
1508 MARK(rela->r_offset);
1509 if(sym){
1510 DL_ERR("%5d odd RELATIVE form...", pid);
1511 return -1;
1512 }
1513 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1514 reloc, si->base);
1515 *((unsigned*)reloc) += si->base;
1516 break;
1517
1518 default:
1519 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
1520 pid, type, rela, (int) (rela - start));
1521 return -1;
1522 }
1523 rela++;
1524 }
1525 return 0;
1526}
1527#endif /* ANDROID_SH_LINKER */
1528
David 'Digit' Turner82156792009-05-18 14:37:41 +02001529
1530/* Please read the "Initialization and Termination functions" functions.
1531 * of the linker design note in bionic/linker/README.TXT to understand
1532 * what the following code is doing.
1533 *
1534 * The important things to remember are:
1535 *
1536 * DT_PREINIT_ARRAY must be called first for executables, and should
1537 * not appear in shared libraries.
1538 *
1539 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1540 *
1541 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1542 *
1543 * DT_FINI_ARRAY must be parsed in reverse order.
1544 */
1545
1546static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001547{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001548 int n, inc = 1;
1549
1550 if (reverse) {
1551 ctor += (count-1);
1552 inc = -1;
1553 }
1554
1555 for(n = count; n > 0; n--) {
1556 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1557 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001558 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001559 void (*func)() = (void (*)()) *ctor;
1560 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001561 if(((int) func == 0) || ((int) func == -1)) continue;
1562 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1563 func();
1564 }
1565}
1566
1567static void call_constructors(soinfo *si)
1568{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001569 if (si->flags & FLAG_EXE) {
1570 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1571 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1572 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001573 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001574 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1575 } else {
1576 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001577 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
Erik Gillingd00d23a2009-07-22 17:06:11 -07001578 " This is INVALID.", pid, si->name,
Dima Zavin2e855792009-05-20 18:28:09 -07001579 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001580 }
1581 }
1582
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001583 if (si->init_func) {
1584 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1585 (unsigned)si->init_func, si->name);
1586 si->init_func();
1587 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1588 }
1589
1590 if (si->init_array) {
1591 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1592 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001593 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001594 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1595 }
1596}
1597
David 'Digit' Turner82156792009-05-18 14:37:41 +02001598
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001599static void call_destructors(soinfo *si)
1600{
1601 if (si->fini_array) {
1602 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1603 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001604 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001605 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1606 }
1607
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001608 if (si->fini_func) {
1609 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1610 (unsigned)si->fini_func, si->name);
1611 si->fini_func();
1612 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1613 }
1614}
1615
1616/* Force any of the closed stdin, stdout and stderr to be associated with
1617 /dev/null. */
1618static int nullify_closed_stdio (void)
1619{
1620 int dev_null, i, status;
1621 int return_value = 0;
1622
1623 dev_null = open("/dev/null", O_RDWR);
1624 if (dev_null < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001625 DL_ERR("Cannot open /dev/null.");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001626 return -1;
1627 }
1628 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1629
1630 /* If any of the stdio file descriptors is valid and not associated
1631 with /dev/null, dup /dev/null to it. */
1632 for (i = 0; i < 3; i++) {
1633 /* If it is /dev/null already, we are done. */
1634 if (i == dev_null)
1635 continue;
1636
1637 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1638 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1639 can be interrupted but we do this just to be safe. */
1640 do {
1641 status = fcntl(i, F_GETFL);
1642 } while (status < 0 && errno == EINTR);
1643
1644 /* If file is openned, we are good. */
1645 if (status >= 0)
1646 continue;
1647
1648 /* The only error we allow is that the file descriptor does not
1649 exist, in which case we dup /dev/null to it. */
1650 if (errno != EBADF) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001651 DL_ERR("nullify_stdio: unhandled error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652 return_value = -1;
1653 continue;
1654 }
1655
1656 /* Try dupping /dev/null to this stdio file descriptor and
1657 repeat if there is a signal. Note that any errors in closing
1658 the stdio descriptor are lost. */
1659 do {
1660 status = dup2(dev_null, i);
1661 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001662
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001663 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001664 DL_ERR("nullify_stdio: dup2 error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001665 return_value = -1;
1666 continue;
1667 }
1668 }
1669
1670 /* If /dev/null is not one of the stdio file descriptors, close it. */
1671 if (dev_null > 2) {
1672 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001673 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001674 status = close(dev_null);
1675 } while (status < 0 && errno == EINTR);
1676
1677 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001678 DL_ERR("nullify_stdio: close error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001679 return_value = -1;
1680 }
1681 }
1682
1683 return return_value;
1684}
1685
1686static int link_image(soinfo *si, unsigned wr_offset)
1687{
1688 unsigned *d;
1689 Elf32_Phdr *phdr = si->phdr;
1690 int phnum = si->phnum;
1691
1692 INFO("[ %5d linking %s ]\n", pid, si->name);
1693 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1694 si->base, si->flags);
1695
1696 if (si->flags & FLAG_EXE) {
1697 /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for
1698 * linkage info if this is the executable. If this was a
1699 * dynamic lib, that would have been done at load time.
1700 *
1701 * TODO: It's unfortunate that small pieces of this are
1702 * repeated from the load_library routine. Refactor this just
1703 * slightly to reuse these bits.
1704 */
1705 si->size = 0;
1706 for(; phnum > 0; --phnum, ++phdr) {
1707#ifdef ANDROID_ARM_LINKER
1708 if(phdr->p_type == PT_ARM_EXIDX) {
1709 /* exidx entries (used for stack unwinding) are 8 bytes each.
1710 */
1711 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1712 si->ARM_exidx_count = phdr->p_memsz / 8;
1713 }
1714#endif
1715 if (phdr->p_type == PT_LOAD) {
1716 /* For the executable, we use the si->size field only in
1717 dl_unwind_find_exidx(), so the meaning of si->size
1718 is not the size of the executable; it is the last
1719 virtual address of the loadable part of the executable;
1720 since si->base == 0 for an executable, we use the
1721 range [0, si->size) to determine whether a PC value
1722 falls within the executable section. Of course, if
1723 a value is below phdr->p_vaddr, it's not in the
1724 executable section, but a) we shouldn't be asking for
1725 such a value anyway, and b) if we have to provide
1726 an EXIDX for such a value, then the executable's
1727 EXIDX is probably the better choice.
1728 */
1729 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
1730 if (phdr->p_vaddr + phdr->p_memsz > si->size)
1731 si->size = phdr->p_vaddr + phdr->p_memsz;
1732 /* try to remember what range of addresses should be write
1733 * protected */
1734 if (!(phdr->p_flags & PF_W)) {
1735 unsigned _end;
1736
1737 if (phdr->p_vaddr < si->wrprotect_start)
1738 si->wrprotect_start = phdr->p_vaddr;
1739 _end = (((phdr->p_vaddr + phdr->p_memsz + PAGE_SIZE - 1) &
1740 (~PAGE_MASK)));
1741 if (_end > si->wrprotect_end)
1742 si->wrprotect_end = _end;
1743 }
1744 } else if (phdr->p_type == PT_DYNAMIC) {
1745 if (si->dynamic != (unsigned *)-1) {
Dima Zavin2e855792009-05-20 18:28:09 -07001746 DL_ERR("%5d multiple PT_DYNAMIC segments found in '%s'. "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001747 "Segment at 0x%08x, previously one found at 0x%08x",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001748 pid, si->name, si->base + phdr->p_vaddr,
1749 (unsigned)si->dynamic);
1750 goto fail;
1751 }
1752 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
1753 si->dynamic = (unsigned *) (si->base + phdr->p_vaddr);
1754 }
1755 }
1756 }
1757
1758 if (si->dynamic == (unsigned *)-1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001759 DL_ERR("%5d missing PT_DYNAMIC?!", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001760 goto fail;
1761 }
1762
1763 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1764
1765 /* extract useful information from dynamic section */
1766 for(d = si->dynamic; *d; d++){
1767 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1768 switch(*d++){
1769 case DT_HASH:
1770 si->nbucket = ((unsigned *) (si->base + *d))[0];
1771 si->nchain = ((unsigned *) (si->base + *d))[1];
1772 si->bucket = (unsigned *) (si->base + *d + 8);
1773 si->chain = (unsigned *) (si->base + *d + 8 + si->nbucket * 4);
1774 break;
1775 case DT_STRTAB:
1776 si->strtab = (const char *) (si->base + *d);
1777 break;
1778 case DT_SYMTAB:
1779 si->symtab = (Elf32_Sym *) (si->base + *d);
1780 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001781#if !defined(ANDROID_SH_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001782 case DT_PLTREL:
1783 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001784 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001785 goto fail;
1786 }
1787 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001788#endif
1789#ifdef ANDROID_SH_LINKER
1790 case DT_JMPREL:
1791 si->plt_rela = (Elf32_Rela*) (si->base + *d);
1792 break;
1793 case DT_PLTRELSZ:
1794 si->plt_rela_count = *d / sizeof(Elf32_Rela);
1795 break;
1796#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001797 case DT_JMPREL:
1798 si->plt_rel = (Elf32_Rel*) (si->base + *d);
1799 break;
1800 case DT_PLTRELSZ:
1801 si->plt_rel_count = *d / 8;
1802 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001803#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001804 case DT_REL:
1805 si->rel = (Elf32_Rel*) (si->base + *d);
1806 break;
1807 case DT_RELSZ:
1808 si->rel_count = *d / 8;
1809 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001810#ifdef ANDROID_SH_LINKER
1811 case DT_RELASZ:
1812 si->rela_count = *d / sizeof(Elf32_Rela);
1813 break;
1814#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001815 case DT_PLTGOT:
1816 /* Save this in case we decide to do lazy binding. We don't yet. */
1817 si->plt_got = (unsigned *)(si->base + *d);
1818 break;
1819 case DT_DEBUG:
1820 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1821 *d = (int) &_r_debug;
1822 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001823#ifdef ANDROID_SH_LINKER
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001824 case DT_RELA:
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001825 si->rela = (Elf32_Rela *) (si->base + *d);
1826 break;
1827#else
1828 case DT_RELA:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001829 DL_ERR("%5d DT_RELA not supported", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001830 goto fail;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001831#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001832 case DT_INIT:
1833 si->init_func = (void (*)(void))(si->base + *d);
1834 DEBUG("%5d %s constructors (init func) found at %p\n",
1835 pid, si->name, si->init_func);
1836 break;
1837 case DT_FINI:
1838 si->fini_func = (void (*)(void))(si->base + *d);
1839 DEBUG("%5d %s destructors (fini func) found at %p\n",
1840 pid, si->name, si->fini_func);
1841 break;
1842 case DT_INIT_ARRAY:
1843 si->init_array = (unsigned *)(si->base + *d);
1844 DEBUG("%5d %s constructors (init_array) found at %p\n",
1845 pid, si->name, si->init_array);
1846 break;
1847 case DT_INIT_ARRAYSZ:
1848 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1849 break;
1850 case DT_FINI_ARRAY:
1851 si->fini_array = (unsigned *)(si->base + *d);
1852 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1853 pid, si->name, si->fini_array);
1854 break;
1855 case DT_FINI_ARRAYSZ:
1856 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1857 break;
1858 case DT_PREINIT_ARRAY:
1859 si->preinit_array = (unsigned *)(si->base + *d);
1860 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1861 pid, si->name, si->preinit_array);
1862 break;
1863 case DT_PREINIT_ARRAYSZ:
1864 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1865 break;
1866 case DT_TEXTREL:
1867 /* TODO: make use of this. */
1868 /* this means that we might have to write into where the text
1869 * segment was loaded during relocation... Do something with
1870 * it.
1871 */
1872 DEBUG("%5d Text segment should be writable during relocation.\n",
1873 pid);
1874 break;
1875 }
1876 }
1877
1878 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
1879 pid, si->base, si->strtab, si->symtab);
1880
1881 if((si->strtab == 0) || (si->symtab == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001882 DL_ERR("%5d missing essential tables", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001883 goto fail;
1884 }
1885
Matt Fischer4fd42c12009-12-31 12:09:10 -06001886 /* if this is the main executable, then load all of the preloads now */
1887 if(si->flags & FLAG_EXE) {
1888 int i;
1889 memset(preloads, 0, sizeof(preloads));
1890 for(i = 0; ldpreload_names[i] != NULL; i++) {
1891 soinfo *lsi = find_library(ldpreload_names[i]);
1892 if(lsi == 0) {
1893 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
1894 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
1895 pid, ldpreload_names[i], si->name, tmp_err_buf);
1896 goto fail;
1897 }
1898 lsi->refcount++;
1899 preloads[i] = lsi;
1900 }
1901 }
1902
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001903 for(d = si->dynamic; *d; d += 2) {
1904 if(d[0] == DT_NEEDED){
1905 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001906 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001907 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001908 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Erik Gillingd00d23a2009-07-22 17:06:11 -07001909 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
Dima Zavin03531952009-05-29 17:30:25 -07001910 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001911 goto fail;
1912 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001913 /* Save the soinfo of the loaded DT_NEEDED library in the payload
1914 of the DT_NEEDED entry itself, so that we can retrieve the
1915 soinfo directly later from the dynamic segment. This is a hack,
1916 but it allows us to map from DT_NEEDED to soinfo efficiently
1917 later on when we resolve relocations, trying to look up a symgol
1918 with dlsym().
1919 */
1920 d[1] = (unsigned)lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001921 lsi->refcount++;
1922 }
1923 }
1924
1925 if(si->plt_rel) {
1926 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1927 if(reloc_library(si, si->plt_rel, si->plt_rel_count))
1928 goto fail;
1929 }
1930 if(si->rel) {
1931 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1932 if(reloc_library(si, si->rel, si->rel_count))
1933 goto fail;
1934 }
1935
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001936#ifdef ANDROID_SH_LINKER
1937 if(si->plt_rela) {
1938 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1939 if(reloc_library_a(si, si->plt_rela, si->plt_rela_count))
1940 goto fail;
1941 }
1942 if(si->rela) {
1943 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1944 if(reloc_library_a(si, si->rela, si->rela_count))
1945 goto fail;
1946 }
1947#endif /* ANDROID_SH_LINKER */
1948
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001949 si->flags |= FLAG_LINKED;
1950 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1951
1952#if 0
1953 /* This is the way that the old dynamic linker did protection of
1954 * non-writable areas. It would scan section headers and find where
1955 * .text ended (rather where .data/.bss began) and assume that this is
1956 * the upper range of the non-writable area. This is too coarse,
1957 * and is kept here for reference until we fully move away from single
1958 * segment elf objects. See the code in get_wr_offset (also #if'd 0)
1959 * that made this possible.
1960 */
1961 if(wr_offset < 0xffffffff){
1962 mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC);
1963 }
1964#else
1965 /* TODO: Verify that this does the right thing in all cases, as it
1966 * presently probably does not. It is possible that an ELF image will
1967 * come with multiple read-only segments. What we ought to do is scan
1968 * the program headers again and mprotect all the read-only segments.
1969 * To prevent re-scanning the program header, we would have to build a
1970 * list of loadable segments in si, and then scan that instead. */
1971 if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) {
1972 mprotect((void *)si->wrprotect_start,
1973 si->wrprotect_end - si->wrprotect_start,
1974 PROT_READ | PROT_EXEC);
1975 }
1976#endif
1977
1978 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1979 stdout and stderr to close a security hole described in:
1980
1981 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1982
1983 */
1984 if (getuid() != geteuid() || getgid() != getegid())
1985 nullify_closed_stdio ();
1986 call_constructors(si);
1987 notify_gdb_of_load(si);
1988 return 0;
1989
1990fail:
1991 ERROR("failed to link %s\n", si->name);
1992 si->flags |= FLAG_ERROR;
1993 return -1;
1994}
1995
David Bartleybc3a5c22009-06-02 18:27:28 -07001996static void parse_library_path(char *path, char *delim)
1997{
1998 size_t len;
1999 char *ldpaths_bufp = ldpaths_buf;
2000 int i = 0;
2001
2002 len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf));
2003
2004 while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) {
2005 if (*ldpaths[i] != '\0')
2006 ++i;
2007 }
2008
2009 /* Forget the last path if we had to truncate; this occurs if the 2nd to
2010 * last char isn't '\0' (i.e. not originally a delim). */
2011 if (i > 0 && len >= sizeof(ldpaths_buf) &&
2012 ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') {
2013 ldpaths[i - 1] = NULL;
2014 } else {
2015 ldpaths[i] = NULL;
2016 }
2017}
2018
Matt Fischer4fd42c12009-12-31 12:09:10 -06002019static void parse_preloads(char *path, char *delim)
2020{
2021 size_t len;
2022 char *ldpreloads_bufp = ldpreloads_buf;
2023 int i = 0;
2024
2025 len = strlcpy(ldpreloads_buf, path, sizeof(ldpreloads_buf));
2026
2027 while (i < LDPRELOAD_MAX && (ldpreload_names[i] = strsep(&ldpreloads_bufp, delim))) {
2028 if (*ldpreload_names[i] != '\0') {
2029 ++i;
2030 }
2031 }
2032
2033 /* Forget the last path if we had to truncate; this occurs if the 2nd to
2034 * last char isn't '\0' (i.e. not originally a delim). */
2035 if (i > 0 && len >= sizeof(ldpreloads_buf) &&
2036 ldpreloads_buf[sizeof(ldpreloads_buf) - 2] != '\0') {
2037 ldpreload_names[i - 1] = NULL;
2038 } else {
2039 ldpreload_names[i] = NULL;
2040 }
2041}
2042
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002043int main(int argc, char **argv)
2044{
2045 return 0;
2046}
2047
2048#define ANDROID_TLS_SLOTS BIONIC_TLS_SLOTS
2049
2050static void * __tls_area[ANDROID_TLS_SLOTS];
2051
2052unsigned __linker_init(unsigned **elfdata)
2053{
2054 static soinfo linker_soinfo;
2055
2056 int argc = (int) *elfdata;
2057 char **argv = (char**) (elfdata + 1);
2058 unsigned *vecs = (unsigned*) (argv + argc + 1);
2059 soinfo *si;
2060 struct link_map * map;
David Bartleybc3a5c22009-06-02 18:27:28 -07002061 char *ldpath_env = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -06002062 char *ldpreload_env = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002063
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02002064 /* Setup a temporary TLS area that is used to get a working
2065 * errno for system calls.
2066 */
2067 __set_tls(__tls_area);
2068
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002069 pid = getpid();
2070
2071#if TIMING
2072 struct timeval t0, t1;
2073 gettimeofday(&t0, 0);
2074#endif
2075
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02002076 /* NOTE: we store the elfdata pointer on a special location
2077 * of the temporary TLS area in order to pass it to
2078 * the C Library's runtime initializer.
2079 *
2080 * The initializer must clear the slot and reset the TLS
2081 * to point to a different location to ensure that no other
2082 * shared library constructor can access it.
2083 */
2084 __tls_area[TLS_SLOT_BIONIC_PREINIT] = elfdata;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002085
2086 debugger_init();
2087
2088 /* skip past the environment */
2089 while(vecs[0] != 0) {
2090 if(!strncmp((char*) vecs[0], "DEBUG=", 6)) {
2091 debug_verbosity = atoi(((char*) vecs[0]) + 6);
David Bartleybc3a5c22009-06-02 18:27:28 -07002092 } else if(!strncmp((char*) vecs[0], "LD_LIBRARY_PATH=", 16)) {
2093 ldpath_env = (char*) vecs[0] + 16;
Matt Fischer4fd42c12009-12-31 12:09:10 -06002094 } else if(!strncmp((char*) vecs[0], "LD_PRELOAD=", 11)) {
2095 ldpreload_env = (char*) vecs[0] + 11;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002096 }
2097 vecs++;
2098 }
2099 vecs++;
2100
2101 INFO("[ android linker & debugger ]\n");
2102 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
2103
2104 si = alloc_info(argv[0]);
2105 if(si == 0) {
2106 exit(-1);
2107 }
2108
2109 /* bootstrap the link map, the main exe always needs to be first */
2110 si->flags |= FLAG_EXE;
2111 map = &(si->linkmap);
2112
2113 map->l_addr = 0;
2114 map->l_name = argv[0];
2115 map->l_prev = NULL;
2116 map->l_next = NULL;
2117
2118 _r_debug.r_map = map;
2119 r_debug_tail = map;
2120
2121 /* gdb expects the linker to be in the debug shared object list,
2122 * and we need to make sure that the reported load address is zero.
2123 * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
2124 * is. Don't use alloc_info(), because the linker shouldn't
2125 * be on the soinfo list.
2126 */
2127 strcpy((char*) linker_soinfo.name, "/system/bin/linker");
2128 linker_soinfo.flags = 0;
2129 linker_soinfo.base = 0; // This is the important part; must be zero.
2130 insert_soinfo_into_debug_map(&linker_soinfo);
2131
2132 /* extract information passed from the kernel */
2133 while(vecs[0] != 0){
2134 switch(vecs[0]){
2135 case AT_PHDR:
2136 si->phdr = (Elf32_Phdr*) vecs[1];
2137 break;
2138 case AT_PHNUM:
2139 si->phnum = (int) vecs[1];
2140 break;
2141 case AT_ENTRY:
2142 si->entry = vecs[1];
2143 break;
2144 }
2145 vecs += 2;
2146 }
2147
Iliyan Malchevaf7315a2009-10-16 17:50:42 -07002148 ba_init(&ba_prelink);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002149
2150 si->base = 0;
2151 si->dynamic = (unsigned *)-1;
2152 si->wrprotect_start = 0xffffffff;
2153 si->wrprotect_end = 0;
2154
David Bartleybc3a5c22009-06-02 18:27:28 -07002155 /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */
2156 if (ldpath_env && getuid() == geteuid() && getgid() == getegid())
2157 parse_library_path(ldpath_env, ":");
2158
Matt Fischer4fd42c12009-12-31 12:09:10 -06002159 if (ldpreload_env && getuid() == geteuid() && getgid() == getegid()) {
2160 parse_preloads(ldpreload_env, " :");
2161 }
2162
Dima Zavin2e855792009-05-20 18:28:09 -07002163 if(link_image(si, 0)) {
2164 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
2165 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2166 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002167 exit(-1);
2168 }
2169
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07002170#if ALLOW_SYMBOLS_FROM_MAIN
2171 /* Set somain after we've loaded all the libraries in order to prevent
2172 * linking of symbols back to the main image, which is not set up at that
2173 * point yet.
2174 */
2175 somain = si;
2176#endif
2177
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002178#if TIMING
2179 gettimeofday(&t1,NULL);
2180 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
2181 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2182 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
2183 ));
2184#endif
2185#if STATS
2186 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
2187 linker_stats.reloc[RELOC_ABSOLUTE],
2188 linker_stats.reloc[RELOC_RELATIVE],
2189 linker_stats.reloc[RELOC_COPY],
2190 linker_stats.reloc[RELOC_SYMBOL]);
2191#endif
2192#if COUNT_PAGES
2193 {
2194 unsigned n;
2195 unsigned i;
2196 unsigned count = 0;
2197 for(n = 0; n < 4096; n++){
2198 if(bitmask[n]){
2199 unsigned x = bitmask[n];
2200 for(i = 0; i < 8; i++){
2201 if(x & 1) count++;
2202 x >>= 1;
2203 }
2204 }
2205 }
2206 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
2207 }
2208#endif
2209
2210#if TIMING || STATS || COUNT_PAGES
2211 fflush(stdout);
2212#endif
2213
2214 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
2215 si->entry);
2216 return si->entry;
2217}