blob: a1f4fffb75a17398bf30eaef716c0a6738a62660 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Doug Kwan94304352009-10-23 18:11:40 -07002 * Copyright (C) 2008, 2009 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
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"
David 'Digit' Turner5c734642010-01-20 12:36:51 -080051#include "linker_format.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
53#include "ba.h"
54
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070055#define ALLOW_SYMBOLS_FROM_MAIN 1
James Dongba52b302009-04-30 20:37:36 -070056#define SO_MAX 96
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057
David Bartleybc3a5c22009-06-02 18:27:28 -070058/* Assume average path length of 64 and max 8 paths */
59#define LDPATH_BUFSIZE 512
60#define LDPATH_MAX 8
61
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
63 *
64 * Do NOT use malloc() and friends or pthread_*() code here.
65 * Don't use printf() either; it's caused mysterious memory
66 * corruption in the past.
67 * The linker runs before we bring up libc and it's easiest
68 * to make sure it does not depend on any complex libc features
69 *
70 * open issues / todo:
71 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072 * - are we doing everything we should for ARM_COPY relocations?
73 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074 * - after linking, set as much stuff as possible to READONLY
75 * and NOEXEC
76 * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
77 * headers provide versions that are negative...
78 * - allocate space for soinfo structs dynamically instead of
79 * having a hard limit (64)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080*/
81
82
83static int link_image(soinfo *si, unsigned wr_offset);
84
85static int socount = 0;
86static soinfo sopool[SO_MAX];
87static soinfo *freelist = NULL;
88static soinfo *solist = &libdl_info;
89static soinfo *sonext = &libdl_info;
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070090#if ALLOW_SYMBOLS_FROM_MAIN
91static soinfo *somain; /* main process, always the one after libdl_info */
92#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
Iliyan Malchevaf7315a2009-10-16 17:50:42 -070094
Iliyan Malcheve100f522010-02-10 15:19:37 -080095/* Set up for the buddy allocator managing the non-prelinked libraries. */
96static struct ba_bits ba_nonprelink_bitmap[(LIBLAST - LIBBASE) / LIBINC];
97static struct ba ba_nonprelink = {
Iliyan Malchevaf7315a2009-10-16 17:50:42 -070098 .base = LIBBASE,
99 .size = LIBLAST - LIBBASE,
100 .min_alloc = LIBINC,
Iliyan Malchevbb9eede2009-10-19 14:25:17 -0700101 /* max_order will be determined automatically */
Iliyan Malcheve100f522010-02-10 15:19:37 -0800102 .bitmap = ba_nonprelink_bitmap,
103 .num_entries = sizeof(ba_nonprelink_bitmap)/sizeof(ba_nonprelink_bitmap[0]),
Iliyan Malchevaf7315a2009-10-16 17:50:42 -0700104};
105
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700106static inline int validate_soinfo(soinfo *si)
107{
108 return (si >= sopool && si < sopool + SO_MAX) ||
109 si == &libdl_info;
110}
111
David Bartleybc3a5c22009-06-02 18:27:28 -0700112static char ldpaths_buf[LDPATH_BUFSIZE];
113static const char *ldpaths[LDPATH_MAX + 1];
114
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115int debug_verbosity;
116static int pid;
117
118#if STATS
119struct _link_stats linker_stats;
120#endif
121
122#if COUNT_PAGES
123unsigned bitmask[4096];
124#endif
125
126#ifndef PT_ARM_EXIDX
127#define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */
128#endif
129
Dima Zavin2e855792009-05-20 18:28:09 -0700130#define HOODLUM(name, ret, ...) \
131 ret name __VA_ARGS__ \
132 { \
133 char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \
134 write(2, errstr, sizeof(errstr)); \
135 abort(); \
136 }
137HOODLUM(malloc, void *, (size_t size));
138HOODLUM(free, void, (void *ptr));
139HOODLUM(realloc, void *, (void *ptr, size_t size));
140HOODLUM(calloc, void *, (size_t cnt, size_t size));
141
Dima Zavin03531952009-05-29 17:30:25 -0700142static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700143static char __linker_dl_err_buf[768];
144#define DL_ERR(fmt, x...) \
145 do { \
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800146 format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), \
Dima Zavin2e855792009-05-20 18:28:09 -0700147 "%s[%d]: " fmt, __func__, __LINE__, ##x); \
Erik Gillingd00d23a2009-07-22 17:06:11 -0700148 ERROR(fmt "\n", ##x); \
Dima Zavin2e855792009-05-20 18:28:09 -0700149 } while(0)
150
151const char *linker_get_error(void)
152{
153 return (const char *)&__linker_dl_err_buf[0];
154}
155
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800156/*
157 * This function is an empty stub where GDB locates a breakpoint to get notified
158 * about linker activity.
159 */
160extern void __attribute__((noinline)) rtld_db_dlactivity(void);
161
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
163 RT_CONSISTENT, 0};
164static struct link_map *r_debug_tail = 0;
165
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700166static pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167
168static void insert_soinfo_into_debug_map(soinfo * info)
169{
170 struct link_map * map;
171
172 /* Copy the necessary fields into the debug structure.
173 */
174 map = &(info->linkmap);
175 map->l_addr = info->base;
176 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800177 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178
179 /* Stick the new library at the end of the list.
180 * gdb tends to care more about libc than it does
181 * about leaf libraries, and ordering it this way
182 * reduces the back-and-forth over the wire.
183 */
184 if (r_debug_tail) {
185 r_debug_tail->l_next = map;
186 map->l_prev = r_debug_tail;
187 map->l_next = 0;
188 } else {
189 _r_debug.r_map = map;
190 map->l_prev = 0;
191 map->l_next = 0;
192 }
193 r_debug_tail = map;
194}
195
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700196static void remove_soinfo_from_debug_map(soinfo * info)
197{
198 struct link_map * map = &(info->linkmap);
199
200 if (r_debug_tail == map)
201 r_debug_tail = map->l_prev;
202
203 if (map->l_prev) map->l_prev->l_next = map->l_next;
204 if (map->l_next) map->l_next->l_prev = map->l_prev;
205}
206
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800207void notify_gdb_of_load(soinfo * info)
208{
209 if (info->flags & FLAG_EXE) {
210 // GDB already knows about the main executable
211 return;
212 }
213
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700214 pthread_mutex_lock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800215
216 _r_debug.r_state = RT_ADD;
217 rtld_db_dlactivity();
218
219 insert_soinfo_into_debug_map(info);
220
221 _r_debug.r_state = RT_CONSISTENT;
222 rtld_db_dlactivity();
223
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700224 pthread_mutex_unlock(&_r_debug_lock);
225}
226
227void notify_gdb_of_unload(soinfo * info)
228{
229 if (info->flags & FLAG_EXE) {
230 // GDB already knows about the main executable
231 return;
232 }
233
234 pthread_mutex_lock(&_r_debug_lock);
235
236 _r_debug.r_state = RT_DELETE;
237 rtld_db_dlactivity();
238
239 remove_soinfo_from_debug_map(info);
240
241 _r_debug.r_state = RT_CONSISTENT;
242 rtld_db_dlactivity();
243
244 pthread_mutex_unlock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245}
246
247void notify_gdb_of_libraries()
248{
249 _r_debug.r_state = RT_ADD;
250 rtld_db_dlactivity();
251 _r_debug.r_state = RT_CONSISTENT;
252 rtld_db_dlactivity();
253}
254
255static soinfo *alloc_info(const char *name)
256{
257 soinfo *si;
258
259 if(strlen(name) >= SOINFO_NAME_LEN) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700260 DL_ERR("%5d library name %s too long", pid, name);
Doug Kwan94304352009-10-23 18:11:40 -0700261 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800262 }
263
264 /* The freelist is populated when we call free_info(), which in turn is
265 done only by dlclose(), which is not likely to be used.
266 */
267 if (!freelist) {
268 if(socount == SO_MAX) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700269 DL_ERR("%5d too many libraries when loading %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800270 return NULL;
271 }
272 freelist = sopool + socount++;
273 freelist->next = NULL;
274 }
275
276 si = freelist;
277 freelist = freelist->next;
278
279 /* Make sure we get a clean block of soinfo */
280 memset(si, 0, sizeof(soinfo));
281 strcpy((char*) si->name, name);
282 sonext->next = si;
283 si->ba_index = -1; /* by default, prelinked */
284 si->next = NULL;
285 si->refcount = 0;
286 sonext = si;
287
288 TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
289 return si;
290}
291
292static void free_info(soinfo *si)
293{
294 soinfo *prev = NULL, *trav;
295
296 TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
297
298 for(trav = solist; trav != NULL; trav = trav->next){
299 if (trav == si)
300 break;
301 prev = trav;
302 }
303 if (trav == NULL) {
304 /* si was not ni solist */
Erik Gillingd00d23a2009-07-22 17:06:11 -0700305 DL_ERR("%5d name %s is not in solist!", pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800306 return;
307 }
308
309 /* prev will never be NULL, because the first entry in solist is
310 always the static libdl_info.
311 */
312 prev->next = si->next;
313 if (si == sonext) sonext = prev;
314 si->next = freelist;
315 freelist = si;
316}
317
318#ifndef LINKER_TEXT_BASE
319#error "linker's makefile must define LINKER_TEXT_BASE"
320#endif
321#ifndef LINKER_AREA_SIZE
322#error "linker's makefile must define LINKER_AREA_SIZE"
323#endif
324#define LINKER_BASE ((LINKER_TEXT_BASE) & 0xfff00000)
325#define LINKER_TOP (LINKER_BASE + (LINKER_AREA_SIZE))
326
327const char *addr_to_name(unsigned addr)
328{
329 soinfo *si;
330
331 for(si = solist; si != 0; si = si->next){
332 if((addr >= si->base) && (addr < (si->base + si->size))) {
333 return si->name;
334 }
335 }
336
337 if((addr >= LINKER_BASE) && (addr < LINKER_TOP)){
338 return "linker";
339 }
340
341 return "";
342}
343
344/* For a given PC, find the .so that it belongs to.
345 * Returns the base address of the .ARM.exidx section
346 * for that .so, and the number of 8-byte entries
347 * in that section (via *pcount).
348 *
349 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
350 *
351 * This function is exposed via dlfcn.c and libdl.so.
352 */
353#ifdef ANDROID_ARM_LINKER
354_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
355{
356 soinfo *si;
357 unsigned addr = (unsigned)pc;
358
359 if ((addr < LINKER_BASE) || (addr >= LINKER_TOP)) {
360 for (si = solist; si != 0; si = si->next){
361 if ((addr >= si->base) && (addr < (si->base + si->size))) {
362 *pcount = si->ARM_exidx_count;
363 return (_Unwind_Ptr)(si->base + (unsigned long)si->ARM_exidx);
364 }
365 }
366 }
367 *pcount = 0;
368 return NULL;
369}
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900370#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_SH_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800371/* Here, we only have to provide a callback to iterate across all the
372 * loaded libraries. gcc_eh does the rest. */
373int
374dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
375 void *data)
376{
377 soinfo *si;
378 struct dl_phdr_info dl_info;
379 int rv = 0;
380
381 for (si = solist; si != NULL; si = si->next) {
382 dl_info.dlpi_addr = si->linkmap.l_addr;
383 dl_info.dlpi_name = si->linkmap.l_name;
384 dl_info.dlpi_phdr = si->phdr;
385 dl_info.dlpi_phnum = si->phnum;
386 rv = cb(&dl_info, sizeof (struct dl_phdr_info), data);
387 if (rv != 0)
388 break;
389 }
390 return rv;
391}
392#endif
393
394static Elf32_Sym *_elf_lookup(soinfo *si, unsigned hash, const char *name)
395{
396 Elf32_Sym *s;
397 Elf32_Sym *symtab = si->symtab;
398 const char *strtab = si->strtab;
399 unsigned n;
400
401 TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
402 name, si->name, si->base, hash, hash % si->nbucket);
403 n = hash % si->nbucket;
404
405 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
406 s = symtab + n;
407 if(strcmp(strtab + s->st_name, name)) continue;
408
Doug Kwane8238072009-10-26 12:05:23 -0700409 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800410 switch(ELF32_ST_BIND(s->st_info)){
411 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700412 case STB_WEAK:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800413 /* no section == undefined */
414 if(s->st_shndx == 0) continue;
415
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416 TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
417 name, si->name, s->st_value, s->st_size);
418 return s;
419 }
420 }
421
Doug Kwan94304352009-10-23 18:11:40 -0700422 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800423}
424
425static unsigned elfhash(const char *_name)
426{
427 const unsigned char *name = (const unsigned char *) _name;
428 unsigned h = 0, g;
429
430 while(*name) {
431 h = (h << 4) + *name++;
432 g = h & 0xf0000000;
433 h ^= g;
434 h ^= g >> 24;
435 }
436 return h;
437}
438
439static Elf32_Sym *
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700440_do_lookup(soinfo *si, const char *name, unsigned *base)
441{
Doug Kwan94304352009-10-23 18:11:40 -0700442 unsigned elf_hash = elfhash(name);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700443 Elf32_Sym *s;
444 unsigned *d;
445 soinfo *lsi = si;
446
447 /* Look for symbols in the local scope first (the object who is
448 * searching). This happens with C++ templates on i386 for some
Doug Kwane8238072009-10-26 12:05:23 -0700449 * reason.
450 *
451 * Notes on weak symbols:
452 * The ELF specs are ambigious about treatment of weak definitions in
453 * dynamic linking. Some systems return the first definition found
454 * and some the first non-weak definition. This is system dependent.
455 * Here we return the first definition found for simplicity. */
Doug Kwan94304352009-10-23 18:11:40 -0700456 s = _elf_lookup(si, elf_hash, name);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700457 if(s != NULL)
458 goto done;
459
460 for(d = si->dynamic; *d; d += 2) {
461 if(d[0] == DT_NEEDED){
462 lsi = (soinfo *)d[1];
463 if (!validate_soinfo(lsi)) {
464 DL_ERR("%5d bad DT_NEEDED pointer in %s",
465 pid, si->name);
Doug Kwan94304352009-10-23 18:11:40 -0700466 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700467 }
468
469 DEBUG("%5d %s: looking up %s in %s\n",
470 pid, si->name, name, lsi->name);
Doug Kwan94304352009-10-23 18:11:40 -0700471 s = _elf_lookup(lsi, elf_hash, name);
Min-su, Kim3cab22c2010-01-19 10:05:33 +0900472 if ((s != NULL) && (s->st_shndx != SHN_UNDEF))
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700473 goto done;
474 }
475 }
476
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -0700477#if ALLOW_SYMBOLS_FROM_MAIN
478 /* If we are resolving relocations while dlopen()ing a library, it's OK for
479 * the library to resolve a symbol that's defined in the executable itself,
480 * although this is rare and is generally a bad idea.
481 */
482 if (somain) {
483 lsi = somain;
484 DEBUG("%5d %s: looking up %s in executable %s\n",
485 pid, si->name, name, lsi->name);
Doug Kwan94304352009-10-23 18:11:40 -0700486 s = _elf_lookup(lsi, elf_hash, name);
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -0700487 }
488#endif
489
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700490done:
491 if(s != NULL) {
492 TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
493 "found in %s, base = 0x%08x\n",
494 pid, si->name, name, s->st_value, lsi->name, lsi->base);
495 *base = lsi->base;
496 return s;
497 }
498
Doug Kwan94304352009-10-23 18:11:40 -0700499 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700500}
501
502/* This is used by dl_sym(). It performs symbol lookup only within the
503 specified soinfo object and not in any of its dependencies.
504 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800505Elf32_Sym *lookup_in_library(soinfo *si, const char *name)
506{
Doug Kwan94304352009-10-23 18:11:40 -0700507 return _elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800508}
509
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700510/* This is used by dl_sym(). It performs a global symbol lookup.
511 */
Matt Fischer1698d9e2009-12-31 12:17:56 -0600512Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800513{
Doug Kwan94304352009-10-23 18:11:40 -0700514 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800515 Elf32_Sym *s = NULL;
516 soinfo *si;
517
Matt Fischer1698d9e2009-12-31 12:17:56 -0600518 if(start == NULL) {
519 start = solist;
520 }
521
522 for(si = start; (s == NULL) && (si != NULL); si = si->next)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800523 {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700524 if(si->flags & FLAG_ERROR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800525 continue;
Doug Kwane8238072009-10-26 12:05:23 -0700526 s = _elf_lookup(si, elf_hash, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800527 if (s != NULL) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700528 *found = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800529 break;
530 }
531 }
532
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700533 if(s != NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534 TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
535 "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
536 return s;
537 }
538
Doug Kwan94304352009-10-23 18:11:40 -0700539 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800540}
541
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600542soinfo *find_containing_library(void *addr)
543{
544 soinfo *si;
545
546 for(si = solist; si != NULL; si = si->next)
547 {
548 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
549 return si;
550 }
551 }
552
553 return NULL;
554}
555
556Elf32_Sym *find_containing_symbol(void *addr, soinfo *si)
557{
558 unsigned int i;
559 unsigned soaddr = (unsigned)addr - si->base;
560
561 /* Search the library's symbol table for any defined symbol which
562 * contains this address */
563 for(i=0; i<si->nchain; i++) {
564 Elf32_Sym *sym = &si->symtab[i];
565
566 if(sym->st_shndx != SHN_UNDEF &&
567 soaddr >= sym->st_value &&
568 soaddr < sym->st_value + sym->st_size) {
569 return sym;
570 }
571 }
572
573 return NULL;
574}
575
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800576#if 0
577static void dump(soinfo *si)
578{
579 Elf32_Sym *s = si->symtab;
580 unsigned n;
581
582 for(n = 0; n < si->nchain; n++) {
583 TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
584 s->st_info, s->st_shndx, s->st_value, s->st_size,
585 si->strtab + s->st_name);
586 s++;
587 }
588}
589#endif
590
591static const char *sopaths[] = {
592 "/system/lib",
593 "/lib",
594 0
595};
596
597static int _open_lib(const char *name)
598{
599 int fd;
600 struct stat filestat;
601
602 if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
603 if ((fd = open(name, O_RDONLY)) >= 0)
604 return fd;
605 }
606
607 return -1;
608}
609
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800610static int open_library(const char *name)
611{
612 int fd;
613 char buf[512];
614 const char **path;
David Bartleybc3a5c22009-06-02 18:27:28 -0700615 int n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800616
617 TRACE("[ %5d opening %s ]\n", pid, name);
618
619 if(name == 0) return -1;
620 if(strlen(name) > 256) return -1;
621
622 if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
623 return fd;
624
David Bartleybc3a5c22009-06-02 18:27:28 -0700625 for (path = ldpaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800626 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700627 if (n < 0 || n >= (int)sizeof(buf)) {
628 WARN("Ignoring very long library path: %s/%s\n", *path, name);
629 continue;
630 }
631 if ((fd = _open_lib(buf)) >= 0)
632 return fd;
633 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800634 for (path = sopaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800635 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700636 if (n < 0 || n >= (int)sizeof(buf)) {
637 WARN("Ignoring very long library path: %s/%s\n", *path, name);
638 continue;
639 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800640 if ((fd = _open_lib(buf)) >= 0)
641 return fd;
642 }
643
644 return -1;
645}
646
647/* temporary space for holding the first page of the shared lib
648 * which contains the elf header (with the pht). */
649static unsigned char __header[PAGE_SIZE];
650
651typedef struct {
652 long mmap_addr;
653 char tag[4]; /* 'P', 'R', 'E', ' ' */
654} prelink_info_t;
655
656/* Returns the requested base address if the library is prelinked,
657 * and 0 otherwise. */
658static unsigned long
659is_prelinked(int fd, const char *name)
660{
661 off_t sz;
662 prelink_info_t info;
663
664 sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
665 if (sz < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700666 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800667 return 0;
668 }
669
670 if (read(fd, &info, sizeof(info)) != sizeof(info)) {
671 WARN("Could not read prelink_info_t structure for `%s`\n", name);
672 return 0;
673 }
674
675 if (strncmp(info.tag, "PRE ", 4)) {
676 WARN("`%s` is not a prelinked library\n", name);
677 return 0;
678 }
679
680 return (unsigned long)info.mmap_addr;
681}
682
683/* verify_elf_object
684 * Verifies if the object @ base is a valid ELF object
685 *
686 * Args:
687 *
688 * Returns:
689 * 0 on success
690 * -1 if no valid ELF object is found @ base.
691 */
692static int
693verify_elf_object(void *base, const char *name)
694{
695 Elf32_Ehdr *hdr = (Elf32_Ehdr *) base;
696
697 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
698 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
699 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
700 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
701
702 /* TODO: Should we verify anything else in the header? */
703
704 return 0;
705}
706
707
708/* get_lib_extents
709 * Retrieves the base (*base) address where the ELF object should be
710 * mapped and its overall memory size (*total_sz).
711 *
712 * Args:
713 * fd: Opened file descriptor for the library
714 * name: The name of the library
715 * _hdr: Pointer to the header page of the library
716 * total_sz: Total size of the memory that should be allocated for
717 * this library
718 *
719 * Returns:
720 * -1 if there was an error while trying to get the lib extents.
721 * The possible reasons are:
722 * - Could not determine if the library was prelinked.
723 * - The library provided is not a valid ELF object
724 * 0 if the library did not request a specific base offset (normal
725 * for non-prelinked libs)
726 * > 0 if the library requests a specific address to be mapped to.
727 * This indicates a pre-linked library.
728 */
729static unsigned
730get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz)
731{
732 unsigned req_base;
733 unsigned min_vaddr = 0xffffffff;
734 unsigned max_vaddr = 0;
735 unsigned char *_hdr = (unsigned char *)__hdr;
736 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)_hdr;
737 Elf32_Phdr *phdr;
738 int cnt;
739
740 TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name);
741 if (verify_elf_object(_hdr, name) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700742 DL_ERR("%5d - %s is not a valid ELF object", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800743 return (unsigned)-1;
744 }
745
746 req_base = (unsigned) is_prelinked(fd, name);
747 if (req_base == (unsigned)-1)
748 return -1;
749 else if (req_base != 0) {
750 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
751 pid, name, req_base);
752 } else {
753 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
754 }
755
756 phdr = (Elf32_Phdr *)(_hdr + ehdr->e_phoff);
757
758 /* find the min/max p_vaddrs from all the PT_LOAD segments so we can
759 * get the range. */
760 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
761 if (phdr->p_type == PT_LOAD) {
762 if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr)
763 max_vaddr = phdr->p_vaddr + phdr->p_memsz;
764 if (phdr->p_vaddr < min_vaddr)
765 min_vaddr = phdr->p_vaddr;
766 }
767 }
768
769 if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700770 DL_ERR("%5d - No loadable segments found in %s.", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800771 return (unsigned)-1;
772 }
773
774 /* truncate min_vaddr down to page boundary */
775 min_vaddr &= ~PAGE_MASK;
776
777 /* round max_vaddr up to the next page */
778 max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK;
779
780 *total_sz = (max_vaddr - min_vaddr);
781 return (unsigned)req_base;
782}
783
784/* alloc_mem_region
785 *
786 * This function reserves a chunk of memory to be used for mapping in
787 * the shared library. We reserve the entire memory region here, and
788 * then the rest of the linker will relocate the individual loadable
789 * segments into the correct locations within this memory range.
790 *
791 * Args:
792 * si->base: The requested base of the allocation. If 0, a sane one will be
793 * chosen in the range LIBBASE <= base < LIBLAST.
794 * si->size: The size of the allocation.
795 *
796 * Returns:
797 * -1 on failure, and 0 on success. On success, si->base will contain
798 * the virtual address at which the library will be mapped.
799 */
800
801static int reserve_mem_region(soinfo *si)
802{
803 void *base = mmap((void *)si->base, si->size, PROT_READ | PROT_EXEC,
804 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
805 if (base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700806 DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700807 "as requested, will try general pool: %d (%s)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800808 pid, (si->base ? "" : "non-"), si->name, si->base,
809 errno, strerror(errno));
810 return -1;
811 } else if (base != (void *)si->base) {
Dima Zavin2e855792009-05-20 18:28:09 -0700812 DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700813 "not at 0x%08x", pid, (si->base ? "" : "non-"),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800814 si->name, (unsigned)base, si->base);
815 munmap(base, si->size);
816 return -1;
817 }
818 return 0;
819}
820
821static int
822alloc_mem_region(soinfo *si)
823{
824 if (si->base) {
825 /* Attempt to mmap a prelinked library. */
826 si->ba_index = -1;
827 return reserve_mem_region(si);
828 }
829
830 /* This is not a prelinked library, so we attempt to allocate space
831 for it from the buddy allocator, which manages the area between
832 LIBBASE and LIBLAST.
833 */
Iliyan Malcheve100f522010-02-10 15:19:37 -0800834 si->ba_index = ba_allocate(&ba_nonprelink, si->size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800835 if(si->ba_index >= 0) {
Iliyan Malcheve100f522010-02-10 15:19:37 -0800836 si->base = ba_start_addr(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800837 PRINT("%5d mapping library '%s' at %08x (index %d) " \
838 "through buddy allocator.\n",
839 pid, si->name, si->base, si->ba_index);
840 if (reserve_mem_region(si) < 0) {
Iliyan Malcheve100f522010-02-10 15:19:37 -0800841 ba_free(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800842 si->ba_index = -1;
843 si->base = 0;
844 goto err;
845 }
846 return 0;
847 }
848
849err:
Erik Gillingd00d23a2009-07-22 17:06:11 -0700850 DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800851 pid, si->name);
852 return -1;
853}
854
855#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
856#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
857 MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
858 MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
859/* load_segments
860 *
861 * This function loads all the loadable (PT_LOAD) segments into memory
862 * at their appropriate memory offsets off the base address.
863 *
864 * Args:
865 * fd: Open file descriptor to the library to load.
866 * header: Pointer to a header page that contains the ELF header.
867 * This is needed since we haven't mapped in the real file yet.
868 * si: ptr to soinfo struct describing the shared object.
869 *
870 * Returns:
871 * 0 on success, -1 on failure.
872 */
873static int
874load_segments(int fd, void *header, soinfo *si)
875{
876 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
877 Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned char *)header + ehdr->e_phoff);
878 unsigned char *base = (unsigned char *)si->base;
879 int cnt;
880 unsigned len;
881 unsigned char *tmp;
882 unsigned char *pbase;
883 unsigned char *extra_base;
884 unsigned extra_len;
885 unsigned total_sz = 0;
886
887 si->wrprotect_start = 0xffffffff;
888 si->wrprotect_end = 0;
889
890 TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n",
891 pid, si->name, (unsigned)si->base);
892 /* Now go through all the PT_LOAD segments and map them into memory
893 * at the appropriate locations. */
894 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
895 if (phdr->p_type == PT_LOAD) {
896 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
897 /* we want to map in the segment on a page boundary */
898 tmp = base + (phdr->p_vaddr & (~PAGE_MASK));
899 /* add the # of bytes we masked off above to the total length. */
900 len = phdr->p_filesz + (phdr->p_vaddr & PAGE_MASK);
901
902 TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x "
903 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name,
904 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
905 pbase = mmap(tmp, len, PFLAGS_TO_PROT(phdr->p_flags),
906 MAP_PRIVATE | MAP_FIXED, fd,
907 phdr->p_offset & (~PAGE_MASK));
908 if (pbase == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700909 DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700910 "p_vaddr=0x%08x p_offset=0x%08x", pid, si->name,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800911 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
912 goto fail;
913 }
914
915 /* If 'len' didn't end on page boundary, and it's a writable
916 * segment, zero-fill the rest. */
917 if ((len & PAGE_MASK) && (phdr->p_flags & PF_W))
918 memset((void *)(pbase + len), 0, PAGE_SIZE - (len & PAGE_MASK));
919
920 /* Check to see if we need to extend the map for this segment to
921 * cover the diff between filesz and memsz (i.e. for bss).
922 *
923 * base _+---------------------+ page boundary
924 * . .
925 * | |
926 * . .
927 * pbase _+---------------------+ page boundary
928 * | |
929 * . .
930 * base + p_vaddr _| |
931 * . \ \ .
932 * . | filesz | .
933 * pbase + len _| / | |
934 * <0 pad> . . .
935 * extra_base _+------------|--------+ page boundary
936 * / . . .
937 * | . . .
938 * | +------------|--------+ page boundary
939 * extra_len-> | | | |
940 * | . | memsz .
941 * | . | .
942 * \ _| / |
943 * . .
944 * | |
945 * _+---------------------+ page boundary
946 */
947 tmp = (unsigned char *)(((unsigned)pbase + len + PAGE_SIZE - 1) &
948 (~PAGE_MASK));
949 if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) {
950 extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp;
951 TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x "
952 "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len);
953 /* map in the extra page(s) as anonymous into the range.
954 * This is probably not necessary as we already mapped in
955 * the entire region previously, but we just want to be
956 * sure. This will also set the right flags on the region
957 * (though we can probably accomplish the same thing with
958 * mprotect).
959 */
960 extra_base = mmap((void *)tmp, extra_len,
961 PFLAGS_TO_PROT(phdr->p_flags),
962 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
963 -1, 0);
964 if (extra_base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700965 DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x"
Erik Gillingd00d23a2009-07-22 17:06:11 -0700966 " (0x%08x) ]", pid, si->name, (unsigned)tmp,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800967 extra_len);
968 goto fail;
969 }
970 /* TODO: Check if we need to memset-0 this region.
971 * Anonymous mappings are zero-filled copy-on-writes, so we
972 * shouldn't need to. */
973 TRACE("[ %5d - Segment from '%s' extended @ 0x%08x "
974 "(0x%08x)\n", pid, si->name, (unsigned)extra_base,
975 extra_len);
976 }
977 /* set the len here to show the full extent of the segment we
978 * just loaded, mostly for debugging */
979 len = (((unsigned)base + phdr->p_vaddr + phdr->p_memsz +
980 PAGE_SIZE - 1) & (~PAGE_MASK)) - (unsigned)pbase;
981 TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x "
982 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
983 (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset);
984 total_sz += len;
985 /* Make the section writable just in case we'll have to write to
986 * it during relocation (i.e. text segment). However, we will
987 * remember what range of addresses should be write protected.
988 *
989 */
990 if (!(phdr->p_flags & PF_W)) {
991 if ((unsigned)pbase < si->wrprotect_start)
992 si->wrprotect_start = (unsigned)pbase;
993 if (((unsigned)pbase + len) > si->wrprotect_end)
994 si->wrprotect_end = (unsigned)pbase + len;
995 mprotect(pbase, len,
996 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
997 }
998 } else if (phdr->p_type == PT_DYNAMIC) {
999 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
1000 /* this segment contains the dynamic linking information */
1001 si->dynamic = (unsigned *)(base + phdr->p_vaddr);
1002 } else {
1003#ifdef ANDROID_ARM_LINKER
1004 if (phdr->p_type == PT_ARM_EXIDX) {
1005 DEBUG_DUMP_PHDR(phdr, "PT_ARM_EXIDX", pid);
1006 /* exidx entries (used for stack unwinding) are 8 bytes each.
1007 */
1008 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1009 si->ARM_exidx_count = phdr->p_memsz / 8;
1010 }
1011#endif
1012 }
1013
1014 }
1015
1016 /* Sanity check */
1017 if (total_sz > si->size) {
Dima Zavin2e855792009-05-20 18:28:09 -07001018 DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001019 "greater than what was allocated (0x%08x). THIS IS BAD!",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001020 pid, total_sz, si->name, si->size);
1021 goto fail;
1022 }
1023
1024 TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. "
1025 "Total memory footprint: 0x%08x bytes ]\n", pid, si->name,
1026 (unsigned)si->base, si->size);
1027 return 0;
1028
1029fail:
1030 /* We can just blindly unmap the entire region even though some things
1031 * were mapped in originally with anonymous and others could have been
1032 * been mapped in from the file before we failed. The kernel will unmap
1033 * all the pages in the range, irrespective of how they got there.
1034 */
1035 munmap((void *)si->base, si->size);
1036 si->flags |= FLAG_ERROR;
1037 return -1;
1038}
1039
1040/* TODO: Implement this to take care of the fact that Android ARM
1041 * ELF objects shove everything into a single loadable segment that has the
1042 * write bit set. wr_offset is then used to set non-(data|bss) pages to be
1043 * non-writable.
1044 */
1045#if 0
1046static unsigned
1047get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr)
1048{
1049 Elf32_Shdr *shdr_start;
1050 Elf32_Shdr *shdr;
1051 int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr);
1052 int cnt;
1053 unsigned wr_offset = 0xffffffff;
1054
1055 shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd,
1056 ehdr->e_shoff & (~PAGE_MASK));
1057 if (shdr_start == MAP_FAILED) {
1058 WARN("%5d - Could not read section header info from '%s'. Will not "
1059 "not be able to determine write-protect offset.\n", pid, name);
1060 return (unsigned)-1;
1061 }
1062
1063 for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) {
1064 if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) &&
1065 (shdr->sh_addr < wr_offset)) {
1066 wr_offset = shdr->sh_addr;
1067 }
1068 }
1069
1070 munmap(shdr_start, shdr_sz);
1071 return wr_offset;
1072}
1073#endif
1074
1075static soinfo *
1076load_library(const char *name)
1077{
1078 int fd = open_library(name);
1079 int cnt;
1080 unsigned ext_sz;
1081 unsigned req_base;
Erik Gillingfde86422009-07-28 20:28:19 -07001082 const char *bname;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001083 soinfo *si = NULL;
1084 Elf32_Ehdr *hdr;
1085
Dima Zavin2e855792009-05-20 18:28:09 -07001086 if(fd == -1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001087 DL_ERR("Library '%s' not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001088 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -07001089 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001090
1091 /* We have to read the ELF header to figure out what to do with this image
1092 */
1093 if (lseek(fd, 0, SEEK_SET) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001094 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001095 goto fail;
1096 }
1097
1098 if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001099 DL_ERR("read() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001100 goto fail;
1101 }
1102
1103 /* Parse the ELF header and get the size of the memory footprint for
1104 * the library */
1105 req_base = get_lib_extents(fd, name, &__header[0], &ext_sz);
1106 if (req_base == (unsigned)-1)
1107 goto fail;
1108 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
1109 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
1110
1111 /* Now configure the soinfo struct where we'll store all of our data
1112 * for the ELF object. If the loading fails, we waste the entry, but
1113 * same thing would happen if we failed during linking. Configuring the
1114 * soinfo struct here is a lot more convenient.
1115 */
Erik Gillingfde86422009-07-28 20:28:19 -07001116 bname = strrchr(name, '/');
1117 si = alloc_info(bname ? bname + 1 : name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001118 if (si == NULL)
1119 goto fail;
1120
1121 /* Carve out a chunk of memory where we will map in the individual
1122 * segments */
1123 si->base = req_base;
1124 si->size = ext_sz;
1125 si->flags = 0;
1126 si->entry = 0;
1127 si->dynamic = (unsigned *)-1;
1128 if (alloc_mem_region(si) < 0)
1129 goto fail;
1130
1131 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
1132 pid, name, (void *)si->base, (unsigned) ext_sz);
1133
1134 /* Now actually load the library's segments into right places in memory */
1135 if (load_segments(fd, &__header[0], si) < 0) {
1136 if (si->ba_index >= 0) {
Iliyan Malcheve100f522010-02-10 15:19:37 -08001137 ba_free(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001138 si->ba_index = -1;
1139 }
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
1153fail:
1154 if (si) free_info(si);
1155 close(fd);
1156 return NULL;
1157}
1158
1159static soinfo *
1160init_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 (si->base < LIBBASE || si->base >= LIBLAST)
1170 si->flags |= FLAG_PRELINKED;
1171
1172 if(link_image(si, wr_offset)) {
1173 /* We failed to link. However, we can only restore libbase
1174 ** if no additional libraries have moved it since we updated it.
1175 */
1176 munmap((void *)si->base, si->size);
1177 return NULL;
1178 }
1179
1180 return si;
1181}
1182
1183soinfo *find_library(const char *name)
1184{
1185 soinfo *si;
Erik Gillingfde86422009-07-28 20:28:19 -07001186 const char *bname = strrchr(name, '/');
1187 bname = bname ? bname + 1 : name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001188
1189 for(si = solist; si != 0; si = si->next){
Erik Gillingfde86422009-07-28 20:28:19 -07001190 if(!strcmp(bname, si->name)) {
Erik Gilling30eb4022009-08-13 16:05:30 -07001191 if(si->flags & FLAG_ERROR) {
1192 DL_ERR("%5d '%s' failed to load previously", pid, bname);
1193 return NULL;
1194 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001195 if(si->flags & FLAG_LINKED) return si;
Erik Gillingd00d23a2009-07-22 17:06:11 -07001196 DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name);
Dima Zavin2e855792009-05-20 18:28:09 -07001197 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001198 }
1199 }
1200
1201 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
1202 si = load_library(name);
1203 if(si == NULL)
1204 return NULL;
1205 return init_library(si);
1206}
1207
1208/* TODO:
1209 * notify gdb of unload
1210 * for non-prelinked libraries, find a way to decrement libbase
1211 */
1212static void call_destructors(soinfo *si);
1213unsigned unload_library(soinfo *si)
1214{
1215 unsigned *d;
1216 if (si->refcount == 1) {
1217 TRACE("%5d unloading '%s'\n", pid, si->name);
1218 call_destructors(si);
1219
1220 for(d = si->dynamic; *d; d += 2) {
1221 if(d[0] == DT_NEEDED){
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001222 soinfo *lsi = (soinfo *)d[1];
1223 d[1] = 0;
1224 if (validate_soinfo(lsi)) {
1225 TRACE("%5d %s needs to unload %s\n", pid,
1226 si->name, lsi->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001227 unload_library(lsi);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001228 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001229 else
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001230 DL_ERR("%5d %s: could not unload dependent library",
1231 pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001232 }
1233 }
1234
1235 munmap((char *)si->base, si->size);
1236 if (si->ba_index >= 0) {
1237 PRINT("%5d releasing library '%s' address space at %08x "\
1238 "through buddy allocator.\n",
1239 pid, si->name, si->base);
Iliyan Malcheve100f522010-02-10 15:19:37 -08001240 ba_free(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001241 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001242 notify_gdb_of_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001243 free_info(si);
1244 si->refcount = 0;
1245 }
1246 else {
1247 si->refcount--;
1248 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
1249 pid, si->name, si->refcount);
1250 }
1251 return si->refcount;
1252}
1253
1254/* TODO: don't use unsigned for addrs below. It works, but is not
1255 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1256 * long.
1257 */
1258static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count)
1259{
1260 Elf32_Sym *symtab = si->symtab;
1261 const char *strtab = si->strtab;
1262 Elf32_Sym *s;
1263 unsigned base;
1264 Elf32_Rel *start = rel;
1265 unsigned idx;
1266
1267 for (idx = 0; idx < count; ++idx) {
1268 unsigned type = ELF32_R_TYPE(rel->r_info);
1269 unsigned sym = ELF32_R_SYM(rel->r_info);
1270 unsigned reloc = (unsigned)(rel->r_offset + si->base);
1271 unsigned sym_addr = 0;
1272 char *sym_name = NULL;
1273
1274 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1275 si->name, idx);
1276 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001277 sym_name = (char *)(strtab + symtab[sym].st_name);
1278 s = _do_lookup(si, sym_name, &base);
Doug Kwane8238072009-10-26 12:05:23 -07001279 if(s == NULL) {
1280 /* We only allow an undefined symbol if this is a weak
1281 reference.. */
1282 s = &symtab[sym];
1283 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1284 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
1285 return -1;
1286 }
1287
1288 /* IHI0044C AAELF 4.5.1.1:
1289
1290 Libraries are not searched to resolve weak references.
1291 It is not an error for a weak reference to remain
1292 unsatisfied.
1293
1294 During linking, the value of an undefined weak reference is:
1295 - Zero if the relocation type is absolute
1296 - The address of the place if the relocation is pc-relative
1297 - The address of nominial base address if the relocation
1298 type is base-relative.
1299 */
1300
1301 switch (type) {
1302#if defined(ANDROID_ARM_LINKER)
1303 case R_ARM_JUMP_SLOT:
1304 case R_ARM_GLOB_DAT:
1305 case R_ARM_ABS32:
1306 case R_ARM_RELATIVE: /* Don't care. */
1307 case R_ARM_NONE: /* Don't care. */
1308#elif defined(ANDROID_X86_LINKER)
1309 case R_386_JUMP_SLOT:
1310 case R_386_GLOB_DAT:
1311 case R_386_32:
1312 case R_386_RELATIVE: /* Dont' care. */
1313#endif /* ANDROID_*_LINKER */
1314 /* sym_addr was initialized to be zero above or relocation
1315 code below does not care about value of sym_addr.
1316 No need to do anything. */
1317 break;
1318
1319#if defined(ANDROID_X86_LINKER)
1320 case R_386_PC32:
1321 sym_addr = reloc;
1322 break;
1323#endif /* ANDROID_X86_LINKER */
1324
1325#if defined(ANDROID_ARM_LINKER)
1326 case R_ARM_COPY:
1327 /* Fall through. Can't really copy if weak symbol is
1328 not found in run-time. */
1329#endif /* ANDROID_ARM_LINKER */
1330 default:
1331 DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n",
1332 pid, type, rel, (int) (rel - start));
1333 return -1;
1334 }
1335 } else {
1336 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001337#if 0
1338 if((base == 0) && (si->base != 0)){
1339 /* linking from libraries to main image is bad */
Erik Gillingd00d23a2009-07-22 17:06:11 -07001340 DL_ERR("%5d cannot locate '%s'...",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001341 pid, strtab + symtab[sym].st_name);
1342 return -1;
1343 }
1344#endif
Doug Kwane8238072009-10-26 12:05:23 -07001345 sym_addr = (unsigned)(s->st_value + base);
1346 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001347 COUNT_RELOC(RELOC_SYMBOL);
1348 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001349 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001350 }
1351
1352/* TODO: This is ugly. Split up the relocations by arch into
1353 * different files.
1354 */
1355 switch(type){
1356#if defined(ANDROID_ARM_LINKER)
1357 case R_ARM_JUMP_SLOT:
1358 COUNT_RELOC(RELOC_ABSOLUTE);
1359 MARK(rel->r_offset);
1360 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1361 reloc, sym_addr, sym_name);
1362 *((unsigned*)reloc) = sym_addr;
1363 break;
1364 case R_ARM_GLOB_DAT:
1365 COUNT_RELOC(RELOC_ABSOLUTE);
1366 MARK(rel->r_offset);
1367 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1368 reloc, sym_addr, sym_name);
1369 *((unsigned*)reloc) = sym_addr;
1370 break;
1371 case R_ARM_ABS32:
1372 COUNT_RELOC(RELOC_ABSOLUTE);
1373 MARK(rel->r_offset);
1374 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1375 reloc, sym_addr, sym_name);
1376 *((unsigned*)reloc) += sym_addr;
1377 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001378 case R_ARM_REL32:
1379 COUNT_RELOC(RELOC_RELATIVE);
1380 MARK(rel->r_offset);
1381 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1382 reloc, sym_addr, rel->r_offset, sym_name);
1383 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1384 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001385#elif defined(ANDROID_X86_LINKER)
1386 case R_386_JUMP_SLOT:
1387 COUNT_RELOC(RELOC_ABSOLUTE);
1388 MARK(rel->r_offset);
1389 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1390 reloc, sym_addr, sym_name);
1391 *((unsigned*)reloc) = sym_addr;
1392 break;
1393 case R_386_GLOB_DAT:
1394 COUNT_RELOC(RELOC_ABSOLUTE);
1395 MARK(rel->r_offset);
1396 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1397 reloc, sym_addr, sym_name);
1398 *((unsigned*)reloc) = sym_addr;
1399 break;
1400#endif /* ANDROID_*_LINKER */
1401
1402#if defined(ANDROID_ARM_LINKER)
1403 case R_ARM_RELATIVE:
1404#elif defined(ANDROID_X86_LINKER)
1405 case R_386_RELATIVE:
1406#endif /* ANDROID_*_LINKER */
1407 COUNT_RELOC(RELOC_RELATIVE);
1408 MARK(rel->r_offset);
1409 if(sym){
Erik Gillingd00d23a2009-07-22 17:06:11 -07001410 DL_ERR("%5d odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001411 return -1;
1412 }
1413 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1414 reloc, si->base);
1415 *((unsigned*)reloc) += si->base;
1416 break;
1417
1418#if defined(ANDROID_X86_LINKER)
1419 case R_386_32:
1420 COUNT_RELOC(RELOC_RELATIVE);
1421 MARK(rel->r_offset);
1422
1423 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1424 reloc, sym_addr, sym_name);
1425 *((unsigned *)reloc) += (unsigned)sym_addr;
1426 break;
1427
1428 case R_386_PC32:
1429 COUNT_RELOC(RELOC_RELATIVE);
1430 MARK(rel->r_offset);
1431 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1432 "+%08x (%08x - %08x) %s\n", pid, reloc,
1433 (sym_addr - reloc), sym_addr, reloc, sym_name);
1434 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1435 break;
1436#endif /* ANDROID_X86_LINKER */
1437
1438#ifdef ANDROID_ARM_LINKER
1439 case R_ARM_COPY:
1440 COUNT_RELOC(RELOC_COPY);
1441 MARK(rel->r_offset);
1442 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1443 reloc, s->st_size, sym_addr, sym_name);
1444 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1445 break;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001446 case R_ARM_NONE:
1447 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001448#endif /* ANDROID_ARM_LINKER */
1449
1450 default:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001451 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001452 pid, type, rel, (int) (rel - start));
1453 return -1;
1454 }
1455 rel++;
1456 }
1457 return 0;
1458}
1459
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001460#if defined(ANDROID_SH_LINKER)
1461static int reloc_library_a(soinfo *si, Elf32_Rela *rela, unsigned count)
1462{
1463 Elf32_Sym *symtab = si->symtab;
1464 const char *strtab = si->strtab;
1465 Elf32_Sym *s;
1466 unsigned base;
1467 Elf32_Rela *start = rela;
1468 unsigned idx;
1469
1470 for (idx = 0; idx < count; ++idx) {
1471 unsigned type = ELF32_R_TYPE(rela->r_info);
1472 unsigned sym = ELF32_R_SYM(rela->r_info);
1473 unsigned reloc = (unsigned)(rela->r_offset + si->base);
1474 unsigned sym_addr = 0;
1475 char *sym_name = NULL;
1476
1477 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1478 si->name, idx);
1479 if(sym != 0) {
1480 sym_name = (char *)(strtab + symtab[sym].st_name);
1481 s = _do_lookup(si, sym_name, &base);
1482 if(s == 0) {
1483 DL_ERR("%5d cannot locate '%s'...", pid, sym_name);
1484 return -1;
1485 }
1486#if 0
1487 if((base == 0) && (si->base != 0)){
1488 /* linking from libraries to main image is bad */
1489 DL_ERR("%5d cannot locate '%s'...",
1490 pid, strtab + symtab[sym].st_name);
1491 return -1;
1492 }
1493#endif
1494 if ((s->st_shndx == SHN_UNDEF) && (s->st_value != 0)) {
1495 DL_ERR("%5d In '%s', shndx=%d && value=0x%08x. We do not "
1496 "handle this yet", pid, si->name, s->st_shndx,
1497 s->st_value);
1498 return -1;
1499 }
1500 sym_addr = (unsigned)(s->st_value + base);
1501 COUNT_RELOC(RELOC_SYMBOL);
1502 } else {
1503 s = 0;
1504 }
1505
1506/* TODO: This is ugly. Split up the relocations by arch into
1507 * different files.
1508 */
1509 switch(type){
1510 case R_SH_JUMP_SLOT:
1511 COUNT_RELOC(RELOC_ABSOLUTE);
1512 MARK(rela->r_offset);
1513 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1514 reloc, sym_addr, sym_name);
1515 *((unsigned*)reloc) = sym_addr;
1516 break;
1517 case R_SH_GLOB_DAT:
1518 COUNT_RELOC(RELOC_ABSOLUTE);
1519 MARK(rela->r_offset);
1520 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1521 reloc, sym_addr, sym_name);
1522 *((unsigned*)reloc) = sym_addr;
1523 break;
1524 case R_SH_DIR32:
1525 COUNT_RELOC(RELOC_ABSOLUTE);
1526 MARK(rela->r_offset);
1527 TRACE_TYPE(RELO, "%5d RELO DIR32 %08x <- %08x %s\n", pid,
1528 reloc, sym_addr, sym_name);
1529 *((unsigned*)reloc) += sym_addr;
1530 break;
1531 case R_SH_RELATIVE:
1532 COUNT_RELOC(RELOC_RELATIVE);
1533 MARK(rela->r_offset);
1534 if(sym){
1535 DL_ERR("%5d odd RELATIVE form...", pid);
1536 return -1;
1537 }
1538 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1539 reloc, si->base);
1540 *((unsigned*)reloc) += si->base;
1541 break;
1542
1543 default:
1544 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
1545 pid, type, rela, (int) (rela - start));
1546 return -1;
1547 }
1548 rela++;
1549 }
1550 return 0;
1551}
1552#endif /* ANDROID_SH_LINKER */
1553
David 'Digit' Turner82156792009-05-18 14:37:41 +02001554
1555/* Please read the "Initialization and Termination functions" functions.
1556 * of the linker design note in bionic/linker/README.TXT to understand
1557 * what the following code is doing.
1558 *
1559 * The important things to remember are:
1560 *
1561 * DT_PREINIT_ARRAY must be called first for executables, and should
1562 * not appear in shared libraries.
1563 *
1564 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1565 *
1566 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1567 *
1568 * DT_FINI_ARRAY must be parsed in reverse order.
1569 */
1570
1571static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001572{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001573 int n, inc = 1;
1574
1575 if (reverse) {
1576 ctor += (count-1);
1577 inc = -1;
1578 }
1579
1580 for(n = count; n > 0; n--) {
1581 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1582 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001583 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001584 void (*func)() = (void (*)()) *ctor;
1585 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001586 if(((int) func == 0) || ((int) func == -1)) continue;
1587 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1588 func();
1589 }
1590}
1591
1592static void call_constructors(soinfo *si)
1593{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001594 if (si->flags & FLAG_EXE) {
1595 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1596 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1597 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001598 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001599 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1600 } else {
1601 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001602 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
Erik Gillingd00d23a2009-07-22 17:06:11 -07001603 " This is INVALID.", pid, si->name,
Dima Zavin2e855792009-05-20 18:28:09 -07001604 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001605 }
1606 }
1607
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001608 if (si->init_func) {
1609 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1610 (unsigned)si->init_func, si->name);
1611 si->init_func();
1612 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1613 }
1614
1615 if (si->init_array) {
1616 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1617 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001618 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001619 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1620 }
1621}
1622
David 'Digit' Turner82156792009-05-18 14:37:41 +02001623
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001624static void call_destructors(soinfo *si)
1625{
1626 if (si->fini_array) {
1627 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1628 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001629 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001630 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1631 }
1632
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001633 if (si->fini_func) {
1634 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1635 (unsigned)si->fini_func, si->name);
1636 si->fini_func();
1637 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1638 }
1639}
1640
1641/* Force any of the closed stdin, stdout and stderr to be associated with
1642 /dev/null. */
1643static int nullify_closed_stdio (void)
1644{
1645 int dev_null, i, status;
1646 int return_value = 0;
1647
1648 dev_null = open("/dev/null", O_RDWR);
1649 if (dev_null < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001650 DL_ERR("Cannot open /dev/null.");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001651 return -1;
1652 }
1653 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1654
1655 /* If any of the stdio file descriptors is valid and not associated
1656 with /dev/null, dup /dev/null to it. */
1657 for (i = 0; i < 3; i++) {
1658 /* If it is /dev/null already, we are done. */
1659 if (i == dev_null)
1660 continue;
1661
1662 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1663 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1664 can be interrupted but we do this just to be safe. */
1665 do {
1666 status = fcntl(i, F_GETFL);
1667 } while (status < 0 && errno == EINTR);
1668
1669 /* If file is openned, we are good. */
1670 if (status >= 0)
1671 continue;
1672
1673 /* The only error we allow is that the file descriptor does not
1674 exist, in which case we dup /dev/null to it. */
1675 if (errno != EBADF) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001676 DL_ERR("nullify_stdio: unhandled error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001677 return_value = -1;
1678 continue;
1679 }
1680
1681 /* Try dupping /dev/null to this stdio file descriptor and
1682 repeat if there is a signal. Note that any errors in closing
1683 the stdio descriptor are lost. */
1684 do {
1685 status = dup2(dev_null, i);
1686 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001687
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001688 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001689 DL_ERR("nullify_stdio: dup2 error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001690 return_value = -1;
1691 continue;
1692 }
1693 }
1694
1695 /* If /dev/null is not one of the stdio file descriptors, close it. */
1696 if (dev_null > 2) {
1697 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001698 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001699 status = close(dev_null);
1700 } while (status < 0 && errno == EINTR);
1701
1702 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001703 DL_ERR("nullify_stdio: close error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001704 return_value = -1;
1705 }
1706 }
1707
1708 return return_value;
1709}
1710
1711static int link_image(soinfo *si, unsigned wr_offset)
1712{
1713 unsigned *d;
1714 Elf32_Phdr *phdr = si->phdr;
1715 int phnum = si->phnum;
1716
1717 INFO("[ %5d linking %s ]\n", pid, si->name);
1718 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1719 si->base, si->flags);
1720
1721 if (si->flags & FLAG_EXE) {
1722 /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for
1723 * linkage info if this is the executable. If this was a
1724 * dynamic lib, that would have been done at load time.
1725 *
1726 * TODO: It's unfortunate that small pieces of this are
1727 * repeated from the load_library routine. Refactor this just
1728 * slightly to reuse these bits.
1729 */
1730 si->size = 0;
1731 for(; phnum > 0; --phnum, ++phdr) {
1732#ifdef ANDROID_ARM_LINKER
1733 if(phdr->p_type == PT_ARM_EXIDX) {
1734 /* exidx entries (used for stack unwinding) are 8 bytes each.
1735 */
1736 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1737 si->ARM_exidx_count = phdr->p_memsz / 8;
1738 }
1739#endif
1740 if (phdr->p_type == PT_LOAD) {
1741 /* For the executable, we use the si->size field only in
1742 dl_unwind_find_exidx(), so the meaning of si->size
1743 is not the size of the executable; it is the last
1744 virtual address of the loadable part of the executable;
1745 since si->base == 0 for an executable, we use the
1746 range [0, si->size) to determine whether a PC value
1747 falls within the executable section. Of course, if
1748 a value is below phdr->p_vaddr, it's not in the
1749 executable section, but a) we shouldn't be asking for
1750 such a value anyway, and b) if we have to provide
1751 an EXIDX for such a value, then the executable's
1752 EXIDX is probably the better choice.
1753 */
1754 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
1755 if (phdr->p_vaddr + phdr->p_memsz > si->size)
1756 si->size = phdr->p_vaddr + phdr->p_memsz;
1757 /* try to remember what range of addresses should be write
1758 * protected */
1759 if (!(phdr->p_flags & PF_W)) {
1760 unsigned _end;
1761
1762 if (phdr->p_vaddr < si->wrprotect_start)
1763 si->wrprotect_start = phdr->p_vaddr;
1764 _end = (((phdr->p_vaddr + phdr->p_memsz + PAGE_SIZE - 1) &
1765 (~PAGE_MASK)));
1766 if (_end > si->wrprotect_end)
1767 si->wrprotect_end = _end;
1768 }
1769 } else if (phdr->p_type == PT_DYNAMIC) {
1770 if (si->dynamic != (unsigned *)-1) {
Dima Zavin2e855792009-05-20 18:28:09 -07001771 DL_ERR("%5d multiple PT_DYNAMIC segments found in '%s'. "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001772 "Segment at 0x%08x, previously one found at 0x%08x",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001773 pid, si->name, si->base + phdr->p_vaddr,
1774 (unsigned)si->dynamic);
1775 goto fail;
1776 }
1777 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
1778 si->dynamic = (unsigned *) (si->base + phdr->p_vaddr);
1779 }
1780 }
1781 }
1782
1783 if (si->dynamic == (unsigned *)-1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001784 DL_ERR("%5d missing PT_DYNAMIC?!", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001785 goto fail;
1786 }
1787
1788 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1789
1790 /* extract useful information from dynamic section */
1791 for(d = si->dynamic; *d; d++){
1792 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1793 switch(*d++){
1794 case DT_HASH:
1795 si->nbucket = ((unsigned *) (si->base + *d))[0];
1796 si->nchain = ((unsigned *) (si->base + *d))[1];
1797 si->bucket = (unsigned *) (si->base + *d + 8);
1798 si->chain = (unsigned *) (si->base + *d + 8 + si->nbucket * 4);
1799 break;
1800 case DT_STRTAB:
1801 si->strtab = (const char *) (si->base + *d);
1802 break;
1803 case DT_SYMTAB:
1804 si->symtab = (Elf32_Sym *) (si->base + *d);
1805 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001806#if !defined(ANDROID_SH_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001807 case DT_PLTREL:
1808 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001809 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001810 goto fail;
1811 }
1812 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001813#endif
1814#ifdef ANDROID_SH_LINKER
1815 case DT_JMPREL:
1816 si->plt_rela = (Elf32_Rela*) (si->base + *d);
1817 break;
1818 case DT_PLTRELSZ:
1819 si->plt_rela_count = *d / sizeof(Elf32_Rela);
1820 break;
1821#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001822 case DT_JMPREL:
1823 si->plt_rel = (Elf32_Rel*) (si->base + *d);
1824 break;
1825 case DT_PLTRELSZ:
1826 si->plt_rel_count = *d / 8;
1827 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001828#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001829 case DT_REL:
1830 si->rel = (Elf32_Rel*) (si->base + *d);
1831 break;
1832 case DT_RELSZ:
1833 si->rel_count = *d / 8;
1834 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001835#ifdef ANDROID_SH_LINKER
1836 case DT_RELASZ:
1837 si->rela_count = *d / sizeof(Elf32_Rela);
1838 break;
1839#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001840 case DT_PLTGOT:
1841 /* Save this in case we decide to do lazy binding. We don't yet. */
1842 si->plt_got = (unsigned *)(si->base + *d);
1843 break;
1844 case DT_DEBUG:
1845 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1846 *d = (int) &_r_debug;
1847 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001848#ifdef ANDROID_SH_LINKER
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001849 case DT_RELA:
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001850 si->rela = (Elf32_Rela *) (si->base + *d);
1851 break;
1852#else
1853 case DT_RELA:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001854 DL_ERR("%5d DT_RELA not supported", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001855 goto fail;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001856#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001857 case DT_INIT:
1858 si->init_func = (void (*)(void))(si->base + *d);
1859 DEBUG("%5d %s constructors (init func) found at %p\n",
1860 pid, si->name, si->init_func);
1861 break;
1862 case DT_FINI:
1863 si->fini_func = (void (*)(void))(si->base + *d);
1864 DEBUG("%5d %s destructors (fini func) found at %p\n",
1865 pid, si->name, si->fini_func);
1866 break;
1867 case DT_INIT_ARRAY:
1868 si->init_array = (unsigned *)(si->base + *d);
1869 DEBUG("%5d %s constructors (init_array) found at %p\n",
1870 pid, si->name, si->init_array);
1871 break;
1872 case DT_INIT_ARRAYSZ:
1873 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1874 break;
1875 case DT_FINI_ARRAY:
1876 si->fini_array = (unsigned *)(si->base + *d);
1877 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1878 pid, si->name, si->fini_array);
1879 break;
1880 case DT_FINI_ARRAYSZ:
1881 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1882 break;
1883 case DT_PREINIT_ARRAY:
1884 si->preinit_array = (unsigned *)(si->base + *d);
1885 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1886 pid, si->name, si->preinit_array);
1887 break;
1888 case DT_PREINIT_ARRAYSZ:
1889 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1890 break;
1891 case DT_TEXTREL:
1892 /* TODO: make use of this. */
1893 /* this means that we might have to write into where the text
1894 * segment was loaded during relocation... Do something with
1895 * it.
1896 */
1897 DEBUG("%5d Text segment should be writable during relocation.\n",
1898 pid);
1899 break;
1900 }
1901 }
1902
1903 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
1904 pid, si->base, si->strtab, si->symtab);
1905
1906 if((si->strtab == 0) || (si->symtab == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001907 DL_ERR("%5d missing essential tables", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001908 goto fail;
1909 }
1910
1911 for(d = si->dynamic; *d; d += 2) {
1912 if(d[0] == DT_NEEDED){
1913 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001914 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001915 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001916 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Erik Gillingd00d23a2009-07-22 17:06:11 -07001917 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
Dima Zavin03531952009-05-29 17:30:25 -07001918 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001919 goto fail;
1920 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001921 /* Save the soinfo of the loaded DT_NEEDED library in the payload
1922 of the DT_NEEDED entry itself, so that we can retrieve the
1923 soinfo directly later from the dynamic segment. This is a hack,
1924 but it allows us to map from DT_NEEDED to soinfo efficiently
1925 later on when we resolve relocations, trying to look up a symgol
1926 with dlsym().
1927 */
1928 d[1] = (unsigned)lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001929 lsi->refcount++;
1930 }
1931 }
1932
1933 if(si->plt_rel) {
1934 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1935 if(reloc_library(si, si->plt_rel, si->plt_rel_count))
1936 goto fail;
1937 }
1938 if(si->rel) {
1939 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1940 if(reloc_library(si, si->rel, si->rel_count))
1941 goto fail;
1942 }
1943
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001944#ifdef ANDROID_SH_LINKER
1945 if(si->plt_rela) {
1946 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1947 if(reloc_library_a(si, si->plt_rela, si->plt_rela_count))
1948 goto fail;
1949 }
1950 if(si->rela) {
1951 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1952 if(reloc_library_a(si, si->rela, si->rela_count))
1953 goto fail;
1954 }
1955#endif /* ANDROID_SH_LINKER */
1956
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001957 si->flags |= FLAG_LINKED;
1958 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1959
1960#if 0
1961 /* This is the way that the old dynamic linker did protection of
1962 * non-writable areas. It would scan section headers and find where
1963 * .text ended (rather where .data/.bss began) and assume that this is
1964 * the upper range of the non-writable area. This is too coarse,
1965 * and is kept here for reference until we fully move away from single
1966 * segment elf objects. See the code in get_wr_offset (also #if'd 0)
1967 * that made this possible.
1968 */
1969 if(wr_offset < 0xffffffff){
1970 mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC);
1971 }
1972#else
1973 /* TODO: Verify that this does the right thing in all cases, as it
1974 * presently probably does not. It is possible that an ELF image will
1975 * come with multiple read-only segments. What we ought to do is scan
1976 * the program headers again and mprotect all the read-only segments.
1977 * To prevent re-scanning the program header, we would have to build a
1978 * list of loadable segments in si, and then scan that instead. */
1979 if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) {
1980 mprotect((void *)si->wrprotect_start,
1981 si->wrprotect_end - si->wrprotect_start,
1982 PROT_READ | PROT_EXEC);
1983 }
1984#endif
1985
1986 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1987 stdout and stderr to close a security hole described in:
1988
1989 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1990
1991 */
1992 if (getuid() != geteuid() || getgid() != getegid())
1993 nullify_closed_stdio ();
1994 call_constructors(si);
1995 notify_gdb_of_load(si);
1996 return 0;
1997
1998fail:
Doug Kwan94304352009-10-23 18:11:40 -07001999 DL_ERR("failed to link %s\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002000 si->flags |= FLAG_ERROR;
2001 return -1;
2002}
2003
David Bartleybc3a5c22009-06-02 18:27:28 -07002004static void parse_library_path(char *path, char *delim)
2005{
2006 size_t len;
2007 char *ldpaths_bufp = ldpaths_buf;
2008 int i = 0;
2009
2010 len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf));
2011
2012 while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) {
2013 if (*ldpaths[i] != '\0')
2014 ++i;
2015 }
2016
2017 /* Forget the last path if we had to truncate; this occurs if the 2nd to
2018 * last char isn't '\0' (i.e. not originally a delim). */
2019 if (i > 0 && len >= sizeof(ldpaths_buf) &&
2020 ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') {
2021 ldpaths[i - 1] = NULL;
2022 } else {
2023 ldpaths[i] = NULL;
2024 }
2025}
2026
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002027int main(int argc, char **argv)
2028{
2029 return 0;
2030}
2031
2032#define ANDROID_TLS_SLOTS BIONIC_TLS_SLOTS
2033
2034static void * __tls_area[ANDROID_TLS_SLOTS];
2035
2036unsigned __linker_init(unsigned **elfdata)
2037{
2038 static soinfo linker_soinfo;
2039
2040 int argc = (int) *elfdata;
2041 char **argv = (char**) (elfdata + 1);
2042 unsigned *vecs = (unsigned*) (argv + argc + 1);
2043 soinfo *si;
2044 struct link_map * map;
David Bartleybc3a5c22009-06-02 18:27:28 -07002045 char *ldpath_env = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002046
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02002047 /* Setup a temporary TLS area that is used to get a working
2048 * errno for system calls.
2049 */
2050 __set_tls(__tls_area);
2051
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002052 pid = getpid();
2053
2054#if TIMING
2055 struct timeval t0, t1;
2056 gettimeofday(&t0, 0);
2057#endif
2058
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02002059 /* NOTE: we store the elfdata pointer on a special location
2060 * of the temporary TLS area in order to pass it to
2061 * the C Library's runtime initializer.
2062 *
2063 * The initializer must clear the slot and reset the TLS
2064 * to point to a different location to ensure that no other
2065 * shared library constructor can access it.
2066 */
2067 __tls_area[TLS_SLOT_BIONIC_PREINIT] = elfdata;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002068
2069 debugger_init();
2070
2071 /* skip past the environment */
2072 while(vecs[0] != 0) {
2073 if(!strncmp((char*) vecs[0], "DEBUG=", 6)) {
2074 debug_verbosity = atoi(((char*) vecs[0]) + 6);
David Bartleybc3a5c22009-06-02 18:27:28 -07002075 } else if(!strncmp((char*) vecs[0], "LD_LIBRARY_PATH=", 16)) {
2076 ldpath_env = (char*) vecs[0] + 16;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002077 }
2078 vecs++;
2079 }
2080 vecs++;
2081
2082 INFO("[ android linker & debugger ]\n");
2083 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
2084
2085 si = alloc_info(argv[0]);
2086 if(si == 0) {
2087 exit(-1);
2088 }
2089
2090 /* bootstrap the link map, the main exe always needs to be first */
2091 si->flags |= FLAG_EXE;
2092 map = &(si->linkmap);
2093
2094 map->l_addr = 0;
2095 map->l_name = argv[0];
2096 map->l_prev = NULL;
2097 map->l_next = NULL;
2098
2099 _r_debug.r_map = map;
2100 r_debug_tail = map;
2101
2102 /* gdb expects the linker to be in the debug shared object list,
2103 * and we need to make sure that the reported load address is zero.
2104 * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
2105 * is. Don't use alloc_info(), because the linker shouldn't
2106 * be on the soinfo list.
2107 */
2108 strcpy((char*) linker_soinfo.name, "/system/bin/linker");
2109 linker_soinfo.flags = 0;
2110 linker_soinfo.base = 0; // This is the important part; must be zero.
2111 insert_soinfo_into_debug_map(&linker_soinfo);
2112
2113 /* extract information passed from the kernel */
2114 while(vecs[0] != 0){
2115 switch(vecs[0]){
2116 case AT_PHDR:
2117 si->phdr = (Elf32_Phdr*) vecs[1];
2118 break;
2119 case AT_PHNUM:
2120 si->phnum = (int) vecs[1];
2121 break;
2122 case AT_ENTRY:
2123 si->entry = vecs[1];
2124 break;
2125 }
2126 vecs += 2;
2127 }
2128
Iliyan Malcheve100f522010-02-10 15:19:37 -08002129 ba_init(&ba_nonprelink);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002130
2131 si->base = 0;
2132 si->dynamic = (unsigned *)-1;
2133 si->wrprotect_start = 0xffffffff;
2134 si->wrprotect_end = 0;
2135
David Bartleybc3a5c22009-06-02 18:27:28 -07002136 /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */
2137 if (ldpath_env && getuid() == geteuid() && getgid() == getegid())
2138 parse_library_path(ldpath_env, ":");
2139
Dima Zavin2e855792009-05-20 18:28:09 -07002140 if(link_image(si, 0)) {
2141 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
2142 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2143 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002144 exit(-1);
2145 }
2146
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07002147#if ALLOW_SYMBOLS_FROM_MAIN
2148 /* Set somain after we've loaded all the libraries in order to prevent
2149 * linking of symbols back to the main image, which is not set up at that
2150 * point yet.
2151 */
2152 somain = si;
2153#endif
2154
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002155#if TIMING
2156 gettimeofday(&t1,NULL);
2157 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
2158 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2159 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
2160 ));
2161#endif
2162#if STATS
2163 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
2164 linker_stats.reloc[RELOC_ABSOLUTE],
2165 linker_stats.reloc[RELOC_RELATIVE],
2166 linker_stats.reloc[RELOC_COPY],
2167 linker_stats.reloc[RELOC_SYMBOL]);
2168#endif
2169#if COUNT_PAGES
2170 {
2171 unsigned n;
2172 unsigned i;
2173 unsigned count = 0;
2174 for(n = 0; n < 4096; n++){
2175 if(bitmask[n]){
2176 unsigned x = bitmask[n];
2177 for(i = 0; i < 8; i++){
2178 if(x & 1) count++;
2179 x >>= 1;
2180 }
2181 }
2182 }
2183 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
2184 }
2185#endif
2186
2187#if TIMING || STATS || COUNT_PAGES
2188 fflush(stdout);
2189#endif
2190
2191 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
2192 si->entry);
2193 return si->entry;
2194}