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