blob: 504fb9359622e576ee484f8c63c76340737b6821 [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}
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +0900368#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_SH_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? */
710
711 return 0;
712}
713
714
715/* get_lib_extents
716 * Retrieves the base (*base) address where the ELF object should be
717 * mapped and its overall memory size (*total_sz).
718 *
719 * Args:
720 * fd: Opened file descriptor for the library
721 * name: The name of the library
722 * _hdr: Pointer to the header page of the library
723 * total_sz: Total size of the memory that should be allocated for
724 * this library
725 *
726 * Returns:
727 * -1 if there was an error while trying to get the lib extents.
728 * The possible reasons are:
729 * - Could not determine if the library was prelinked.
730 * - The library provided is not a valid ELF object
731 * 0 if the library did not request a specific base offset (normal
732 * for non-prelinked libs)
733 * > 0 if the library requests a specific address to be mapped to.
734 * This indicates a pre-linked library.
735 */
736static unsigned
737get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz)
738{
739 unsigned req_base;
740 unsigned min_vaddr = 0xffffffff;
741 unsigned max_vaddr = 0;
742 unsigned char *_hdr = (unsigned char *)__hdr;
743 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)_hdr;
744 Elf32_Phdr *phdr;
745 int cnt;
746
747 TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name);
748 if (verify_elf_object(_hdr, name) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700749 DL_ERR("%5d - %s is not a valid ELF object", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800750 return (unsigned)-1;
751 }
752
753 req_base = (unsigned) is_prelinked(fd, name);
754 if (req_base == (unsigned)-1)
755 return -1;
756 else if (req_base != 0) {
757 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
758 pid, name, req_base);
759 } else {
760 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
761 }
762
763 phdr = (Elf32_Phdr *)(_hdr + ehdr->e_phoff);
764
765 /* find the min/max p_vaddrs from all the PT_LOAD segments so we can
766 * get the range. */
767 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
768 if (phdr->p_type == PT_LOAD) {
769 if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr)
770 max_vaddr = phdr->p_vaddr + phdr->p_memsz;
771 if (phdr->p_vaddr < min_vaddr)
772 min_vaddr = phdr->p_vaddr;
773 }
774 }
775
776 if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700777 DL_ERR("%5d - No loadable segments found in %s.", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800778 return (unsigned)-1;
779 }
780
781 /* truncate min_vaddr down to page boundary */
782 min_vaddr &= ~PAGE_MASK;
783
784 /* round max_vaddr up to the next page */
785 max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK;
786
787 *total_sz = (max_vaddr - min_vaddr);
788 return (unsigned)req_base;
789}
790
791/* alloc_mem_region
792 *
793 * This function reserves a chunk of memory to be used for mapping in
794 * the shared library. We reserve the entire memory region here, and
795 * then the rest of the linker will relocate the individual loadable
796 * segments into the correct locations within this memory range.
797 *
798 * Args:
799 * si->base: The requested base of the allocation. If 0, a sane one will be
800 * chosen in the range LIBBASE <= base < LIBLAST.
801 * si->size: The size of the allocation.
802 *
803 * Returns:
804 * -1 on failure, and 0 on success. On success, si->base will contain
805 * the virtual address at which the library will be mapped.
806 */
807
808static int reserve_mem_region(soinfo *si)
809{
810 void *base = mmap((void *)si->base, si->size, PROT_READ | PROT_EXEC,
Chris Dearmandb4bce02011-03-10 10:48:14 -0800811 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800812 if (base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700813 DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700814 "as requested, will try general pool: %d (%s)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800815 pid, (si->base ? "" : "non-"), si->name, si->base,
816 errno, strerror(errno));
817 return -1;
818 } else if (base != (void *)si->base) {
Dima Zavin2e855792009-05-20 18:28:09 -0700819 DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700820 "not at 0x%08x", pid, (si->base ? "" : "non-"),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800821 si->name, (unsigned)base, si->base);
822 munmap(base, si->size);
823 return -1;
824 }
825 return 0;
826}
827
828static int
829alloc_mem_region(soinfo *si)
830{
831 if (si->base) {
832 /* Attempt to mmap a prelinked library. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800833 return reserve_mem_region(si);
834 }
835
Shih-wei Liao48527c32011-07-17 12:32:43 -0700836 /* This is not a prelinked library, so we use the kernel's default
837 allocator.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800838 */
Shih-wei Liao48527c32011-07-17 12:32:43 -0700839
840 void *base = mmap(NULL, si->size, PROT_READ | PROT_EXEC,
841 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
842 if (base == MAP_FAILED) {
843 DL_ERR("%5d mmap of library '%s' failed: %d (%s)\n",
844 pid, si->name,
845 errno, strerror(errno));
846 goto err;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800847 }
Shih-wei Liao48527c32011-07-17 12:32:43 -0700848 si->base = (unsigned) base;
849 PRINT("%5d mapped library '%s' to %08x via kernel allocator.\n",
850 pid, si->name, si->base);
851 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800852
853err:
Erik Gillingd00d23a2009-07-22 17:06:11 -0700854 DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800855 pid, si->name);
856 return -1;
857}
858
859#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
860#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
861 MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
862 MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
863/* load_segments
864 *
865 * This function loads all the loadable (PT_LOAD) segments into memory
866 * at their appropriate memory offsets off the base address.
867 *
868 * Args:
869 * fd: Open file descriptor to the library to load.
870 * header: Pointer to a header page that contains the ELF header.
871 * This is needed since we haven't mapped in the real file yet.
872 * si: ptr to soinfo struct describing the shared object.
873 *
874 * Returns:
875 * 0 on success, -1 on failure.
876 */
877static int
878load_segments(int fd, void *header, soinfo *si)
879{
880 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
881 Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned char *)header + ehdr->e_phoff);
882 unsigned char *base = (unsigned char *)si->base;
883 int cnt;
884 unsigned len;
885 unsigned char *tmp;
886 unsigned char *pbase;
887 unsigned char *extra_base;
888 unsigned extra_len;
889 unsigned total_sz = 0;
890
891 si->wrprotect_start = 0xffffffff;
892 si->wrprotect_end = 0;
893
894 TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n",
895 pid, si->name, (unsigned)si->base);
896 /* Now go through all the PT_LOAD segments and map them into memory
897 * at the appropriate locations. */
898 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
899 if (phdr->p_type == PT_LOAD) {
900 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
901 /* we want to map in the segment on a page boundary */
902 tmp = base + (phdr->p_vaddr & (~PAGE_MASK));
903 /* add the # of bytes we masked off above to the total length. */
904 len = phdr->p_filesz + (phdr->p_vaddr & PAGE_MASK);
905
906 TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x "
907 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name,
908 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
909 pbase = mmap(tmp, len, PFLAGS_TO_PROT(phdr->p_flags),
910 MAP_PRIVATE | MAP_FIXED, fd,
911 phdr->p_offset & (~PAGE_MASK));
912 if (pbase == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700913 DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700914 "p_vaddr=0x%08x p_offset=0x%08x", pid, si->name,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800915 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
916 goto fail;
917 }
918
919 /* If 'len' didn't end on page boundary, and it's a writable
920 * segment, zero-fill the rest. */
921 if ((len & PAGE_MASK) && (phdr->p_flags & PF_W))
922 memset((void *)(pbase + len), 0, PAGE_SIZE - (len & PAGE_MASK));
923
924 /* Check to see if we need to extend the map for this segment to
925 * cover the diff between filesz and memsz (i.e. for bss).
926 *
927 * base _+---------------------+ page boundary
928 * . .
929 * | |
930 * . .
931 * pbase _+---------------------+ page boundary
932 * | |
933 * . .
934 * base + p_vaddr _| |
935 * . \ \ .
936 * . | filesz | .
937 * pbase + len _| / | |
938 * <0 pad> . . .
939 * extra_base _+------------|--------+ page boundary
940 * / . . .
941 * | . . .
942 * | +------------|--------+ page boundary
943 * extra_len-> | | | |
944 * | . | memsz .
945 * | . | .
946 * \ _| / |
947 * . .
948 * | |
949 * _+---------------------+ page boundary
950 */
951 tmp = (unsigned char *)(((unsigned)pbase + len + PAGE_SIZE - 1) &
952 (~PAGE_MASK));
953 if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) {
954 extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp;
955 TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x "
956 "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len);
957 /* map in the extra page(s) as anonymous into the range.
958 * This is probably not necessary as we already mapped in
959 * the entire region previously, but we just want to be
960 * sure. This will also set the right flags on the region
961 * (though we can probably accomplish the same thing with
962 * mprotect).
963 */
964 extra_base = mmap((void *)tmp, extra_len,
965 PFLAGS_TO_PROT(phdr->p_flags),
966 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
967 -1, 0);
968 if (extra_base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700969 DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x"
Erik Gillingd00d23a2009-07-22 17:06:11 -0700970 " (0x%08x) ]", pid, si->name, (unsigned)tmp,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800971 extra_len);
972 goto fail;
973 }
974 /* TODO: Check if we need to memset-0 this region.
975 * Anonymous mappings are zero-filled copy-on-writes, so we
976 * shouldn't need to. */
977 TRACE("[ %5d - Segment from '%s' extended @ 0x%08x "
978 "(0x%08x)\n", pid, si->name, (unsigned)extra_base,
979 extra_len);
980 }
981 /* set the len here to show the full extent of the segment we
982 * just loaded, mostly for debugging */
983 len = (((unsigned)base + phdr->p_vaddr + phdr->p_memsz +
984 PAGE_SIZE - 1) & (~PAGE_MASK)) - (unsigned)pbase;
985 TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x "
986 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
987 (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset);
988 total_sz += len;
989 /* Make the section writable just in case we'll have to write to
990 * it during relocation (i.e. text segment). However, we will
991 * remember what range of addresses should be write protected.
992 *
993 */
994 if (!(phdr->p_flags & PF_W)) {
995 if ((unsigned)pbase < si->wrprotect_start)
996 si->wrprotect_start = (unsigned)pbase;
997 if (((unsigned)pbase + len) > si->wrprotect_end)
998 si->wrprotect_end = (unsigned)pbase + len;
999 mprotect(pbase, len,
1000 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
1001 }
1002 } else if (phdr->p_type == PT_DYNAMIC) {
1003 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
1004 /* this segment contains the dynamic linking information */
1005 si->dynamic = (unsigned *)(base + phdr->p_vaddr);
1006 } else {
1007#ifdef ANDROID_ARM_LINKER
1008 if (phdr->p_type == PT_ARM_EXIDX) {
1009 DEBUG_DUMP_PHDR(phdr, "PT_ARM_EXIDX", pid);
1010 /* exidx entries (used for stack unwinding) are 8 bytes each.
1011 */
1012 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1013 si->ARM_exidx_count = phdr->p_memsz / 8;
1014 }
1015#endif
1016 }
1017
1018 }
1019
1020 /* Sanity check */
1021 if (total_sz > si->size) {
Dima Zavin2e855792009-05-20 18:28:09 -07001022 DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001023 "greater than what was allocated (0x%08x). THIS IS BAD!",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001024 pid, total_sz, si->name, si->size);
1025 goto fail;
1026 }
1027
1028 TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. "
1029 "Total memory footprint: 0x%08x bytes ]\n", pid, si->name,
1030 (unsigned)si->base, si->size);
1031 return 0;
1032
1033fail:
1034 /* We can just blindly unmap the entire region even though some things
1035 * were mapped in originally with anonymous and others could have been
1036 * been mapped in from the file before we failed. The kernel will unmap
1037 * all the pages in the range, irrespective of how they got there.
1038 */
1039 munmap((void *)si->base, si->size);
1040 si->flags |= FLAG_ERROR;
1041 return -1;
1042}
1043
1044/* TODO: Implement this to take care of the fact that Android ARM
1045 * ELF objects shove everything into a single loadable segment that has the
1046 * write bit set. wr_offset is then used to set non-(data|bss) pages to be
1047 * non-writable.
1048 */
1049#if 0
1050static unsigned
1051get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr)
1052{
1053 Elf32_Shdr *shdr_start;
1054 Elf32_Shdr *shdr;
1055 int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr);
1056 int cnt;
1057 unsigned wr_offset = 0xffffffff;
1058
1059 shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd,
1060 ehdr->e_shoff & (~PAGE_MASK));
1061 if (shdr_start == MAP_FAILED) {
1062 WARN("%5d - Could not read section header info from '%s'. Will not "
1063 "not be able to determine write-protect offset.\n", pid, name);
1064 return (unsigned)-1;
1065 }
1066
1067 for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) {
1068 if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) &&
1069 (shdr->sh_addr < wr_offset)) {
1070 wr_offset = shdr->sh_addr;
1071 }
1072 }
1073
1074 munmap(shdr_start, shdr_sz);
1075 return wr_offset;
1076}
1077#endif
1078
1079static soinfo *
1080load_library(const char *name)
1081{
1082 int fd = open_library(name);
1083 int cnt;
1084 unsigned ext_sz;
1085 unsigned req_base;
Erik Gillingfde86422009-07-28 20:28:19 -07001086 const char *bname;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001087 soinfo *si = NULL;
1088 Elf32_Ehdr *hdr;
1089
Dima Zavin2e855792009-05-20 18:28:09 -07001090 if(fd == -1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001091 DL_ERR("Library '%s' not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001092 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -07001093 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001094
1095 /* We have to read the ELF header to figure out what to do with this image
1096 */
1097 if (lseek(fd, 0, SEEK_SET) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001098 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001099 goto fail;
1100 }
1101
1102 if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001103 DL_ERR("read() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001104 goto fail;
1105 }
1106
1107 /* Parse the ELF header and get the size of the memory footprint for
1108 * the library */
1109 req_base = get_lib_extents(fd, name, &__header[0], &ext_sz);
1110 if (req_base == (unsigned)-1)
1111 goto fail;
1112 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
1113 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
1114
1115 /* Now configure the soinfo struct where we'll store all of our data
1116 * for the ELF object. If the loading fails, we waste the entry, but
1117 * same thing would happen if we failed during linking. Configuring the
1118 * soinfo struct here is a lot more convenient.
1119 */
Erik Gillingfde86422009-07-28 20:28:19 -07001120 bname = strrchr(name, '/');
1121 si = alloc_info(bname ? bname + 1 : name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001122 if (si == NULL)
1123 goto fail;
1124
1125 /* Carve out a chunk of memory where we will map in the individual
1126 * segments */
1127 si->base = req_base;
1128 si->size = ext_sz;
1129 si->flags = 0;
1130 si->entry = 0;
1131 si->dynamic = (unsigned *)-1;
1132 if (alloc_mem_region(si) < 0)
1133 goto fail;
1134
1135 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
1136 pid, name, (void *)si->base, (unsigned) ext_sz);
1137
1138 /* Now actually load the library's segments into right places in memory */
1139 if (load_segments(fd, &__header[0], si) < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001140 goto fail;
1141 }
1142
1143 /* this might not be right. Technically, we don't even need this info
1144 * once we go through 'load_segments'. */
1145 hdr = (Elf32_Ehdr *)si->base;
1146 si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff);
1147 si->phnum = hdr->e_phnum;
1148 /**/
1149
1150 close(fd);
1151 return si;
1152
1153fail:
1154 if (si) free_info(si);
1155 close(fd);
1156 return NULL;
1157}
1158
1159static soinfo *
1160init_library(soinfo *si)
1161{
1162 unsigned wr_offset = 0xffffffff;
1163
1164 /* At this point we know that whatever is loaded @ base is a valid ELF
1165 * shared library whose segments are properly mapped in. */
1166 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
1167 pid, si->base, si->size, si->name);
1168
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001169 if(link_image(si, wr_offset)) {
1170 /* We failed to link. However, we can only restore libbase
1171 ** if no additional libraries have moved it since we updated it.
1172 */
1173 munmap((void *)si->base, si->size);
1174 return NULL;
1175 }
1176
1177 return si;
1178}
1179
1180soinfo *find_library(const char *name)
1181{
1182 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001183 const char *bname;
1184
1185#if ALLOW_SYMBOLS_FROM_MAIN
1186 if (name == NULL)
1187 return somain;
1188#else
1189 if (name == NULL)
1190 return NULL;
1191#endif
1192
1193 bname = strrchr(name, '/');
Erik Gillingfde86422009-07-28 20:28:19 -07001194 bname = bname ? bname + 1 : name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001195
1196 for(si = solist; si != 0; si = si->next){
Erik Gillingfde86422009-07-28 20:28:19 -07001197 if(!strcmp(bname, si->name)) {
Erik Gilling30eb4022009-08-13 16:05:30 -07001198 if(si->flags & FLAG_ERROR) {
1199 DL_ERR("%5d '%s' failed to load previously", pid, bname);
1200 return NULL;
1201 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001202 if(si->flags & FLAG_LINKED) return si;
Erik Gillingd00d23a2009-07-22 17:06:11 -07001203 DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name);
Dima Zavin2e855792009-05-20 18:28:09 -07001204 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001205 }
1206 }
1207
1208 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
1209 si = load_library(name);
1210 if(si == NULL)
1211 return NULL;
1212 return init_library(si);
1213}
1214
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001215/* TODO:
1216 * notify gdb of unload
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001217 * for non-prelinked libraries, find a way to decrement libbase
1218 */
1219static void call_destructors(soinfo *si);
1220unsigned unload_library(soinfo *si)
1221{
1222 unsigned *d;
1223 if (si->refcount == 1) {
1224 TRACE("%5d unloading '%s'\n", pid, si->name);
1225 call_destructors(si);
1226
1227 for(d = si->dynamic; *d; d += 2) {
1228 if(d[0] == DT_NEEDED){
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001229 soinfo *lsi = (soinfo *)d[1];
1230 d[1] = 0;
1231 if (validate_soinfo(lsi)) {
1232 TRACE("%5d %s needs to unload %s\n", pid,
1233 si->name, lsi->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001234 unload_library(lsi);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001235 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001236 else
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001237 DL_ERR("%5d %s: could not unload dependent library",
1238 pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001239 }
1240 }
1241
1242 munmap((char *)si->base, si->size);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001243 notify_gdb_of_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001244 free_info(si);
1245 si->refcount = 0;
1246 }
1247 else {
1248 si->refcount--;
1249 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
1250 pid, si->name, si->refcount);
1251 }
1252 return si->refcount;
1253}
1254
1255/* TODO: don't use unsigned for addrs below. It works, but is not
1256 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1257 * long.
1258 */
1259static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count)
1260{
1261 Elf32_Sym *symtab = si->symtab;
1262 const char *strtab = si->strtab;
1263 Elf32_Sym *s;
1264 unsigned base;
1265 Elf32_Rel *start = rel;
1266 unsigned idx;
1267
1268 for (idx = 0; idx < count; ++idx) {
1269 unsigned type = ELF32_R_TYPE(rel->r_info);
1270 unsigned sym = ELF32_R_SYM(rel->r_info);
1271 unsigned reloc = (unsigned)(rel->r_offset + si->base);
1272 unsigned sym_addr = 0;
1273 char *sym_name = NULL;
1274
1275 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1276 si->name, idx);
1277 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001278 sym_name = (char *)(strtab + symtab[sym].st_name);
1279 s = _do_lookup(si, sym_name, &base);
Doug Kwane8238072009-10-26 12:05:23 -07001280 if(s == NULL) {
1281 /* We only allow an undefined symbol if this is a weak
1282 reference.. */
1283 s = &symtab[sym];
1284 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1285 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
1286 return -1;
1287 }
1288
1289 /* IHI0044C AAELF 4.5.1.1:
1290
1291 Libraries are not searched to resolve weak references.
1292 It is not an error for a weak reference to remain
1293 unsatisfied.
1294
1295 During linking, the value of an undefined weak reference is:
1296 - Zero if the relocation type is absolute
1297 - The address of the place if the relocation is pc-relative
1298 - The address of nominial base address if the relocation
1299 type is base-relative.
1300 */
1301
1302 switch (type) {
1303#if defined(ANDROID_ARM_LINKER)
1304 case R_ARM_JUMP_SLOT:
1305 case R_ARM_GLOB_DAT:
1306 case R_ARM_ABS32:
1307 case R_ARM_RELATIVE: /* Don't care. */
1308 case R_ARM_NONE: /* Don't care. */
1309#elif defined(ANDROID_X86_LINKER)
1310 case R_386_JUMP_SLOT:
1311 case R_386_GLOB_DAT:
1312 case R_386_32:
1313 case R_386_RELATIVE: /* Dont' care. */
1314#endif /* ANDROID_*_LINKER */
1315 /* sym_addr was initialized to be zero above or relocation
1316 code below does not care about value of sym_addr.
1317 No need to do anything. */
1318 break;
1319
1320#if defined(ANDROID_X86_LINKER)
1321 case R_386_PC32:
1322 sym_addr = reloc;
1323 break;
1324#endif /* ANDROID_X86_LINKER */
1325
1326#if defined(ANDROID_ARM_LINKER)
1327 case R_ARM_COPY:
1328 /* Fall through. Can't really copy if weak symbol is
1329 not found in run-time. */
1330#endif /* ANDROID_ARM_LINKER */
1331 default:
1332 DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n",
1333 pid, type, rel, (int) (rel - start));
1334 return -1;
1335 }
1336 } else {
1337 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001338#if 0
1339 if((base == 0) && (si->base != 0)){
1340 /* linking from libraries to main image is bad */
Erik Gillingd00d23a2009-07-22 17:06:11 -07001341 DL_ERR("%5d cannot locate '%s'...",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001342 pid, strtab + symtab[sym].st_name);
1343 return -1;
1344 }
1345#endif
Doug Kwane8238072009-10-26 12:05:23 -07001346 sym_addr = (unsigned)(s->st_value + base);
1347 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001348 COUNT_RELOC(RELOC_SYMBOL);
1349 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001350 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001351 }
1352
1353/* TODO: This is ugly. Split up the relocations by arch into
1354 * different files.
1355 */
1356 switch(type){
1357#if defined(ANDROID_ARM_LINKER)
1358 case R_ARM_JUMP_SLOT:
1359 COUNT_RELOC(RELOC_ABSOLUTE);
1360 MARK(rel->r_offset);
1361 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1362 reloc, sym_addr, sym_name);
1363 *((unsigned*)reloc) = sym_addr;
1364 break;
1365 case R_ARM_GLOB_DAT:
1366 COUNT_RELOC(RELOC_ABSOLUTE);
1367 MARK(rel->r_offset);
1368 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1369 reloc, sym_addr, sym_name);
1370 *((unsigned*)reloc) = sym_addr;
1371 break;
1372 case R_ARM_ABS32:
1373 COUNT_RELOC(RELOC_ABSOLUTE);
1374 MARK(rel->r_offset);
1375 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1376 reloc, sym_addr, sym_name);
1377 *((unsigned*)reloc) += sym_addr;
1378 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001379 case R_ARM_REL32:
1380 COUNT_RELOC(RELOC_RELATIVE);
1381 MARK(rel->r_offset);
1382 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1383 reloc, sym_addr, rel->r_offset, sym_name);
1384 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1385 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001386#elif defined(ANDROID_X86_LINKER)
1387 case R_386_JUMP_SLOT:
1388 COUNT_RELOC(RELOC_ABSOLUTE);
1389 MARK(rel->r_offset);
1390 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1391 reloc, sym_addr, sym_name);
1392 *((unsigned*)reloc) = sym_addr;
1393 break;
1394 case R_386_GLOB_DAT:
1395 COUNT_RELOC(RELOC_ABSOLUTE);
1396 MARK(rel->r_offset);
1397 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1398 reloc, sym_addr, sym_name);
1399 *((unsigned*)reloc) = sym_addr;
1400 break;
1401#endif /* ANDROID_*_LINKER */
1402
1403#if defined(ANDROID_ARM_LINKER)
1404 case R_ARM_RELATIVE:
1405#elif defined(ANDROID_X86_LINKER)
1406 case R_386_RELATIVE:
1407#endif /* ANDROID_*_LINKER */
1408 COUNT_RELOC(RELOC_RELATIVE);
1409 MARK(rel->r_offset);
1410 if(sym){
Erik Gillingd00d23a2009-07-22 17:06:11 -07001411 DL_ERR("%5d odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001412 return -1;
1413 }
1414 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1415 reloc, si->base);
1416 *((unsigned*)reloc) += si->base;
1417 break;
1418
1419#if defined(ANDROID_X86_LINKER)
1420 case R_386_32:
1421 COUNT_RELOC(RELOC_RELATIVE);
1422 MARK(rel->r_offset);
1423
1424 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1425 reloc, sym_addr, sym_name);
1426 *((unsigned *)reloc) += (unsigned)sym_addr;
1427 break;
1428
1429 case R_386_PC32:
1430 COUNT_RELOC(RELOC_RELATIVE);
1431 MARK(rel->r_offset);
1432 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1433 "+%08x (%08x - %08x) %s\n", pid, reloc,
1434 (sym_addr - reloc), sym_addr, reloc, sym_name);
1435 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1436 break;
1437#endif /* ANDROID_X86_LINKER */
1438
1439#ifdef ANDROID_ARM_LINKER
1440 case R_ARM_COPY:
1441 COUNT_RELOC(RELOC_COPY);
1442 MARK(rel->r_offset);
1443 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1444 reloc, s->st_size, sym_addr, sym_name);
1445 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1446 break;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001447 case R_ARM_NONE:
1448 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001449#endif /* ANDROID_ARM_LINKER */
1450
1451 default:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001452 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001453 pid, type, rel, (int) (rel - start));
1454 return -1;
1455 }
1456 rel++;
1457 }
1458 return 0;
1459}
1460
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001461#if defined(ANDROID_SH_LINKER)
1462static int reloc_library_a(soinfo *si, Elf32_Rela *rela, unsigned count)
1463{
1464 Elf32_Sym *symtab = si->symtab;
1465 const char *strtab = si->strtab;
1466 Elf32_Sym *s;
1467 unsigned base;
1468 Elf32_Rela *start = rela;
1469 unsigned idx;
1470
1471 for (idx = 0; idx < count; ++idx) {
1472 unsigned type = ELF32_R_TYPE(rela->r_info);
1473 unsigned sym = ELF32_R_SYM(rela->r_info);
1474 unsigned reloc = (unsigned)(rela->r_offset + si->base);
1475 unsigned sym_addr = 0;
1476 char *sym_name = NULL;
1477
1478 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1479 si->name, idx);
1480 if(sym != 0) {
1481 sym_name = (char *)(strtab + symtab[sym].st_name);
1482 s = _do_lookup(si, sym_name, &base);
1483 if(s == 0) {
1484 DL_ERR("%5d cannot locate '%s'...", pid, sym_name);
1485 return -1;
1486 }
1487#if 0
1488 if((base == 0) && (si->base != 0)){
1489 /* linking from libraries to main image is bad */
1490 DL_ERR("%5d cannot locate '%s'...",
1491 pid, strtab + symtab[sym].st_name);
1492 return -1;
1493 }
1494#endif
1495 if ((s->st_shndx == SHN_UNDEF) && (s->st_value != 0)) {
1496 DL_ERR("%5d In '%s', shndx=%d && value=0x%08x. We do not "
1497 "handle this yet", pid, si->name, s->st_shndx,
1498 s->st_value);
1499 return -1;
1500 }
1501 sym_addr = (unsigned)(s->st_value + base);
1502 COUNT_RELOC(RELOC_SYMBOL);
1503 } else {
1504 s = 0;
1505 }
1506
1507/* TODO: This is ugly. Split up the relocations by arch into
1508 * different files.
1509 */
1510 switch(type){
1511 case R_SH_JUMP_SLOT:
1512 COUNT_RELOC(RELOC_ABSOLUTE);
1513 MARK(rela->r_offset);
1514 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1515 reloc, sym_addr, sym_name);
1516 *((unsigned*)reloc) = sym_addr;
1517 break;
1518 case R_SH_GLOB_DAT:
1519 COUNT_RELOC(RELOC_ABSOLUTE);
1520 MARK(rela->r_offset);
1521 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1522 reloc, sym_addr, sym_name);
1523 *((unsigned*)reloc) = sym_addr;
1524 break;
1525 case R_SH_DIR32:
1526 COUNT_RELOC(RELOC_ABSOLUTE);
1527 MARK(rela->r_offset);
1528 TRACE_TYPE(RELO, "%5d RELO DIR32 %08x <- %08x %s\n", pid,
1529 reloc, sym_addr, sym_name);
1530 *((unsigned*)reloc) += sym_addr;
1531 break;
1532 case R_SH_RELATIVE:
1533 COUNT_RELOC(RELOC_RELATIVE);
1534 MARK(rela->r_offset);
1535 if(sym){
1536 DL_ERR("%5d odd RELATIVE form...", pid);
1537 return -1;
1538 }
1539 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1540 reloc, si->base);
1541 *((unsigned*)reloc) += si->base;
1542 break;
1543
1544 default:
1545 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
1546 pid, type, rela, (int) (rela - start));
1547 return -1;
1548 }
1549 rela++;
1550 }
1551 return 0;
1552}
1553#endif /* ANDROID_SH_LINKER */
1554
David 'Digit' Turner82156792009-05-18 14:37:41 +02001555
1556/* Please read the "Initialization and Termination functions" functions.
1557 * of the linker design note in bionic/linker/README.TXT to understand
1558 * what the following code is doing.
1559 *
1560 * The important things to remember are:
1561 *
1562 * DT_PREINIT_ARRAY must be called first for executables, and should
1563 * not appear in shared libraries.
1564 *
1565 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1566 *
1567 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1568 *
1569 * DT_FINI_ARRAY must be parsed in reverse order.
1570 */
1571
1572static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001573{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001574 int n, inc = 1;
1575
1576 if (reverse) {
1577 ctor += (count-1);
1578 inc = -1;
1579 }
1580
1581 for(n = count; n > 0; n--) {
1582 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1583 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001584 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001585 void (*func)() = (void (*)()) *ctor;
1586 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001587 if(((int) func == 0) || ((int) func == -1)) continue;
1588 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1589 func();
1590 }
1591}
1592
1593static void call_constructors(soinfo *si)
1594{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001595 if (si->flags & FLAG_EXE) {
1596 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1597 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1598 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001599 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001600 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1601 } else {
1602 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001603 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
Erik Gillingd00d23a2009-07-22 17:06:11 -07001604 " This is INVALID.", pid, si->name,
Dima Zavin2e855792009-05-20 18:28:09 -07001605 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001606 }
1607 }
1608
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001609 if (si->init_func) {
1610 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1611 (unsigned)si->init_func, si->name);
1612 si->init_func();
1613 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1614 }
1615
1616 if (si->init_array) {
1617 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1618 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001619 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001620 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1621 }
1622}
1623
David 'Digit' Turner82156792009-05-18 14:37:41 +02001624
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001625static void call_destructors(soinfo *si)
1626{
1627 if (si->fini_array) {
1628 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1629 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001630 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001631 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1632 }
1633
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001634 if (si->fini_func) {
1635 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1636 (unsigned)si->fini_func, si->name);
1637 si->fini_func();
1638 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1639 }
1640}
1641
1642/* Force any of the closed stdin, stdout and stderr to be associated with
1643 /dev/null. */
1644static int nullify_closed_stdio (void)
1645{
1646 int dev_null, i, status;
1647 int return_value = 0;
1648
1649 dev_null = open("/dev/null", O_RDWR);
1650 if (dev_null < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001651 DL_ERR("Cannot open /dev/null.");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652 return -1;
1653 }
1654 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1655
1656 /* If any of the stdio file descriptors is valid and not associated
1657 with /dev/null, dup /dev/null to it. */
1658 for (i = 0; i < 3; i++) {
1659 /* If it is /dev/null already, we are done. */
1660 if (i == dev_null)
1661 continue;
1662
1663 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1664 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1665 can be interrupted but we do this just to be safe. */
1666 do {
1667 status = fcntl(i, F_GETFL);
1668 } while (status < 0 && errno == EINTR);
1669
1670 /* If file is openned, we are good. */
1671 if (status >= 0)
1672 continue;
1673
1674 /* The only error we allow is that the file descriptor does not
1675 exist, in which case we dup /dev/null to it. */
1676 if (errno != EBADF) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001677 DL_ERR("nullify_stdio: unhandled error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001678 return_value = -1;
1679 continue;
1680 }
1681
1682 /* Try dupping /dev/null to this stdio file descriptor and
1683 repeat if there is a signal. Note that any errors in closing
1684 the stdio descriptor are lost. */
1685 do {
1686 status = dup2(dev_null, i);
1687 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001688
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001689 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001690 DL_ERR("nullify_stdio: dup2 error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001691 return_value = -1;
1692 continue;
1693 }
1694 }
1695
1696 /* If /dev/null is not one of the stdio file descriptors, close it. */
1697 if (dev_null > 2) {
1698 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001699 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001700 status = close(dev_null);
1701 } while (status < 0 && errno == EINTR);
1702
1703 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001704 DL_ERR("nullify_stdio: close error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001705 return_value = -1;
1706 }
1707 }
1708
1709 return return_value;
1710}
1711
1712static int link_image(soinfo *si, unsigned wr_offset)
1713{
1714 unsigned *d;
1715 Elf32_Phdr *phdr = si->phdr;
1716 int phnum = si->phnum;
1717
1718 INFO("[ %5d linking %s ]\n", pid, si->name);
1719 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1720 si->base, si->flags);
1721
1722 if (si->flags & FLAG_EXE) {
1723 /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for
1724 * linkage info if this is the executable. If this was a
1725 * dynamic lib, that would have been done at load time.
1726 *
1727 * TODO: It's unfortunate that small pieces of this are
1728 * repeated from the load_library routine. Refactor this just
1729 * slightly to reuse these bits.
1730 */
1731 si->size = 0;
1732 for(; phnum > 0; --phnum, ++phdr) {
1733#ifdef ANDROID_ARM_LINKER
1734 if(phdr->p_type == PT_ARM_EXIDX) {
1735 /* exidx entries (used for stack unwinding) are 8 bytes each.
1736 */
1737 si->ARM_exidx = (unsigned *)phdr->p_vaddr;
1738 si->ARM_exidx_count = phdr->p_memsz / 8;
1739 }
1740#endif
1741 if (phdr->p_type == PT_LOAD) {
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001742 /* For the executable, we use the si->size field only in
1743 dl_unwind_find_exidx(), so the meaning of si->size
Nick Kralevichd9ad6232011-10-20 14:57:56 -07001744 is not the size of the executable; it is the distance
1745 between the load location of the executable and the last
1746 address of the loadable part of the executable.
1747 We use the range [si->base, si->base + si->size) to
1748 determine whether a PC value falls within the executable
1749 section. Of course, if a value is between si->base and
1750 (si->base + phdr->p_vaddr), it's not in the executable
1751 section, but a) we shouldn't be asking for such a value
1752 anyway, and b) if we have to provide an EXIDX for such a
1753 value, then the executable's EXIDX is probably the better
1754 choice.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001755 */
1756 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
1757 if (phdr->p_vaddr + phdr->p_memsz > si->size)
1758 si->size = phdr->p_vaddr + phdr->p_memsz;
1759 /* try to remember what range of addresses should be write
1760 * protected */
1761 if (!(phdr->p_flags & PF_W)) {
1762 unsigned _end;
1763
Nick Kralevichd9ad6232011-10-20 14:57:56 -07001764 if (si->base + phdr->p_vaddr < si->wrprotect_start)
1765 si->wrprotect_start = si->base + phdr->p_vaddr;
1766 _end = (((si->base + phdr->p_vaddr + phdr->p_memsz + PAGE_SIZE - 1) &
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001767 (~PAGE_MASK)));
1768 if (_end > si->wrprotect_end)
1769 si->wrprotect_end = _end;
Nick Kralevichd9ad6232011-10-20 14:57:56 -07001770 /* Make the section writable just in case we'll have to
1771 * write to it during relocation (i.e. text segment).
1772 * However, we will remember what range of addresses
1773 * should be write protected.
1774 */
1775 mprotect((void *) (si->base + phdr->p_vaddr),
1776 phdr->p_memsz,
1777 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001778 }
1779 } else if (phdr->p_type == PT_DYNAMIC) {
1780 if (si->dynamic != (unsigned *)-1) {
Dima Zavin2e855792009-05-20 18:28:09 -07001781 DL_ERR("%5d multiple PT_DYNAMIC segments found in '%s'. "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001782 "Segment at 0x%08x, previously one found at 0x%08x",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001783 pid, si->name, si->base + phdr->p_vaddr,
1784 (unsigned)si->dynamic);
1785 goto fail;
1786 }
1787 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
1788 si->dynamic = (unsigned *) (si->base + phdr->p_vaddr);
1789 }
1790 }
1791 }
1792
1793 if (si->dynamic == (unsigned *)-1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001794 DL_ERR("%5d missing PT_DYNAMIC?!", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001795 goto fail;
1796 }
1797
1798 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1799
1800 /* extract useful information from dynamic section */
1801 for(d = si->dynamic; *d; d++){
1802 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1803 switch(*d++){
1804 case DT_HASH:
1805 si->nbucket = ((unsigned *) (si->base + *d))[0];
1806 si->nchain = ((unsigned *) (si->base + *d))[1];
1807 si->bucket = (unsigned *) (si->base + *d + 8);
1808 si->chain = (unsigned *) (si->base + *d + 8 + si->nbucket * 4);
1809 break;
1810 case DT_STRTAB:
1811 si->strtab = (const char *) (si->base + *d);
1812 break;
1813 case DT_SYMTAB:
1814 si->symtab = (Elf32_Sym *) (si->base + *d);
1815 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001816#if !defined(ANDROID_SH_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001817 case DT_PLTREL:
1818 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001819 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001820 goto fail;
1821 }
1822 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001823#endif
1824#ifdef ANDROID_SH_LINKER
1825 case DT_JMPREL:
1826 si->plt_rela = (Elf32_Rela*) (si->base + *d);
1827 break;
1828 case DT_PLTRELSZ:
1829 si->plt_rela_count = *d / sizeof(Elf32_Rela);
1830 break;
1831#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001832 case DT_JMPREL:
1833 si->plt_rel = (Elf32_Rel*) (si->base + *d);
1834 break;
1835 case DT_PLTRELSZ:
1836 si->plt_rel_count = *d / 8;
1837 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001838#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001839 case DT_REL:
1840 si->rel = (Elf32_Rel*) (si->base + *d);
1841 break;
1842 case DT_RELSZ:
1843 si->rel_count = *d / 8;
1844 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001845#ifdef ANDROID_SH_LINKER
1846 case DT_RELASZ:
1847 si->rela_count = *d / sizeof(Elf32_Rela);
1848 break;
1849#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001850 case DT_PLTGOT:
1851 /* Save this in case we decide to do lazy binding. We don't yet. */
1852 si->plt_got = (unsigned *)(si->base + *d);
1853 break;
1854 case DT_DEBUG:
1855 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1856 *d = (int) &_r_debug;
1857 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001858#ifdef ANDROID_SH_LINKER
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001859 case DT_RELA:
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001860 si->rela = (Elf32_Rela *) (si->base + *d);
1861 break;
1862#else
1863 case DT_RELA:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001864 DL_ERR("%5d DT_RELA not supported", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001865 goto fail;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001866#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001867 case DT_INIT:
1868 si->init_func = (void (*)(void))(si->base + *d);
1869 DEBUG("%5d %s constructors (init func) found at %p\n",
1870 pid, si->name, si->init_func);
1871 break;
1872 case DT_FINI:
1873 si->fini_func = (void (*)(void))(si->base + *d);
1874 DEBUG("%5d %s destructors (fini func) found at %p\n",
1875 pid, si->name, si->fini_func);
1876 break;
1877 case DT_INIT_ARRAY:
1878 si->init_array = (unsigned *)(si->base + *d);
1879 DEBUG("%5d %s constructors (init_array) found at %p\n",
1880 pid, si->name, si->init_array);
1881 break;
1882 case DT_INIT_ARRAYSZ:
1883 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1884 break;
1885 case DT_FINI_ARRAY:
1886 si->fini_array = (unsigned *)(si->base + *d);
1887 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1888 pid, si->name, si->fini_array);
1889 break;
1890 case DT_FINI_ARRAYSZ:
1891 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1892 break;
1893 case DT_PREINIT_ARRAY:
1894 si->preinit_array = (unsigned *)(si->base + *d);
1895 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1896 pid, si->name, si->preinit_array);
1897 break;
1898 case DT_PREINIT_ARRAYSZ:
1899 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1900 break;
1901 case DT_TEXTREL:
1902 /* TODO: make use of this. */
1903 /* this means that we might have to write into where the text
1904 * segment was loaded during relocation... Do something with
1905 * it.
1906 */
1907 DEBUG("%5d Text segment should be writable during relocation.\n",
1908 pid);
1909 break;
1910 }
1911 }
1912
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001913 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001914 pid, si->base, si->strtab, si->symtab);
1915
1916 if((si->strtab == 0) || (si->symtab == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001917 DL_ERR("%5d missing essential tables", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001918 goto fail;
1919 }
1920
Matt Fischer4fd42c12009-12-31 12:09:10 -06001921 /* if this is the main executable, then load all of the preloads now */
1922 if(si->flags & FLAG_EXE) {
1923 int i;
1924 memset(preloads, 0, sizeof(preloads));
1925 for(i = 0; ldpreload_names[i] != NULL; i++) {
1926 soinfo *lsi = find_library(ldpreload_names[i]);
1927 if(lsi == 0) {
1928 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
1929 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
1930 pid, ldpreload_names[i], si->name, tmp_err_buf);
1931 goto fail;
1932 }
1933 lsi->refcount++;
1934 preloads[i] = lsi;
1935 }
1936 }
1937
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001938 for(d = si->dynamic; *d; d += 2) {
1939 if(d[0] == DT_NEEDED){
1940 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001941 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001942 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001943 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Erik Gillingd00d23a2009-07-22 17:06:11 -07001944 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
Dima Zavin03531952009-05-29 17:30:25 -07001945 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001946 goto fail;
1947 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001948 /* Save the soinfo of the loaded DT_NEEDED library in the payload
1949 of the DT_NEEDED entry itself, so that we can retrieve the
1950 soinfo directly later from the dynamic segment. This is a hack,
1951 but it allows us to map from DT_NEEDED to soinfo efficiently
1952 later on when we resolve relocations, trying to look up a symgol
1953 with dlsym().
1954 */
1955 d[1] = (unsigned)lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001956 lsi->refcount++;
1957 }
1958 }
1959
1960 if(si->plt_rel) {
1961 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1962 if(reloc_library(si, si->plt_rel, si->plt_rel_count))
1963 goto fail;
1964 }
1965 if(si->rel) {
1966 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1967 if(reloc_library(si, si->rel, si->rel_count))
1968 goto fail;
1969 }
1970
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001971#ifdef ANDROID_SH_LINKER
1972 if(si->plt_rela) {
1973 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
1974 if(reloc_library_a(si, si->plt_rela, si->plt_rela_count))
1975 goto fail;
1976 }
1977 if(si->rela) {
1978 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
1979 if(reloc_library_a(si, si->rela, si->rela_count))
1980 goto fail;
1981 }
1982#endif /* ANDROID_SH_LINKER */
1983
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001984 si->flags |= FLAG_LINKED;
1985 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1986
1987#if 0
1988 /* This is the way that the old dynamic linker did protection of
1989 * non-writable areas. It would scan section headers and find where
1990 * .text ended (rather where .data/.bss began) and assume that this is
1991 * the upper range of the non-writable area. This is too coarse,
1992 * and is kept here for reference until we fully move away from single
1993 * segment elf objects. See the code in get_wr_offset (also #if'd 0)
1994 * that made this possible.
1995 */
1996 if(wr_offset < 0xffffffff){
1997 mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC);
1998 }
1999#else
2000 /* TODO: Verify that this does the right thing in all cases, as it
2001 * presently probably does not. It is possible that an ELF image will
2002 * come with multiple read-only segments. What we ought to do is scan
2003 * the program headers again and mprotect all the read-only segments.
2004 * To prevent re-scanning the program header, we would have to build a
2005 * list of loadable segments in si, and then scan that instead. */
2006 if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) {
2007 mprotect((void *)si->wrprotect_start,
2008 si->wrprotect_end - si->wrprotect_start,
2009 PROT_READ | PROT_EXEC);
2010 }
2011#endif
2012
2013 /* If this is a SET?ID program, dup /dev/null to opened stdin,
2014 stdout and stderr to close a security hole described in:
2015
2016 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2017
2018 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002019 if (program_is_setuid)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002020 nullify_closed_stdio ();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002021 notify_gdb_of_load(si);
David 'Digit' Turnere5ea4552011-08-27 10:21:01 +02002022 call_constructors(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002023 return 0;
2024
2025fail:
Dima Zavina7161902010-08-17 15:56:40 -07002026 ERROR("failed to link %s\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002027 si->flags |= FLAG_ERROR;
2028 return -1;
2029}
2030
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002031static void parse_library_path(const char *path, char *delim)
David Bartleybc3a5c22009-06-02 18:27:28 -07002032{
2033 size_t len;
2034 char *ldpaths_bufp = ldpaths_buf;
2035 int i = 0;
2036
2037 len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf));
2038
2039 while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) {
2040 if (*ldpaths[i] != '\0')
2041 ++i;
2042 }
2043
2044 /* Forget the last path if we had to truncate; this occurs if the 2nd to
2045 * last char isn't '\0' (i.e. not originally a delim). */
2046 if (i > 0 && len >= sizeof(ldpaths_buf) &&
2047 ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') {
2048 ldpaths[i - 1] = NULL;
2049 } else {
2050 ldpaths[i] = NULL;
2051 }
2052}
2053
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002054static void parse_preloads(const char *path, char *delim)
Matt Fischer4fd42c12009-12-31 12:09:10 -06002055{
2056 size_t len;
2057 char *ldpreloads_bufp = ldpreloads_buf;
2058 int i = 0;
2059
2060 len = strlcpy(ldpreloads_buf, path, sizeof(ldpreloads_buf));
2061
2062 while (i < LDPRELOAD_MAX && (ldpreload_names[i] = strsep(&ldpreloads_bufp, delim))) {
2063 if (*ldpreload_names[i] != '\0') {
2064 ++i;
2065 }
2066 }
2067
2068 /* Forget the last path if we had to truncate; this occurs if the 2nd to
2069 * last char isn't '\0' (i.e. not originally a delim). */
2070 if (i > 0 && len >= sizeof(ldpreloads_buf) &&
2071 ldpreloads_buf[sizeof(ldpreloads_buf) - 2] != '\0') {
2072 ldpreload_names[i - 1] = NULL;
2073 } else {
2074 ldpreload_names[i] = NULL;
2075 }
2076}
2077
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002078int main(int argc, char **argv)
2079{
2080 return 0;
2081}
2082
2083#define ANDROID_TLS_SLOTS BIONIC_TLS_SLOTS
2084
2085static void * __tls_area[ANDROID_TLS_SLOTS];
2086
2087unsigned __linker_init(unsigned **elfdata)
2088{
2089 static soinfo linker_soinfo;
2090
2091 int argc = (int) *elfdata;
2092 char **argv = (char**) (elfdata + 1);
2093 unsigned *vecs = (unsigned*) (argv + argc + 1);
2094 soinfo *si;
2095 struct link_map * map;
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002096 const char *ldpath_env = NULL;
2097 const char *ldpreload_env = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002098
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02002099 /* Setup a temporary TLS area that is used to get a working
2100 * errno for system calls.
2101 */
2102 __set_tls(__tls_area);
2103
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002104 pid = getpid();
2105
2106#if TIMING
2107 struct timeval t0, t1;
2108 gettimeofday(&t0, 0);
2109#endif
2110
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02002111 /* NOTE: we store the elfdata pointer on a special location
2112 * of the temporary TLS area in order to pass it to
2113 * the C Library's runtime initializer.
2114 *
2115 * The initializer must clear the slot and reset the TLS
2116 * to point to a different location to ensure that no other
2117 * shared library constructor can access it.
2118 */
2119 __tls_area[TLS_SLOT_BIONIC_PREINIT] = elfdata;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002120
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002121 /* Are we setuid? */
2122 program_is_setuid = (getuid() != geteuid()) || (getgid() != getegid());
2123
2124 /* Initialize environment functions, and get to the ELF aux vectors table */
2125 vecs = linker_env_init(vecs);
2126
2127 /* Sanitize environment if we're loading a setuid program */
2128 if (program_is_setuid)
2129 linker_env_secure();
2130
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002131 debugger_init();
2132
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002133 /* Get a few environment variables */
2134 {
2135 const char* env;
2136 env = linker_env_get("DEBUG"); /* XXX: TODO: Change to LD_DEBUG */
2137 if (env)
2138 debug_verbosity = atoi(env);
2139
2140 /* Normally, these are cleaned by linker_env_secure, but the test
2141 * against program_is_setuid doesn't cost us anything */
2142 if (!program_is_setuid) {
2143 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2144 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002145 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002146 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002147
2148 INFO("[ android linker & debugger ]\n");
2149 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
2150
2151 si = alloc_info(argv[0]);
2152 if(si == 0) {
2153 exit(-1);
2154 }
2155
2156 /* bootstrap the link map, the main exe always needs to be first */
2157 si->flags |= FLAG_EXE;
2158 map = &(si->linkmap);
2159
2160 map->l_addr = 0;
2161 map->l_name = argv[0];
2162 map->l_prev = NULL;
2163 map->l_next = NULL;
2164
2165 _r_debug.r_map = map;
2166 r_debug_tail = map;
2167
2168 /* gdb expects the linker to be in the debug shared object list,
2169 * and we need to make sure that the reported load address is zero.
2170 * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
2171 * is. Don't use alloc_info(), because the linker shouldn't
2172 * be on the soinfo list.
2173 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002174 strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002175 linker_soinfo.flags = 0;
2176 linker_soinfo.base = 0; // This is the important part; must be zero.
2177 insert_soinfo_into_debug_map(&linker_soinfo);
2178
2179 /* extract information passed from the kernel */
2180 while(vecs[0] != 0){
2181 switch(vecs[0]){
2182 case AT_PHDR:
2183 si->phdr = (Elf32_Phdr*) vecs[1];
2184 break;
2185 case AT_PHNUM:
2186 si->phnum = (int) vecs[1];
2187 break;
2188 case AT_ENTRY:
2189 si->entry = vecs[1];
2190 break;
2191 }
2192 vecs += 2;
2193 }
2194
Nick Kralevichd9ad6232011-10-20 14:57:56 -07002195 si->base = (Elf32_Addr) si->phdr - si->phdr->p_vaddr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002196 si->dynamic = (unsigned *)-1;
2197 si->wrprotect_start = 0xffffffff;
2198 si->wrprotect_end = 0;
David 'Digit' Turner67748092010-07-21 16:18:21 -07002199 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002200
David Bartleybc3a5c22009-06-02 18:27:28 -07002201 /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002202 if (ldpath_env)
David Bartleybc3a5c22009-06-02 18:27:28 -07002203 parse_library_path(ldpath_env, ":");
2204
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002205 if (ldpreload_env) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06002206 parse_preloads(ldpreload_env, " :");
2207 }
2208
Dima Zavin2e855792009-05-20 18:28:09 -07002209 if(link_image(si, 0)) {
2210 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
2211 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2212 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002213 exit(-1);
2214 }
2215
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07002216#if ALLOW_SYMBOLS_FROM_MAIN
2217 /* Set somain after we've loaded all the libraries in order to prevent
2218 * linking of symbols back to the main image, which is not set up at that
2219 * point yet.
2220 */
2221 somain = si;
2222#endif
2223
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002224#if TIMING
2225 gettimeofday(&t1,NULL);
2226 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
2227 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2228 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
2229 ));
2230#endif
2231#if STATS
2232 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
2233 linker_stats.reloc[RELOC_ABSOLUTE],
2234 linker_stats.reloc[RELOC_RELATIVE],
2235 linker_stats.reloc[RELOC_COPY],
2236 linker_stats.reloc[RELOC_SYMBOL]);
2237#endif
2238#if COUNT_PAGES
2239 {
2240 unsigned n;
2241 unsigned i;
2242 unsigned count = 0;
2243 for(n = 0; n < 4096; n++){
2244 if(bitmask[n]){
2245 unsigned x = bitmask[n];
2246 for(i = 0; i < 8; i++){
2247 if(x & 1) count++;
2248 x >>= 1;
2249 }
2250 }
2251 }
2252 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
2253 }
2254#endif
2255
2256#if TIMING || STATS || COUNT_PAGES
2257 fflush(stdout);
2258#endif
2259
2260 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
2261 si->entry);
2262 return si->entry;
2263}