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