blob: 6719b12210e5a76fe6e14b0a9e98b4155fe4539f [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 */
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700512Elf32_Sym *lookup(const char *name, soinfo **found)
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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800518 for(si = solist; (s == NULL) && (si != NULL); si = si->next)
519 {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700520 if(si->flags & FLAG_ERROR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800521 continue;
Doug Kwane8238072009-10-26 12:05:23 -0700522 s = _elf_lookup(si, elf_hash, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800523 if (s != NULL) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700524 *found = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800525 break;
526 }
527 }
528
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700529 if(s != NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800530 TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
531 "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
532 return s;
533 }
534
Doug Kwan94304352009-10-23 18:11:40 -0700535 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800536}
537
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600538soinfo *find_containing_library(void *addr)
539{
540 soinfo *si;
541
542 for(si = solist; si != NULL; si = si->next)
543 {
544 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
545 return si;
546 }
547 }
548
549 return NULL;
550}
551
552Elf32_Sym *find_containing_symbol(void *addr, soinfo *si)
553{
554 unsigned int i;
555 unsigned soaddr = (unsigned)addr - si->base;
556
557 /* Search the library's symbol table for any defined symbol which
558 * contains this address */
559 for(i=0; i<si->nchain; i++) {
560 Elf32_Sym *sym = &si->symtab[i];
561
562 if(sym->st_shndx != SHN_UNDEF &&
563 soaddr >= sym->st_value &&
564 soaddr < sym->st_value + sym->st_size) {
565 return sym;
566 }
567 }
568
569 return NULL;
570}
571
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800572#if 0
573static void dump(soinfo *si)
574{
575 Elf32_Sym *s = si->symtab;
576 unsigned n;
577
578 for(n = 0; n < si->nchain; n++) {
579 TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
580 s->st_info, s->st_shndx, s->st_value, s->st_size,
581 si->strtab + s->st_name);
582 s++;
583 }
584}
585#endif
586
587static const char *sopaths[] = {
588 "/system/lib",
589 "/lib",
590 0
591};
592
593static int _open_lib(const char *name)
594{
595 int fd;
596 struct stat filestat;
597
598 if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
599 if ((fd = open(name, O_RDONLY)) >= 0)
600 return fd;
601 }
602
603 return -1;
604}
605
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800606static int open_library(const char *name)
607{
608 int fd;
609 char buf[512];
610 const char **path;
David Bartleybc3a5c22009-06-02 18:27:28 -0700611 int n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800612
613 TRACE("[ %5d opening %s ]\n", pid, name);
614
615 if(name == 0) return -1;
616 if(strlen(name) > 256) return -1;
617
618 if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
619 return fd;
620
David Bartleybc3a5c22009-06-02 18:27:28 -0700621 for (path = ldpaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800622 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700623 if (n < 0 || n >= (int)sizeof(buf)) {
624 WARN("Ignoring very long library path: %s/%s\n", *path, name);
625 continue;
626 }
627 if ((fd = _open_lib(buf)) >= 0)
628 return fd;
629 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630 for (path = sopaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800631 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700632 if (n < 0 || n >= (int)sizeof(buf)) {
633 WARN("Ignoring very long library path: %s/%s\n", *path, name);
634 continue;
635 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800636 if ((fd = _open_lib(buf)) >= 0)
637 return fd;
638 }
639
640 return -1;
641}
642
643/* temporary space for holding the first page of the shared lib
644 * which contains the elf header (with the pht). */
645static unsigned char __header[PAGE_SIZE];
646
647typedef struct {
648 long mmap_addr;
649 char tag[4]; /* 'P', 'R', 'E', ' ' */
650} prelink_info_t;
651
652/* Returns the requested base address if the library is prelinked,
653 * and 0 otherwise. */
654static unsigned long
655is_prelinked(int fd, const char *name)
656{
657 off_t sz;
658 prelink_info_t info;
659
660 sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
661 if (sz < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700662 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800663 return 0;
664 }
665
666 if (read(fd, &info, sizeof(info)) != sizeof(info)) {
667 WARN("Could not read prelink_info_t structure for `%s`\n", name);
668 return 0;
669 }
670
671 if (strncmp(info.tag, "PRE ", 4)) {
672 WARN("`%s` is not a prelinked library\n", name);
673 return 0;
674 }
675
676 return (unsigned long)info.mmap_addr;
677}
678
679/* verify_elf_object
680 * Verifies if the object @ base is a valid ELF object
681 *
682 * Args:
683 *
684 * Returns:
685 * 0 on success
686 * -1 if no valid ELF object is found @ base.
687 */
688static int
689verify_elf_object(void *base, const char *name)
690{
691 Elf32_Ehdr *hdr = (Elf32_Ehdr *) base;
692
693 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
694 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
695 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
696 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
697
698 /* TODO: Should we verify anything else in the header? */
699
700 return 0;
701}
702
703
704/* get_lib_extents
705 * Retrieves the base (*base) address where the ELF object should be
706 * mapped and its overall memory size (*total_sz).
707 *
708 * Args:
709 * fd: Opened file descriptor for the library
710 * name: The name of the library
711 * _hdr: Pointer to the header page of the library
712 * total_sz: Total size of the memory that should be allocated for
713 * this library
714 *
715 * Returns:
716 * -1 if there was an error while trying to get the lib extents.
717 * The possible reasons are:
718 * - Could not determine if the library was prelinked.
719 * - The library provided is not a valid ELF object
720 * 0 if the library did not request a specific base offset (normal
721 * for non-prelinked libs)
722 * > 0 if the library requests a specific address to be mapped to.
723 * This indicates a pre-linked library.
724 */
725static unsigned
726get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz)
727{
728 unsigned req_base;
729 unsigned min_vaddr = 0xffffffff;
730 unsigned max_vaddr = 0;
731 unsigned char *_hdr = (unsigned char *)__hdr;
732 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)_hdr;
733 Elf32_Phdr *phdr;
734 int cnt;
735
736 TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name);
737 if (verify_elf_object(_hdr, name) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700738 DL_ERR("%5d - %s is not a valid ELF object", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800739 return (unsigned)-1;
740 }
741
742 req_base = (unsigned) is_prelinked(fd, name);
743 if (req_base == (unsigned)-1)
744 return -1;
745 else if (req_base != 0) {
746 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
747 pid, name, req_base);
748 } else {
749 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
750 }
751
752 phdr = (Elf32_Phdr *)(_hdr + ehdr->e_phoff);
753
754 /* find the min/max p_vaddrs from all the PT_LOAD segments so we can
755 * get the range. */
756 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
757 if (phdr->p_type == PT_LOAD) {
758 if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr)
759 max_vaddr = phdr->p_vaddr + phdr->p_memsz;
760 if (phdr->p_vaddr < min_vaddr)
761 min_vaddr = phdr->p_vaddr;
762 }
763 }
764
765 if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700766 DL_ERR("%5d - No loadable segments found in %s.", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800767 return (unsigned)-1;
768 }
769
770 /* truncate min_vaddr down to page boundary */
771 min_vaddr &= ~PAGE_MASK;
772
773 /* round max_vaddr up to the next page */
774 max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK;
775
776 *total_sz = (max_vaddr - min_vaddr);
777 return (unsigned)req_base;
778}
779
780/* alloc_mem_region
781 *
782 * This function reserves a chunk of memory to be used for mapping in
783 * the shared library. We reserve the entire memory region here, and
784 * then the rest of the linker will relocate the individual loadable
785 * segments into the correct locations within this memory range.
786 *
787 * Args:
788 * si->base: The requested base of the allocation. If 0, a sane one will be
789 * chosen in the range LIBBASE <= base < LIBLAST.
790 * si->size: The size of the allocation.
791 *
792 * Returns:
793 * -1 on failure, and 0 on success. On success, si->base will contain
794 * the virtual address at which the library will be mapped.
795 */
796
797static int reserve_mem_region(soinfo *si)
798{
799 void *base = mmap((void *)si->base, si->size, PROT_READ | PROT_EXEC,
800 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
801 if (base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700802 DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700803 "as requested, will try general pool: %d (%s)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800804 pid, (si->base ? "" : "non-"), si->name, si->base,
805 errno, strerror(errno));
806 return -1;
807 } else if (base != (void *)si->base) {
Dima Zavin2e855792009-05-20 18:28:09 -0700808 DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700809 "not at 0x%08x", pid, (si->base ? "" : "non-"),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800810 si->name, (unsigned)base, si->base);
811 munmap(base, si->size);
812 return -1;
813 }
814 return 0;
815}
816
817static int
818alloc_mem_region(soinfo *si)
819{
820 if (si->base) {
821 /* Attempt to mmap a prelinked library. */
822 si->ba_index = -1;
823 return reserve_mem_region(si);
824 }
825
826 /* This is not a prelinked library, so we attempt to allocate space
827 for it from the buddy allocator, which manages the area between
828 LIBBASE and LIBLAST.
829 */
Iliyan Malcheve100f522010-02-10 15:19:37 -0800830 si->ba_index = ba_allocate(&ba_nonprelink, si->size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800831 if(si->ba_index >= 0) {
Iliyan Malcheve100f522010-02-10 15:19:37 -0800832 si->base = ba_start_addr(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800833 PRINT("%5d mapping library '%s' at %08x (index %d) " \
834 "through buddy allocator.\n",
835 pid, si->name, si->base, si->ba_index);
836 if (reserve_mem_region(si) < 0) {
Iliyan Malcheve100f522010-02-10 15:19:37 -0800837 ba_free(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800838 si->ba_index = -1;
839 si->base = 0;
840 goto err;
841 }
842 return 0;
843 }
844
845err:
Erik Gillingd00d23a2009-07-22 17:06:11 -0700846 DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800847 pid, si->name);
848 return -1;
849}
850
851#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
852#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
853 MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
854 MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
855/* load_segments
856 *
857 * This function loads all the loadable (PT_LOAD) segments into memory
858 * at their appropriate memory offsets off the base address.
859 *
860 * Args:
861 * fd: Open file descriptor to the library to load.
862 * header: Pointer to a header page that contains the ELF header.
863 * This is needed since we haven't mapped in the real file yet.
864 * si: ptr to soinfo struct describing the shared object.
865 *
866 * Returns:
867 * 0 on success, -1 on failure.
868 */
869static int
870load_segments(int fd, void *header, soinfo *si)
871{
872 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
873 Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned char *)header + ehdr->e_phoff);
874 unsigned char *base = (unsigned char *)si->base;
875 int cnt;
876 unsigned len;
877 unsigned char *tmp;
878 unsigned char *pbase;
879 unsigned char *extra_base;
880 unsigned extra_len;
881 unsigned total_sz = 0;
882
883 si->wrprotect_start = 0xffffffff;
884 si->wrprotect_end = 0;
885
886 TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n",
887 pid, si->name, (unsigned)si->base);
888 /* Now go through all the PT_LOAD segments and map them into memory
889 * at the appropriate locations. */
890 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
891 if (phdr->p_type == PT_LOAD) {
892 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
893 /* we want to map in the segment on a page boundary */
894 tmp = base + (phdr->p_vaddr & (~PAGE_MASK));
895 /* add the # of bytes we masked off above to the total length. */
896 len = phdr->p_filesz + (phdr->p_vaddr & PAGE_MASK);
897
898 TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x "
899 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name,
900 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
901 pbase = mmap(tmp, len, PFLAGS_TO_PROT(phdr->p_flags),
902 MAP_PRIVATE | MAP_FIXED, fd,
903 phdr->p_offset & (~PAGE_MASK));
904 if (pbase == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700905 DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700906 "p_vaddr=0x%08x p_offset=0x%08x", pid, si->name,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800907 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
908 goto fail;
909 }
910
911 /* If 'len' didn't end on page boundary, and it's a writable
912 * segment, zero-fill the rest. */
913 if ((len & PAGE_MASK) && (phdr->p_flags & PF_W))
914 memset((void *)(pbase + len), 0, PAGE_SIZE - (len & PAGE_MASK));
915
916 /* Check to see if we need to extend the map for this segment to
917 * cover the diff between filesz and memsz (i.e. for bss).
918 *
919 * base _+---------------------+ page boundary
920 * . .
921 * | |
922 * . .
923 * pbase _+---------------------+ page boundary
924 * | |
925 * . .
926 * base + p_vaddr _| |
927 * . \ \ .
928 * . | filesz | .
929 * pbase + len _| / | |
930 * <0 pad> . . .
931 * extra_base _+------------|--------+ page boundary
932 * / . . .
933 * | . . .
934 * | +------------|--------+ page boundary
935 * extra_len-> | | | |
936 * | . | memsz .
937 * | . | .
938 * \ _| / |
939 * . .
940 * | |
941 * _+---------------------+ page boundary
942 */
943 tmp = (unsigned char *)(((unsigned)pbase + len + PAGE_SIZE - 1) &
944 (~PAGE_MASK));
945 if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) {
946 extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp;
947 TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x "
948 "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len);
949 /* map in the extra page(s) as anonymous into the range.
950 * This is probably not necessary as we already mapped in
951 * the entire region previously, but we just want to be
952 * sure. This will also set the right flags on the region
953 * (though we can probably accomplish the same thing with
954 * mprotect).
955 */
956 extra_base = mmap((void *)tmp, extra_len,
957 PFLAGS_TO_PROT(phdr->p_flags),
958 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
959 -1, 0);
960 if (extra_base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700961 DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x"
Erik Gillingd00d23a2009-07-22 17:06:11 -0700962 " (0x%08x) ]", pid, si->name, (unsigned)tmp,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800963 extra_len);
964 goto fail;
965 }
966 /* TODO: Check if we need to memset-0 this region.
967 * Anonymous mappings are zero-filled copy-on-writes, so we
968 * shouldn't need to. */
969 TRACE("[ %5d - Segment from '%s' extended @ 0x%08x "
970 "(0x%08x)\n", pid, si->name, (unsigned)extra_base,
971 extra_len);
972 }
973 /* set the len here to show the full extent of the segment we
974 * just loaded, mostly for debugging */
975 len = (((unsigned)base + phdr->p_vaddr + phdr->p_memsz +
976 PAGE_SIZE - 1) & (~PAGE_MASK)) - (unsigned)pbase;
977 TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x "
978 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
979 (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset);
980 total_sz += len;
981 /* Make the section writable just in case we'll have to write to
982 * it during relocation (i.e. text segment). However, we will
983 * remember what range of addresses should be write protected.
984 *
985 */
986 if (!(phdr->p_flags & PF_W)) {
987 if ((unsigned)pbase < si->wrprotect_start)
988 si->wrprotect_start = (unsigned)pbase;
989 if (((unsigned)pbase + len) > si->wrprotect_end)
990 si->wrprotect_end = (unsigned)pbase + len;
991 mprotect(pbase, len,
992 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
993 }
994 } else if (phdr->p_type == PT_DYNAMIC) {
995 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
996 /* this segment contains the dynamic linking information */
997 si->dynamic = (unsigned *)(base + phdr->p_vaddr);
998 } else {
999#ifdef ANDROID_ARM_LINKER
1000 if (phdr->p_type == PT_ARM_EXIDX) {
1001 DEBUG_DUMP_PHDR(phdr, "PT_ARM_EXIDX", pid);
1002 /* exidx entries (used for stack unwinding) are 8 bytes each.
1003 */
1004 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1005 si->ARM_exidx_count = phdr->p_memsz / 8;
1006 }
1007#endif
1008 }
1009
1010 }
1011
1012 /* Sanity check */
1013 if (total_sz > si->size) {
Dima Zavin2e855792009-05-20 18:28:09 -07001014 DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001015 "greater than what was allocated (0x%08x). THIS IS BAD!",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001016 pid, total_sz, si->name, si->size);
1017 goto fail;
1018 }
1019
1020 TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. "
1021 "Total memory footprint: 0x%08x bytes ]\n", pid, si->name,
1022 (unsigned)si->base, si->size);
1023 return 0;
1024
1025fail:
1026 /* We can just blindly unmap the entire region even though some things
1027 * were mapped in originally with anonymous and others could have been
1028 * been mapped in from the file before we failed. The kernel will unmap
1029 * all the pages in the range, irrespective of how they got there.
1030 */
1031 munmap((void *)si->base, si->size);
1032 si->flags |= FLAG_ERROR;
1033 return -1;
1034}
1035
1036/* TODO: Implement this to take care of the fact that Android ARM
1037 * ELF objects shove everything into a single loadable segment that has the
1038 * write bit set. wr_offset is then used to set non-(data|bss) pages to be
1039 * non-writable.
1040 */
1041#if 0
1042static unsigned
1043get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr)
1044{
1045 Elf32_Shdr *shdr_start;
1046 Elf32_Shdr *shdr;
1047 int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr);
1048 int cnt;
1049 unsigned wr_offset = 0xffffffff;
1050
1051 shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd,
1052 ehdr->e_shoff & (~PAGE_MASK));
1053 if (shdr_start == MAP_FAILED) {
1054 WARN("%5d - Could not read section header info from '%s'. Will not "
1055 "not be able to determine write-protect offset.\n", pid, name);
1056 return (unsigned)-1;
1057 }
1058
1059 for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) {
1060 if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) &&
1061 (shdr->sh_addr < wr_offset)) {
1062 wr_offset = shdr->sh_addr;
1063 }
1064 }
1065
1066 munmap(shdr_start, shdr_sz);
1067 return wr_offset;
1068}
1069#endif
1070
1071static soinfo *
1072load_library(const char *name)
1073{
1074 int fd = open_library(name);
1075 int cnt;
1076 unsigned ext_sz;
1077 unsigned req_base;
Erik Gillingfde86422009-07-28 20:28:19 -07001078 const char *bname;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001079 soinfo *si = NULL;
1080 Elf32_Ehdr *hdr;
1081
Dima Zavin2e855792009-05-20 18:28:09 -07001082 if(fd == -1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001083 DL_ERR("Library '%s' not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001084 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -07001085 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001086
1087 /* We have to read the ELF header to figure out what to do with this image
1088 */
1089 if (lseek(fd, 0, SEEK_SET) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001090 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001091 goto fail;
1092 }
1093
1094 if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001095 DL_ERR("read() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001096 goto fail;
1097 }
1098
1099 /* Parse the ELF header and get the size of the memory footprint for
1100 * the library */
1101 req_base = get_lib_extents(fd, name, &__header[0], &ext_sz);
1102 if (req_base == (unsigned)-1)
1103 goto fail;
1104 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
1105 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
1106
1107 /* Now configure the soinfo struct where we'll store all of our data
1108 * for the ELF object. If the loading fails, we waste the entry, but
1109 * same thing would happen if we failed during linking. Configuring the
1110 * soinfo struct here is a lot more convenient.
1111 */
Erik Gillingfde86422009-07-28 20:28:19 -07001112 bname = strrchr(name, '/');
1113 si = alloc_info(bname ? bname + 1 : name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001114 if (si == NULL)
1115 goto fail;
1116
1117 /* Carve out a chunk of memory where we will map in the individual
1118 * segments */
1119 si->base = req_base;
1120 si->size = ext_sz;
1121 si->flags = 0;
1122 si->entry = 0;
1123 si->dynamic = (unsigned *)-1;
1124 if (alloc_mem_region(si) < 0)
1125 goto fail;
1126
1127 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
1128 pid, name, (void *)si->base, (unsigned) ext_sz);
1129
1130 /* Now actually load the library's segments into right places in memory */
1131 if (load_segments(fd, &__header[0], si) < 0) {
1132 if (si->ba_index >= 0) {
Iliyan Malcheve100f522010-02-10 15:19:37 -08001133 ba_free(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001134 si->ba_index = -1;
1135 }
1136 goto fail;
1137 }
1138
1139 /* this might not be right. Technically, we don't even need this info
1140 * once we go through 'load_segments'. */
1141 hdr = (Elf32_Ehdr *)si->base;
1142 si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff);
1143 si->phnum = hdr->e_phnum;
1144 /**/
1145
1146 close(fd);
1147 return si;
1148
1149fail:
1150 if (si) free_info(si);
1151 close(fd);
1152 return NULL;
1153}
1154
1155static soinfo *
1156init_library(soinfo *si)
1157{
1158 unsigned wr_offset = 0xffffffff;
1159
1160 /* At this point we know that whatever is loaded @ base is a valid ELF
1161 * shared library whose segments are properly mapped in. */
1162 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
1163 pid, si->base, si->size, si->name);
1164
1165 if (si->base < LIBBASE || si->base >= LIBLAST)
1166 si->flags |= FLAG_PRELINKED;
1167
1168 if(link_image(si, wr_offset)) {
1169 /* We failed to link. However, we can only restore libbase
1170 ** if no additional libraries have moved it since we updated it.
1171 */
1172 munmap((void *)si->base, si->size);
1173 return NULL;
1174 }
1175
1176 return si;
1177}
1178
1179soinfo *find_library(const char *name)
1180{
1181 soinfo *si;
Erik Gillingfde86422009-07-28 20:28:19 -07001182 const char *bname = strrchr(name, '/');
1183 bname = bname ? bname + 1 : name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001184
1185 for(si = solist; si != 0; si = si->next){
Erik Gillingfde86422009-07-28 20:28:19 -07001186 if(!strcmp(bname, si->name)) {
Erik Gilling30eb4022009-08-13 16:05:30 -07001187 if(si->flags & FLAG_ERROR) {
1188 DL_ERR("%5d '%s' failed to load previously", pid, bname);
1189 return NULL;
1190 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001191 if(si->flags & FLAG_LINKED) return si;
Erik Gillingd00d23a2009-07-22 17:06:11 -07001192 DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name);
Dima Zavin2e855792009-05-20 18:28:09 -07001193 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001194 }
1195 }
1196
1197 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
1198 si = load_library(name);
1199 if(si == NULL)
1200 return NULL;
1201 return init_library(si);
1202}
1203
1204/* TODO:
1205 * notify gdb of unload
1206 * for non-prelinked libraries, find a way to decrement libbase
1207 */
1208static void call_destructors(soinfo *si);
1209unsigned unload_library(soinfo *si)
1210{
1211 unsigned *d;
1212 if (si->refcount == 1) {
1213 TRACE("%5d unloading '%s'\n", pid, si->name);
1214 call_destructors(si);
1215
1216 for(d = si->dynamic; *d; d += 2) {
1217 if(d[0] == DT_NEEDED){
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001218 soinfo *lsi = (soinfo *)d[1];
1219 d[1] = 0;
1220 if (validate_soinfo(lsi)) {
1221 TRACE("%5d %s needs to unload %s\n", pid,
1222 si->name, lsi->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001223 unload_library(lsi);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001224 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001225 else
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001226 DL_ERR("%5d %s: could not unload dependent library",
1227 pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001228 }
1229 }
1230
1231 munmap((char *)si->base, si->size);
1232 if (si->ba_index >= 0) {
1233 PRINT("%5d releasing library '%s' address space at %08x "\
1234 "through buddy allocator.\n",
1235 pid, si->name, si->base);
Iliyan Malcheve100f522010-02-10 15:19:37 -08001236 ba_free(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001237 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001238 notify_gdb_of_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001239 free_info(si);
1240 si->refcount = 0;
1241 }
1242 else {
1243 si->refcount--;
1244 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
1245 pid, si->name, si->refcount);
1246 }
1247 return si->refcount;
1248}
1249
1250/* TODO: don't use unsigned for addrs below. It works, but is not
1251 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1252 * long.
1253 */
1254static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count)
1255{
1256 Elf32_Sym *symtab = si->symtab;
1257 const char *strtab = si->strtab;
1258 Elf32_Sym *s;
1259 unsigned base;
1260 Elf32_Rel *start = rel;
1261 unsigned idx;
1262
1263 for (idx = 0; idx < count; ++idx) {
1264 unsigned type = ELF32_R_TYPE(rel->r_info);
1265 unsigned sym = ELF32_R_SYM(rel->r_info);
1266 unsigned reloc = (unsigned)(rel->r_offset + si->base);
1267 unsigned sym_addr = 0;
1268 char *sym_name = NULL;
1269
1270 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1271 si->name, idx);
1272 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001273 sym_name = (char *)(strtab + symtab[sym].st_name);
1274 s = _do_lookup(si, sym_name, &base);
Doug Kwane8238072009-10-26 12:05:23 -07001275 if(s == NULL) {
1276 /* We only allow an undefined symbol if this is a weak
1277 reference.. */
1278 s = &symtab[sym];
1279 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1280 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
1281 return -1;
1282 }
1283
1284 /* IHI0044C AAELF 4.5.1.1:
1285
1286 Libraries are not searched to resolve weak references.
1287 It is not an error for a weak reference to remain
1288 unsatisfied.
1289
1290 During linking, the value of an undefined weak reference is:
1291 - Zero if the relocation type is absolute
1292 - The address of the place if the relocation is pc-relative
1293 - The address of nominial base address if the relocation
1294 type is base-relative.
1295 */
1296
1297 switch (type) {
1298#if defined(ANDROID_ARM_LINKER)
1299 case R_ARM_JUMP_SLOT:
1300 case R_ARM_GLOB_DAT:
1301 case R_ARM_ABS32:
1302 case R_ARM_RELATIVE: /* Don't care. */
1303 case R_ARM_NONE: /* Don't care. */
1304#elif defined(ANDROID_X86_LINKER)
1305 case R_386_JUMP_SLOT:
1306 case R_386_GLOB_DAT:
1307 case R_386_32:
1308 case R_386_RELATIVE: /* Dont' care. */
1309#endif /* ANDROID_*_LINKER */
1310 /* sym_addr was initialized to be zero above or relocation
1311 code below does not care about value of sym_addr.
1312 No need to do anything. */
1313 break;
1314
1315#if defined(ANDROID_X86_LINKER)
1316 case R_386_PC32:
1317 sym_addr = reloc;
1318 break;
1319#endif /* ANDROID_X86_LINKER */
1320
1321#if defined(ANDROID_ARM_LINKER)
1322 case R_ARM_COPY:
1323 /* Fall through. Can't really copy if weak symbol is
1324 not found in run-time. */
1325#endif /* ANDROID_ARM_LINKER */
1326 default:
1327 DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n",
1328 pid, type, rel, (int) (rel - start));
1329 return -1;
1330 }
1331 } else {
1332 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001333#if 0
1334 if((base == 0) && (si->base != 0)){
1335 /* linking from libraries to main image is bad */
Erik Gillingd00d23a2009-07-22 17:06:11 -07001336 DL_ERR("%5d cannot locate '%s'...",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001337 pid, strtab + symtab[sym].st_name);
1338 return -1;
1339 }
1340#endif
Doug Kwane8238072009-10-26 12:05:23 -07001341 sym_addr = (unsigned)(s->st_value + base);
1342 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001343 COUNT_RELOC(RELOC_SYMBOL);
1344 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001345 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001346 }
1347
1348/* TODO: This is ugly. Split up the relocations by arch into
1349 * different files.
1350 */
1351 switch(type){
1352#if defined(ANDROID_ARM_LINKER)
1353 case R_ARM_JUMP_SLOT:
1354 COUNT_RELOC(RELOC_ABSOLUTE);
1355 MARK(rel->r_offset);
1356 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1357 reloc, sym_addr, sym_name);
1358 *((unsigned*)reloc) = sym_addr;
1359 break;
1360 case R_ARM_GLOB_DAT:
1361 COUNT_RELOC(RELOC_ABSOLUTE);
1362 MARK(rel->r_offset);
1363 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1364 reloc, sym_addr, sym_name);
1365 *((unsigned*)reloc) = sym_addr;
1366 break;
1367 case R_ARM_ABS32:
1368 COUNT_RELOC(RELOC_ABSOLUTE);
1369 MARK(rel->r_offset);
1370 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1371 reloc, sym_addr, sym_name);
1372 *((unsigned*)reloc) += sym_addr;
1373 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001374 case R_ARM_REL32:
1375 COUNT_RELOC(RELOC_RELATIVE);
1376 MARK(rel->r_offset);
1377 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1378 reloc, sym_addr, rel->r_offset, sym_name);
1379 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1380 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001381#elif defined(ANDROID_X86_LINKER)
1382 case R_386_JUMP_SLOT:
1383 COUNT_RELOC(RELOC_ABSOLUTE);
1384 MARK(rel->r_offset);
1385 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1386 reloc, sym_addr, sym_name);
1387 *((unsigned*)reloc) = sym_addr;
1388 break;
1389 case R_386_GLOB_DAT:
1390 COUNT_RELOC(RELOC_ABSOLUTE);
1391 MARK(rel->r_offset);
1392 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1393 reloc, sym_addr, sym_name);
1394 *((unsigned*)reloc) = sym_addr;
1395 break;
1396#endif /* ANDROID_*_LINKER */
1397
1398#if defined(ANDROID_ARM_LINKER)
1399 case R_ARM_RELATIVE:
1400#elif defined(ANDROID_X86_LINKER)
1401 case R_386_RELATIVE:
1402#endif /* ANDROID_*_LINKER */
1403 COUNT_RELOC(RELOC_RELATIVE);
1404 MARK(rel->r_offset);
1405 if(sym){
Erik Gillingd00d23a2009-07-22 17:06:11 -07001406 DL_ERR("%5d odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001407 return -1;
1408 }
1409 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1410 reloc, si->base);
1411 *((unsigned*)reloc) += si->base;
1412 break;
1413
1414#if defined(ANDROID_X86_LINKER)
1415 case R_386_32:
1416 COUNT_RELOC(RELOC_RELATIVE);
1417 MARK(rel->r_offset);
1418
1419 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1420 reloc, sym_addr, sym_name);
1421 *((unsigned *)reloc) += (unsigned)sym_addr;
1422 break;
1423
1424 case R_386_PC32:
1425 COUNT_RELOC(RELOC_RELATIVE);
1426 MARK(rel->r_offset);
1427 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1428 "+%08x (%08x - %08x) %s\n", pid, reloc,
1429 (sym_addr - reloc), sym_addr, reloc, sym_name);
1430 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1431 break;
1432#endif /* ANDROID_X86_LINKER */
1433
1434#ifdef ANDROID_ARM_LINKER
1435 case R_ARM_COPY:
1436 COUNT_RELOC(RELOC_COPY);
1437 MARK(rel->r_offset);
1438 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1439 reloc, s->st_size, sym_addr, sym_name);
1440 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1441 break;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001442 case R_ARM_NONE:
1443 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001444#endif /* ANDROID_ARM_LINKER */
1445
1446 default:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001447 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001448 pid, type, rel, (int) (rel - start));
1449 return -1;
1450 }
1451 rel++;
1452 }
1453 return 0;
1454}
1455
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001456#if defined(ANDROID_SH_LINKER)
1457static int reloc_library_a(soinfo *si, Elf32_Rela *rela, unsigned count)
1458{
1459 Elf32_Sym *symtab = si->symtab;
1460 const char *strtab = si->strtab;
1461 Elf32_Sym *s;
1462 unsigned base;
1463 Elf32_Rela *start = rela;
1464 unsigned idx;
1465
1466 for (idx = 0; idx < count; ++idx) {
1467 unsigned type = ELF32_R_TYPE(rela->r_info);
1468 unsigned sym = ELF32_R_SYM(rela->r_info);
1469 unsigned reloc = (unsigned)(rela->r_offset + si->base);
1470 unsigned sym_addr = 0;
1471 char *sym_name = NULL;
1472
1473 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1474 si->name, idx);
1475 if(sym != 0) {
1476 sym_name = (char *)(strtab + symtab[sym].st_name);
1477 s = _do_lookup(si, sym_name, &base);
1478 if(s == 0) {
1479 DL_ERR("%5d cannot locate '%s'...", pid, sym_name);
1480 return -1;
1481 }
1482#if 0
1483 if((base == 0) && (si->base != 0)){
1484 /* linking from libraries to main image is bad */
1485 DL_ERR("%5d cannot locate '%s'...",
1486 pid, strtab + symtab[sym].st_name);
1487 return -1;
1488 }
1489#endif
1490 if ((s->st_shndx == SHN_UNDEF) && (s->st_value != 0)) {
1491 DL_ERR("%5d In '%s', shndx=%d && value=0x%08x. We do not "
1492 "handle this yet", pid, si->name, s->st_shndx,
1493 s->st_value);
1494 return -1;
1495 }
1496 sym_addr = (unsigned)(s->st_value + base);
1497 COUNT_RELOC(RELOC_SYMBOL);
1498 } else {
1499 s = 0;
1500 }
1501
1502/* TODO: This is ugly. Split up the relocations by arch into
1503 * different files.
1504 */
1505 switch(type){
1506 case R_SH_JUMP_SLOT:
1507 COUNT_RELOC(RELOC_ABSOLUTE);
1508 MARK(rela->r_offset);
1509 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1510 reloc, sym_addr, sym_name);
1511 *((unsigned*)reloc) = sym_addr;
1512 break;
1513 case R_SH_GLOB_DAT:
1514 COUNT_RELOC(RELOC_ABSOLUTE);
1515 MARK(rela->r_offset);
1516 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1517 reloc, sym_addr, sym_name);
1518 *((unsigned*)reloc) = sym_addr;
1519 break;
1520 case R_SH_DIR32:
1521 COUNT_RELOC(RELOC_ABSOLUTE);
1522 MARK(rela->r_offset);
1523 TRACE_TYPE(RELO, "%5d RELO DIR32 %08x <- %08x %s\n", pid,
1524 reloc, sym_addr, sym_name);
1525 *((unsigned*)reloc) += sym_addr;
1526 break;
1527 case R_SH_RELATIVE:
1528 COUNT_RELOC(RELOC_RELATIVE);
1529 MARK(rela->r_offset);
1530 if(sym){
1531 DL_ERR("%5d odd RELATIVE form...", pid);
1532 return -1;
1533 }
1534 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1535 reloc, si->base);
1536 *((unsigned*)reloc) += si->base;
1537 break;
1538
1539 default:
1540 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
1541 pid, type, rela, (int) (rela - start));
1542 return -1;
1543 }
1544 rela++;
1545 }
1546 return 0;
1547}
1548#endif /* ANDROID_SH_LINKER */
1549
David 'Digit' Turner82156792009-05-18 14:37:41 +02001550
1551/* Please read the "Initialization and Termination functions" functions.
1552 * of the linker design note in bionic/linker/README.TXT to understand
1553 * what the following code is doing.
1554 *
1555 * The important things to remember are:
1556 *
1557 * DT_PREINIT_ARRAY must be called first for executables, and should
1558 * not appear in shared libraries.
1559 *
1560 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1561 *
1562 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1563 *
1564 * DT_FINI_ARRAY must be parsed in reverse order.
1565 */
1566
1567static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001568{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001569 int n, inc = 1;
1570
1571 if (reverse) {
1572 ctor += (count-1);
1573 inc = -1;
1574 }
1575
1576 for(n = count; n > 0; n--) {
1577 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1578 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001579 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001580 void (*func)() = (void (*)()) *ctor;
1581 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001582 if(((int) func == 0) || ((int) func == -1)) continue;
1583 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1584 func();
1585 }
1586}
1587
1588static void call_constructors(soinfo *si)
1589{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001590 if (si->flags & FLAG_EXE) {
1591 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1592 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1593 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001594 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001595 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1596 } else {
1597 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001598 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
Erik Gillingd00d23a2009-07-22 17:06:11 -07001599 " This is INVALID.", pid, si->name,
Dima Zavin2e855792009-05-20 18:28:09 -07001600 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001601 }
1602 }
1603
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001604 if (si->init_func) {
1605 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1606 (unsigned)si->init_func, si->name);
1607 si->init_func();
1608 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1609 }
1610
1611 if (si->init_array) {
1612 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1613 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001614 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001615 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1616 }
1617}
1618
David 'Digit' Turner82156792009-05-18 14:37:41 +02001619
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001620static void call_destructors(soinfo *si)
1621{
1622 if (si->fini_array) {
1623 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1624 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001625 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001626 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1627 }
1628
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001629 if (si->fini_func) {
1630 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1631 (unsigned)si->fini_func, si->name);
1632 si->fini_func();
1633 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1634 }
1635}
1636
1637/* Force any of the closed stdin, stdout and stderr to be associated with
1638 /dev/null. */
1639static int nullify_closed_stdio (void)
1640{
1641 int dev_null, i, status;
1642 int return_value = 0;
1643
1644 dev_null = open("/dev/null", O_RDWR);
1645 if (dev_null < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001646 DL_ERR("Cannot open /dev/null.");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001647 return -1;
1648 }
1649 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1650
1651 /* If any of the stdio file descriptors is valid and not associated
1652 with /dev/null, dup /dev/null to it. */
1653 for (i = 0; i < 3; i++) {
1654 /* If it is /dev/null already, we are done. */
1655 if (i == dev_null)
1656 continue;
1657
1658 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1659 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1660 can be interrupted but we do this just to be safe. */
1661 do {
1662 status = fcntl(i, F_GETFL);
1663 } while (status < 0 && errno == EINTR);
1664
1665 /* If file is openned, we are good. */
1666 if (status >= 0)
1667 continue;
1668
1669 /* The only error we allow is that the file descriptor does not
1670 exist, in which case we dup /dev/null to it. */
1671 if (errno != EBADF) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001672 DL_ERR("nullify_stdio: unhandled error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001673 return_value = -1;
1674 continue;
1675 }
1676
1677 /* Try dupping /dev/null to this stdio file descriptor and
1678 repeat if there is a signal. Note that any errors in closing
1679 the stdio descriptor are lost. */
1680 do {
1681 status = dup2(dev_null, i);
1682 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001683
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001684 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001685 DL_ERR("nullify_stdio: dup2 error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001686 return_value = -1;
1687 continue;
1688 }
1689 }
1690
1691 /* If /dev/null is not one of the stdio file descriptors, close it. */
1692 if (dev_null > 2) {
1693 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001694 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001695 status = close(dev_null);
1696 } while (status < 0 && errno == EINTR);
1697
1698 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001699 DL_ERR("nullify_stdio: close error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001700 return_value = -1;
1701 }
1702 }
1703
1704 return return_value;
1705}
1706
1707static int link_image(soinfo *si, unsigned wr_offset)
1708{
1709 unsigned *d;
1710 Elf32_Phdr *phdr = si->phdr;
1711 int phnum = si->phnum;
1712
1713 INFO("[ %5d linking %s ]\n", pid, si->name);
1714 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1715 si->base, si->flags);
1716
1717 if (si->flags & FLAG_EXE) {
1718 /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for
1719 * linkage info if this is the executable. If this was a
1720 * dynamic lib, that would have been done at load time.
1721 *
1722 * TODO: It's unfortunate that small pieces of this are
1723 * repeated from the load_library routine. Refactor this just
1724 * slightly to reuse these bits.
1725 */
1726 si->size = 0;
1727 for(; phnum > 0; --phnum, ++phdr) {
1728#ifdef ANDROID_ARM_LINKER
1729 if(phdr->p_type == PT_ARM_EXIDX) {
1730 /* exidx entries (used for stack unwinding) are 8 bytes each.
1731 */
1732 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1733 si->ARM_exidx_count = phdr->p_memsz / 8;
1734 }
1735#endif
1736 if (phdr->p_type == PT_LOAD) {
1737 /* For the executable, we use the si->size field only in
1738 dl_unwind_find_exidx(), so the meaning of si->size
1739 is not the size of the executable; it is the last
1740 virtual address of the loadable part of the executable;
1741 since si->base == 0 for an executable, we use the
1742 range [0, si->size) to determine whether a PC value
1743 falls within the executable section. Of course, if
1744 a value is below phdr->p_vaddr, it's not in the
1745 executable section, but a) we shouldn't be asking for
1746 such a value anyway, and b) if we have to provide
1747 an EXIDX for such a value, then the executable's
1748 EXIDX is probably the better choice.
1749 */
1750 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
1751 if (phdr->p_vaddr + phdr->p_memsz > si->size)
1752 si->size = phdr->p_vaddr + phdr->p_memsz;
1753 /* try to remember what range of addresses should be write
1754 * protected */
1755 if (!(phdr->p_flags & PF_W)) {
1756 unsigned _end;
1757
1758 if (phdr->p_vaddr < si->wrprotect_start)
1759 si->wrprotect_start = phdr->p_vaddr;
1760 _end = (((phdr->p_vaddr + phdr->p_memsz + PAGE_SIZE - 1) &
1761 (~PAGE_MASK)));
1762 if (_end > si->wrprotect_end)
1763 si->wrprotect_end = _end;
1764 }
1765 } else if (phdr->p_type == PT_DYNAMIC) {
1766 if (si->dynamic != (unsigned *)-1) {
Dima Zavin2e855792009-05-20 18:28:09 -07001767 DL_ERR("%5d multiple PT_DYNAMIC segments found in '%s'. "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001768 "Segment at 0x%08x, previously one found at 0x%08x",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001769 pid, si->name, si->base + phdr->p_vaddr,
1770 (unsigned)si->dynamic);
1771 goto fail;
1772 }
1773 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
1774 si->dynamic = (unsigned *) (si->base + phdr->p_vaddr);
1775 }
1776 }
1777 }
1778
1779 if (si->dynamic == (unsigned *)-1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001780 DL_ERR("%5d missing PT_DYNAMIC?!", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001781 goto fail;
1782 }
1783
1784 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1785
1786 /* extract useful information from dynamic section */
1787 for(d = si->dynamic; *d; d++){
1788 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1789 switch(*d++){
1790 case DT_HASH:
1791 si->nbucket = ((unsigned *) (si->base + *d))[0];
1792 si->nchain = ((unsigned *) (si->base + *d))[1];
1793 si->bucket = (unsigned *) (si->base + *d + 8);
1794 si->chain = (unsigned *) (si->base + *d + 8 + si->nbucket * 4);
1795 break;
1796 case DT_STRTAB:
1797 si->strtab = (const char *) (si->base + *d);
1798 break;
1799 case DT_SYMTAB:
1800 si->symtab = (Elf32_Sym *) (si->base + *d);
1801 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001802#if !defined(ANDROID_SH_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001803 case DT_PLTREL:
1804 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001805 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001806 goto fail;
1807 }
1808 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001809#endif
1810#ifdef ANDROID_SH_LINKER
1811 case DT_JMPREL:
1812 si->plt_rela = (Elf32_Rela*) (si->base + *d);
1813 break;
1814 case DT_PLTRELSZ:
1815 si->plt_rela_count = *d / sizeof(Elf32_Rela);
1816 break;
1817#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001818 case DT_JMPREL:
1819 si->plt_rel = (Elf32_Rel*) (si->base + *d);
1820 break;
1821 case DT_PLTRELSZ:
1822 si->plt_rel_count = *d / 8;
1823 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001824#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001825 case DT_REL:
1826 si->rel = (Elf32_Rel*) (si->base + *d);
1827 break;
1828 case DT_RELSZ:
1829 si->rel_count = *d / 8;
1830 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001831#ifdef ANDROID_SH_LINKER
1832 case DT_RELASZ:
1833 si->rela_count = *d / sizeof(Elf32_Rela);
1834 break;
1835#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001836 case DT_PLTGOT:
1837 /* Save this in case we decide to do lazy binding. We don't yet. */
1838 si->plt_got = (unsigned *)(si->base + *d);
1839 break;
1840 case DT_DEBUG:
1841 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1842 *d = (int) &_r_debug;
1843 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001844#ifdef ANDROID_SH_LINKER
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001845 case DT_RELA:
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001846 si->rela = (Elf32_Rela *) (si->base + *d);
1847 break;
1848#else
1849 case DT_RELA:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001850 DL_ERR("%5d DT_RELA not supported", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001851 goto fail;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001852#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001853 case DT_INIT:
1854 si->init_func = (void (*)(void))(si->base + *d);
1855 DEBUG("%5d %s constructors (init func) found at %p\n",
1856 pid, si->name, si->init_func);
1857 break;
1858 case DT_FINI:
1859 si->fini_func = (void (*)(void))(si->base + *d);
1860 DEBUG("%5d %s destructors (fini func) found at %p\n",
1861 pid, si->name, si->fini_func);
1862 break;
1863 case DT_INIT_ARRAY:
1864 si->init_array = (unsigned *)(si->base + *d);
1865 DEBUG("%5d %s constructors (init_array) found at %p\n",
1866 pid, si->name, si->init_array);
1867 break;
1868 case DT_INIT_ARRAYSZ:
1869 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1870 break;
1871 case DT_FINI_ARRAY:
1872 si->fini_array = (unsigned *)(si->base + *d);
1873 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1874 pid, si->name, si->fini_array);
1875 break;
1876 case DT_FINI_ARRAYSZ:
1877 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1878 break;
1879 case DT_PREINIT_ARRAY:
1880 si->preinit_array = (unsigned *)(si->base + *d);
1881 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1882 pid, si->name, si->preinit_array);
1883 break;
1884 case DT_PREINIT_ARRAYSZ:
1885 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1886 break;
1887 case DT_TEXTREL:
1888 /* TODO: make use of this. */
1889 /* this means that we might have to write into where the text
1890 * segment was loaded during relocation... Do something with
1891 * it.
1892 */
1893 DEBUG("%5d Text segment should be writable during relocation.\n",
1894 pid);
1895 break;
1896 }
1897 }
1898
1899 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
1900 pid, si->base, si->strtab, si->symtab);
1901
1902 if((si->strtab == 0) || (si->symtab == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001903 DL_ERR("%5d missing essential tables", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001904 goto fail;
1905 }
1906
1907 for(d = si->dynamic; *d; d += 2) {
1908 if(d[0] == DT_NEEDED){
1909 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001910 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001911 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001912 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Erik Gillingd00d23a2009-07-22 17:06:11 -07001913 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
Dima Zavin03531952009-05-29 17:30:25 -07001914 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001915 goto fail;
1916 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001917 /* Save the soinfo of the loaded DT_NEEDED library in the payload
1918 of the DT_NEEDED entry itself, so that we can retrieve the
1919 soinfo directly later from the dynamic segment. This is a hack,
1920 but it allows us to map from DT_NEEDED to soinfo efficiently
1921 later on when we resolve relocations, trying to look up a symgol
1922 with dlsym().
1923 */
1924 d[1] = (unsigned)lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001925 lsi->refcount++;
1926 }
1927 }
1928
1929 if(si->plt_rel) {
1930 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1931 if(reloc_library(si, si->plt_rel, si->plt_rel_count))
1932 goto fail;
1933 }
1934 if(si->rel) {
1935 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1936 if(reloc_library(si, si->rel, si->rel_count))
1937 goto fail;
1938 }
1939
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001940#ifdef ANDROID_SH_LINKER
1941 if(si->plt_rela) {
1942 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1943 if(reloc_library_a(si, si->plt_rela, si->plt_rela_count))
1944 goto fail;
1945 }
1946 if(si->rela) {
1947 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1948 if(reloc_library_a(si, si->rela, si->rela_count))
1949 goto fail;
1950 }
1951#endif /* ANDROID_SH_LINKER */
1952
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001953 si->flags |= FLAG_LINKED;
1954 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1955
1956#if 0
1957 /* This is the way that the old dynamic linker did protection of
1958 * non-writable areas. It would scan section headers and find where
1959 * .text ended (rather where .data/.bss began) and assume that this is
1960 * the upper range of the non-writable area. This is too coarse,
1961 * and is kept here for reference until we fully move away from single
1962 * segment elf objects. See the code in get_wr_offset (also #if'd 0)
1963 * that made this possible.
1964 */
1965 if(wr_offset < 0xffffffff){
1966 mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC);
1967 }
1968#else
1969 /* TODO: Verify that this does the right thing in all cases, as it
1970 * presently probably does not. It is possible that an ELF image will
1971 * come with multiple read-only segments. What we ought to do is scan
1972 * the program headers again and mprotect all the read-only segments.
1973 * To prevent re-scanning the program header, we would have to build a
1974 * list of loadable segments in si, and then scan that instead. */
1975 if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) {
1976 mprotect((void *)si->wrprotect_start,
1977 si->wrprotect_end - si->wrprotect_start,
1978 PROT_READ | PROT_EXEC);
1979 }
1980#endif
1981
1982 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1983 stdout and stderr to close a security hole described in:
1984
1985 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1986
1987 */
1988 if (getuid() != geteuid() || getgid() != getegid())
1989 nullify_closed_stdio ();
1990 call_constructors(si);
1991 notify_gdb_of_load(si);
1992 return 0;
1993
1994fail:
Doug Kwan94304352009-10-23 18:11:40 -07001995 DL_ERR("failed to link %s\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001996 si->flags |= FLAG_ERROR;
1997 return -1;
1998}
1999
David Bartleybc3a5c22009-06-02 18:27:28 -07002000static void parse_library_path(char *path, char *delim)
2001{
2002 size_t len;
2003 char *ldpaths_bufp = ldpaths_buf;
2004 int i = 0;
2005
2006 len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf));
2007
2008 while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) {
2009 if (*ldpaths[i] != '\0')
2010 ++i;
2011 }
2012
2013 /* Forget the last path if we had to truncate; this occurs if the 2nd to
2014 * last char isn't '\0' (i.e. not originally a delim). */
2015 if (i > 0 && len >= sizeof(ldpaths_buf) &&
2016 ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') {
2017 ldpaths[i - 1] = NULL;
2018 } else {
2019 ldpaths[i] = NULL;
2020 }
2021}
2022
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002023int main(int argc, char **argv)
2024{
2025 return 0;
2026}
2027
2028#define ANDROID_TLS_SLOTS BIONIC_TLS_SLOTS
2029
2030static void * __tls_area[ANDROID_TLS_SLOTS];
2031
2032unsigned __linker_init(unsigned **elfdata)
2033{
2034 static soinfo linker_soinfo;
2035
2036 int argc = (int) *elfdata;
2037 char **argv = (char**) (elfdata + 1);
2038 unsigned *vecs = (unsigned*) (argv + argc + 1);
2039 soinfo *si;
2040 struct link_map * map;
David Bartleybc3a5c22009-06-02 18:27:28 -07002041 char *ldpath_env = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002042
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02002043 /* Setup a temporary TLS area that is used to get a working
2044 * errno for system calls.
2045 */
2046 __set_tls(__tls_area);
2047
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002048 pid = getpid();
2049
2050#if TIMING
2051 struct timeval t0, t1;
2052 gettimeofday(&t0, 0);
2053#endif
2054
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02002055 /* NOTE: we store the elfdata pointer on a special location
2056 * of the temporary TLS area in order to pass it to
2057 * the C Library's runtime initializer.
2058 *
2059 * The initializer must clear the slot and reset the TLS
2060 * to point to a different location to ensure that no other
2061 * shared library constructor can access it.
2062 */
2063 __tls_area[TLS_SLOT_BIONIC_PREINIT] = elfdata;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002064
2065 debugger_init();
2066
2067 /* skip past the environment */
2068 while(vecs[0] != 0) {
2069 if(!strncmp((char*) vecs[0], "DEBUG=", 6)) {
2070 debug_verbosity = atoi(((char*) vecs[0]) + 6);
David Bartleybc3a5c22009-06-02 18:27:28 -07002071 } else if(!strncmp((char*) vecs[0], "LD_LIBRARY_PATH=", 16)) {
2072 ldpath_env = (char*) vecs[0] + 16;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002073 }
2074 vecs++;
2075 }
2076 vecs++;
2077
2078 INFO("[ android linker & debugger ]\n");
2079 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
2080
2081 si = alloc_info(argv[0]);
2082 if(si == 0) {
2083 exit(-1);
2084 }
2085
2086 /* bootstrap the link map, the main exe always needs to be first */
2087 si->flags |= FLAG_EXE;
2088 map = &(si->linkmap);
2089
2090 map->l_addr = 0;
2091 map->l_name = argv[0];
2092 map->l_prev = NULL;
2093 map->l_next = NULL;
2094
2095 _r_debug.r_map = map;
2096 r_debug_tail = map;
2097
2098 /* gdb expects the linker to be in the debug shared object list,
2099 * and we need to make sure that the reported load address is zero.
2100 * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
2101 * is. Don't use alloc_info(), because the linker shouldn't
2102 * be on the soinfo list.
2103 */
2104 strcpy((char*) linker_soinfo.name, "/system/bin/linker");
2105 linker_soinfo.flags = 0;
2106 linker_soinfo.base = 0; // This is the important part; must be zero.
2107 insert_soinfo_into_debug_map(&linker_soinfo);
2108
2109 /* extract information passed from the kernel */
2110 while(vecs[0] != 0){
2111 switch(vecs[0]){
2112 case AT_PHDR:
2113 si->phdr = (Elf32_Phdr*) vecs[1];
2114 break;
2115 case AT_PHNUM:
2116 si->phnum = (int) vecs[1];
2117 break;
2118 case AT_ENTRY:
2119 si->entry = vecs[1];
2120 break;
2121 }
2122 vecs += 2;
2123 }
2124
Iliyan Malcheve100f522010-02-10 15:19:37 -08002125 ba_init(&ba_nonprelink);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002126
2127 si->base = 0;
2128 si->dynamic = (unsigned *)-1;
2129 si->wrprotect_start = 0xffffffff;
2130 si->wrprotect_end = 0;
2131
David Bartleybc3a5c22009-06-02 18:27:28 -07002132 /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */
2133 if (ldpath_env && getuid() == geteuid() && getgid() == getegid())
2134 parse_library_path(ldpath_env, ":");
2135
Dima Zavin2e855792009-05-20 18:28:09 -07002136 if(link_image(si, 0)) {
2137 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
2138 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2139 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002140 exit(-1);
2141 }
2142
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07002143#if ALLOW_SYMBOLS_FROM_MAIN
2144 /* Set somain after we've loaded all the libraries in order to prevent
2145 * linking of symbols back to the main image, which is not set up at that
2146 * point yet.
2147 */
2148 somain = si;
2149#endif
2150
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002151#if TIMING
2152 gettimeofday(&t1,NULL);
2153 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
2154 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2155 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
2156 ));
2157#endif
2158#if STATS
2159 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
2160 linker_stats.reloc[RELOC_ABSOLUTE],
2161 linker_stats.reloc[RELOC_RELATIVE],
2162 linker_stats.reloc[RELOC_COPY],
2163 linker_stats.reloc[RELOC_SYMBOL]);
2164#endif
2165#if COUNT_PAGES
2166 {
2167 unsigned n;
2168 unsigned i;
2169 unsigned count = 0;
2170 for(n = 0; n < 4096; n++){
2171 if(bitmask[n]){
2172 unsigned x = bitmask[n];
2173 for(i = 0; i < 8; i++){
2174 if(x & 1) count++;
2175 x >>= 1;
2176 }
2177 }
2178 }
2179 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
2180 }
2181#endif
2182
2183#if TIMING || STATS || COUNT_PAGES
2184 fflush(stdout);
2185#endif
2186
2187 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
2188 si->entry);
2189 return si->entry;
2190}