blob: bcfa8dc768c9261e03b58c6d2cec28b5219a0d28 [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' Turnerbe575592010-12-16 19:52:02 +010051#include "linker_environ.h"
David 'Digit' Turner5c734642010-01-20 12:36:51 -080052#include "linker_format.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070054#define ALLOW_SYMBOLS_FROM_MAIN 1
Kenny Root72f9a5c2011-02-10 17:02:21 -080055#define SO_MAX 128
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056
David Bartleybc3a5c22009-06-02 18:27:28 -070057/* Assume average path length of 64 and max 8 paths */
58#define LDPATH_BUFSIZE 512
59#define LDPATH_MAX 8
60
Matt Fischer4fd42c12009-12-31 12:09:10 -060061#define LDPRELOAD_BUFSIZE 512
62#define LDPRELOAD_MAX 8
63
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
65 *
66 * Do NOT use malloc() and friends or pthread_*() code here.
67 * Don't use printf() either; it's caused mysterious memory
68 * corruption in the past.
69 * The linker runs before we bring up libc and it's easiest
70 * to make sure it does not depend on any complex libc features
71 *
72 * open issues / todo:
73 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074 * - are we doing everything we should for ARM_COPY relocations?
75 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076 * - after linking, set as much stuff as possible to READONLY
77 * and NOEXEC
78 * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
79 * headers provide versions that are negative...
80 * - allocate space for soinfo structs dynamically instead of
81 * having a hard limit (64)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082*/
83
84
85static int link_image(soinfo *si, unsigned wr_offset);
86
87static int socount = 0;
88static soinfo sopool[SO_MAX];
89static soinfo *freelist = NULL;
90static soinfo *solist = &libdl_info;
91static soinfo *sonext = &libdl_info;
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070092#if ALLOW_SYMBOLS_FROM_MAIN
93static soinfo *somain; /* main process, always the one after libdl_info */
94#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
Iliyan Malchevaf7315a2009-10-16 17:50:42 -070096
Iliyan Malchev6ed80c82009-09-28 19:38:04 -070097static inline int validate_soinfo(soinfo *si)
98{
99 return (si >= sopool && si < sopool + SO_MAX) ||
100 si == &libdl_info;
101}
102
David Bartleybc3a5c22009-06-02 18:27:28 -0700103static char ldpaths_buf[LDPATH_BUFSIZE];
104static const char *ldpaths[LDPATH_MAX + 1];
105
Matt Fischer4fd42c12009-12-31 12:09:10 -0600106static char ldpreloads_buf[LDPRELOAD_BUFSIZE];
107static const char *ldpreload_names[LDPRELOAD_MAX + 1];
108
109static soinfo *preloads[LDPRELOAD_MAX + 1];
110
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111int debug_verbosity;
112static int pid;
113
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100114/* This boolean is set if the program being loaded is setuid */
115static int program_is_setuid;
116
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117#if STATS
118struct _link_stats linker_stats;
119#endif
120
121#if COUNT_PAGES
122unsigned bitmask[4096];
123#endif
124
125#ifndef PT_ARM_EXIDX
126#define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */
127#endif
128
Dima Zavin2e855792009-05-20 18:28:09 -0700129#define HOODLUM(name, ret, ...) \
130 ret name __VA_ARGS__ \
131 { \
132 char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \
133 write(2, errstr, sizeof(errstr)); \
134 abort(); \
135 }
136HOODLUM(malloc, void *, (size_t size));
137HOODLUM(free, void, (void *ptr));
138HOODLUM(realloc, void *, (void *ptr, size_t size));
139HOODLUM(calloc, void *, (size_t cnt, size_t size));
140
Dima Zavin03531952009-05-29 17:30:25 -0700141static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700142static char __linker_dl_err_buf[768];
143#define DL_ERR(fmt, x...) \
144 do { \
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800145 format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), \
Dima Zavin2e855792009-05-20 18:28:09 -0700146 "%s[%d]: " fmt, __func__, __LINE__, ##x); \
Erik Gillingd00d23a2009-07-22 17:06:11 -0700147 ERROR(fmt "\n", ##x); \
Dima Zavin2e855792009-05-20 18:28:09 -0700148 } while(0)
149
150const char *linker_get_error(void)
151{
152 return (const char *)&__linker_dl_err_buf[0];
153}
154
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155/*
156 * This function is an empty stub where GDB locates a breakpoint to get notified
157 * about linker activity.
158 */
159extern void __attribute__((noinline)) rtld_db_dlactivity(void);
160
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
162 RT_CONSISTENT, 0};
163static struct link_map *r_debug_tail = 0;
164
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700165static pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800166
167static void insert_soinfo_into_debug_map(soinfo * info)
168{
169 struct link_map * map;
170
171 /* Copy the necessary fields into the debug structure.
172 */
173 map = &(info->linkmap);
174 map->l_addr = info->base;
175 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800176 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800177
178 /* Stick the new library at the end of the list.
179 * gdb tends to care more about libc than it does
180 * about leaf libraries, and ordering it this way
181 * reduces the back-and-forth over the wire.
182 */
183 if (r_debug_tail) {
184 r_debug_tail->l_next = map;
185 map->l_prev = r_debug_tail;
186 map->l_next = 0;
187 } else {
188 _r_debug.r_map = map;
189 map->l_prev = 0;
190 map->l_next = 0;
191 }
192 r_debug_tail = map;
193}
194
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700195static void remove_soinfo_from_debug_map(soinfo * info)
196{
197 struct link_map * map = &(info->linkmap);
198
199 if (r_debug_tail == map)
200 r_debug_tail = map->l_prev;
201
202 if (map->l_prev) map->l_prev->l_next = map->l_next;
203 if (map->l_next) map->l_next->l_prev = map->l_prev;
204}
205
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206void notify_gdb_of_load(soinfo * info)
207{
208 if (info->flags & FLAG_EXE) {
209 // GDB already knows about the main executable
210 return;
211 }
212
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700213 pthread_mutex_lock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214
215 _r_debug.r_state = RT_ADD;
216 rtld_db_dlactivity();
217
218 insert_soinfo_into_debug_map(info);
219
220 _r_debug.r_state = RT_CONSISTENT;
221 rtld_db_dlactivity();
222
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700223 pthread_mutex_unlock(&_r_debug_lock);
224}
225
226void notify_gdb_of_unload(soinfo * info)
227{
228 if (info->flags & FLAG_EXE) {
229 // GDB already knows about the main executable
230 return;
231 }
232
233 pthread_mutex_lock(&_r_debug_lock);
234
235 _r_debug.r_state = RT_DELETE;
236 rtld_db_dlactivity();
237
238 remove_soinfo_from_debug_map(info);
239
240 _r_debug.r_state = RT_CONSISTENT;
241 rtld_db_dlactivity();
242
243 pthread_mutex_unlock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244}
245
246void notify_gdb_of_libraries()
247{
248 _r_debug.r_state = RT_ADD;
249 rtld_db_dlactivity();
250 _r_debug.r_state = RT_CONSISTENT;
251 rtld_db_dlactivity();
252}
253
254static soinfo *alloc_info(const char *name)
255{
256 soinfo *si;
257
258 if(strlen(name) >= SOINFO_NAME_LEN) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700259 DL_ERR("%5d library name %s too long", pid, name);
Doug Kwan94304352009-10-23 18:11:40 -0700260 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800261 }
262
263 /* The freelist is populated when we call free_info(), which in turn is
264 done only by dlclose(), which is not likely to be used.
265 */
266 if (!freelist) {
267 if(socount == SO_MAX) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700268 DL_ERR("%5d too many libraries when loading %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269 return NULL;
270 }
271 freelist = sopool + socount++;
272 freelist->next = NULL;
273 }
274
275 si = freelist;
276 freelist = freelist->next;
277
278 /* Make sure we get a clean block of soinfo */
279 memset(si, 0, sizeof(soinfo));
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100280 strlcpy((char*) si->name, name, sizeof(si->name));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800281 sonext->next = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282 si->next = NULL;
283 si->refcount = 0;
284 sonext = si;
285
286 TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
287 return si;
288}
289
290static void free_info(soinfo *si)
291{
292 soinfo *prev = NULL, *trav;
293
294 TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
295
296 for(trav = solist; trav != NULL; trav = trav->next){
297 if (trav == si)
298 break;
299 prev = trav;
300 }
301 if (trav == NULL) {
302 /* si was not ni solist */
Erik Gillingd00d23a2009-07-22 17:06:11 -0700303 DL_ERR("%5d name %s is not in solist!", pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800304 return;
305 }
306
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100307 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308 always the static libdl_info.
309 */
310 prev->next = si->next;
311 if (si == sonext) sonext = prev;
312 si->next = freelist;
313 freelist = si;
314}
315
316#ifndef LINKER_TEXT_BASE
317#error "linker's makefile must define LINKER_TEXT_BASE"
318#endif
319#ifndef LINKER_AREA_SIZE
320#error "linker's makefile must define LINKER_AREA_SIZE"
321#endif
322#define LINKER_BASE ((LINKER_TEXT_BASE) & 0xfff00000)
323#define LINKER_TOP (LINKER_BASE + (LINKER_AREA_SIZE))
324
325const char *addr_to_name(unsigned addr)
326{
327 soinfo *si;
328
329 for(si = solist; si != 0; si = si->next){
330 if((addr >= si->base) && (addr < (si->base + si->size))) {
331 return si->name;
332 }
333 }
334
335 if((addr >= LINKER_BASE) && (addr < LINKER_TOP)){
336 return "linker";
337 }
338
339 return "";
340}
341
342/* For a given PC, find the .so that it belongs to.
343 * Returns the base address of the .ARM.exidx section
344 * for that .so, and the number of 8-byte entries
345 * in that section (via *pcount).
346 *
347 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
348 *
349 * This function is exposed via dlfcn.c and libdl.so.
350 */
351#ifdef ANDROID_ARM_LINKER
352_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
353{
354 soinfo *si;
355 unsigned addr = (unsigned)pc;
356
357 if ((addr < LINKER_BASE) || (addr >= LINKER_TOP)) {
358 for (si = solist; si != 0; si = si->next){
359 if ((addr >= si->base) && (addr < (si->base + si->size))) {
360 *pcount = si->ARM_exidx_count;
361 return (_Unwind_Ptr)(si->base + (unsigned long)si->ARM_exidx);
362 }
363 }
364 }
365 *pcount = 0;
366 return NULL;
367}
David 'Digit' Turner70b16682012-01-30 17:17:58 +0100368#elif defined(ANDROID_X86_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800369/* Here, we only have to provide a callback to iterate across all the
370 * loaded libraries. gcc_eh does the rest. */
371int
372dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
373 void *data)
374{
375 soinfo *si;
376 struct dl_phdr_info dl_info;
377 int rv = 0;
378
379 for (si = solist; si != NULL; si = si->next) {
380 dl_info.dlpi_addr = si->linkmap.l_addr;
381 dl_info.dlpi_name = si->linkmap.l_name;
382 dl_info.dlpi_phdr = si->phdr;
383 dl_info.dlpi_phnum = si->phnum;
384 rv = cb(&dl_info, sizeof (struct dl_phdr_info), data);
385 if (rv != 0)
386 break;
387 }
388 return rv;
389}
390#endif
391
392static Elf32_Sym *_elf_lookup(soinfo *si, unsigned hash, const char *name)
393{
394 Elf32_Sym *s;
395 Elf32_Sym *symtab = si->symtab;
396 const char *strtab = si->strtab;
397 unsigned n;
398
399 TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
400 name, si->name, si->base, hash, hash % si->nbucket);
401 n = hash % si->nbucket;
402
403 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
404 s = symtab + n;
405 if(strcmp(strtab + s->st_name, name)) continue;
406
Doug Kwane8238072009-10-26 12:05:23 -0700407 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800408 switch(ELF32_ST_BIND(s->st_info)){
409 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700410 case STB_WEAK:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411 /* no section == undefined */
412 if(s->st_shndx == 0) continue;
413
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800414 TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
415 name, si->name, s->st_value, s->st_size);
416 return s;
417 }
418 }
419
Doug Kwan94304352009-10-23 18:11:40 -0700420 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800421}
422
423static unsigned elfhash(const char *_name)
424{
425 const unsigned char *name = (const unsigned char *) _name;
426 unsigned h = 0, g;
427
428 while(*name) {
429 h = (h << 4) + *name++;
430 g = h & 0xf0000000;
431 h ^= g;
432 h ^= g >> 24;
433 }
434 return h;
435}
436
437static Elf32_Sym *
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700438_do_lookup(soinfo *si, const char *name, unsigned *base)
439{
Doug Kwan94304352009-10-23 18:11:40 -0700440 unsigned elf_hash = elfhash(name);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700441 Elf32_Sym *s;
442 unsigned *d;
443 soinfo *lsi = si;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600444 int i;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700445
446 /* Look for symbols in the local scope first (the object who is
447 * searching). This happens with C++ templates on i386 for some
Doug Kwane8238072009-10-26 12:05:23 -0700448 * reason.
449 *
450 * Notes on weak symbols:
451 * The ELF specs are ambigious about treatment of weak definitions in
452 * dynamic linking. Some systems return the first definition found
453 * and some the first non-weak definition. This is system dependent.
454 * Here we return the first definition found for simplicity. */
Doug Kwan94304352009-10-23 18:11:40 -0700455 s = _elf_lookup(si, elf_hash, name);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700456 if(s != NULL)
457 goto done;
458
Matt Fischer4fd42c12009-12-31 12:09:10 -0600459 /* Next, look for it in the preloads list */
460 for(i = 0; preloads[i] != NULL; i++) {
461 lsi = preloads[i];
Jean-Baptiste Queruf4394452010-05-12 10:05:59 -0700462 s = _elf_lookup(lsi, elf_hash, name);
Matt Fischer4fd42c12009-12-31 12:09:10 -0600463 if(s != NULL)
464 goto done;
465 }
466
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700467 for(d = si->dynamic; *d; d += 2) {
468 if(d[0] == DT_NEEDED){
469 lsi = (soinfo *)d[1];
470 if (!validate_soinfo(lsi)) {
471 DL_ERR("%5d bad DT_NEEDED pointer in %s",
472 pid, si->name);
Doug Kwan94304352009-10-23 18:11:40 -0700473 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700474 }
475
476 DEBUG("%5d %s: looking up %s in %s\n",
477 pid, si->name, name, lsi->name);
Doug Kwan94304352009-10-23 18:11:40 -0700478 s = _elf_lookup(lsi, elf_hash, name);
Min-su, Kim3cab22c2010-01-19 10:05:33 +0900479 if ((s != NULL) && (s->st_shndx != SHN_UNDEF))
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700480 goto done;
481 }
482 }
483
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -0700484#if ALLOW_SYMBOLS_FROM_MAIN
485 /* If we are resolving relocations while dlopen()ing a library, it's OK for
486 * the library to resolve a symbol that's defined in the executable itself,
487 * although this is rare and is generally a bad idea.
488 */
489 if (somain) {
490 lsi = somain;
491 DEBUG("%5d %s: looking up %s in executable %s\n",
492 pid, si->name, name, lsi->name);
Doug Kwan94304352009-10-23 18:11:40 -0700493 s = _elf_lookup(lsi, elf_hash, name);
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -0700494 }
495#endif
496
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700497done:
498 if(s != NULL) {
499 TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
500 "found in %s, base = 0x%08x\n",
501 pid, si->name, name, s->st_value, lsi->name, lsi->base);
502 *base = lsi->base;
503 return s;
504 }
505
Doug Kwan94304352009-10-23 18:11:40 -0700506 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700507}
508
509/* This is used by dl_sym(). It performs symbol lookup only within the
510 specified soinfo object and not in any of its dependencies.
511 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800512Elf32_Sym *lookup_in_library(soinfo *si, const char *name)
513{
Doug Kwan94304352009-10-23 18:11:40 -0700514 return _elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800515}
516
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700517/* This is used by dl_sym(). It performs a global symbol lookup.
518 */
Matt Fischer1698d9e2009-12-31 12:17:56 -0600519Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800520{
Doug Kwan94304352009-10-23 18:11:40 -0700521 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800522 Elf32_Sym *s = NULL;
523 soinfo *si;
524
Matt Fischer1698d9e2009-12-31 12:17:56 -0600525 if(start == NULL) {
526 start = solist;
527 }
528
529 for(si = start; (s == NULL) && (si != NULL); si = si->next)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800530 {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700531 if(si->flags & FLAG_ERROR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800532 continue;
Doug Kwane8238072009-10-26 12:05:23 -0700533 s = _elf_lookup(si, elf_hash, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534 if (s != NULL) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700535 *found = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800536 break;
537 }
538 }
539
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700540 if(s != NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800541 TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
542 "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
543 return s;
544 }
545
Doug Kwan94304352009-10-23 18:11:40 -0700546 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800547}
548
Mathias Agopianbda5da02011-09-27 22:30:19 -0700549soinfo *find_containing_library(const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600550{
551 soinfo *si;
552
553 for(si = solist; si != NULL; si = si->next)
554 {
555 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
556 return si;
557 }
558 }
559
560 return NULL;
561}
562
Mathias Agopianbda5da02011-09-27 22:30:19 -0700563Elf32_Sym *find_containing_symbol(const void *addr, soinfo *si)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600564{
565 unsigned int i;
566 unsigned soaddr = (unsigned)addr - si->base;
567
568 /* Search the library's symbol table for any defined symbol which
569 * contains this address */
570 for(i=0; i<si->nchain; i++) {
571 Elf32_Sym *sym = &si->symtab[i];
572
573 if(sym->st_shndx != SHN_UNDEF &&
574 soaddr >= sym->st_value &&
575 soaddr < sym->st_value + sym->st_size) {
576 return sym;
577 }
578 }
579
580 return NULL;
581}
582
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800583#if 0
584static void dump(soinfo *si)
585{
586 Elf32_Sym *s = si->symtab;
587 unsigned n;
588
589 for(n = 0; n < si->nchain; n++) {
590 TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
591 s->st_info, s->st_shndx, s->st_value, s->st_size,
592 si->strtab + s->st_name);
593 s++;
594 }
595}
596#endif
597
598static const char *sopaths[] = {
Brian Swetlandfedbcde2010-09-19 03:39:13 -0700599 "/vendor/lib",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800600 "/system/lib",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800601 0
602};
603
604static int _open_lib(const char *name)
605{
606 int fd;
607 struct stat filestat;
608
609 if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
610 if ((fd = open(name, O_RDONLY)) >= 0)
611 return fd;
612 }
613
614 return -1;
615}
616
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800617static int open_library(const char *name)
618{
619 int fd;
620 char buf[512];
621 const char **path;
David Bartleybc3a5c22009-06-02 18:27:28 -0700622 int n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800623
624 TRACE("[ %5d opening %s ]\n", pid, name);
625
626 if(name == 0) return -1;
627 if(strlen(name) > 256) return -1;
628
629 if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
630 return fd;
631
David Bartleybc3a5c22009-06-02 18:27:28 -0700632 for (path = ldpaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800633 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700634 if (n < 0 || n >= (int)sizeof(buf)) {
635 WARN("Ignoring very long library path: %s/%s\n", *path, name);
636 continue;
637 }
638 if ((fd = _open_lib(buf)) >= 0)
639 return fd;
640 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800641 for (path = sopaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800642 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700643 if (n < 0 || n >= (int)sizeof(buf)) {
644 WARN("Ignoring very long library path: %s/%s\n", *path, name);
645 continue;
646 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800647 if ((fd = _open_lib(buf)) >= 0)
648 return fd;
649 }
650
651 return -1;
652}
653
654/* temporary space for holding the first page of the shared lib
655 * which contains the elf header (with the pht). */
656static unsigned char __header[PAGE_SIZE];
657
658typedef struct {
659 long mmap_addr;
660 char tag[4]; /* 'P', 'R', 'E', ' ' */
661} prelink_info_t;
662
663/* Returns the requested base address if the library is prelinked,
664 * and 0 otherwise. */
665static unsigned long
666is_prelinked(int fd, const char *name)
667{
668 off_t sz;
669 prelink_info_t info;
670
671 sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
672 if (sz < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700673 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800674 return 0;
675 }
676
677 if (read(fd, &info, sizeof(info)) != sizeof(info)) {
678 WARN("Could not read prelink_info_t structure for `%s`\n", name);
679 return 0;
680 }
681
682 if (strncmp(info.tag, "PRE ", 4)) {
683 WARN("`%s` is not a prelinked library\n", name);
684 return 0;
685 }
686
687 return (unsigned long)info.mmap_addr;
688}
689
690/* verify_elf_object
691 * Verifies if the object @ base is a valid ELF object
692 *
693 * Args:
694 *
695 * Returns:
696 * 0 on success
697 * -1 if no valid ELF object is found @ base.
698 */
699static int
700verify_elf_object(void *base, const char *name)
701{
702 Elf32_Ehdr *hdr = (Elf32_Ehdr *) base;
703
704 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
705 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
706 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
707 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
708
709 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800710#ifdef ANDROID_ARM_LINKER
711 if (hdr->e_machine != EM_ARM) return -1;
712#elif defined(ANDROID_X86_LINKER)
713 if (hdr->e_machine != EM_386) return -1;
714#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800715 return 0;
716}
717
718
719/* get_lib_extents
720 * Retrieves the base (*base) address where the ELF object should be
721 * mapped and its overall memory size (*total_sz).
722 *
723 * Args:
724 * fd: Opened file descriptor for the library
725 * name: The name of the library
726 * _hdr: Pointer to the header page of the library
727 * total_sz: Total size of the memory that should be allocated for
728 * this library
729 *
730 * Returns:
731 * -1 if there was an error while trying to get the lib extents.
732 * The possible reasons are:
733 * - Could not determine if the library was prelinked.
734 * - The library provided is not a valid ELF object
735 * 0 if the library did not request a specific base offset (normal
736 * for non-prelinked libs)
737 * > 0 if the library requests a specific address to be mapped to.
738 * This indicates a pre-linked library.
739 */
740static unsigned
741get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz)
742{
743 unsigned req_base;
744 unsigned min_vaddr = 0xffffffff;
745 unsigned max_vaddr = 0;
746 unsigned char *_hdr = (unsigned char *)__hdr;
747 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)_hdr;
748 Elf32_Phdr *phdr;
749 int cnt;
750
751 TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name);
752 if (verify_elf_object(_hdr, name) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700753 DL_ERR("%5d - %s is not a valid ELF object", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800754 return (unsigned)-1;
755 }
756
757 req_base = (unsigned) is_prelinked(fd, name);
758 if (req_base == (unsigned)-1)
759 return -1;
760 else if (req_base != 0) {
761 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
762 pid, name, req_base);
763 } else {
764 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
765 }
766
767 phdr = (Elf32_Phdr *)(_hdr + ehdr->e_phoff);
768
769 /* find the min/max p_vaddrs from all the PT_LOAD segments so we can
770 * get the range. */
771 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
772 if (phdr->p_type == PT_LOAD) {
773 if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr)
774 max_vaddr = phdr->p_vaddr + phdr->p_memsz;
775 if (phdr->p_vaddr < min_vaddr)
776 min_vaddr = phdr->p_vaddr;
777 }
778 }
779
780 if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700781 DL_ERR("%5d - No loadable segments found in %s.", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800782 return (unsigned)-1;
783 }
784
785 /* truncate min_vaddr down to page boundary */
786 min_vaddr &= ~PAGE_MASK;
787
788 /* round max_vaddr up to the next page */
789 max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK;
790
791 *total_sz = (max_vaddr - min_vaddr);
792 return (unsigned)req_base;
793}
794
795/* alloc_mem_region
796 *
797 * This function reserves a chunk of memory to be used for mapping in
798 * the shared library. We reserve the entire memory region here, and
799 * then the rest of the linker will relocate the individual loadable
800 * segments into the correct locations within this memory range.
801 *
802 * Args:
803 * si->base: The requested base of the allocation. If 0, a sane one will be
804 * chosen in the range LIBBASE <= base < LIBLAST.
805 * si->size: The size of the allocation.
806 *
807 * Returns:
808 * -1 on failure, and 0 on success. On success, si->base will contain
809 * the virtual address at which the library will be mapped.
810 */
811
812static int reserve_mem_region(soinfo *si)
813{
814 void *base = mmap((void *)si->base, si->size, PROT_READ | PROT_EXEC,
Chris Dearmandb4bce02011-03-10 10:48:14 -0800815 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800816 if (base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700817 DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700818 "as requested, will try general pool: %d (%s)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800819 pid, (si->base ? "" : "non-"), si->name, si->base,
820 errno, strerror(errno));
821 return -1;
822 } else if (base != (void *)si->base) {
Dima Zavin2e855792009-05-20 18:28:09 -0700823 DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700824 "not at 0x%08x", pid, (si->base ? "" : "non-"),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800825 si->name, (unsigned)base, si->base);
826 munmap(base, si->size);
827 return -1;
828 }
829 return 0;
830}
831
832static int
833alloc_mem_region(soinfo *si)
834{
835 if (si->base) {
836 /* Attempt to mmap a prelinked library. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800837 return reserve_mem_region(si);
838 }
839
Shih-wei Liao48527c32011-07-17 12:32:43 -0700840 /* This is not a prelinked library, so we use the kernel's default
841 allocator.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800842 */
Shih-wei Liao48527c32011-07-17 12:32:43 -0700843
844 void *base = mmap(NULL, si->size, PROT_READ | PROT_EXEC,
845 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
846 if (base == MAP_FAILED) {
847 DL_ERR("%5d mmap of library '%s' failed: %d (%s)\n",
848 pid, si->name,
849 errno, strerror(errno));
850 goto err;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800851 }
Shih-wei Liao48527c32011-07-17 12:32:43 -0700852 si->base = (unsigned) base;
853 PRINT("%5d mapped library '%s' to %08x via kernel allocator.\n",
854 pid, si->name, si->base);
855 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800856
857err:
Erik Gillingd00d23a2009-07-22 17:06:11 -0700858 DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800859 pid, si->name);
860 return -1;
861}
862
863#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
864#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
865 MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
866 MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
867/* load_segments
868 *
869 * This function loads all the loadable (PT_LOAD) segments into memory
870 * at their appropriate memory offsets off the base address.
871 *
872 * Args:
873 * fd: Open file descriptor to the library to load.
874 * header: Pointer to a header page that contains the ELF header.
875 * This is needed since we haven't mapped in the real file yet.
876 * si: ptr to soinfo struct describing the shared object.
877 *
878 * Returns:
879 * 0 on success, -1 on failure.
880 */
881static int
882load_segments(int fd, void *header, soinfo *si)
883{
884 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
885 Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned char *)header + ehdr->e_phoff);
886 unsigned char *base = (unsigned char *)si->base;
887 int cnt;
888 unsigned len;
889 unsigned char *tmp;
890 unsigned char *pbase;
891 unsigned char *extra_base;
892 unsigned extra_len;
893 unsigned total_sz = 0;
894
895 si->wrprotect_start = 0xffffffff;
896 si->wrprotect_end = 0;
897
898 TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n",
899 pid, si->name, (unsigned)si->base);
900 /* Now go through all the PT_LOAD segments and map them into memory
901 * at the appropriate locations. */
902 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
903 if (phdr->p_type == PT_LOAD) {
904 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
905 /* we want to map in the segment on a page boundary */
906 tmp = base + (phdr->p_vaddr & (~PAGE_MASK));
907 /* add the # of bytes we masked off above to the total length. */
908 len = phdr->p_filesz + (phdr->p_vaddr & PAGE_MASK);
909
910 TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x "
911 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name,
912 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
913 pbase = mmap(tmp, len, PFLAGS_TO_PROT(phdr->p_flags),
914 MAP_PRIVATE | MAP_FIXED, fd,
915 phdr->p_offset & (~PAGE_MASK));
916 if (pbase == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700917 DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700918 "p_vaddr=0x%08x p_offset=0x%08x", pid, si->name,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800919 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
920 goto fail;
921 }
922
923 /* If 'len' didn't end on page boundary, and it's a writable
924 * segment, zero-fill the rest. */
925 if ((len & PAGE_MASK) && (phdr->p_flags & PF_W))
926 memset((void *)(pbase + len), 0, PAGE_SIZE - (len & PAGE_MASK));
927
928 /* Check to see if we need to extend the map for this segment to
929 * cover the diff between filesz and memsz (i.e. for bss).
930 *
931 * base _+---------------------+ page boundary
932 * . .
933 * | |
934 * . .
935 * pbase _+---------------------+ page boundary
936 * | |
937 * . .
938 * base + p_vaddr _| |
939 * . \ \ .
940 * . | filesz | .
941 * pbase + len _| / | |
942 * <0 pad> . . .
943 * extra_base _+------------|--------+ page boundary
944 * / . . .
945 * | . . .
946 * | +------------|--------+ page boundary
947 * extra_len-> | | | |
948 * | . | memsz .
949 * | . | .
950 * \ _| / |
951 * . .
952 * | |
953 * _+---------------------+ page boundary
954 */
955 tmp = (unsigned char *)(((unsigned)pbase + len + PAGE_SIZE - 1) &
956 (~PAGE_MASK));
957 if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) {
958 extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp;
959 TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x "
960 "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len);
961 /* map in the extra page(s) as anonymous into the range.
962 * This is probably not necessary as we already mapped in
963 * the entire region previously, but we just want to be
964 * sure. This will also set the right flags on the region
965 * (though we can probably accomplish the same thing with
966 * mprotect).
967 */
968 extra_base = mmap((void *)tmp, extra_len,
969 PFLAGS_TO_PROT(phdr->p_flags),
970 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
971 -1, 0);
972 if (extra_base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700973 DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x"
Erik Gillingd00d23a2009-07-22 17:06:11 -0700974 " (0x%08x) ]", pid, si->name, (unsigned)tmp,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800975 extra_len);
976 goto fail;
977 }
978 /* TODO: Check if we need to memset-0 this region.
979 * Anonymous mappings are zero-filled copy-on-writes, so we
980 * shouldn't need to. */
981 TRACE("[ %5d - Segment from '%s' extended @ 0x%08x "
982 "(0x%08x)\n", pid, si->name, (unsigned)extra_base,
983 extra_len);
984 }
985 /* set the len here to show the full extent of the segment we
986 * just loaded, mostly for debugging */
987 len = (((unsigned)base + phdr->p_vaddr + phdr->p_memsz +
988 PAGE_SIZE - 1) & (~PAGE_MASK)) - (unsigned)pbase;
989 TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x "
990 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
991 (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset);
992 total_sz += len;
993 /* Make the section writable just in case we'll have to write to
994 * it during relocation (i.e. text segment). However, we will
995 * remember what range of addresses should be write protected.
996 *
997 */
998 if (!(phdr->p_flags & PF_W)) {
999 if ((unsigned)pbase < si->wrprotect_start)
1000 si->wrprotect_start = (unsigned)pbase;
1001 if (((unsigned)pbase + len) > si->wrprotect_end)
1002 si->wrprotect_end = (unsigned)pbase + len;
1003 mprotect(pbase, len,
1004 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
1005 }
1006 } else if (phdr->p_type == PT_DYNAMIC) {
1007 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
1008 /* this segment contains the dynamic linking information */
1009 si->dynamic = (unsigned *)(base + phdr->p_vaddr);
1010 } else {
1011#ifdef ANDROID_ARM_LINKER
1012 if (phdr->p_type == PT_ARM_EXIDX) {
1013 DEBUG_DUMP_PHDR(phdr, "PT_ARM_EXIDX", pid);
1014 /* exidx entries (used for stack unwinding) are 8 bytes each.
1015 */
1016 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1017 si->ARM_exidx_count = phdr->p_memsz / 8;
1018 }
1019#endif
1020 }
1021
1022 }
1023
1024 /* Sanity check */
1025 if (total_sz > si->size) {
Dima Zavin2e855792009-05-20 18:28:09 -07001026 DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001027 "greater than what was allocated (0x%08x). THIS IS BAD!",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001028 pid, total_sz, si->name, si->size);
1029 goto fail;
1030 }
1031
1032 TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. "
1033 "Total memory footprint: 0x%08x bytes ]\n", pid, si->name,
1034 (unsigned)si->base, si->size);
1035 return 0;
1036
1037fail:
1038 /* We can just blindly unmap the entire region even though some things
1039 * were mapped in originally with anonymous and others could have been
1040 * been mapped in from the file before we failed. The kernel will unmap
1041 * all the pages in the range, irrespective of how they got there.
1042 */
1043 munmap((void *)si->base, si->size);
1044 si->flags |= FLAG_ERROR;
1045 return -1;
1046}
1047
1048/* TODO: Implement this to take care of the fact that Android ARM
1049 * ELF objects shove everything into a single loadable segment that has the
1050 * write bit set. wr_offset is then used to set non-(data|bss) pages to be
1051 * non-writable.
1052 */
1053#if 0
1054static unsigned
1055get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr)
1056{
1057 Elf32_Shdr *shdr_start;
1058 Elf32_Shdr *shdr;
1059 int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr);
1060 int cnt;
1061 unsigned wr_offset = 0xffffffff;
1062
1063 shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd,
1064 ehdr->e_shoff & (~PAGE_MASK));
1065 if (shdr_start == MAP_FAILED) {
1066 WARN("%5d - Could not read section header info from '%s'. Will not "
1067 "not be able to determine write-protect offset.\n", pid, name);
1068 return (unsigned)-1;
1069 }
1070
1071 for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) {
1072 if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) &&
1073 (shdr->sh_addr < wr_offset)) {
1074 wr_offset = shdr->sh_addr;
1075 }
1076 }
1077
1078 munmap(shdr_start, shdr_sz);
1079 return wr_offset;
1080}
1081#endif
1082
1083static soinfo *
1084load_library(const char *name)
1085{
1086 int fd = open_library(name);
1087 int cnt;
1088 unsigned ext_sz;
1089 unsigned req_base;
Erik Gillingfde86422009-07-28 20:28:19 -07001090 const char *bname;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001091 soinfo *si = NULL;
1092 Elf32_Ehdr *hdr;
1093
Dima Zavin2e855792009-05-20 18:28:09 -07001094 if(fd == -1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001095 DL_ERR("Library '%s' not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001096 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -07001097 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001098
1099 /* We have to read the ELF header to figure out what to do with this image
1100 */
1101 if (lseek(fd, 0, SEEK_SET) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001102 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001103 goto fail;
1104 }
1105
1106 if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001107 DL_ERR("read() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001108 goto fail;
1109 }
1110
1111 /* Parse the ELF header and get the size of the memory footprint for
1112 * the library */
1113 req_base = get_lib_extents(fd, name, &__header[0], &ext_sz);
1114 if (req_base == (unsigned)-1)
1115 goto fail;
1116 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
1117 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
1118
1119 /* Now configure the soinfo struct where we'll store all of our data
1120 * for the ELF object. If the loading fails, we waste the entry, but
1121 * same thing would happen if we failed during linking. Configuring the
1122 * soinfo struct here is a lot more convenient.
1123 */
Erik Gillingfde86422009-07-28 20:28:19 -07001124 bname = strrchr(name, '/');
1125 si = alloc_info(bname ? bname + 1 : name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001126 if (si == NULL)
1127 goto fail;
1128
1129 /* Carve out a chunk of memory where we will map in the individual
1130 * segments */
1131 si->base = req_base;
1132 si->size = ext_sz;
1133 si->flags = 0;
1134 si->entry = 0;
1135 si->dynamic = (unsigned *)-1;
1136 if (alloc_mem_region(si) < 0)
1137 goto fail;
1138
1139 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
1140 pid, name, (void *)si->base, (unsigned) ext_sz);
1141
1142 /* Now actually load the library's segments into right places in memory */
1143 if (load_segments(fd, &__header[0], si) < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001144 goto fail;
1145 }
1146
1147 /* this might not be right. Technically, we don't even need this info
1148 * once we go through 'load_segments'. */
1149 hdr = (Elf32_Ehdr *)si->base;
1150 si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff);
1151 si->phnum = hdr->e_phnum;
1152 /**/
1153
1154 close(fd);
1155 return si;
1156
1157fail:
1158 if (si) free_info(si);
1159 close(fd);
1160 return NULL;
1161}
1162
1163static soinfo *
1164init_library(soinfo *si)
1165{
1166 unsigned wr_offset = 0xffffffff;
1167
1168 /* At this point we know that whatever is loaded @ base is a valid ELF
1169 * shared library whose segments are properly mapped in. */
1170 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
1171 pid, si->base, si->size, si->name);
1172
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001173 if(link_image(si, wr_offset)) {
1174 /* We failed to link. However, we can only restore libbase
1175 ** if no additional libraries have moved it since we updated it.
1176 */
1177 munmap((void *)si->base, si->size);
1178 return NULL;
1179 }
1180
1181 return si;
1182}
1183
1184soinfo *find_library(const char *name)
1185{
1186 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001187 const char *bname;
1188
1189#if ALLOW_SYMBOLS_FROM_MAIN
1190 if (name == NULL)
1191 return somain;
1192#else
1193 if (name == NULL)
1194 return NULL;
1195#endif
1196
1197 bname = strrchr(name, '/');
Erik Gillingfde86422009-07-28 20:28:19 -07001198 bname = bname ? bname + 1 : name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001199
1200 for(si = solist; si != 0; si = si->next){
Erik Gillingfde86422009-07-28 20:28:19 -07001201 if(!strcmp(bname, si->name)) {
Erik Gilling30eb4022009-08-13 16:05:30 -07001202 if(si->flags & FLAG_ERROR) {
1203 DL_ERR("%5d '%s' failed to load previously", pid, bname);
1204 return NULL;
1205 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001206 if(si->flags & FLAG_LINKED) return si;
Erik Gillingd00d23a2009-07-22 17:06:11 -07001207 DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name);
Dima Zavin2e855792009-05-20 18:28:09 -07001208 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001209 }
1210 }
1211
1212 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
1213 si = load_library(name);
1214 if(si == NULL)
1215 return NULL;
1216 return init_library(si);
1217}
1218
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001219/* TODO:
1220 * notify gdb of unload
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001221 * for non-prelinked libraries, find a way to decrement libbase
1222 */
1223static void call_destructors(soinfo *si);
1224unsigned unload_library(soinfo *si)
1225{
1226 unsigned *d;
1227 if (si->refcount == 1) {
1228 TRACE("%5d unloading '%s'\n", pid, si->name);
1229 call_destructors(si);
1230
1231 for(d = si->dynamic; *d; d += 2) {
1232 if(d[0] == DT_NEEDED){
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001233 soinfo *lsi = (soinfo *)d[1];
1234 d[1] = 0;
1235 if (validate_soinfo(lsi)) {
1236 TRACE("%5d %s needs to unload %s\n", pid,
1237 si->name, lsi->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001238 unload_library(lsi);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001239 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001240 else
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001241 DL_ERR("%5d %s: could not unload dependent library",
1242 pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001243 }
1244 }
1245
1246 munmap((char *)si->base, si->size);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001247 notify_gdb_of_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001248 free_info(si);
1249 si->refcount = 0;
1250 }
1251 else {
1252 si->refcount--;
1253 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
1254 pid, si->name, si->refcount);
1255 }
1256 return si->refcount;
1257}
1258
1259/* TODO: don't use unsigned for addrs below. It works, but is not
1260 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1261 * long.
1262 */
1263static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count)
1264{
1265 Elf32_Sym *symtab = si->symtab;
1266 const char *strtab = si->strtab;
1267 Elf32_Sym *s;
1268 unsigned base;
1269 Elf32_Rel *start = rel;
1270 unsigned idx;
1271
1272 for (idx = 0; idx < count; ++idx) {
1273 unsigned type = ELF32_R_TYPE(rel->r_info);
1274 unsigned sym = ELF32_R_SYM(rel->r_info);
1275 unsigned reloc = (unsigned)(rel->r_offset + si->base);
1276 unsigned sym_addr = 0;
1277 char *sym_name = NULL;
1278
1279 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1280 si->name, idx);
1281 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001282 sym_name = (char *)(strtab + symtab[sym].st_name);
1283 s = _do_lookup(si, sym_name, &base);
Doug Kwane8238072009-10-26 12:05:23 -07001284 if(s == NULL) {
1285 /* We only allow an undefined symbol if this is a weak
1286 reference.. */
1287 s = &symtab[sym];
1288 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1289 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
1290 return -1;
1291 }
1292
1293 /* IHI0044C AAELF 4.5.1.1:
1294
1295 Libraries are not searched to resolve weak references.
1296 It is not an error for a weak reference to remain
1297 unsatisfied.
1298
1299 During linking, the value of an undefined weak reference is:
1300 - Zero if the relocation type is absolute
1301 - The address of the place if the relocation is pc-relative
1302 - The address of nominial base address if the relocation
1303 type is base-relative.
1304 */
1305
1306 switch (type) {
1307#if defined(ANDROID_ARM_LINKER)
1308 case R_ARM_JUMP_SLOT:
1309 case R_ARM_GLOB_DAT:
1310 case R_ARM_ABS32:
1311 case R_ARM_RELATIVE: /* Don't care. */
1312 case R_ARM_NONE: /* Don't care. */
1313#elif defined(ANDROID_X86_LINKER)
1314 case R_386_JUMP_SLOT:
1315 case R_386_GLOB_DAT:
1316 case R_386_32:
1317 case R_386_RELATIVE: /* Dont' care. */
1318#endif /* ANDROID_*_LINKER */
1319 /* sym_addr was initialized to be zero above or relocation
1320 code below does not care about value of sym_addr.
1321 No need to do anything. */
1322 break;
1323
1324#if defined(ANDROID_X86_LINKER)
1325 case R_386_PC32:
1326 sym_addr = reloc;
1327 break;
1328#endif /* ANDROID_X86_LINKER */
1329
1330#if defined(ANDROID_ARM_LINKER)
1331 case R_ARM_COPY:
1332 /* Fall through. Can't really copy if weak symbol is
1333 not found in run-time. */
1334#endif /* ANDROID_ARM_LINKER */
1335 default:
1336 DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n",
1337 pid, type, rel, (int) (rel - start));
1338 return -1;
1339 }
1340 } else {
1341 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001342#if 0
1343 if((base == 0) && (si->base != 0)){
1344 /* linking from libraries to main image is bad */
Erik Gillingd00d23a2009-07-22 17:06:11 -07001345 DL_ERR("%5d cannot locate '%s'...",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001346 pid, strtab + symtab[sym].st_name);
1347 return -1;
1348 }
1349#endif
Doug Kwane8238072009-10-26 12:05:23 -07001350 sym_addr = (unsigned)(s->st_value + base);
1351 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001352 COUNT_RELOC(RELOC_SYMBOL);
1353 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001354 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001355 }
1356
1357/* TODO: This is ugly. Split up the relocations by arch into
1358 * different files.
1359 */
1360 switch(type){
1361#if defined(ANDROID_ARM_LINKER)
1362 case R_ARM_JUMP_SLOT:
1363 COUNT_RELOC(RELOC_ABSOLUTE);
1364 MARK(rel->r_offset);
1365 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1366 reloc, sym_addr, sym_name);
1367 *((unsigned*)reloc) = sym_addr;
1368 break;
1369 case R_ARM_GLOB_DAT:
1370 COUNT_RELOC(RELOC_ABSOLUTE);
1371 MARK(rel->r_offset);
1372 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1373 reloc, sym_addr, sym_name);
1374 *((unsigned*)reloc) = sym_addr;
1375 break;
1376 case R_ARM_ABS32:
1377 COUNT_RELOC(RELOC_ABSOLUTE);
1378 MARK(rel->r_offset);
1379 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1380 reloc, sym_addr, sym_name);
1381 *((unsigned*)reloc) += sym_addr;
1382 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001383 case R_ARM_REL32:
1384 COUNT_RELOC(RELOC_RELATIVE);
1385 MARK(rel->r_offset);
1386 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1387 reloc, sym_addr, rel->r_offset, sym_name);
1388 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1389 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001390#elif defined(ANDROID_X86_LINKER)
1391 case R_386_JUMP_SLOT:
1392 COUNT_RELOC(RELOC_ABSOLUTE);
1393 MARK(rel->r_offset);
1394 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1395 reloc, sym_addr, sym_name);
1396 *((unsigned*)reloc) = sym_addr;
1397 break;
1398 case R_386_GLOB_DAT:
1399 COUNT_RELOC(RELOC_ABSOLUTE);
1400 MARK(rel->r_offset);
1401 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1402 reloc, sym_addr, sym_name);
1403 *((unsigned*)reloc) = sym_addr;
1404 break;
1405#endif /* ANDROID_*_LINKER */
1406
1407#if defined(ANDROID_ARM_LINKER)
1408 case R_ARM_RELATIVE:
1409#elif defined(ANDROID_X86_LINKER)
1410 case R_386_RELATIVE:
1411#endif /* ANDROID_*_LINKER */
1412 COUNT_RELOC(RELOC_RELATIVE);
1413 MARK(rel->r_offset);
1414 if(sym){
Erik Gillingd00d23a2009-07-22 17:06:11 -07001415 DL_ERR("%5d odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001416 return -1;
1417 }
1418 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1419 reloc, si->base);
1420 *((unsigned*)reloc) += si->base;
1421 break;
1422
1423#if defined(ANDROID_X86_LINKER)
1424 case R_386_32:
1425 COUNT_RELOC(RELOC_RELATIVE);
1426 MARK(rel->r_offset);
1427
1428 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1429 reloc, sym_addr, sym_name);
1430 *((unsigned *)reloc) += (unsigned)sym_addr;
1431 break;
1432
1433 case R_386_PC32:
1434 COUNT_RELOC(RELOC_RELATIVE);
1435 MARK(rel->r_offset);
1436 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1437 "+%08x (%08x - %08x) %s\n", pid, reloc,
1438 (sym_addr - reloc), sym_addr, reloc, sym_name);
1439 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1440 break;
1441#endif /* ANDROID_X86_LINKER */
1442
1443#ifdef ANDROID_ARM_LINKER
1444 case R_ARM_COPY:
1445 COUNT_RELOC(RELOC_COPY);
1446 MARK(rel->r_offset);
1447 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1448 reloc, s->st_size, sym_addr, sym_name);
1449 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1450 break;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001451 case R_ARM_NONE:
1452 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001453#endif /* ANDROID_ARM_LINKER */
1454
1455 default:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001456 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001457 pid, type, rel, (int) (rel - start));
1458 return -1;
1459 }
1460 rel++;
1461 }
1462 return 0;
1463}
1464
David 'Digit' Turner82156792009-05-18 14:37:41 +02001465/* Please read the "Initialization and Termination functions" functions.
1466 * of the linker design note in bionic/linker/README.TXT to understand
1467 * what the following code is doing.
1468 *
1469 * The important things to remember are:
1470 *
1471 * DT_PREINIT_ARRAY must be called first for executables, and should
1472 * not appear in shared libraries.
1473 *
1474 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1475 *
1476 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1477 *
1478 * DT_FINI_ARRAY must be parsed in reverse order.
1479 */
1480
1481static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001482{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001483 int n, inc = 1;
1484
1485 if (reverse) {
1486 ctor += (count-1);
1487 inc = -1;
1488 }
1489
1490 for(n = count; n > 0; n--) {
1491 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1492 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001493 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001494 void (*func)() = (void (*)()) *ctor;
1495 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001496 if(((int) func == 0) || ((int) func == -1)) continue;
1497 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1498 func();
1499 }
1500}
1501
1502static void call_constructors(soinfo *si)
1503{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001504 if (si->flags & FLAG_EXE) {
1505 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1506 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1507 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001508 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001509 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1510 } else {
1511 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001512 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
Erik Gillingd00d23a2009-07-22 17:06:11 -07001513 " This is INVALID.", pid, si->name,
Dima Zavin2e855792009-05-20 18:28:09 -07001514 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001515 }
1516 }
1517
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001518 if (si->init_func) {
1519 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1520 (unsigned)si->init_func, si->name);
1521 si->init_func();
1522 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1523 }
1524
1525 if (si->init_array) {
1526 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1527 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001528 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001529 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1530 }
1531}
1532
David 'Digit' Turner82156792009-05-18 14:37:41 +02001533
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001534static void call_destructors(soinfo *si)
1535{
1536 if (si->fini_array) {
1537 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1538 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001539 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001540 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1541 }
1542
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001543 if (si->fini_func) {
1544 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1545 (unsigned)si->fini_func, si->name);
1546 si->fini_func();
1547 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1548 }
1549}
1550
1551/* Force any of the closed stdin, stdout and stderr to be associated with
1552 /dev/null. */
1553static int nullify_closed_stdio (void)
1554{
1555 int dev_null, i, status;
1556 int return_value = 0;
1557
1558 dev_null = open("/dev/null", O_RDWR);
1559 if (dev_null < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001560 DL_ERR("Cannot open /dev/null.");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001561 return -1;
1562 }
1563 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1564
1565 /* If any of the stdio file descriptors is valid and not associated
1566 with /dev/null, dup /dev/null to it. */
1567 for (i = 0; i < 3; i++) {
1568 /* If it is /dev/null already, we are done. */
1569 if (i == dev_null)
1570 continue;
1571
1572 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1573 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1574 can be interrupted but we do this just to be safe. */
1575 do {
1576 status = fcntl(i, F_GETFL);
1577 } while (status < 0 && errno == EINTR);
1578
1579 /* If file is openned, we are good. */
1580 if (status >= 0)
1581 continue;
1582
1583 /* The only error we allow is that the file descriptor does not
1584 exist, in which case we dup /dev/null to it. */
1585 if (errno != EBADF) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001586 DL_ERR("nullify_stdio: unhandled error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001587 return_value = -1;
1588 continue;
1589 }
1590
1591 /* Try dupping /dev/null to this stdio file descriptor and
1592 repeat if there is a signal. Note that any errors in closing
1593 the stdio descriptor are lost. */
1594 do {
1595 status = dup2(dev_null, i);
1596 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001597
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001598 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001599 DL_ERR("nullify_stdio: dup2 error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001600 return_value = -1;
1601 continue;
1602 }
1603 }
1604
1605 /* If /dev/null is not one of the stdio file descriptors, close it. */
1606 if (dev_null > 2) {
1607 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001608 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001609 status = close(dev_null);
1610 } while (status < 0 && errno == EINTR);
1611
1612 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001613 DL_ERR("nullify_stdio: close error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001614 return_value = -1;
1615 }
1616 }
1617
1618 return return_value;
1619}
1620
1621static int link_image(soinfo *si, unsigned wr_offset)
1622{
1623 unsigned *d;
1624 Elf32_Phdr *phdr = si->phdr;
1625 int phnum = si->phnum;
1626
1627 INFO("[ %5d linking %s ]\n", pid, si->name);
1628 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1629 si->base, si->flags);
1630
1631 if (si->flags & FLAG_EXE) {
1632 /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for
1633 * linkage info if this is the executable. If this was a
1634 * dynamic lib, that would have been done at load time.
1635 *
1636 * TODO: It's unfortunate that small pieces of this are
1637 * repeated from the load_library routine. Refactor this just
1638 * slightly to reuse these bits.
1639 */
1640 si->size = 0;
1641 for(; phnum > 0; --phnum, ++phdr) {
1642#ifdef ANDROID_ARM_LINKER
1643 if(phdr->p_type == PT_ARM_EXIDX) {
1644 /* exidx entries (used for stack unwinding) are 8 bytes each.
1645 */
1646 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1647 si->ARM_exidx_count = phdr->p_memsz / 8;
1648 }
1649#endif
1650 if (phdr->p_type == PT_LOAD) {
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001651 /* For the executable, we use the si->size field only in
1652 dl_unwind_find_exidx(), so the meaning of si->size
1653 is not the size of the executable; it is the last
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001654 virtual address of the loadable part of the executable;
1655 since si->base == 0 for an executable, we use the
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001656 range [0, si->size) to determine whether a PC value
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001657 falls within the executable section. Of course, if
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001658 a value is below phdr->p_vaddr, it's not in the
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001659 executable section, but a) we shouldn't be asking for
1660 such a value anyway, and b) if we have to provide
1661 an EXIDX for such a value, then the executable's
1662 EXIDX is probably the better choice.
1663 */
1664 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
1665 if (phdr->p_vaddr + phdr->p_memsz > si->size)
1666 si->size = phdr->p_vaddr + phdr->p_memsz;
1667 /* try to remember what range of addresses should be write
1668 * protected */
1669 if (!(phdr->p_flags & PF_W)) {
1670 unsigned _end;
1671
1672 if (phdr->p_vaddr < si->wrprotect_start)
1673 si->wrprotect_start = phdr->p_vaddr;
1674 _end = (((phdr->p_vaddr + phdr->p_memsz + PAGE_SIZE - 1) &
1675 (~PAGE_MASK)));
1676 if (_end > si->wrprotect_end)
1677 si->wrprotect_end = _end;
1678 }
1679 } else if (phdr->p_type == PT_DYNAMIC) {
1680 if (si->dynamic != (unsigned *)-1) {
Dima Zavin2e855792009-05-20 18:28:09 -07001681 DL_ERR("%5d multiple PT_DYNAMIC segments found in '%s'. "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001682 "Segment at 0x%08x, previously one found at 0x%08x",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001683 pid, si->name, si->base + phdr->p_vaddr,
1684 (unsigned)si->dynamic);
1685 goto fail;
1686 }
1687 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
1688 si->dynamic = (unsigned *) (si->base + phdr->p_vaddr);
1689 }
1690 }
1691 }
1692
1693 if (si->dynamic == (unsigned *)-1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001694 DL_ERR("%5d missing PT_DYNAMIC?!", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001695 goto fail;
1696 }
1697
1698 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1699
1700 /* extract useful information from dynamic section */
1701 for(d = si->dynamic; *d; d++){
1702 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1703 switch(*d++){
1704 case DT_HASH:
1705 si->nbucket = ((unsigned *) (si->base + *d))[0];
1706 si->nchain = ((unsigned *) (si->base + *d))[1];
1707 si->bucket = (unsigned *) (si->base + *d + 8);
1708 si->chain = (unsigned *) (si->base + *d + 8 + si->nbucket * 4);
1709 break;
1710 case DT_STRTAB:
1711 si->strtab = (const char *) (si->base + *d);
1712 break;
1713 case DT_SYMTAB:
1714 si->symtab = (Elf32_Sym *) (si->base + *d);
1715 break;
1716 case DT_PLTREL:
1717 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001718 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001719 goto fail;
1720 }
1721 break;
1722 case DT_JMPREL:
1723 si->plt_rel = (Elf32_Rel*) (si->base + *d);
1724 break;
1725 case DT_PLTRELSZ:
1726 si->plt_rel_count = *d / 8;
1727 break;
1728 case DT_REL:
1729 si->rel = (Elf32_Rel*) (si->base + *d);
1730 break;
1731 case DT_RELSZ:
1732 si->rel_count = *d / 8;
1733 break;
1734 case DT_PLTGOT:
1735 /* Save this in case we decide to do lazy binding. We don't yet. */
1736 si->plt_got = (unsigned *)(si->base + *d);
1737 break;
1738 case DT_DEBUG:
1739 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1740 *d = (int) &_r_debug;
1741 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001742 case DT_RELA:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001743 DL_ERR("%5d DT_RELA not supported", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001744 goto fail;
1745 case DT_INIT:
1746 si->init_func = (void (*)(void))(si->base + *d);
1747 DEBUG("%5d %s constructors (init func) found at %p\n",
1748 pid, si->name, si->init_func);
1749 break;
1750 case DT_FINI:
1751 si->fini_func = (void (*)(void))(si->base + *d);
1752 DEBUG("%5d %s destructors (fini func) found at %p\n",
1753 pid, si->name, si->fini_func);
1754 break;
1755 case DT_INIT_ARRAY:
1756 si->init_array = (unsigned *)(si->base + *d);
1757 DEBUG("%5d %s constructors (init_array) found at %p\n",
1758 pid, si->name, si->init_array);
1759 break;
1760 case DT_INIT_ARRAYSZ:
1761 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1762 break;
1763 case DT_FINI_ARRAY:
1764 si->fini_array = (unsigned *)(si->base + *d);
1765 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1766 pid, si->name, si->fini_array);
1767 break;
1768 case DT_FINI_ARRAYSZ:
1769 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1770 break;
1771 case DT_PREINIT_ARRAY:
1772 si->preinit_array = (unsigned *)(si->base + *d);
1773 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1774 pid, si->name, si->preinit_array);
1775 break;
1776 case DT_PREINIT_ARRAYSZ:
1777 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1778 break;
1779 case DT_TEXTREL:
1780 /* TODO: make use of this. */
1781 /* this means that we might have to write into where the text
1782 * segment was loaded during relocation... Do something with
1783 * it.
1784 */
1785 DEBUG("%5d Text segment should be writable during relocation.\n",
1786 pid);
1787 break;
1788 }
1789 }
1790
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001791 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001792 pid, si->base, si->strtab, si->symtab);
1793
1794 if((si->strtab == 0) || (si->symtab == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001795 DL_ERR("%5d missing essential tables", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001796 goto fail;
1797 }
1798
Matt Fischer4fd42c12009-12-31 12:09:10 -06001799 /* if this is the main executable, then load all of the preloads now */
1800 if(si->flags & FLAG_EXE) {
1801 int i;
1802 memset(preloads, 0, sizeof(preloads));
1803 for(i = 0; ldpreload_names[i] != NULL; i++) {
1804 soinfo *lsi = find_library(ldpreload_names[i]);
1805 if(lsi == 0) {
1806 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
1807 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
1808 pid, ldpreload_names[i], si->name, tmp_err_buf);
1809 goto fail;
1810 }
1811 lsi->refcount++;
1812 preloads[i] = lsi;
1813 }
1814 }
1815
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001816 for(d = si->dynamic; *d; d += 2) {
1817 if(d[0] == DT_NEEDED){
1818 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001819 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001820 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001821 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Erik Gillingd00d23a2009-07-22 17:06:11 -07001822 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
Dima Zavin03531952009-05-29 17:30:25 -07001823 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001824 goto fail;
1825 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001826 /* Save the soinfo of the loaded DT_NEEDED library in the payload
1827 of the DT_NEEDED entry itself, so that we can retrieve the
1828 soinfo directly later from the dynamic segment. This is a hack,
1829 but it allows us to map from DT_NEEDED to soinfo efficiently
1830 later on when we resolve relocations, trying to look up a symgol
1831 with dlsym().
1832 */
1833 d[1] = (unsigned)lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001834 lsi->refcount++;
1835 }
1836 }
1837
1838 if(si->plt_rel) {
1839 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1840 if(reloc_library(si, si->plt_rel, si->plt_rel_count))
1841 goto fail;
1842 }
1843 if(si->rel) {
1844 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1845 if(reloc_library(si, si->rel, si->rel_count))
1846 goto fail;
1847 }
1848
1849 si->flags |= FLAG_LINKED;
1850 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1851
1852#if 0
1853 /* This is the way that the old dynamic linker did protection of
1854 * non-writable areas. It would scan section headers and find where
1855 * .text ended (rather where .data/.bss began) and assume that this is
1856 * the upper range of the non-writable area. This is too coarse,
1857 * and is kept here for reference until we fully move away from single
1858 * segment elf objects. See the code in get_wr_offset (also #if'd 0)
1859 * that made this possible.
1860 */
1861 if(wr_offset < 0xffffffff){
1862 mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC);
1863 }
1864#else
1865 /* TODO: Verify that this does the right thing in all cases, as it
1866 * presently probably does not. It is possible that an ELF image will
1867 * come with multiple read-only segments. What we ought to do is scan
1868 * the program headers again and mprotect all the read-only segments.
1869 * To prevent re-scanning the program header, we would have to build a
1870 * list of loadable segments in si, and then scan that instead. */
1871 if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) {
1872 mprotect((void *)si->wrprotect_start,
1873 si->wrprotect_end - si->wrprotect_start,
1874 PROT_READ | PROT_EXEC);
1875 }
1876#endif
1877
1878 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1879 stdout and stderr to close a security hole described in:
1880
1881 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1882
1883 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001884 if (program_is_setuid)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001885 nullify_closed_stdio ();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001886 notify_gdb_of_load(si);
David 'Digit' Turnere5ea4552011-08-27 10:21:01 +02001887 call_constructors(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001888 return 0;
1889
1890fail:
Dima Zavina7161902010-08-17 15:56:40 -07001891 ERROR("failed to link %s\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001892 si->flags |= FLAG_ERROR;
1893 return -1;
1894}
1895
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001896static void parse_library_path(const char *path, char *delim)
David Bartleybc3a5c22009-06-02 18:27:28 -07001897{
1898 size_t len;
1899 char *ldpaths_bufp = ldpaths_buf;
1900 int i = 0;
1901
1902 len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf));
1903
1904 while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) {
1905 if (*ldpaths[i] != '\0')
1906 ++i;
1907 }
1908
1909 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1910 * last char isn't '\0' (i.e. not originally a delim). */
1911 if (i > 0 && len >= sizeof(ldpaths_buf) &&
1912 ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') {
1913 ldpaths[i - 1] = NULL;
1914 } else {
1915 ldpaths[i] = NULL;
1916 }
1917}
1918
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001919static void parse_preloads(const char *path, char *delim)
Matt Fischer4fd42c12009-12-31 12:09:10 -06001920{
1921 size_t len;
1922 char *ldpreloads_bufp = ldpreloads_buf;
1923 int i = 0;
1924
1925 len = strlcpy(ldpreloads_buf, path, sizeof(ldpreloads_buf));
1926
1927 while (i < LDPRELOAD_MAX && (ldpreload_names[i] = strsep(&ldpreloads_bufp, delim))) {
1928 if (*ldpreload_names[i] != '\0') {
1929 ++i;
1930 }
1931 }
1932
1933 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1934 * last char isn't '\0' (i.e. not originally a delim). */
1935 if (i > 0 && len >= sizeof(ldpreloads_buf) &&
1936 ldpreloads_buf[sizeof(ldpreloads_buf) - 2] != '\0') {
1937 ldpreload_names[i - 1] = NULL;
1938 } else {
1939 ldpreload_names[i] = NULL;
1940 }
1941}
1942
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001943int main(int argc, char **argv)
1944{
1945 return 0;
1946}
1947
1948#define ANDROID_TLS_SLOTS BIONIC_TLS_SLOTS
1949
1950static void * __tls_area[ANDROID_TLS_SLOTS];
1951
1952unsigned __linker_init(unsigned **elfdata)
1953{
1954 static soinfo linker_soinfo;
1955
1956 int argc = (int) *elfdata;
1957 char **argv = (char**) (elfdata + 1);
Stephen Smalleybb440552012-01-20 10:59:15 -08001958 unsigned *vecs = (unsigned*) (argv + argc + 1);
1959 unsigned *v;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001960 soinfo *si;
1961 struct link_map * map;
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001962 const char *ldpath_env = NULL;
1963 const char *ldpreload_env = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001964
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001965 /* Setup a temporary TLS area that is used to get a working
1966 * errno for system calls.
1967 */
1968 __set_tls(__tls_area);
1969
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001970 pid = getpid();
1971
1972#if TIMING
1973 struct timeval t0, t1;
1974 gettimeofday(&t0, 0);
1975#endif
1976
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001977 /* NOTE: we store the elfdata pointer on a special location
1978 * of the temporary TLS area in order to pass it to
1979 * the C Library's runtime initializer.
1980 *
1981 * The initializer must clear the slot and reset the TLS
1982 * to point to a different location to ensure that no other
1983 * shared library constructor can access it.
1984 */
1985 __tls_area[TLS_SLOT_BIONIC_PREINIT] = elfdata;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001986
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001987 /* Initialize environment functions, and get to the ELF aux vectors table */
1988 vecs = linker_env_init(vecs);
1989
Stephen Smalley861b42a2012-01-13 07:48:11 -05001990 /* Check auxv for AT_SECURE first to see if program is setuid, setgid,
1991 has file caps, or caused a SELinux/AppArmor domain transition. */
1992 for (v = vecs; v[0]; v += 2) {
1993 if (v[0] == AT_SECURE) {
1994 /* kernel told us whether to enable secure mode */
1995 program_is_setuid = v[1];
1996 goto sanitize;
1997 }
1998 }
1999
2000 /* Kernel did not provide AT_SECURE - fall back on legacy test. */
2001 program_is_setuid = (getuid() != geteuid()) || (getgid() != getegid());
2002
2003sanitize:
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002004 /* Sanitize environment if we're loading a setuid program */
2005 if (program_is_setuid)
2006 linker_env_secure();
2007
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002008 debugger_init();
2009
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002010 /* Get a few environment variables */
2011 {
2012 const char* env;
2013 env = linker_env_get("DEBUG"); /* XXX: TODO: Change to LD_DEBUG */
2014 if (env)
2015 debug_verbosity = atoi(env);
2016
2017 /* Normally, these are cleaned by linker_env_secure, but the test
2018 * against program_is_setuid doesn't cost us anything */
2019 if (!program_is_setuid) {
2020 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2021 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002022 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002023 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002024
2025 INFO("[ android linker & debugger ]\n");
2026 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
2027
2028 si = alloc_info(argv[0]);
2029 if(si == 0) {
2030 exit(-1);
2031 }
2032
2033 /* bootstrap the link map, the main exe always needs to be first */
2034 si->flags |= FLAG_EXE;
2035 map = &(si->linkmap);
2036
2037 map->l_addr = 0;
2038 map->l_name = argv[0];
2039 map->l_prev = NULL;
2040 map->l_next = NULL;
2041
2042 _r_debug.r_map = map;
2043 r_debug_tail = map;
2044
2045 /* gdb expects the linker to be in the debug shared object list,
2046 * and we need to make sure that the reported load address is zero.
2047 * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
2048 * is. Don't use alloc_info(), because the linker shouldn't
2049 * be on the soinfo list.
2050 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002051 strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002052 linker_soinfo.flags = 0;
2053 linker_soinfo.base = 0; // This is the important part; must be zero.
2054 insert_soinfo_into_debug_map(&linker_soinfo);
2055
2056 /* extract information passed from the kernel */
2057 while(vecs[0] != 0){
2058 switch(vecs[0]){
2059 case AT_PHDR:
2060 si->phdr = (Elf32_Phdr*) vecs[1];
2061 break;
2062 case AT_PHNUM:
2063 si->phnum = (int) vecs[1];
2064 break;
2065 case AT_ENTRY:
2066 si->entry = vecs[1];
2067 break;
2068 }
2069 vecs += 2;
2070 }
2071
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002072 si->base = 0;
2073 si->dynamic = (unsigned *)-1;
2074 si->wrprotect_start = 0xffffffff;
2075 si->wrprotect_end = 0;
David 'Digit' Turner67748092010-07-21 16:18:21 -07002076 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002077
David Bartleybc3a5c22009-06-02 18:27:28 -07002078 /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002079 if (ldpath_env)
David Bartleybc3a5c22009-06-02 18:27:28 -07002080 parse_library_path(ldpath_env, ":");
2081
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002082 if (ldpreload_env) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06002083 parse_preloads(ldpreload_env, " :");
2084 }
2085
Dima Zavin2e855792009-05-20 18:28:09 -07002086 if(link_image(si, 0)) {
2087 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
2088 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2089 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002090 exit(-1);
2091 }
2092
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07002093#if ALLOW_SYMBOLS_FROM_MAIN
2094 /* Set somain after we've loaded all the libraries in order to prevent
2095 * linking of symbols back to the main image, which is not set up at that
2096 * point yet.
2097 */
2098 somain = si;
2099#endif
2100
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002101#if TIMING
2102 gettimeofday(&t1,NULL);
2103 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
2104 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2105 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
2106 ));
2107#endif
2108#if STATS
2109 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
2110 linker_stats.reloc[RELOC_ABSOLUTE],
2111 linker_stats.reloc[RELOC_RELATIVE],
2112 linker_stats.reloc[RELOC_COPY],
2113 linker_stats.reloc[RELOC_SYMBOL]);
2114#endif
2115#if COUNT_PAGES
2116 {
2117 unsigned n;
2118 unsigned i;
2119 unsigned count = 0;
2120 for(n = 0; n < 4096; n++){
2121 if(bitmask[n]){
2122 unsigned x = bitmask[n];
2123 for(i = 0; i < 8; i++){
2124 if(x & 1) count++;
2125 x >>= 1;
2126 }
2127 }
2128 }
2129 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
2130 }
2131#endif
2132
2133#if TIMING || STATS || COUNT_PAGES
2134 fflush(stdout);
2135#endif
2136
2137 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
2138 si->entry);
2139 return si->entry;
2140}