blob: 7eb1ef948e2940ed33326aa9f6f38dc466c1edcf [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);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700472 if(s != NULL)
473 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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800538#if 0
539static void dump(soinfo *si)
540{
541 Elf32_Sym *s = si->symtab;
542 unsigned n;
543
544 for(n = 0; n < si->nchain; n++) {
545 TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
546 s->st_info, s->st_shndx, s->st_value, s->st_size,
547 si->strtab + s->st_name);
548 s++;
549 }
550}
551#endif
552
553static const char *sopaths[] = {
554 "/system/lib",
555 "/lib",
556 0
557};
558
559static int _open_lib(const char *name)
560{
561 int fd;
562 struct stat filestat;
563
564 if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
565 if ((fd = open(name, O_RDONLY)) >= 0)
566 return fd;
567 }
568
569 return -1;
570}
571
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800572static int open_library(const char *name)
573{
574 int fd;
575 char buf[512];
576 const char **path;
David Bartleybc3a5c22009-06-02 18:27:28 -0700577 int n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800578
579 TRACE("[ %5d opening %s ]\n", pid, name);
580
581 if(name == 0) return -1;
582 if(strlen(name) > 256) return -1;
583
584 if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
585 return fd;
586
David Bartleybc3a5c22009-06-02 18:27:28 -0700587 for (path = ldpaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800588 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700589 if (n < 0 || n >= (int)sizeof(buf)) {
590 WARN("Ignoring very long library path: %s/%s\n", *path, name);
591 continue;
592 }
593 if ((fd = _open_lib(buf)) >= 0)
594 return fd;
595 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800596 for (path = sopaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800597 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700598 if (n < 0 || n >= (int)sizeof(buf)) {
599 WARN("Ignoring very long library path: %s/%s\n", *path, name);
600 continue;
601 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800602 if ((fd = _open_lib(buf)) >= 0)
603 return fd;
604 }
605
606 return -1;
607}
608
609/* temporary space for holding the first page of the shared lib
610 * which contains the elf header (with the pht). */
611static unsigned char __header[PAGE_SIZE];
612
613typedef struct {
614 long mmap_addr;
615 char tag[4]; /* 'P', 'R', 'E', ' ' */
616} prelink_info_t;
617
618/* Returns the requested base address if the library is prelinked,
619 * and 0 otherwise. */
620static unsigned long
621is_prelinked(int fd, const char *name)
622{
623 off_t sz;
624 prelink_info_t info;
625
626 sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
627 if (sz < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700628 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800629 return 0;
630 }
631
632 if (read(fd, &info, sizeof(info)) != sizeof(info)) {
633 WARN("Could not read prelink_info_t structure for `%s`\n", name);
634 return 0;
635 }
636
637 if (strncmp(info.tag, "PRE ", 4)) {
638 WARN("`%s` is not a prelinked library\n", name);
639 return 0;
640 }
641
642 return (unsigned long)info.mmap_addr;
643}
644
645/* verify_elf_object
646 * Verifies if the object @ base is a valid ELF object
647 *
648 * Args:
649 *
650 * Returns:
651 * 0 on success
652 * -1 if no valid ELF object is found @ base.
653 */
654static int
655verify_elf_object(void *base, const char *name)
656{
657 Elf32_Ehdr *hdr = (Elf32_Ehdr *) base;
658
659 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
660 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
661 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
662 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
663
664 /* TODO: Should we verify anything else in the header? */
665
666 return 0;
667}
668
669
670/* get_lib_extents
671 * Retrieves the base (*base) address where the ELF object should be
672 * mapped and its overall memory size (*total_sz).
673 *
674 * Args:
675 * fd: Opened file descriptor for the library
676 * name: The name of the library
677 * _hdr: Pointer to the header page of the library
678 * total_sz: Total size of the memory that should be allocated for
679 * this library
680 *
681 * Returns:
682 * -1 if there was an error while trying to get the lib extents.
683 * The possible reasons are:
684 * - Could not determine if the library was prelinked.
685 * - The library provided is not a valid ELF object
686 * 0 if the library did not request a specific base offset (normal
687 * for non-prelinked libs)
688 * > 0 if the library requests a specific address to be mapped to.
689 * This indicates a pre-linked library.
690 */
691static unsigned
692get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz)
693{
694 unsigned req_base;
695 unsigned min_vaddr = 0xffffffff;
696 unsigned max_vaddr = 0;
697 unsigned char *_hdr = (unsigned char *)__hdr;
698 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)_hdr;
699 Elf32_Phdr *phdr;
700 int cnt;
701
702 TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name);
703 if (verify_elf_object(_hdr, name) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700704 DL_ERR("%5d - %s is not a valid ELF object", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800705 return (unsigned)-1;
706 }
707
708 req_base = (unsigned) is_prelinked(fd, name);
709 if (req_base == (unsigned)-1)
710 return -1;
711 else if (req_base != 0) {
712 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
713 pid, name, req_base);
714 } else {
715 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
716 }
717
718 phdr = (Elf32_Phdr *)(_hdr + ehdr->e_phoff);
719
720 /* find the min/max p_vaddrs from all the PT_LOAD segments so we can
721 * get the range. */
722 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
723 if (phdr->p_type == PT_LOAD) {
724 if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr)
725 max_vaddr = phdr->p_vaddr + phdr->p_memsz;
726 if (phdr->p_vaddr < min_vaddr)
727 min_vaddr = phdr->p_vaddr;
728 }
729 }
730
731 if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700732 DL_ERR("%5d - No loadable segments found in %s.", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800733 return (unsigned)-1;
734 }
735
736 /* truncate min_vaddr down to page boundary */
737 min_vaddr &= ~PAGE_MASK;
738
739 /* round max_vaddr up to the next page */
740 max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK;
741
742 *total_sz = (max_vaddr - min_vaddr);
743 return (unsigned)req_base;
744}
745
746/* alloc_mem_region
747 *
748 * This function reserves a chunk of memory to be used for mapping in
749 * the shared library. We reserve the entire memory region here, and
750 * then the rest of the linker will relocate the individual loadable
751 * segments into the correct locations within this memory range.
752 *
753 * Args:
754 * si->base: The requested base of the allocation. If 0, a sane one will be
755 * chosen in the range LIBBASE <= base < LIBLAST.
756 * si->size: The size of the allocation.
757 *
758 * Returns:
759 * -1 on failure, and 0 on success. On success, si->base will contain
760 * the virtual address at which the library will be mapped.
761 */
762
763static int reserve_mem_region(soinfo *si)
764{
765 void *base = mmap((void *)si->base, si->size, PROT_READ | PROT_EXEC,
766 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
767 if (base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700768 DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700769 "as requested, will try general pool: %d (%s)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800770 pid, (si->base ? "" : "non-"), si->name, si->base,
771 errno, strerror(errno));
772 return -1;
773 } else if (base != (void *)si->base) {
Dima Zavin2e855792009-05-20 18:28:09 -0700774 DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700775 "not at 0x%08x", pid, (si->base ? "" : "non-"),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800776 si->name, (unsigned)base, si->base);
777 munmap(base, si->size);
778 return -1;
779 }
780 return 0;
781}
782
783static int
784alloc_mem_region(soinfo *si)
785{
786 if (si->base) {
787 /* Attempt to mmap a prelinked library. */
788 si->ba_index = -1;
789 return reserve_mem_region(si);
790 }
791
792 /* This is not a prelinked library, so we attempt to allocate space
793 for it from the buddy allocator, which manages the area between
794 LIBBASE and LIBLAST.
795 */
Iliyan Malcheve100f522010-02-10 15:19:37 -0800796 si->ba_index = ba_allocate(&ba_nonprelink, si->size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800797 if(si->ba_index >= 0) {
Iliyan Malcheve100f522010-02-10 15:19:37 -0800798 si->base = ba_start_addr(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800799 PRINT("%5d mapping library '%s' at %08x (index %d) " \
800 "through buddy allocator.\n",
801 pid, si->name, si->base, si->ba_index);
802 if (reserve_mem_region(si) < 0) {
Iliyan Malcheve100f522010-02-10 15:19:37 -0800803 ba_free(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800804 si->ba_index = -1;
805 si->base = 0;
806 goto err;
807 }
808 return 0;
809 }
810
811err:
Erik Gillingd00d23a2009-07-22 17:06:11 -0700812 DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800813 pid, si->name);
814 return -1;
815}
816
817#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
818#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
819 MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
820 MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
821/* load_segments
822 *
823 * This function loads all the loadable (PT_LOAD) segments into memory
824 * at their appropriate memory offsets off the base address.
825 *
826 * Args:
827 * fd: Open file descriptor to the library to load.
828 * header: Pointer to a header page that contains the ELF header.
829 * This is needed since we haven't mapped in the real file yet.
830 * si: ptr to soinfo struct describing the shared object.
831 *
832 * Returns:
833 * 0 on success, -1 on failure.
834 */
835static int
836load_segments(int fd, void *header, soinfo *si)
837{
838 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
839 Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned char *)header + ehdr->e_phoff);
840 unsigned char *base = (unsigned char *)si->base;
841 int cnt;
842 unsigned len;
843 unsigned char *tmp;
844 unsigned char *pbase;
845 unsigned char *extra_base;
846 unsigned extra_len;
847 unsigned total_sz = 0;
848
849 si->wrprotect_start = 0xffffffff;
850 si->wrprotect_end = 0;
851
852 TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n",
853 pid, si->name, (unsigned)si->base);
854 /* Now go through all the PT_LOAD segments and map them into memory
855 * at the appropriate locations. */
856 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
857 if (phdr->p_type == PT_LOAD) {
858 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
859 /* we want to map in the segment on a page boundary */
860 tmp = base + (phdr->p_vaddr & (~PAGE_MASK));
861 /* add the # of bytes we masked off above to the total length. */
862 len = phdr->p_filesz + (phdr->p_vaddr & PAGE_MASK);
863
864 TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x "
865 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name,
866 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
867 pbase = mmap(tmp, len, PFLAGS_TO_PROT(phdr->p_flags),
868 MAP_PRIVATE | MAP_FIXED, fd,
869 phdr->p_offset & (~PAGE_MASK));
870 if (pbase == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700871 DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700872 "p_vaddr=0x%08x p_offset=0x%08x", pid, si->name,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800873 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
874 goto fail;
875 }
876
877 /* If 'len' didn't end on page boundary, and it's a writable
878 * segment, zero-fill the rest. */
879 if ((len & PAGE_MASK) && (phdr->p_flags & PF_W))
880 memset((void *)(pbase + len), 0, PAGE_SIZE - (len & PAGE_MASK));
881
882 /* Check to see if we need to extend the map for this segment to
883 * cover the diff between filesz and memsz (i.e. for bss).
884 *
885 * base _+---------------------+ page boundary
886 * . .
887 * | |
888 * . .
889 * pbase _+---------------------+ page boundary
890 * | |
891 * . .
892 * base + p_vaddr _| |
893 * . \ \ .
894 * . | filesz | .
895 * pbase + len _| / | |
896 * <0 pad> . . .
897 * extra_base _+------------|--------+ page boundary
898 * / . . .
899 * | . . .
900 * | +------------|--------+ page boundary
901 * extra_len-> | | | |
902 * | . | memsz .
903 * | . | .
904 * \ _| / |
905 * . .
906 * | |
907 * _+---------------------+ page boundary
908 */
909 tmp = (unsigned char *)(((unsigned)pbase + len + PAGE_SIZE - 1) &
910 (~PAGE_MASK));
911 if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) {
912 extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp;
913 TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x "
914 "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len);
915 /* map in the extra page(s) as anonymous into the range.
916 * This is probably not necessary as we already mapped in
917 * the entire region previously, but we just want to be
918 * sure. This will also set the right flags on the region
919 * (though we can probably accomplish the same thing with
920 * mprotect).
921 */
922 extra_base = mmap((void *)tmp, extra_len,
923 PFLAGS_TO_PROT(phdr->p_flags),
924 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
925 -1, 0);
926 if (extra_base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700927 DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x"
Erik Gillingd00d23a2009-07-22 17:06:11 -0700928 " (0x%08x) ]", pid, si->name, (unsigned)tmp,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800929 extra_len);
930 goto fail;
931 }
932 /* TODO: Check if we need to memset-0 this region.
933 * Anonymous mappings are zero-filled copy-on-writes, so we
934 * shouldn't need to. */
935 TRACE("[ %5d - Segment from '%s' extended @ 0x%08x "
936 "(0x%08x)\n", pid, si->name, (unsigned)extra_base,
937 extra_len);
938 }
939 /* set the len here to show the full extent of the segment we
940 * just loaded, mostly for debugging */
941 len = (((unsigned)base + phdr->p_vaddr + phdr->p_memsz +
942 PAGE_SIZE - 1) & (~PAGE_MASK)) - (unsigned)pbase;
943 TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x "
944 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
945 (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset);
946 total_sz += len;
947 /* Make the section writable just in case we'll have to write to
948 * it during relocation (i.e. text segment). However, we will
949 * remember what range of addresses should be write protected.
950 *
951 */
952 if (!(phdr->p_flags & PF_W)) {
953 if ((unsigned)pbase < si->wrprotect_start)
954 si->wrprotect_start = (unsigned)pbase;
955 if (((unsigned)pbase + len) > si->wrprotect_end)
956 si->wrprotect_end = (unsigned)pbase + len;
957 mprotect(pbase, len,
958 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
959 }
960 } else if (phdr->p_type == PT_DYNAMIC) {
961 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
962 /* this segment contains the dynamic linking information */
963 si->dynamic = (unsigned *)(base + phdr->p_vaddr);
964 } else {
965#ifdef ANDROID_ARM_LINKER
966 if (phdr->p_type == PT_ARM_EXIDX) {
967 DEBUG_DUMP_PHDR(phdr, "PT_ARM_EXIDX", pid);
968 /* exidx entries (used for stack unwinding) are 8 bytes each.
969 */
970 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
971 si->ARM_exidx_count = phdr->p_memsz / 8;
972 }
973#endif
974 }
975
976 }
977
978 /* Sanity check */
979 if (total_sz > si->size) {
Dima Zavin2e855792009-05-20 18:28:09 -0700980 DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700981 "greater than what was allocated (0x%08x). THIS IS BAD!",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800982 pid, total_sz, si->name, si->size);
983 goto fail;
984 }
985
986 TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. "
987 "Total memory footprint: 0x%08x bytes ]\n", pid, si->name,
988 (unsigned)si->base, si->size);
989 return 0;
990
991fail:
992 /* We can just blindly unmap the entire region even though some things
993 * were mapped in originally with anonymous and others could have been
994 * been mapped in from the file before we failed. The kernel will unmap
995 * all the pages in the range, irrespective of how they got there.
996 */
997 munmap((void *)si->base, si->size);
998 si->flags |= FLAG_ERROR;
999 return -1;
1000}
1001
1002/* TODO: Implement this to take care of the fact that Android ARM
1003 * ELF objects shove everything into a single loadable segment that has the
1004 * write bit set. wr_offset is then used to set non-(data|bss) pages to be
1005 * non-writable.
1006 */
1007#if 0
1008static unsigned
1009get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr)
1010{
1011 Elf32_Shdr *shdr_start;
1012 Elf32_Shdr *shdr;
1013 int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr);
1014 int cnt;
1015 unsigned wr_offset = 0xffffffff;
1016
1017 shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd,
1018 ehdr->e_shoff & (~PAGE_MASK));
1019 if (shdr_start == MAP_FAILED) {
1020 WARN("%5d - Could not read section header info from '%s'. Will not "
1021 "not be able to determine write-protect offset.\n", pid, name);
1022 return (unsigned)-1;
1023 }
1024
1025 for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) {
1026 if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) &&
1027 (shdr->sh_addr < wr_offset)) {
1028 wr_offset = shdr->sh_addr;
1029 }
1030 }
1031
1032 munmap(shdr_start, shdr_sz);
1033 return wr_offset;
1034}
1035#endif
1036
1037static soinfo *
1038load_library(const char *name)
1039{
1040 int fd = open_library(name);
1041 int cnt;
1042 unsigned ext_sz;
1043 unsigned req_base;
Erik Gillingfde86422009-07-28 20:28:19 -07001044 const char *bname;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001045 soinfo *si = NULL;
1046 Elf32_Ehdr *hdr;
1047
Dima Zavin2e855792009-05-20 18:28:09 -07001048 if(fd == -1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001049 DL_ERR("Library '%s' not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001050 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -07001051 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001052
1053 /* We have to read the ELF header to figure out what to do with this image
1054 */
1055 if (lseek(fd, 0, SEEK_SET) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001056 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001057 goto fail;
1058 }
1059
1060 if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001061 DL_ERR("read() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001062 goto fail;
1063 }
1064
1065 /* Parse the ELF header and get the size of the memory footprint for
1066 * the library */
1067 req_base = get_lib_extents(fd, name, &__header[0], &ext_sz);
1068 if (req_base == (unsigned)-1)
1069 goto fail;
1070 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
1071 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
1072
1073 /* Now configure the soinfo struct where we'll store all of our data
1074 * for the ELF object. If the loading fails, we waste the entry, but
1075 * same thing would happen if we failed during linking. Configuring the
1076 * soinfo struct here is a lot more convenient.
1077 */
Erik Gillingfde86422009-07-28 20:28:19 -07001078 bname = strrchr(name, '/');
1079 si = alloc_info(bname ? bname + 1 : name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001080 if (si == NULL)
1081 goto fail;
1082
1083 /* Carve out a chunk of memory where we will map in the individual
1084 * segments */
1085 si->base = req_base;
1086 si->size = ext_sz;
1087 si->flags = 0;
1088 si->entry = 0;
1089 si->dynamic = (unsigned *)-1;
1090 if (alloc_mem_region(si) < 0)
1091 goto fail;
1092
1093 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
1094 pid, name, (void *)si->base, (unsigned) ext_sz);
1095
1096 /* Now actually load the library's segments into right places in memory */
1097 if (load_segments(fd, &__header[0], si) < 0) {
1098 if (si->ba_index >= 0) {
Iliyan Malcheve100f522010-02-10 15:19:37 -08001099 ba_free(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001100 si->ba_index = -1;
1101 }
1102 goto fail;
1103 }
1104
1105 /* this might not be right. Technically, we don't even need this info
1106 * once we go through 'load_segments'. */
1107 hdr = (Elf32_Ehdr *)si->base;
1108 si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff);
1109 si->phnum = hdr->e_phnum;
1110 /**/
1111
1112 close(fd);
1113 return si;
1114
1115fail:
1116 if (si) free_info(si);
1117 close(fd);
1118 return NULL;
1119}
1120
1121static soinfo *
1122init_library(soinfo *si)
1123{
1124 unsigned wr_offset = 0xffffffff;
1125
1126 /* At this point we know that whatever is loaded @ base is a valid ELF
1127 * shared library whose segments are properly mapped in. */
1128 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
1129 pid, si->base, si->size, si->name);
1130
1131 if (si->base < LIBBASE || si->base >= LIBLAST)
1132 si->flags |= FLAG_PRELINKED;
1133
1134 if(link_image(si, wr_offset)) {
1135 /* We failed to link. However, we can only restore libbase
1136 ** if no additional libraries have moved it since we updated it.
1137 */
1138 munmap((void *)si->base, si->size);
1139 return NULL;
1140 }
1141
1142 return si;
1143}
1144
1145soinfo *find_library(const char *name)
1146{
1147 soinfo *si;
Erik Gillingfde86422009-07-28 20:28:19 -07001148 const char *bname = strrchr(name, '/');
1149 bname = bname ? bname + 1 : name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001150
1151 for(si = solist; si != 0; si = si->next){
Erik Gillingfde86422009-07-28 20:28:19 -07001152 if(!strcmp(bname, si->name)) {
Erik Gilling30eb4022009-08-13 16:05:30 -07001153 if(si->flags & FLAG_ERROR) {
1154 DL_ERR("%5d '%s' failed to load previously", pid, bname);
1155 return NULL;
1156 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001157 if(si->flags & FLAG_LINKED) return si;
Erik Gillingd00d23a2009-07-22 17:06:11 -07001158 DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name);
Dima Zavin2e855792009-05-20 18:28:09 -07001159 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001160 }
1161 }
1162
1163 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
1164 si = load_library(name);
1165 if(si == NULL)
1166 return NULL;
1167 return init_library(si);
1168}
1169
1170/* TODO:
1171 * notify gdb of unload
1172 * for non-prelinked libraries, find a way to decrement libbase
1173 */
1174static void call_destructors(soinfo *si);
1175unsigned unload_library(soinfo *si)
1176{
1177 unsigned *d;
1178 if (si->refcount == 1) {
1179 TRACE("%5d unloading '%s'\n", pid, si->name);
1180 call_destructors(si);
1181
1182 for(d = si->dynamic; *d; d += 2) {
1183 if(d[0] == DT_NEEDED){
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001184 soinfo *lsi = (soinfo *)d[1];
1185 d[1] = 0;
1186 if (validate_soinfo(lsi)) {
1187 TRACE("%5d %s needs to unload %s\n", pid,
1188 si->name, lsi->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001189 unload_library(lsi);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001190 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001191 else
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001192 DL_ERR("%5d %s: could not unload dependent library",
1193 pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001194 }
1195 }
1196
1197 munmap((char *)si->base, si->size);
1198 if (si->ba_index >= 0) {
1199 PRINT("%5d releasing library '%s' address space at %08x "\
1200 "through buddy allocator.\n",
1201 pid, si->name, si->base);
Iliyan Malcheve100f522010-02-10 15:19:37 -08001202 ba_free(&ba_nonprelink, si->ba_index);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001203 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001204 notify_gdb_of_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001205 free_info(si);
1206 si->refcount = 0;
1207 }
1208 else {
1209 si->refcount--;
1210 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
1211 pid, si->name, si->refcount);
1212 }
1213 return si->refcount;
1214}
1215
1216/* TODO: don't use unsigned for addrs below. It works, but is not
1217 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1218 * long.
1219 */
1220static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count)
1221{
1222 Elf32_Sym *symtab = si->symtab;
1223 const char *strtab = si->strtab;
1224 Elf32_Sym *s;
1225 unsigned base;
1226 Elf32_Rel *start = rel;
1227 unsigned idx;
1228
1229 for (idx = 0; idx < count; ++idx) {
1230 unsigned type = ELF32_R_TYPE(rel->r_info);
1231 unsigned sym = ELF32_R_SYM(rel->r_info);
1232 unsigned reloc = (unsigned)(rel->r_offset + si->base);
1233 unsigned sym_addr = 0;
1234 char *sym_name = NULL;
1235
1236 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1237 si->name, idx);
1238 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001239 sym_name = (char *)(strtab + symtab[sym].st_name);
1240 s = _do_lookup(si, sym_name, &base);
Doug Kwane8238072009-10-26 12:05:23 -07001241 if(s == NULL) {
1242 /* We only allow an undefined symbol if this is a weak
1243 reference.. */
1244 s = &symtab[sym];
1245 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1246 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
1247 return -1;
1248 }
1249
1250 /* IHI0044C AAELF 4.5.1.1:
1251
1252 Libraries are not searched to resolve weak references.
1253 It is not an error for a weak reference to remain
1254 unsatisfied.
1255
1256 During linking, the value of an undefined weak reference is:
1257 - Zero if the relocation type is absolute
1258 - The address of the place if the relocation is pc-relative
1259 - The address of nominial base address if the relocation
1260 type is base-relative.
1261 */
1262
1263 switch (type) {
1264#if defined(ANDROID_ARM_LINKER)
1265 case R_ARM_JUMP_SLOT:
1266 case R_ARM_GLOB_DAT:
1267 case R_ARM_ABS32:
1268 case R_ARM_RELATIVE: /* Don't care. */
1269 case R_ARM_NONE: /* Don't care. */
1270#elif defined(ANDROID_X86_LINKER)
1271 case R_386_JUMP_SLOT:
1272 case R_386_GLOB_DAT:
1273 case R_386_32:
1274 case R_386_RELATIVE: /* Dont' care. */
1275#endif /* ANDROID_*_LINKER */
1276 /* sym_addr was initialized to be zero above or relocation
1277 code below does not care about value of sym_addr.
1278 No need to do anything. */
1279 break;
1280
1281#if defined(ANDROID_X86_LINKER)
1282 case R_386_PC32:
1283 sym_addr = reloc;
1284 break;
1285#endif /* ANDROID_X86_LINKER */
1286
1287#if defined(ANDROID_ARM_LINKER)
1288 case R_ARM_COPY:
1289 /* Fall through. Can't really copy if weak symbol is
1290 not found in run-time. */
1291#endif /* ANDROID_ARM_LINKER */
1292 default:
1293 DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n",
1294 pid, type, rel, (int) (rel - start));
1295 return -1;
1296 }
1297 } else {
1298 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001299#if 0
1300 if((base == 0) && (si->base != 0)){
1301 /* linking from libraries to main image is bad */
Erik Gillingd00d23a2009-07-22 17:06:11 -07001302 DL_ERR("%5d cannot locate '%s'...",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001303 pid, strtab + symtab[sym].st_name);
1304 return -1;
1305 }
1306#endif
Doug Kwane8238072009-10-26 12:05:23 -07001307 sym_addr = (unsigned)(s->st_value + base);
1308 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001309 COUNT_RELOC(RELOC_SYMBOL);
1310 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001311 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001312 }
1313
1314/* TODO: This is ugly. Split up the relocations by arch into
1315 * different files.
1316 */
1317 switch(type){
1318#if defined(ANDROID_ARM_LINKER)
1319 case R_ARM_JUMP_SLOT:
1320 COUNT_RELOC(RELOC_ABSOLUTE);
1321 MARK(rel->r_offset);
1322 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1323 reloc, sym_addr, sym_name);
1324 *((unsigned*)reloc) = sym_addr;
1325 break;
1326 case R_ARM_GLOB_DAT:
1327 COUNT_RELOC(RELOC_ABSOLUTE);
1328 MARK(rel->r_offset);
1329 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1330 reloc, sym_addr, sym_name);
1331 *((unsigned*)reloc) = sym_addr;
1332 break;
1333 case R_ARM_ABS32:
1334 COUNT_RELOC(RELOC_ABSOLUTE);
1335 MARK(rel->r_offset);
1336 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1337 reloc, sym_addr, sym_name);
1338 *((unsigned*)reloc) += sym_addr;
1339 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001340 case R_ARM_REL32:
1341 COUNT_RELOC(RELOC_RELATIVE);
1342 MARK(rel->r_offset);
1343 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1344 reloc, sym_addr, rel->r_offset, sym_name);
1345 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1346 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001347#elif defined(ANDROID_X86_LINKER)
1348 case R_386_JUMP_SLOT:
1349 COUNT_RELOC(RELOC_ABSOLUTE);
1350 MARK(rel->r_offset);
1351 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1352 reloc, sym_addr, sym_name);
1353 *((unsigned*)reloc) = sym_addr;
1354 break;
1355 case R_386_GLOB_DAT:
1356 COUNT_RELOC(RELOC_ABSOLUTE);
1357 MARK(rel->r_offset);
1358 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1359 reloc, sym_addr, sym_name);
1360 *((unsigned*)reloc) = sym_addr;
1361 break;
1362#endif /* ANDROID_*_LINKER */
1363
1364#if defined(ANDROID_ARM_LINKER)
1365 case R_ARM_RELATIVE:
1366#elif defined(ANDROID_X86_LINKER)
1367 case R_386_RELATIVE:
1368#endif /* ANDROID_*_LINKER */
1369 COUNT_RELOC(RELOC_RELATIVE);
1370 MARK(rel->r_offset);
1371 if(sym){
Erik Gillingd00d23a2009-07-22 17:06:11 -07001372 DL_ERR("%5d odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001373 return -1;
1374 }
1375 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1376 reloc, si->base);
1377 *((unsigned*)reloc) += si->base;
1378 break;
1379
1380#if defined(ANDROID_X86_LINKER)
1381 case R_386_32:
1382 COUNT_RELOC(RELOC_RELATIVE);
1383 MARK(rel->r_offset);
1384
1385 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1386 reloc, sym_addr, sym_name);
1387 *((unsigned *)reloc) += (unsigned)sym_addr;
1388 break;
1389
1390 case R_386_PC32:
1391 COUNT_RELOC(RELOC_RELATIVE);
1392 MARK(rel->r_offset);
1393 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1394 "+%08x (%08x - %08x) %s\n", pid, reloc,
1395 (sym_addr - reloc), sym_addr, reloc, sym_name);
1396 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1397 break;
1398#endif /* ANDROID_X86_LINKER */
1399
1400#ifdef ANDROID_ARM_LINKER
1401 case R_ARM_COPY:
1402 COUNT_RELOC(RELOC_COPY);
1403 MARK(rel->r_offset);
1404 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1405 reloc, s->st_size, sym_addr, sym_name);
1406 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1407 break;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001408 case R_ARM_NONE:
1409 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001410#endif /* ANDROID_ARM_LINKER */
1411
1412 default:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001413 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001414 pid, type, rel, (int) (rel - start));
1415 return -1;
1416 }
1417 rel++;
1418 }
1419 return 0;
1420}
1421
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001422#if defined(ANDROID_SH_LINKER)
1423static int reloc_library_a(soinfo *si, Elf32_Rela *rela, unsigned count)
1424{
1425 Elf32_Sym *symtab = si->symtab;
1426 const char *strtab = si->strtab;
1427 Elf32_Sym *s;
1428 unsigned base;
1429 Elf32_Rela *start = rela;
1430 unsigned idx;
1431
1432 for (idx = 0; idx < count; ++idx) {
1433 unsigned type = ELF32_R_TYPE(rela->r_info);
1434 unsigned sym = ELF32_R_SYM(rela->r_info);
1435 unsigned reloc = (unsigned)(rela->r_offset + si->base);
1436 unsigned sym_addr = 0;
1437 char *sym_name = NULL;
1438
1439 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1440 si->name, idx);
1441 if(sym != 0) {
1442 sym_name = (char *)(strtab + symtab[sym].st_name);
1443 s = _do_lookup(si, sym_name, &base);
1444 if(s == 0) {
1445 DL_ERR("%5d cannot locate '%s'...", pid, sym_name);
1446 return -1;
1447 }
1448#if 0
1449 if((base == 0) && (si->base != 0)){
1450 /* linking from libraries to main image is bad */
1451 DL_ERR("%5d cannot locate '%s'...",
1452 pid, strtab + symtab[sym].st_name);
1453 return -1;
1454 }
1455#endif
1456 if ((s->st_shndx == SHN_UNDEF) && (s->st_value != 0)) {
1457 DL_ERR("%5d In '%s', shndx=%d && value=0x%08x. We do not "
1458 "handle this yet", pid, si->name, s->st_shndx,
1459 s->st_value);
1460 return -1;
1461 }
1462 sym_addr = (unsigned)(s->st_value + base);
1463 COUNT_RELOC(RELOC_SYMBOL);
1464 } else {
1465 s = 0;
1466 }
1467
1468/* TODO: This is ugly. Split up the relocations by arch into
1469 * different files.
1470 */
1471 switch(type){
1472 case R_SH_JUMP_SLOT:
1473 COUNT_RELOC(RELOC_ABSOLUTE);
1474 MARK(rela->r_offset);
1475 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1476 reloc, sym_addr, sym_name);
1477 *((unsigned*)reloc) = sym_addr;
1478 break;
1479 case R_SH_GLOB_DAT:
1480 COUNT_RELOC(RELOC_ABSOLUTE);
1481 MARK(rela->r_offset);
1482 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1483 reloc, sym_addr, sym_name);
1484 *((unsigned*)reloc) = sym_addr;
1485 break;
1486 case R_SH_DIR32:
1487 COUNT_RELOC(RELOC_ABSOLUTE);
1488 MARK(rela->r_offset);
1489 TRACE_TYPE(RELO, "%5d RELO DIR32 %08x <- %08x %s\n", pid,
1490 reloc, sym_addr, sym_name);
1491 *((unsigned*)reloc) += sym_addr;
1492 break;
1493 case R_SH_RELATIVE:
1494 COUNT_RELOC(RELOC_RELATIVE);
1495 MARK(rela->r_offset);
1496 if(sym){
1497 DL_ERR("%5d odd RELATIVE form...", pid);
1498 return -1;
1499 }
1500 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1501 reloc, si->base);
1502 *((unsigned*)reloc) += si->base;
1503 break;
1504
1505 default:
1506 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
1507 pid, type, rela, (int) (rela - start));
1508 return -1;
1509 }
1510 rela++;
1511 }
1512 return 0;
1513}
1514#endif /* ANDROID_SH_LINKER */
1515
David 'Digit' Turner82156792009-05-18 14:37:41 +02001516
1517/* Please read the "Initialization and Termination functions" functions.
1518 * of the linker design note in bionic/linker/README.TXT to understand
1519 * what the following code is doing.
1520 *
1521 * The important things to remember are:
1522 *
1523 * DT_PREINIT_ARRAY must be called first for executables, and should
1524 * not appear in shared libraries.
1525 *
1526 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1527 *
1528 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1529 *
1530 * DT_FINI_ARRAY must be parsed in reverse order.
1531 */
1532
1533static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001534{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001535 int n, inc = 1;
1536
1537 if (reverse) {
1538 ctor += (count-1);
1539 inc = -1;
1540 }
1541
1542 for(n = count; n > 0; n--) {
1543 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1544 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001545 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001546 void (*func)() = (void (*)()) *ctor;
1547 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001548 if(((int) func == 0) || ((int) func == -1)) continue;
1549 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1550 func();
1551 }
1552}
1553
1554static void call_constructors(soinfo *si)
1555{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001556 if (si->flags & FLAG_EXE) {
1557 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1558 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1559 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001560 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001561 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1562 } else {
1563 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001564 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
Erik Gillingd00d23a2009-07-22 17:06:11 -07001565 " This is INVALID.", pid, si->name,
Dima Zavin2e855792009-05-20 18:28:09 -07001566 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001567 }
1568 }
1569
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001570 if (si->init_func) {
1571 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1572 (unsigned)si->init_func, si->name);
1573 si->init_func();
1574 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1575 }
1576
1577 if (si->init_array) {
1578 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1579 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001580 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001581 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1582 }
1583}
1584
David 'Digit' Turner82156792009-05-18 14:37:41 +02001585
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001586static void call_destructors(soinfo *si)
1587{
1588 if (si->fini_array) {
1589 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1590 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001591 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001592 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1593 }
1594
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001595 if (si->fini_func) {
1596 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1597 (unsigned)si->fini_func, si->name);
1598 si->fini_func();
1599 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1600 }
1601}
1602
1603/* Force any of the closed stdin, stdout and stderr to be associated with
1604 /dev/null. */
1605static int nullify_closed_stdio (void)
1606{
1607 int dev_null, i, status;
1608 int return_value = 0;
1609
1610 dev_null = open("/dev/null", O_RDWR);
1611 if (dev_null < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001612 DL_ERR("Cannot open /dev/null.");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001613 return -1;
1614 }
1615 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1616
1617 /* If any of the stdio file descriptors is valid and not associated
1618 with /dev/null, dup /dev/null to it. */
1619 for (i = 0; i < 3; i++) {
1620 /* If it is /dev/null already, we are done. */
1621 if (i == dev_null)
1622 continue;
1623
1624 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1625 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1626 can be interrupted but we do this just to be safe. */
1627 do {
1628 status = fcntl(i, F_GETFL);
1629 } while (status < 0 && errno == EINTR);
1630
1631 /* If file is openned, we are good. */
1632 if (status >= 0)
1633 continue;
1634
1635 /* The only error we allow is that the file descriptor does not
1636 exist, in which case we dup /dev/null to it. */
1637 if (errno != EBADF) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001638 DL_ERR("nullify_stdio: unhandled error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001639 return_value = -1;
1640 continue;
1641 }
1642
1643 /* Try dupping /dev/null to this stdio file descriptor and
1644 repeat if there is a signal. Note that any errors in closing
1645 the stdio descriptor are lost. */
1646 do {
1647 status = dup2(dev_null, i);
1648 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001649
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001650 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001651 DL_ERR("nullify_stdio: dup2 error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652 return_value = -1;
1653 continue;
1654 }
1655 }
1656
1657 /* If /dev/null is not one of the stdio file descriptors, close it. */
1658 if (dev_null > 2) {
1659 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001660 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001661 status = close(dev_null);
1662 } while (status < 0 && errno == EINTR);
1663
1664 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001665 DL_ERR("nullify_stdio: close error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001666 return_value = -1;
1667 }
1668 }
1669
1670 return return_value;
1671}
1672
1673static int link_image(soinfo *si, unsigned wr_offset)
1674{
1675 unsigned *d;
1676 Elf32_Phdr *phdr = si->phdr;
1677 int phnum = si->phnum;
1678
1679 INFO("[ %5d linking %s ]\n", pid, si->name);
1680 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1681 si->base, si->flags);
1682
1683 if (si->flags & FLAG_EXE) {
1684 /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for
1685 * linkage info if this is the executable. If this was a
1686 * dynamic lib, that would have been done at load time.
1687 *
1688 * TODO: It's unfortunate that small pieces of this are
1689 * repeated from the load_library routine. Refactor this just
1690 * slightly to reuse these bits.
1691 */
1692 si->size = 0;
1693 for(; phnum > 0; --phnum, ++phdr) {
1694#ifdef ANDROID_ARM_LINKER
1695 if(phdr->p_type == PT_ARM_EXIDX) {
1696 /* exidx entries (used for stack unwinding) are 8 bytes each.
1697 */
1698 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1699 si->ARM_exidx_count = phdr->p_memsz / 8;
1700 }
1701#endif
1702 if (phdr->p_type == PT_LOAD) {
1703 /* For the executable, we use the si->size field only in
1704 dl_unwind_find_exidx(), so the meaning of si->size
1705 is not the size of the executable; it is the last
1706 virtual address of the loadable part of the executable;
1707 since si->base == 0 for an executable, we use the
1708 range [0, si->size) to determine whether a PC value
1709 falls within the executable section. Of course, if
1710 a value is below phdr->p_vaddr, it's not in the
1711 executable section, but a) we shouldn't be asking for
1712 such a value anyway, and b) if we have to provide
1713 an EXIDX for such a value, then the executable's
1714 EXIDX is probably the better choice.
1715 */
1716 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
1717 if (phdr->p_vaddr + phdr->p_memsz > si->size)
1718 si->size = phdr->p_vaddr + phdr->p_memsz;
1719 /* try to remember what range of addresses should be write
1720 * protected */
1721 if (!(phdr->p_flags & PF_W)) {
1722 unsigned _end;
1723
1724 if (phdr->p_vaddr < si->wrprotect_start)
1725 si->wrprotect_start = phdr->p_vaddr;
1726 _end = (((phdr->p_vaddr + phdr->p_memsz + PAGE_SIZE - 1) &
1727 (~PAGE_MASK)));
1728 if (_end > si->wrprotect_end)
1729 si->wrprotect_end = _end;
1730 }
1731 } else if (phdr->p_type == PT_DYNAMIC) {
1732 if (si->dynamic != (unsigned *)-1) {
Dima Zavin2e855792009-05-20 18:28:09 -07001733 DL_ERR("%5d multiple PT_DYNAMIC segments found in '%s'. "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001734 "Segment at 0x%08x, previously one found at 0x%08x",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001735 pid, si->name, si->base + phdr->p_vaddr,
1736 (unsigned)si->dynamic);
1737 goto fail;
1738 }
1739 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
1740 si->dynamic = (unsigned *) (si->base + phdr->p_vaddr);
1741 }
1742 }
1743 }
1744
1745 if (si->dynamic == (unsigned *)-1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001746 DL_ERR("%5d missing PT_DYNAMIC?!", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001747 goto fail;
1748 }
1749
1750 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1751
1752 /* extract useful information from dynamic section */
1753 for(d = si->dynamic; *d; d++){
1754 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1755 switch(*d++){
1756 case DT_HASH:
1757 si->nbucket = ((unsigned *) (si->base + *d))[0];
1758 si->nchain = ((unsigned *) (si->base + *d))[1];
1759 si->bucket = (unsigned *) (si->base + *d + 8);
1760 si->chain = (unsigned *) (si->base + *d + 8 + si->nbucket * 4);
1761 break;
1762 case DT_STRTAB:
1763 si->strtab = (const char *) (si->base + *d);
1764 break;
1765 case DT_SYMTAB:
1766 si->symtab = (Elf32_Sym *) (si->base + *d);
1767 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001768#if !defined(ANDROID_SH_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001769 case DT_PLTREL:
1770 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001771 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001772 goto fail;
1773 }
1774 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001775#endif
1776#ifdef ANDROID_SH_LINKER
1777 case DT_JMPREL:
1778 si->plt_rela = (Elf32_Rela*) (si->base + *d);
1779 break;
1780 case DT_PLTRELSZ:
1781 si->plt_rela_count = *d / sizeof(Elf32_Rela);
1782 break;
1783#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001784 case DT_JMPREL:
1785 si->plt_rel = (Elf32_Rel*) (si->base + *d);
1786 break;
1787 case DT_PLTRELSZ:
1788 si->plt_rel_count = *d / 8;
1789 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001790#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001791 case DT_REL:
1792 si->rel = (Elf32_Rel*) (si->base + *d);
1793 break;
1794 case DT_RELSZ:
1795 si->rel_count = *d / 8;
1796 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001797#ifdef ANDROID_SH_LINKER
1798 case DT_RELASZ:
1799 si->rela_count = *d / sizeof(Elf32_Rela);
1800 break;
1801#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001802 case DT_PLTGOT:
1803 /* Save this in case we decide to do lazy binding. We don't yet. */
1804 si->plt_got = (unsigned *)(si->base + *d);
1805 break;
1806 case DT_DEBUG:
1807 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1808 *d = (int) &_r_debug;
1809 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001810#ifdef ANDROID_SH_LINKER
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001811 case DT_RELA:
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001812 si->rela = (Elf32_Rela *) (si->base + *d);
1813 break;
1814#else
1815 case DT_RELA:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001816 DL_ERR("%5d DT_RELA not supported", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001817 goto fail;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001818#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001819 case DT_INIT:
1820 si->init_func = (void (*)(void))(si->base + *d);
1821 DEBUG("%5d %s constructors (init func) found at %p\n",
1822 pid, si->name, si->init_func);
1823 break;
1824 case DT_FINI:
1825 si->fini_func = (void (*)(void))(si->base + *d);
1826 DEBUG("%5d %s destructors (fini func) found at %p\n",
1827 pid, si->name, si->fini_func);
1828 break;
1829 case DT_INIT_ARRAY:
1830 si->init_array = (unsigned *)(si->base + *d);
1831 DEBUG("%5d %s constructors (init_array) found at %p\n",
1832 pid, si->name, si->init_array);
1833 break;
1834 case DT_INIT_ARRAYSZ:
1835 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1836 break;
1837 case DT_FINI_ARRAY:
1838 si->fini_array = (unsigned *)(si->base + *d);
1839 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1840 pid, si->name, si->fini_array);
1841 break;
1842 case DT_FINI_ARRAYSZ:
1843 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1844 break;
1845 case DT_PREINIT_ARRAY:
1846 si->preinit_array = (unsigned *)(si->base + *d);
1847 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1848 pid, si->name, si->preinit_array);
1849 break;
1850 case DT_PREINIT_ARRAYSZ:
1851 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1852 break;
1853 case DT_TEXTREL:
1854 /* TODO: make use of this. */
1855 /* this means that we might have to write into where the text
1856 * segment was loaded during relocation... Do something with
1857 * it.
1858 */
1859 DEBUG("%5d Text segment should be writable during relocation.\n",
1860 pid);
1861 break;
1862 }
1863 }
1864
1865 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
1866 pid, si->base, si->strtab, si->symtab);
1867
1868 if((si->strtab == 0) || (si->symtab == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001869 DL_ERR("%5d missing essential tables", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001870 goto fail;
1871 }
1872
1873 for(d = si->dynamic; *d; d += 2) {
1874 if(d[0] == DT_NEEDED){
1875 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001876 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001877 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001878 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Erik Gillingd00d23a2009-07-22 17:06:11 -07001879 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
Dima Zavin03531952009-05-29 17:30:25 -07001880 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001881 goto fail;
1882 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001883 /* Save the soinfo of the loaded DT_NEEDED library in the payload
1884 of the DT_NEEDED entry itself, so that we can retrieve the
1885 soinfo directly later from the dynamic segment. This is a hack,
1886 but it allows us to map from DT_NEEDED to soinfo efficiently
1887 later on when we resolve relocations, trying to look up a symgol
1888 with dlsym().
1889 */
1890 d[1] = (unsigned)lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001891 lsi->refcount++;
1892 }
1893 }
1894
1895 if(si->plt_rel) {
1896 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1897 if(reloc_library(si, si->plt_rel, si->plt_rel_count))
1898 goto fail;
1899 }
1900 if(si->rel) {
1901 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1902 if(reloc_library(si, si->rel, si->rel_count))
1903 goto fail;
1904 }
1905
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001906#ifdef ANDROID_SH_LINKER
1907 if(si->plt_rela) {
1908 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1909 if(reloc_library_a(si, si->plt_rela, si->plt_rela_count))
1910 goto fail;
1911 }
1912 if(si->rela) {
1913 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1914 if(reloc_library_a(si, si->rela, si->rela_count))
1915 goto fail;
1916 }
1917#endif /* ANDROID_SH_LINKER */
1918
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001919 si->flags |= FLAG_LINKED;
1920 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1921
1922#if 0
1923 /* This is the way that the old dynamic linker did protection of
1924 * non-writable areas. It would scan section headers and find where
1925 * .text ended (rather where .data/.bss began) and assume that this is
1926 * the upper range of the non-writable area. This is too coarse,
1927 * and is kept here for reference until we fully move away from single
1928 * segment elf objects. See the code in get_wr_offset (also #if'd 0)
1929 * that made this possible.
1930 */
1931 if(wr_offset < 0xffffffff){
1932 mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC);
1933 }
1934#else
1935 /* TODO: Verify that this does the right thing in all cases, as it
1936 * presently probably does not. It is possible that an ELF image will
1937 * come with multiple read-only segments. What we ought to do is scan
1938 * the program headers again and mprotect all the read-only segments.
1939 * To prevent re-scanning the program header, we would have to build a
1940 * list of loadable segments in si, and then scan that instead. */
1941 if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) {
1942 mprotect((void *)si->wrprotect_start,
1943 si->wrprotect_end - si->wrprotect_start,
1944 PROT_READ | PROT_EXEC);
1945 }
1946#endif
1947
1948 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1949 stdout and stderr to close a security hole described in:
1950
1951 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1952
1953 */
1954 if (getuid() != geteuid() || getgid() != getegid())
1955 nullify_closed_stdio ();
1956 call_constructors(si);
1957 notify_gdb_of_load(si);
1958 return 0;
1959
1960fail:
Doug Kwan94304352009-10-23 18:11:40 -07001961 DL_ERR("failed to link %s\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001962 si->flags |= FLAG_ERROR;
1963 return -1;
1964}
1965
David Bartleybc3a5c22009-06-02 18:27:28 -07001966static void parse_library_path(char *path, char *delim)
1967{
1968 size_t len;
1969 char *ldpaths_bufp = ldpaths_buf;
1970 int i = 0;
1971
1972 len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf));
1973
1974 while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) {
1975 if (*ldpaths[i] != '\0')
1976 ++i;
1977 }
1978
1979 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1980 * last char isn't '\0' (i.e. not originally a delim). */
1981 if (i > 0 && len >= sizeof(ldpaths_buf) &&
1982 ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') {
1983 ldpaths[i - 1] = NULL;
1984 } else {
1985 ldpaths[i] = NULL;
1986 }
1987}
1988
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001989int main(int argc, char **argv)
1990{
1991 return 0;
1992}
1993
1994#define ANDROID_TLS_SLOTS BIONIC_TLS_SLOTS
1995
1996static void * __tls_area[ANDROID_TLS_SLOTS];
1997
1998unsigned __linker_init(unsigned **elfdata)
1999{
2000 static soinfo linker_soinfo;
2001
2002 int argc = (int) *elfdata;
2003 char **argv = (char**) (elfdata + 1);
2004 unsigned *vecs = (unsigned*) (argv + argc + 1);
2005 soinfo *si;
2006 struct link_map * map;
David Bartleybc3a5c22009-06-02 18:27:28 -07002007 char *ldpath_env = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002008
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02002009 /* Setup a temporary TLS area that is used to get a working
2010 * errno for system calls.
2011 */
2012 __set_tls(__tls_area);
2013
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002014 pid = getpid();
2015
2016#if TIMING
2017 struct timeval t0, t1;
2018 gettimeofday(&t0, 0);
2019#endif
2020
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02002021 /* NOTE: we store the elfdata pointer on a special location
2022 * of the temporary TLS area in order to pass it to
2023 * the C Library's runtime initializer.
2024 *
2025 * The initializer must clear the slot and reset the TLS
2026 * to point to a different location to ensure that no other
2027 * shared library constructor can access it.
2028 */
2029 __tls_area[TLS_SLOT_BIONIC_PREINIT] = elfdata;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002030
2031 debugger_init();
2032
2033 /* skip past the environment */
2034 while(vecs[0] != 0) {
2035 if(!strncmp((char*) vecs[0], "DEBUG=", 6)) {
2036 debug_verbosity = atoi(((char*) vecs[0]) + 6);
David Bartleybc3a5c22009-06-02 18:27:28 -07002037 } else if(!strncmp((char*) vecs[0], "LD_LIBRARY_PATH=", 16)) {
2038 ldpath_env = (char*) vecs[0] + 16;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002039 }
2040 vecs++;
2041 }
2042 vecs++;
2043
2044 INFO("[ android linker & debugger ]\n");
2045 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
2046
2047 si = alloc_info(argv[0]);
2048 if(si == 0) {
2049 exit(-1);
2050 }
2051
2052 /* bootstrap the link map, the main exe always needs to be first */
2053 si->flags |= FLAG_EXE;
2054 map = &(si->linkmap);
2055
2056 map->l_addr = 0;
2057 map->l_name = argv[0];
2058 map->l_prev = NULL;
2059 map->l_next = NULL;
2060
2061 _r_debug.r_map = map;
2062 r_debug_tail = map;
2063
2064 /* gdb expects the linker to be in the debug shared object list,
2065 * and we need to make sure that the reported load address is zero.
2066 * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
2067 * is. Don't use alloc_info(), because the linker shouldn't
2068 * be on the soinfo list.
2069 */
2070 strcpy((char*) linker_soinfo.name, "/system/bin/linker");
2071 linker_soinfo.flags = 0;
2072 linker_soinfo.base = 0; // This is the important part; must be zero.
2073 insert_soinfo_into_debug_map(&linker_soinfo);
2074
2075 /* extract information passed from the kernel */
2076 while(vecs[0] != 0){
2077 switch(vecs[0]){
2078 case AT_PHDR:
2079 si->phdr = (Elf32_Phdr*) vecs[1];
2080 break;
2081 case AT_PHNUM:
2082 si->phnum = (int) vecs[1];
2083 break;
2084 case AT_ENTRY:
2085 si->entry = vecs[1];
2086 break;
2087 }
2088 vecs += 2;
2089 }
2090
Iliyan Malcheve100f522010-02-10 15:19:37 -08002091 ba_init(&ba_nonprelink);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002092
2093 si->base = 0;
2094 si->dynamic = (unsigned *)-1;
2095 si->wrprotect_start = 0xffffffff;
2096 si->wrprotect_end = 0;
2097
David Bartleybc3a5c22009-06-02 18:27:28 -07002098 /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */
2099 if (ldpath_env && getuid() == geteuid() && getgid() == getegid())
2100 parse_library_path(ldpath_env, ":");
2101
Dima Zavin2e855792009-05-20 18:28:09 -07002102 if(link_image(si, 0)) {
2103 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
2104 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2105 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002106 exit(-1);
2107 }
2108
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07002109#if ALLOW_SYMBOLS_FROM_MAIN
2110 /* Set somain after we've loaded all the libraries in order to prevent
2111 * linking of symbols back to the main image, which is not set up at that
2112 * point yet.
2113 */
2114 somain = si;
2115#endif
2116
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002117#if TIMING
2118 gettimeofday(&t1,NULL);
2119 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
2120 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2121 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
2122 ));
2123#endif
2124#if STATS
2125 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
2126 linker_stats.reloc[RELOC_ABSOLUTE],
2127 linker_stats.reloc[RELOC_RELATIVE],
2128 linker_stats.reloc[RELOC_COPY],
2129 linker_stats.reloc[RELOC_SYMBOL]);
2130#endif
2131#if COUNT_PAGES
2132 {
2133 unsigned n;
2134 unsigned i;
2135 unsigned count = 0;
2136 for(n = 0; n < 4096; n++){
2137 if(bitmask[n]){
2138 unsigned x = bitmask[n];
2139 for(i = 0; i < 8; i++){
2140 if(x & 1) count++;
2141 x >>= 1;
2142 }
2143 }
2144 }
2145 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
2146 }
2147#endif
2148
2149#if TIMING || STATS || COUNT_PAGES
2150 fflush(stdout);
2151#endif
2152
2153 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
2154 si->entry);
2155 return si->entry;
2156}