blob: e398f824778bc9e32f5a06e8b507015f86f9c8f1 [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
James Dongba52b302009-04-30 20:37:36 -070054#define SO_MAX 96
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055
56/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
57 *
58 * Do NOT use malloc() and friends or pthread_*() code here.
59 * Don't use printf() either; it's caused mysterious memory
60 * corruption in the past.
61 * The linker runs before we bring up libc and it's easiest
62 * to make sure it does not depend on any complex libc features
63 *
64 * open issues / todo:
65 *
66 * - should we do anything special for STB_WEAK symbols?
67 * - are we doing everything we should for ARM_COPY relocations?
68 * - cleaner error reporting
69 * - configuration for paths (LD_LIBRARY_PATH?)
70 * - after linking, set as much stuff as possible to READONLY
71 * and NOEXEC
72 * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
73 * headers provide versions that are negative...
74 * - allocate space for soinfo structs dynamically instead of
75 * having a hard limit (64)
76 *
77 * features to add someday:
78 *
79 * - dlopen() and friends
80 *
81*/
82
83
84static int link_image(soinfo *si, unsigned wr_offset);
85
86static int socount = 0;
87static soinfo sopool[SO_MAX];
88static soinfo *freelist = NULL;
89static soinfo *solist = &libdl_info;
90static soinfo *sonext = &libdl_info;
91
92int debug_verbosity;
93static int pid;
94
95#if STATS
96struct _link_stats linker_stats;
97#endif
98
99#if COUNT_PAGES
100unsigned bitmask[4096];
101#endif
102
103#ifndef PT_ARM_EXIDX
104#define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */
105#endif
106
Dima Zavin2e855792009-05-20 18:28:09 -0700107#define HOODLUM(name, ret, ...) \
108 ret name __VA_ARGS__ \
109 { \
110 char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \
111 write(2, errstr, sizeof(errstr)); \
112 abort(); \
113 }
114HOODLUM(malloc, void *, (size_t size));
115HOODLUM(free, void, (void *ptr));
116HOODLUM(realloc, void *, (void *ptr, size_t size));
117HOODLUM(calloc, void *, (size_t cnt, size_t size));
118
Dima Zavin03531952009-05-29 17:30:25 -0700119static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700120static char __linker_dl_err_buf[768];
121#define DL_ERR(fmt, x...) \
122 do { \
123 snprintf(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), \
124 "%s[%d]: " fmt, __func__, __LINE__, ##x); \
125 ERROR(fmt, ##x); \
126 } while(0)
127
128const char *linker_get_error(void)
129{
130 return (const char *)&__linker_dl_err_buf[0];
131}
132
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133/*
134 * This function is an empty stub where GDB locates a breakpoint to get notified
135 * about linker activity.
136 */
137extern void __attribute__((noinline)) rtld_db_dlactivity(void);
138
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
140 RT_CONSISTENT, 0};
141static struct link_map *r_debug_tail = 0;
142
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700143static pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144
145static void insert_soinfo_into_debug_map(soinfo * info)
146{
147 struct link_map * map;
148
149 /* Copy the necessary fields into the debug structure.
150 */
151 map = &(info->linkmap);
152 map->l_addr = info->base;
153 map->l_name = (char*) info->name;
154
155 /* Stick the new library at the end of the list.
156 * gdb tends to care more about libc than it does
157 * about leaf libraries, and ordering it this way
158 * reduces the back-and-forth over the wire.
159 */
160 if (r_debug_tail) {
161 r_debug_tail->l_next = map;
162 map->l_prev = r_debug_tail;
163 map->l_next = 0;
164 } else {
165 _r_debug.r_map = map;
166 map->l_prev = 0;
167 map->l_next = 0;
168 }
169 r_debug_tail = map;
170}
171
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700172static void remove_soinfo_from_debug_map(soinfo * info)
173{
174 struct link_map * map = &(info->linkmap);
175
176 if (r_debug_tail == map)
177 r_debug_tail = map->l_prev;
178
179 if (map->l_prev) map->l_prev->l_next = map->l_next;
180 if (map->l_next) map->l_next->l_prev = map->l_prev;
181}
182
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183void notify_gdb_of_load(soinfo * info)
184{
185 if (info->flags & FLAG_EXE) {
186 // GDB already knows about the main executable
187 return;
188 }
189
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700190 pthread_mutex_lock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191
192 _r_debug.r_state = RT_ADD;
193 rtld_db_dlactivity();
194
195 insert_soinfo_into_debug_map(info);
196
197 _r_debug.r_state = RT_CONSISTENT;
198 rtld_db_dlactivity();
199
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700200 pthread_mutex_unlock(&_r_debug_lock);
201}
202
203void notify_gdb_of_unload(soinfo * info)
204{
205 if (info->flags & FLAG_EXE) {
206 // GDB already knows about the main executable
207 return;
208 }
209
210 pthread_mutex_lock(&_r_debug_lock);
211
212 _r_debug.r_state = RT_DELETE;
213 rtld_db_dlactivity();
214
215 remove_soinfo_from_debug_map(info);
216
217 _r_debug.r_state = RT_CONSISTENT;
218 rtld_db_dlactivity();
219
220 pthread_mutex_unlock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221}
222
223void notify_gdb_of_libraries()
224{
225 _r_debug.r_state = RT_ADD;
226 rtld_db_dlactivity();
227 _r_debug.r_state = RT_CONSISTENT;
228 rtld_db_dlactivity();
229}
230
231static soinfo *alloc_info(const char *name)
232{
233 soinfo *si;
234
235 if(strlen(name) >= SOINFO_NAME_LEN) {
Dima Zavin2e855792009-05-20 18:28:09 -0700236 DL_ERR("%5d library name %s too long\n", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237 return 0;
238 }
239
240 /* The freelist is populated when we call free_info(), which in turn is
241 done only by dlclose(), which is not likely to be used.
242 */
243 if (!freelist) {
244 if(socount == SO_MAX) {
Dima Zavin2e855792009-05-20 18:28:09 -0700245 DL_ERR("%5d too many libraries when loading %s\n", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246 return NULL;
247 }
248 freelist = sopool + socount++;
249 freelist->next = NULL;
250 }
251
252 si = freelist;
253 freelist = freelist->next;
254
255 /* Make sure we get a clean block of soinfo */
256 memset(si, 0, sizeof(soinfo));
257 strcpy((char*) si->name, name);
258 sonext->next = si;
259 si->ba_index = -1; /* by default, prelinked */
260 si->next = NULL;
261 si->refcount = 0;
262 sonext = si;
263
264 TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
265 return si;
266}
267
268static void free_info(soinfo *si)
269{
270 soinfo *prev = NULL, *trav;
271
272 TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
273
274 for(trav = solist; trav != NULL; trav = trav->next){
275 if (trav == si)
276 break;
277 prev = trav;
278 }
279 if (trav == NULL) {
280 /* si was not ni solist */
Dima Zavin2e855792009-05-20 18:28:09 -0700281 DL_ERR("%5d name %s is not in solist!\n", pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282 return;
283 }
284
285 /* prev will never be NULL, because the first entry in solist is
286 always the static libdl_info.
287 */
288 prev->next = si->next;
289 if (si == sonext) sonext = prev;
290 si->next = freelist;
291 freelist = si;
292}
293
294#ifndef LINKER_TEXT_BASE
295#error "linker's makefile must define LINKER_TEXT_BASE"
296#endif
297#ifndef LINKER_AREA_SIZE
298#error "linker's makefile must define LINKER_AREA_SIZE"
299#endif
300#define LINKER_BASE ((LINKER_TEXT_BASE) & 0xfff00000)
301#define LINKER_TOP (LINKER_BASE + (LINKER_AREA_SIZE))
302
303const char *addr_to_name(unsigned addr)
304{
305 soinfo *si;
306
307 for(si = solist; si != 0; si = si->next){
308 if((addr >= si->base) && (addr < (si->base + si->size))) {
309 return si->name;
310 }
311 }
312
313 if((addr >= LINKER_BASE) && (addr < LINKER_TOP)){
314 return "linker";
315 }
316
317 return "";
318}
319
320/* For a given PC, find the .so that it belongs to.
321 * Returns the base address of the .ARM.exidx section
322 * for that .so, and the number of 8-byte entries
323 * in that section (via *pcount).
324 *
325 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
326 *
327 * This function is exposed via dlfcn.c and libdl.so.
328 */
329#ifdef ANDROID_ARM_LINKER
330_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
331{
332 soinfo *si;
333 unsigned addr = (unsigned)pc;
334
335 if ((addr < LINKER_BASE) || (addr >= LINKER_TOP)) {
336 for (si = solist; si != 0; si = si->next){
337 if ((addr >= si->base) && (addr < (si->base + si->size))) {
338 *pcount = si->ARM_exidx_count;
339 return (_Unwind_Ptr)(si->base + (unsigned long)si->ARM_exidx);
340 }
341 }
342 }
343 *pcount = 0;
344 return NULL;
345}
346#elif defined(ANDROID_X86_LINKER)
347/* Here, we only have to provide a callback to iterate across all the
348 * loaded libraries. gcc_eh does the rest. */
349int
350dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
351 void *data)
352{
353 soinfo *si;
354 struct dl_phdr_info dl_info;
355 int rv = 0;
356
357 for (si = solist; si != NULL; si = si->next) {
358 dl_info.dlpi_addr = si->linkmap.l_addr;
359 dl_info.dlpi_name = si->linkmap.l_name;
360 dl_info.dlpi_phdr = si->phdr;
361 dl_info.dlpi_phnum = si->phnum;
362 rv = cb(&dl_info, sizeof (struct dl_phdr_info), data);
363 if (rv != 0)
364 break;
365 }
366 return rv;
367}
368#endif
369
370static Elf32_Sym *_elf_lookup(soinfo *si, unsigned hash, const char *name)
371{
372 Elf32_Sym *s;
373 Elf32_Sym *symtab = si->symtab;
374 const char *strtab = si->strtab;
375 unsigned n;
376
377 TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
378 name, si->name, si->base, hash, hash % si->nbucket);
379 n = hash % si->nbucket;
380
381 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
382 s = symtab + n;
383 if(strcmp(strtab + s->st_name, name)) continue;
384
385 /* only concern ourselves with global symbols */
386 switch(ELF32_ST_BIND(s->st_info)){
387 case STB_GLOBAL:
388 /* no section == undefined */
389 if(s->st_shndx == 0) continue;
390
391 case STB_WEAK:
392 TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
393 name, si->name, s->st_value, s->st_size);
394 return s;
395 }
396 }
397
398 return 0;
399}
400
401static unsigned elfhash(const char *_name)
402{
403 const unsigned char *name = (const unsigned char *) _name;
404 unsigned h = 0, g;
405
406 while(*name) {
407 h = (h << 4) + *name++;
408 g = h & 0xf0000000;
409 h ^= g;
410 h ^= g >> 24;
411 }
412 return h;
413}
414
415static Elf32_Sym *
416_do_lookup_in_so(soinfo *si, const char *name, unsigned *elf_hash)
417{
418 if (*elf_hash == 0)
419 *elf_hash = elfhash(name);
420 return _elf_lookup (si, *elf_hash, name);
421}
422
423/* This is used by dl_sym() */
424Elf32_Sym *lookup_in_library(soinfo *si, const char *name)
425{
426 unsigned unused = 0;
427 return _do_lookup_in_so(si, name, &unused);
428}
429
430static Elf32_Sym *
431_do_lookup(soinfo *user_si, const char *name, unsigned *base)
432{
433 unsigned elf_hash = 0;
434 Elf32_Sym *s = NULL;
435 soinfo *si;
436
437 /* Look for symbols in the local scope first (the object who is
438 * searching). This happens with C++ templates on i386 for some
439 * reason. */
440 if (user_si) {
441 s = _do_lookup_in_so(user_si, name, &elf_hash);
442 if (s != NULL)
443 *base = user_si->base;
444 }
445
446 for(si = solist; (s == NULL) && (si != NULL); si = si->next)
447 {
448 if((si->flags & FLAG_ERROR) || (si == user_si))
449 continue;
450 s = _do_lookup_in_so(si, name, &elf_hash);
451 if (s != NULL) {
452 *base = si->base;
453 break;
454 }
455 }
456
457 if (s != NULL) {
458 TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
459 "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
460 return s;
461 }
462
463 return 0;
464}
465
466/* This is used by dl_sym() */
467Elf32_Sym *lookup(const char *name, unsigned *base)
468{
469 return _do_lookup(NULL, name, base);
470}
471
472#if 0
473static void dump(soinfo *si)
474{
475 Elf32_Sym *s = si->symtab;
476 unsigned n;
477
478 for(n = 0; n < si->nchain; n++) {
479 TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
480 s->st_info, s->st_shndx, s->st_value, s->st_size,
481 si->strtab + s->st_name);
482 s++;
483 }
484}
485#endif
486
487static const char *sopaths[] = {
488 "/system/lib",
489 "/lib",
490 0
491};
492
493static int _open_lib(const char *name)
494{
495 int fd;
496 struct stat filestat;
497
498 if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
499 if ((fd = open(name, O_RDONLY)) >= 0)
500 return fd;
501 }
502
503 return -1;
504}
505
506/* TODO: Need to add support for initializing the so search path with
507 * LD_LIBRARY_PATH env variable for non-setuid programs. */
508static int open_library(const char *name)
509{
510 int fd;
511 char buf[512];
512 const char **path;
513
514 TRACE("[ %5d opening %s ]\n", pid, name);
515
516 if(name == 0) return -1;
517 if(strlen(name) > 256) return -1;
518
519 if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
520 return fd;
521
522 for (path = sopaths; *path; path++) {
523 snprintf(buf, sizeof(buf), "%s/%s", *path, name);
524 if ((fd = _open_lib(buf)) >= 0)
525 return fd;
526 }
527
528 return -1;
529}
530
531/* temporary space for holding the first page of the shared lib
532 * which contains the elf header (with the pht). */
533static unsigned char __header[PAGE_SIZE];
534
535typedef struct {
536 long mmap_addr;
537 char tag[4]; /* 'P', 'R', 'E', ' ' */
538} prelink_info_t;
539
540/* Returns the requested base address if the library is prelinked,
541 * and 0 otherwise. */
542static unsigned long
543is_prelinked(int fd, const char *name)
544{
545 off_t sz;
546 prelink_info_t info;
547
548 sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
549 if (sz < 0) {
Dima Zavin2e855792009-05-20 18:28:09 -0700550 DL_ERR("lseek() failed!\n");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800551 return 0;
552 }
553
554 if (read(fd, &info, sizeof(info)) != sizeof(info)) {
555 WARN("Could not read prelink_info_t structure for `%s`\n", name);
556 return 0;
557 }
558
559 if (strncmp(info.tag, "PRE ", 4)) {
560 WARN("`%s` is not a prelinked library\n", name);
561 return 0;
562 }
563
564 return (unsigned long)info.mmap_addr;
565}
566
567/* verify_elf_object
568 * Verifies if the object @ base is a valid ELF object
569 *
570 * Args:
571 *
572 * Returns:
573 * 0 on success
574 * -1 if no valid ELF object is found @ base.
575 */
576static int
577verify_elf_object(void *base, const char *name)
578{
579 Elf32_Ehdr *hdr = (Elf32_Ehdr *) base;
580
581 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
582 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
583 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
584 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
585
586 /* TODO: Should we verify anything else in the header? */
587
588 return 0;
589}
590
591
592/* get_lib_extents
593 * Retrieves the base (*base) address where the ELF object should be
594 * mapped and its overall memory size (*total_sz).
595 *
596 * Args:
597 * fd: Opened file descriptor for the library
598 * name: The name of the library
599 * _hdr: Pointer to the header page of the library
600 * total_sz: Total size of the memory that should be allocated for
601 * this library
602 *
603 * Returns:
604 * -1 if there was an error while trying to get the lib extents.
605 * The possible reasons are:
606 * - Could not determine if the library was prelinked.
607 * - The library provided is not a valid ELF object
608 * 0 if the library did not request a specific base offset (normal
609 * for non-prelinked libs)
610 * > 0 if the library requests a specific address to be mapped to.
611 * This indicates a pre-linked library.
612 */
613static unsigned
614get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz)
615{
616 unsigned req_base;
617 unsigned min_vaddr = 0xffffffff;
618 unsigned max_vaddr = 0;
619 unsigned char *_hdr = (unsigned char *)__hdr;
620 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)_hdr;
621 Elf32_Phdr *phdr;
622 int cnt;
623
624 TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name);
625 if (verify_elf_object(_hdr, name) < 0) {
Dima Zavin2e855792009-05-20 18:28:09 -0700626 DL_ERR("%5d - %s is not a valid ELF object\n", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800627 return (unsigned)-1;
628 }
629
630 req_base = (unsigned) is_prelinked(fd, name);
631 if (req_base == (unsigned)-1)
632 return -1;
633 else if (req_base != 0) {
634 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
635 pid, name, req_base);
636 } else {
637 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
638 }
639
640 phdr = (Elf32_Phdr *)(_hdr + ehdr->e_phoff);
641
642 /* find the min/max p_vaddrs from all the PT_LOAD segments so we can
643 * get the range. */
644 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
645 if (phdr->p_type == PT_LOAD) {
646 if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr)
647 max_vaddr = phdr->p_vaddr + phdr->p_memsz;
648 if (phdr->p_vaddr < min_vaddr)
649 min_vaddr = phdr->p_vaddr;
650 }
651 }
652
653 if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -0700654 DL_ERR("%5d - No loadable segments found in %s.\n", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800655 return (unsigned)-1;
656 }
657
658 /* truncate min_vaddr down to page boundary */
659 min_vaddr &= ~PAGE_MASK;
660
661 /* round max_vaddr up to the next page */
662 max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK;
663
664 *total_sz = (max_vaddr - min_vaddr);
665 return (unsigned)req_base;
666}
667
668/* alloc_mem_region
669 *
670 * This function reserves a chunk of memory to be used for mapping in
671 * the shared library. We reserve the entire memory region here, and
672 * then the rest of the linker will relocate the individual loadable
673 * segments into the correct locations within this memory range.
674 *
675 * Args:
676 * si->base: The requested base of the allocation. If 0, a sane one will be
677 * chosen in the range LIBBASE <= base < LIBLAST.
678 * si->size: The size of the allocation.
679 *
680 * Returns:
681 * -1 on failure, and 0 on success. On success, si->base will contain
682 * the virtual address at which the library will be mapped.
683 */
684
685static int reserve_mem_region(soinfo *si)
686{
687 void *base = mmap((void *)si->base, si->size, PROT_READ | PROT_EXEC,
688 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
689 if (base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700690 DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800691 "as requested, will try general pool: %d (%s)\n",
692 pid, (si->base ? "" : "non-"), si->name, si->base,
693 errno, strerror(errno));
694 return -1;
695 } else if (base != (void *)si->base) {
Dima Zavin2e855792009-05-20 18:28:09 -0700696 DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800697 "not at 0x%08x\n", pid, (si->base ? "" : "non-"),
698 si->name, (unsigned)base, si->base);
699 munmap(base, si->size);
700 return -1;
701 }
702 return 0;
703}
704
705static int
706alloc_mem_region(soinfo *si)
707{
708 if (si->base) {
709 /* Attempt to mmap a prelinked library. */
710 si->ba_index = -1;
711 return reserve_mem_region(si);
712 }
713
714 /* This is not a prelinked library, so we attempt to allocate space
715 for it from the buddy allocator, which manages the area between
716 LIBBASE and LIBLAST.
717 */
718 si->ba_index = ba_allocate(si->size);
719 if(si->ba_index >= 0) {
720 si->base = ba_start_addr(si->ba_index);
721 PRINT("%5d mapping library '%s' at %08x (index %d) " \
722 "through buddy allocator.\n",
723 pid, si->name, si->base, si->ba_index);
724 if (reserve_mem_region(si) < 0) {
725 ba_free(si->ba_index);
726 si->ba_index = -1;
727 si->base = 0;
728 goto err;
729 }
730 return 0;
731 }
732
733err:
Dima Zavin2e855792009-05-20 18:28:09 -0700734 DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800735 pid, si->name);
736 return -1;
737}
738
739#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
740#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
741 MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
742 MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
743/* load_segments
744 *
745 * This function loads all the loadable (PT_LOAD) segments into memory
746 * at their appropriate memory offsets off the base address.
747 *
748 * Args:
749 * fd: Open file descriptor to the library to load.
750 * header: Pointer to a header page that contains the ELF header.
751 * This is needed since we haven't mapped in the real file yet.
752 * si: ptr to soinfo struct describing the shared object.
753 *
754 * Returns:
755 * 0 on success, -1 on failure.
756 */
757static int
758load_segments(int fd, void *header, soinfo *si)
759{
760 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
761 Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned char *)header + ehdr->e_phoff);
762 unsigned char *base = (unsigned char *)si->base;
763 int cnt;
764 unsigned len;
765 unsigned char *tmp;
766 unsigned char *pbase;
767 unsigned char *extra_base;
768 unsigned extra_len;
769 unsigned total_sz = 0;
770
771 si->wrprotect_start = 0xffffffff;
772 si->wrprotect_end = 0;
773
774 TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n",
775 pid, si->name, (unsigned)si->base);
776 /* Now go through all the PT_LOAD segments and map them into memory
777 * at the appropriate locations. */
778 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
779 if (phdr->p_type == PT_LOAD) {
780 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
781 /* we want to map in the segment on a page boundary */
782 tmp = base + (phdr->p_vaddr & (~PAGE_MASK));
783 /* add the # of bytes we masked off above to the total length. */
784 len = phdr->p_filesz + (phdr->p_vaddr & PAGE_MASK);
785
786 TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x "
787 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name,
788 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
789 pbase = mmap(tmp, len, PFLAGS_TO_PROT(phdr->p_flags),
790 MAP_PRIVATE | MAP_FIXED, fd,
791 phdr->p_offset & (~PAGE_MASK));
792 if (pbase == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700793 DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800794 "p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
795 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
796 goto fail;
797 }
798
799 /* If 'len' didn't end on page boundary, and it's a writable
800 * segment, zero-fill the rest. */
801 if ((len & PAGE_MASK) && (phdr->p_flags & PF_W))
802 memset((void *)(pbase + len), 0, PAGE_SIZE - (len & PAGE_MASK));
803
804 /* Check to see if we need to extend the map for this segment to
805 * cover the diff between filesz and memsz (i.e. for bss).
806 *
807 * base _+---------------------+ page boundary
808 * . .
809 * | |
810 * . .
811 * pbase _+---------------------+ page boundary
812 * | |
813 * . .
814 * base + p_vaddr _| |
815 * . \ \ .
816 * . | filesz | .
817 * pbase + len _| / | |
818 * <0 pad> . . .
819 * extra_base _+------------|--------+ page boundary
820 * / . . .
821 * | . . .
822 * | +------------|--------+ page boundary
823 * extra_len-> | | | |
824 * | . | memsz .
825 * | . | .
826 * \ _| / |
827 * . .
828 * | |
829 * _+---------------------+ page boundary
830 */
831 tmp = (unsigned char *)(((unsigned)pbase + len + PAGE_SIZE - 1) &
832 (~PAGE_MASK));
833 if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) {
834 extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp;
835 TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x "
836 "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len);
837 /* map in the extra page(s) as anonymous into the range.
838 * This is probably not necessary as we already mapped in
839 * the entire region previously, but we just want to be
840 * sure. This will also set the right flags on the region
841 * (though we can probably accomplish the same thing with
842 * mprotect).
843 */
844 extra_base = mmap((void *)tmp, extra_len,
845 PFLAGS_TO_PROT(phdr->p_flags),
846 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
847 -1, 0);
848 if (extra_base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700849 DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x"
850 " (0x%08x) ]\n", pid, si->name, (unsigned)tmp,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800851 extra_len);
852 goto fail;
853 }
854 /* TODO: Check if we need to memset-0 this region.
855 * Anonymous mappings are zero-filled copy-on-writes, so we
856 * shouldn't need to. */
857 TRACE("[ %5d - Segment from '%s' extended @ 0x%08x "
858 "(0x%08x)\n", pid, si->name, (unsigned)extra_base,
859 extra_len);
860 }
861 /* set the len here to show the full extent of the segment we
862 * just loaded, mostly for debugging */
863 len = (((unsigned)base + phdr->p_vaddr + phdr->p_memsz +
864 PAGE_SIZE - 1) & (~PAGE_MASK)) - (unsigned)pbase;
865 TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x "
866 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
867 (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset);
868 total_sz += len;
869 /* Make the section writable just in case we'll have to write to
870 * it during relocation (i.e. text segment). However, we will
871 * remember what range of addresses should be write protected.
872 *
873 */
874 if (!(phdr->p_flags & PF_W)) {
875 if ((unsigned)pbase < si->wrprotect_start)
876 si->wrprotect_start = (unsigned)pbase;
877 if (((unsigned)pbase + len) > si->wrprotect_end)
878 si->wrprotect_end = (unsigned)pbase + len;
879 mprotect(pbase, len,
880 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
881 }
882 } else if (phdr->p_type == PT_DYNAMIC) {
883 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
884 /* this segment contains the dynamic linking information */
885 si->dynamic = (unsigned *)(base + phdr->p_vaddr);
886 } else {
887#ifdef ANDROID_ARM_LINKER
888 if (phdr->p_type == PT_ARM_EXIDX) {
889 DEBUG_DUMP_PHDR(phdr, "PT_ARM_EXIDX", pid);
890 /* exidx entries (used for stack unwinding) are 8 bytes each.
891 */
892 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
893 si->ARM_exidx_count = phdr->p_memsz / 8;
894 }
895#endif
896 }
897
898 }
899
900 /* Sanity check */
901 if (total_sz > si->size) {
Dima Zavin2e855792009-05-20 18:28:09 -0700902 DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800903 "greater than what was allocated (0x%08x). THIS IS BAD!\n",
904 pid, total_sz, si->name, si->size);
905 goto fail;
906 }
907
908 TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. "
909 "Total memory footprint: 0x%08x bytes ]\n", pid, si->name,
910 (unsigned)si->base, si->size);
911 return 0;
912
913fail:
914 /* We can just blindly unmap the entire region even though some things
915 * were mapped in originally with anonymous and others could have been
916 * been mapped in from the file before we failed. The kernel will unmap
917 * all the pages in the range, irrespective of how they got there.
918 */
919 munmap((void *)si->base, si->size);
920 si->flags |= FLAG_ERROR;
921 return -1;
922}
923
924/* TODO: Implement this to take care of the fact that Android ARM
925 * ELF objects shove everything into a single loadable segment that has the
926 * write bit set. wr_offset is then used to set non-(data|bss) pages to be
927 * non-writable.
928 */
929#if 0
930static unsigned
931get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr)
932{
933 Elf32_Shdr *shdr_start;
934 Elf32_Shdr *shdr;
935 int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr);
936 int cnt;
937 unsigned wr_offset = 0xffffffff;
938
939 shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd,
940 ehdr->e_shoff & (~PAGE_MASK));
941 if (shdr_start == MAP_FAILED) {
942 WARN("%5d - Could not read section header info from '%s'. Will not "
943 "not be able to determine write-protect offset.\n", pid, name);
944 return (unsigned)-1;
945 }
946
947 for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) {
948 if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) &&
949 (shdr->sh_addr < wr_offset)) {
950 wr_offset = shdr->sh_addr;
951 }
952 }
953
954 munmap(shdr_start, shdr_sz);
955 return wr_offset;
956}
957#endif
958
959static soinfo *
960load_library(const char *name)
961{
962 int fd = open_library(name);
963 int cnt;
964 unsigned ext_sz;
965 unsigned req_base;
966 soinfo *si = NULL;
967 Elf32_Ehdr *hdr;
968
Dima Zavin2e855792009-05-20 18:28:09 -0700969 if(fd == -1) {
970 DL_ERR("Library '%s' not found\n", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800971 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700972 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800973
974 /* We have to read the ELF header to figure out what to do with this image
975 */
976 if (lseek(fd, 0, SEEK_SET) < 0) {
Dima Zavin2e855792009-05-20 18:28:09 -0700977 DL_ERR("lseek() failed!\n");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800978 goto fail;
979 }
980
981 if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) {
Dima Zavin2e855792009-05-20 18:28:09 -0700982 DL_ERR("read() failed!\n");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800983 goto fail;
984 }
985
986 /* Parse the ELF header and get the size of the memory footprint for
987 * the library */
988 req_base = get_lib_extents(fd, name, &__header[0], &ext_sz);
989 if (req_base == (unsigned)-1)
990 goto fail;
991 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
992 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
993
994 /* Now configure the soinfo struct where we'll store all of our data
995 * for the ELF object. If the loading fails, we waste the entry, but
996 * same thing would happen if we failed during linking. Configuring the
997 * soinfo struct here is a lot more convenient.
998 */
999 si = alloc_info(name);
1000 if (si == NULL)
1001 goto fail;
1002
1003 /* Carve out a chunk of memory where we will map in the individual
1004 * segments */
1005 si->base = req_base;
1006 si->size = ext_sz;
1007 si->flags = 0;
1008 si->entry = 0;
1009 si->dynamic = (unsigned *)-1;
1010 if (alloc_mem_region(si) < 0)
1011 goto fail;
1012
1013 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
1014 pid, name, (void *)si->base, (unsigned) ext_sz);
1015
1016 /* Now actually load the library's segments into right places in memory */
1017 if (load_segments(fd, &__header[0], si) < 0) {
1018 if (si->ba_index >= 0) {
1019 ba_free(si->ba_index);
1020 si->ba_index = -1;
1021 }
1022 goto fail;
1023 }
1024
1025 /* this might not be right. Technically, we don't even need this info
1026 * once we go through 'load_segments'. */
1027 hdr = (Elf32_Ehdr *)si->base;
1028 si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff);
1029 si->phnum = hdr->e_phnum;
1030 /**/
1031
1032 close(fd);
1033 return si;
1034
1035fail:
1036 if (si) free_info(si);
1037 close(fd);
1038 return NULL;
1039}
1040
1041static soinfo *
1042init_library(soinfo *si)
1043{
1044 unsigned wr_offset = 0xffffffff;
1045
1046 /* At this point we know that whatever is loaded @ base is a valid ELF
1047 * shared library whose segments are properly mapped in. */
1048 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
1049 pid, si->base, si->size, si->name);
1050
1051 if (si->base < LIBBASE || si->base >= LIBLAST)
1052 si->flags |= FLAG_PRELINKED;
1053
1054 if(link_image(si, wr_offset)) {
1055 /* We failed to link. However, we can only restore libbase
1056 ** if no additional libraries have moved it since we updated it.
1057 */
1058 munmap((void *)si->base, si->size);
1059 return NULL;
1060 }
1061
1062 return si;
1063}
1064
1065soinfo *find_library(const char *name)
1066{
1067 soinfo *si;
1068
1069 for(si = solist; si != 0; si = si->next){
1070 if(!strcmp(name, si->name)) {
1071 if(si->flags & FLAG_ERROR) return 0;
1072 if(si->flags & FLAG_LINKED) return si;
Dima Zavin2e855792009-05-20 18:28:09 -07001073 DL_ERR("OOPS: %5d recursive link to '%s'\n", pid, si->name);
1074 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001075 }
1076 }
1077
1078 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
1079 si = load_library(name);
1080 if(si == NULL)
1081 return NULL;
1082 return init_library(si);
1083}
1084
1085/* TODO:
1086 * notify gdb of unload
1087 * for non-prelinked libraries, find a way to decrement libbase
1088 */
1089static void call_destructors(soinfo *si);
1090unsigned unload_library(soinfo *si)
1091{
1092 unsigned *d;
1093 if (si->refcount == 1) {
1094 TRACE("%5d unloading '%s'\n", pid, si->name);
1095 call_destructors(si);
1096
1097 for(d = si->dynamic; *d; d += 2) {
1098 if(d[0] == DT_NEEDED){
1099 TRACE("%5d %s needs to unload %s\n", pid,
1100 si->name, si->strtab + d[1]);
1101 soinfo *lsi = find_library(si->strtab + d[1]);
1102 if(lsi)
1103 unload_library(lsi);
1104 else
Dima Zavin2e855792009-05-20 18:28:09 -07001105 DL_ERR("%5d could not unload '%s'\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001106 pid, si->strtab + d[1]);
1107 }
1108 }
1109
1110 munmap((char *)si->base, si->size);
1111 if (si->ba_index >= 0) {
1112 PRINT("%5d releasing library '%s' address space at %08x "\
1113 "through buddy allocator.\n",
1114 pid, si->name, si->base);
1115 ba_free(si->ba_index);
1116 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001117 notify_gdb_of_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001118 free_info(si);
1119 si->refcount = 0;
1120 }
1121 else {
1122 si->refcount--;
1123 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
1124 pid, si->name, si->refcount);
1125 }
1126 return si->refcount;
1127}
1128
1129/* TODO: don't use unsigned for addrs below. It works, but is not
1130 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1131 * long.
1132 */
1133static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count)
1134{
1135 Elf32_Sym *symtab = si->symtab;
1136 const char *strtab = si->strtab;
1137 Elf32_Sym *s;
1138 unsigned base;
1139 Elf32_Rel *start = rel;
1140 unsigned idx;
1141
1142 for (idx = 0; idx < count; ++idx) {
1143 unsigned type = ELF32_R_TYPE(rel->r_info);
1144 unsigned sym = ELF32_R_SYM(rel->r_info);
1145 unsigned reloc = (unsigned)(rel->r_offset + si->base);
1146 unsigned sym_addr = 0;
1147 char *sym_name = NULL;
1148
1149 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1150 si->name, idx);
1151 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001152 sym_name = (char *)(strtab + symtab[sym].st_name);
1153 s = _do_lookup(si, sym_name, &base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001154 if(s == 0) {
Dima Zavin2e855792009-05-20 18:28:09 -07001155 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001156 return -1;
1157 }
1158#if 0
1159 if((base == 0) && (si->base != 0)){
1160 /* linking from libraries to main image is bad */
Dima Zavin2e855792009-05-20 18:28:09 -07001161 DL_ERR("%5d cannot locate '%s'...\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001162 pid, strtab + symtab[sym].st_name);
1163 return -1;
1164 }
1165#endif
1166 if ((s->st_shndx == SHN_UNDEF) && (s->st_value != 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -07001167 DL_ERR("%5d In '%s', shndx=%d && value=0x%08x. We do not "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001168 "handle this yet\n", pid, si->name, s->st_shndx,
1169 s->st_value);
1170 return -1;
1171 }
1172 sym_addr = (unsigned)(s->st_value + base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001173 COUNT_RELOC(RELOC_SYMBOL);
1174 } else {
1175 s = 0;
1176 }
1177
1178/* TODO: This is ugly. Split up the relocations by arch into
1179 * different files.
1180 */
1181 switch(type){
1182#if defined(ANDROID_ARM_LINKER)
1183 case R_ARM_JUMP_SLOT:
1184 COUNT_RELOC(RELOC_ABSOLUTE);
1185 MARK(rel->r_offset);
1186 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1187 reloc, sym_addr, sym_name);
1188 *((unsigned*)reloc) = sym_addr;
1189 break;
1190 case R_ARM_GLOB_DAT:
1191 COUNT_RELOC(RELOC_ABSOLUTE);
1192 MARK(rel->r_offset);
1193 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1194 reloc, sym_addr, sym_name);
1195 *((unsigned*)reloc) = sym_addr;
1196 break;
1197 case R_ARM_ABS32:
1198 COUNT_RELOC(RELOC_ABSOLUTE);
1199 MARK(rel->r_offset);
1200 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1201 reloc, sym_addr, sym_name);
1202 *((unsigned*)reloc) += sym_addr;
1203 break;
1204#elif defined(ANDROID_X86_LINKER)
1205 case R_386_JUMP_SLOT:
1206 COUNT_RELOC(RELOC_ABSOLUTE);
1207 MARK(rel->r_offset);
1208 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1209 reloc, sym_addr, sym_name);
1210 *((unsigned*)reloc) = sym_addr;
1211 break;
1212 case R_386_GLOB_DAT:
1213 COUNT_RELOC(RELOC_ABSOLUTE);
1214 MARK(rel->r_offset);
1215 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1216 reloc, sym_addr, sym_name);
1217 *((unsigned*)reloc) = sym_addr;
1218 break;
1219#endif /* ANDROID_*_LINKER */
1220
1221#if defined(ANDROID_ARM_LINKER)
1222 case R_ARM_RELATIVE:
1223#elif defined(ANDROID_X86_LINKER)
1224 case R_386_RELATIVE:
1225#endif /* ANDROID_*_LINKER */
1226 COUNT_RELOC(RELOC_RELATIVE);
1227 MARK(rel->r_offset);
1228 if(sym){
Dima Zavin2e855792009-05-20 18:28:09 -07001229 DL_ERR("%5d odd RELATIVE form...\n", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001230 return -1;
1231 }
1232 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1233 reloc, si->base);
1234 *((unsigned*)reloc) += si->base;
1235 break;
1236
1237#if defined(ANDROID_X86_LINKER)
1238 case R_386_32:
1239 COUNT_RELOC(RELOC_RELATIVE);
1240 MARK(rel->r_offset);
1241
1242 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1243 reloc, sym_addr, sym_name);
1244 *((unsigned *)reloc) += (unsigned)sym_addr;
1245 break;
1246
1247 case R_386_PC32:
1248 COUNT_RELOC(RELOC_RELATIVE);
1249 MARK(rel->r_offset);
1250 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1251 "+%08x (%08x - %08x) %s\n", pid, reloc,
1252 (sym_addr - reloc), sym_addr, reloc, sym_name);
1253 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1254 break;
1255#endif /* ANDROID_X86_LINKER */
1256
1257#ifdef ANDROID_ARM_LINKER
1258 case R_ARM_COPY:
1259 COUNT_RELOC(RELOC_COPY);
1260 MARK(rel->r_offset);
1261 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1262 reloc, s->st_size, sym_addr, sym_name);
1263 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1264 break;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001265 case R_ARM_NONE:
1266 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001267#endif /* ANDROID_ARM_LINKER */
1268
1269 default:
Dima Zavin2e855792009-05-20 18:28:09 -07001270 DL_ERR("%5d unknown reloc type %d @ %p (%d)\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001271 pid, type, rel, (int) (rel - start));
1272 return -1;
1273 }
1274 rel++;
1275 }
1276 return 0;
1277}
1278
David 'Digit' Turner82156792009-05-18 14:37:41 +02001279
1280/* Please read the "Initialization and Termination functions" functions.
1281 * of the linker design note in bionic/linker/README.TXT to understand
1282 * what the following code is doing.
1283 *
1284 * The important things to remember are:
1285 *
1286 * DT_PREINIT_ARRAY must be called first for executables, and should
1287 * not appear in shared libraries.
1288 *
1289 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1290 *
1291 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1292 *
1293 * DT_FINI_ARRAY must be parsed in reverse order.
1294 */
1295
1296static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001297{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001298 int n, inc = 1;
1299
1300 if (reverse) {
1301 ctor += (count-1);
1302 inc = -1;
1303 }
1304
1305 for(n = count; n > 0; n--) {
1306 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1307 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001308 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001309 void (*func)() = (void (*)()) *ctor;
1310 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001311 if(((int) func == 0) || ((int) func == -1)) continue;
1312 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1313 func();
1314 }
1315}
1316
1317static void call_constructors(soinfo *si)
1318{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001319 if (si->flags & FLAG_EXE) {
1320 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1321 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1322 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001323 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001324 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1325 } else {
1326 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001327 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
1328 " This is INVALID.\n", pid, si->name,
1329 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001330 }
1331 }
1332
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001333 if (si->init_func) {
1334 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1335 (unsigned)si->init_func, si->name);
1336 si->init_func();
1337 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1338 }
1339
1340 if (si->init_array) {
1341 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1342 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001343 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001344 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1345 }
1346}
1347
David 'Digit' Turner82156792009-05-18 14:37:41 +02001348
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001349static void call_destructors(soinfo *si)
1350{
1351 if (si->fini_array) {
1352 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1353 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001354 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001355 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1356 }
1357
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001358 if (si->fini_func) {
1359 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1360 (unsigned)si->fini_func, si->name);
1361 si->fini_func();
1362 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1363 }
1364}
1365
1366/* Force any of the closed stdin, stdout and stderr to be associated with
1367 /dev/null. */
1368static int nullify_closed_stdio (void)
1369{
1370 int dev_null, i, status;
1371 int return_value = 0;
1372
1373 dev_null = open("/dev/null", O_RDWR);
1374 if (dev_null < 0) {
Dima Zavin2e855792009-05-20 18:28:09 -07001375 DL_ERR("Cannot open /dev/null.\n");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001376 return -1;
1377 }
1378 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1379
1380 /* If any of the stdio file descriptors is valid and not associated
1381 with /dev/null, dup /dev/null to it. */
1382 for (i = 0; i < 3; i++) {
1383 /* If it is /dev/null already, we are done. */
1384 if (i == dev_null)
1385 continue;
1386
1387 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1388 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1389 can be interrupted but we do this just to be safe. */
1390 do {
1391 status = fcntl(i, F_GETFL);
1392 } while (status < 0 && errno == EINTR);
1393
1394 /* If file is openned, we are good. */
1395 if (status >= 0)
1396 continue;
1397
1398 /* The only error we allow is that the file descriptor does not
1399 exist, in which case we dup /dev/null to it. */
1400 if (errno != EBADF) {
Dima Zavin2e855792009-05-20 18:28:09 -07001401 DL_ERR("nullify_stdio: unhandled error %s\n", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001402 return_value = -1;
1403 continue;
1404 }
1405
1406 /* Try dupping /dev/null to this stdio file descriptor and
1407 repeat if there is a signal. Note that any errors in closing
1408 the stdio descriptor are lost. */
1409 do {
1410 status = dup2(dev_null, i);
1411 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001412
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001413 if (status < 0) {
Dima Zavin2e855792009-05-20 18:28:09 -07001414 DL_ERR("nullify_stdio: dup2 error %s\n", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001415 return_value = -1;
1416 continue;
1417 }
1418 }
1419
1420 /* If /dev/null is not one of the stdio file descriptors, close it. */
1421 if (dev_null > 2) {
1422 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001423 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001424 status = close(dev_null);
1425 } while (status < 0 && errno == EINTR);
1426
1427 if (status < 0) {
Dima Zavin2e855792009-05-20 18:28:09 -07001428 DL_ERR("nullify_stdio: close error %s\n", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001429 return_value = -1;
1430 }
1431 }
1432
1433 return return_value;
1434}
1435
1436static int link_image(soinfo *si, unsigned wr_offset)
1437{
1438 unsigned *d;
1439 Elf32_Phdr *phdr = si->phdr;
1440 int phnum = si->phnum;
1441
1442 INFO("[ %5d linking %s ]\n", pid, si->name);
1443 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1444 si->base, si->flags);
1445
1446 if (si->flags & FLAG_EXE) {
1447 /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for
1448 * linkage info if this is the executable. If this was a
1449 * dynamic lib, that would have been done at load time.
1450 *
1451 * TODO: It's unfortunate that small pieces of this are
1452 * repeated from the load_library routine. Refactor this just
1453 * slightly to reuse these bits.
1454 */
1455 si->size = 0;
1456 for(; phnum > 0; --phnum, ++phdr) {
1457#ifdef ANDROID_ARM_LINKER
1458 if(phdr->p_type == PT_ARM_EXIDX) {
1459 /* exidx entries (used for stack unwinding) are 8 bytes each.
1460 */
1461 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1462 si->ARM_exidx_count = phdr->p_memsz / 8;
1463 }
1464#endif
1465 if (phdr->p_type == PT_LOAD) {
1466 /* For the executable, we use the si->size field only in
1467 dl_unwind_find_exidx(), so the meaning of si->size
1468 is not the size of the executable; it is the last
1469 virtual address of the loadable part of the executable;
1470 since si->base == 0 for an executable, we use the
1471 range [0, si->size) to determine whether a PC value
1472 falls within the executable section. Of course, if
1473 a value is below phdr->p_vaddr, it's not in the
1474 executable section, but a) we shouldn't be asking for
1475 such a value anyway, and b) if we have to provide
1476 an EXIDX for such a value, then the executable's
1477 EXIDX is probably the better choice.
1478 */
1479 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
1480 if (phdr->p_vaddr + phdr->p_memsz > si->size)
1481 si->size = phdr->p_vaddr + phdr->p_memsz;
1482 /* try to remember what range of addresses should be write
1483 * protected */
1484 if (!(phdr->p_flags & PF_W)) {
1485 unsigned _end;
1486
1487 if (phdr->p_vaddr < si->wrprotect_start)
1488 si->wrprotect_start = phdr->p_vaddr;
1489 _end = (((phdr->p_vaddr + phdr->p_memsz + PAGE_SIZE - 1) &
1490 (~PAGE_MASK)));
1491 if (_end > si->wrprotect_end)
1492 si->wrprotect_end = _end;
1493 }
1494 } else if (phdr->p_type == PT_DYNAMIC) {
1495 if (si->dynamic != (unsigned *)-1) {
Dima Zavin2e855792009-05-20 18:28:09 -07001496 DL_ERR("%5d multiple PT_DYNAMIC segments found in '%s'. "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001497 "Segment at 0x%08x, previously one found at 0x%08x\n",
1498 pid, si->name, si->base + phdr->p_vaddr,
1499 (unsigned)si->dynamic);
1500 goto fail;
1501 }
1502 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
1503 si->dynamic = (unsigned *) (si->base + phdr->p_vaddr);
1504 }
1505 }
1506 }
1507
1508 if (si->dynamic == (unsigned *)-1) {
Dima Zavin2e855792009-05-20 18:28:09 -07001509 DL_ERR("%5d missing PT_DYNAMIC?!\n", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001510 goto fail;
1511 }
1512
1513 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1514
1515 /* extract useful information from dynamic section */
1516 for(d = si->dynamic; *d; d++){
1517 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1518 switch(*d++){
1519 case DT_HASH:
1520 si->nbucket = ((unsigned *) (si->base + *d))[0];
1521 si->nchain = ((unsigned *) (si->base + *d))[1];
1522 si->bucket = (unsigned *) (si->base + *d + 8);
1523 si->chain = (unsigned *) (si->base + *d + 8 + si->nbucket * 4);
1524 break;
1525 case DT_STRTAB:
1526 si->strtab = (const char *) (si->base + *d);
1527 break;
1528 case DT_SYMTAB:
1529 si->symtab = (Elf32_Sym *) (si->base + *d);
1530 break;
1531 case DT_PLTREL:
1532 if(*d != DT_REL) {
Dima Zavin2e855792009-05-20 18:28:09 -07001533 DL_ERR("DT_RELA not supported\n");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001534 goto fail;
1535 }
1536 break;
1537 case DT_JMPREL:
1538 si->plt_rel = (Elf32_Rel*) (si->base + *d);
1539 break;
1540 case DT_PLTRELSZ:
1541 si->plt_rel_count = *d / 8;
1542 break;
1543 case DT_REL:
1544 si->rel = (Elf32_Rel*) (si->base + *d);
1545 break;
1546 case DT_RELSZ:
1547 si->rel_count = *d / 8;
1548 break;
1549 case DT_PLTGOT:
1550 /* Save this in case we decide to do lazy binding. We don't yet. */
1551 si->plt_got = (unsigned *)(si->base + *d);
1552 break;
1553 case DT_DEBUG:
1554 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1555 *d = (int) &_r_debug;
1556 break;
1557 case DT_RELA:
Dima Zavin2e855792009-05-20 18:28:09 -07001558 DL_ERR("%5d DT_RELA not supported\n", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001559 goto fail;
1560 case DT_INIT:
1561 si->init_func = (void (*)(void))(si->base + *d);
1562 DEBUG("%5d %s constructors (init func) found at %p\n",
1563 pid, si->name, si->init_func);
1564 break;
1565 case DT_FINI:
1566 si->fini_func = (void (*)(void))(si->base + *d);
1567 DEBUG("%5d %s destructors (fini func) found at %p\n",
1568 pid, si->name, si->fini_func);
1569 break;
1570 case DT_INIT_ARRAY:
1571 si->init_array = (unsigned *)(si->base + *d);
1572 DEBUG("%5d %s constructors (init_array) found at %p\n",
1573 pid, si->name, si->init_array);
1574 break;
1575 case DT_INIT_ARRAYSZ:
1576 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1577 break;
1578 case DT_FINI_ARRAY:
1579 si->fini_array = (unsigned *)(si->base + *d);
1580 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1581 pid, si->name, si->fini_array);
1582 break;
1583 case DT_FINI_ARRAYSZ:
1584 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1585 break;
1586 case DT_PREINIT_ARRAY:
1587 si->preinit_array = (unsigned *)(si->base + *d);
1588 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1589 pid, si->name, si->preinit_array);
1590 break;
1591 case DT_PREINIT_ARRAYSZ:
1592 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1593 break;
1594 case DT_TEXTREL:
1595 /* TODO: make use of this. */
1596 /* this means that we might have to write into where the text
1597 * segment was loaded during relocation... Do something with
1598 * it.
1599 */
1600 DEBUG("%5d Text segment should be writable during relocation.\n",
1601 pid);
1602 break;
1603 }
1604 }
1605
1606 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
1607 pid, si->base, si->strtab, si->symtab);
1608
1609 if((si->strtab == 0) || (si->symtab == 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -07001610 DL_ERR("%5d missing essential tables\n", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001611 goto fail;
1612 }
1613
1614 for(d = si->dynamic; *d; d += 2) {
1615 if(d[0] == DT_NEEDED){
1616 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001617 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001618 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001619 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
1620 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)\n",
1621 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001622 goto fail;
1623 }
1624 lsi->refcount++;
1625 }
1626 }
1627
1628 if(si->plt_rel) {
1629 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1630 if(reloc_library(si, si->plt_rel, si->plt_rel_count))
1631 goto fail;
1632 }
1633 if(si->rel) {
1634 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1635 if(reloc_library(si, si->rel, si->rel_count))
1636 goto fail;
1637 }
1638
1639 si->flags |= FLAG_LINKED;
1640 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1641
1642#if 0
1643 /* This is the way that the old dynamic linker did protection of
1644 * non-writable areas. It would scan section headers and find where
1645 * .text ended (rather where .data/.bss began) and assume that this is
1646 * the upper range of the non-writable area. This is too coarse,
1647 * and is kept here for reference until we fully move away from single
1648 * segment elf objects. See the code in get_wr_offset (also #if'd 0)
1649 * that made this possible.
1650 */
1651 if(wr_offset < 0xffffffff){
1652 mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC);
1653 }
1654#else
1655 /* TODO: Verify that this does the right thing in all cases, as it
1656 * presently probably does not. It is possible that an ELF image will
1657 * come with multiple read-only segments. What we ought to do is scan
1658 * the program headers again and mprotect all the read-only segments.
1659 * To prevent re-scanning the program header, we would have to build a
1660 * list of loadable segments in si, and then scan that instead. */
1661 if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) {
1662 mprotect((void *)si->wrprotect_start,
1663 si->wrprotect_end - si->wrprotect_start,
1664 PROT_READ | PROT_EXEC);
1665 }
1666#endif
1667
1668 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1669 stdout and stderr to close a security hole described in:
1670
1671 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1672
1673 */
1674 if (getuid() != geteuid() || getgid() != getegid())
1675 nullify_closed_stdio ();
1676 call_constructors(si);
1677 notify_gdb_of_load(si);
1678 return 0;
1679
1680fail:
1681 ERROR("failed to link %s\n", si->name);
1682 si->flags |= FLAG_ERROR;
1683 return -1;
1684}
1685
1686int main(int argc, char **argv)
1687{
1688 return 0;
1689}
1690
1691#define ANDROID_TLS_SLOTS BIONIC_TLS_SLOTS
1692
1693static void * __tls_area[ANDROID_TLS_SLOTS];
1694
1695unsigned __linker_init(unsigned **elfdata)
1696{
1697 static soinfo linker_soinfo;
1698
1699 int argc = (int) *elfdata;
1700 char **argv = (char**) (elfdata + 1);
1701 unsigned *vecs = (unsigned*) (argv + argc + 1);
1702 soinfo *si;
1703 struct link_map * map;
1704
1705 pid = getpid();
1706
1707#if TIMING
1708 struct timeval t0, t1;
1709 gettimeofday(&t0, 0);
1710#endif
1711
1712 __set_tls(__tls_area);
1713 ((unsigned *)__get_tls())[TLS_SLOT_THREAD_ID] = gettid();
1714
1715 debugger_init();
1716
1717 /* skip past the environment */
1718 while(vecs[0] != 0) {
1719 if(!strncmp((char*) vecs[0], "DEBUG=", 6)) {
1720 debug_verbosity = atoi(((char*) vecs[0]) + 6);
1721 }
1722 vecs++;
1723 }
1724 vecs++;
1725
1726 INFO("[ android linker & debugger ]\n");
1727 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
1728
1729 si = alloc_info(argv[0]);
1730 if(si == 0) {
1731 exit(-1);
1732 }
1733
1734 /* bootstrap the link map, the main exe always needs to be first */
1735 si->flags |= FLAG_EXE;
1736 map = &(si->linkmap);
1737
1738 map->l_addr = 0;
1739 map->l_name = argv[0];
1740 map->l_prev = NULL;
1741 map->l_next = NULL;
1742
1743 _r_debug.r_map = map;
1744 r_debug_tail = map;
1745
1746 /* gdb expects the linker to be in the debug shared object list,
1747 * and we need to make sure that the reported load address is zero.
1748 * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
1749 * is. Don't use alloc_info(), because the linker shouldn't
1750 * be on the soinfo list.
1751 */
1752 strcpy((char*) linker_soinfo.name, "/system/bin/linker");
1753 linker_soinfo.flags = 0;
1754 linker_soinfo.base = 0; // This is the important part; must be zero.
1755 insert_soinfo_into_debug_map(&linker_soinfo);
1756
1757 /* extract information passed from the kernel */
1758 while(vecs[0] != 0){
1759 switch(vecs[0]){
1760 case AT_PHDR:
1761 si->phdr = (Elf32_Phdr*) vecs[1];
1762 break;
1763 case AT_PHNUM:
1764 si->phnum = (int) vecs[1];
1765 break;
1766 case AT_ENTRY:
1767 si->entry = vecs[1];
1768 break;
1769 }
1770 vecs += 2;
1771 }
1772
1773 ba_init();
1774
1775 si->base = 0;
1776 si->dynamic = (unsigned *)-1;
1777 si->wrprotect_start = 0xffffffff;
1778 si->wrprotect_end = 0;
1779
Dima Zavin2e855792009-05-20 18:28:09 -07001780 if(link_image(si, 0)) {
1781 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
1782 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
1783 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001784 exit(-1);
1785 }
1786
1787#if TIMING
1788 gettimeofday(&t1,NULL);
1789 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
1790 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1791 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1792 ));
1793#endif
1794#if STATS
1795 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
1796 linker_stats.reloc[RELOC_ABSOLUTE],
1797 linker_stats.reloc[RELOC_RELATIVE],
1798 linker_stats.reloc[RELOC_COPY],
1799 linker_stats.reloc[RELOC_SYMBOL]);
1800#endif
1801#if COUNT_PAGES
1802 {
1803 unsigned n;
1804 unsigned i;
1805 unsigned count = 0;
1806 for(n = 0; n < 4096; n++){
1807 if(bitmask[n]){
1808 unsigned x = bitmask[n];
1809 for(i = 0; i < 8; i++){
1810 if(x & 1) count++;
1811 x >>= 1;
1812 }
1813 }
1814 }
1815 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
1816 }
1817#endif
1818
1819#if TIMING || STATS || COUNT_PAGES
1820 fflush(stdout);
1821#endif
1822
1823 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
1824 si->entry);
1825 return si->entry;
1826}