blob: a3bc171a988bef70a06f7b270105d4c9d70bf83a [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"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020053#include "linker_phdr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070055#define ALLOW_SYMBOLS_FROM_MAIN 1
Kenny Root72f9a5c2011-02-10 17:02:21 -080056#define SO_MAX 128
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057
David Bartleybc3a5c22009-06-02 18:27:28 -070058/* Assume average path length of 64 and max 8 paths */
59#define LDPATH_BUFSIZE 512
60#define LDPATH_MAX 8
61
Matt Fischer4fd42c12009-12-31 12:09:10 -060062#define LDPRELOAD_BUFSIZE 512
63#define LDPRELOAD_MAX 8
64
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
66 *
67 * Do NOT use malloc() and friends or pthread_*() code here.
68 * Don't use printf() either; it's caused mysterious memory
69 * corruption in the past.
70 * The linker runs before we bring up libc and it's easiest
71 * to make sure it does not depend on any complex libc features
72 *
73 * open issues / todo:
74 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075 * - are we doing everything we should for ARM_COPY relocations?
76 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077 * - after linking, set as much stuff as possible to READONLY
78 * and NOEXEC
79 * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
80 * headers provide versions that are negative...
81 * - allocate space for soinfo structs dynamically instead of
82 * having a hard limit (64)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083*/
84
85
David 'Digit' Turner16084162012-06-12 16:25:37 +020086static int soinfo_link_image(soinfo *si, unsigned wr_offset);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
88static int socount = 0;
89static soinfo sopool[SO_MAX];
90static soinfo *freelist = NULL;
91static soinfo *solist = &libdl_info;
92static soinfo *sonext = &libdl_info;
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070093#if ALLOW_SYMBOLS_FROM_MAIN
94static soinfo *somain; /* main process, always the one after libdl_info */
95#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096
Iliyan Malchevaf7315a2009-10-16 17:50:42 -070097
Iliyan Malchev6ed80c82009-09-28 19:38:04 -070098static inline int validate_soinfo(soinfo *si)
99{
100 return (si >= sopool && si < sopool + SO_MAX) ||
101 si == &libdl_info;
102}
103
David Bartleybc3a5c22009-06-02 18:27:28 -0700104static char ldpaths_buf[LDPATH_BUFSIZE];
105static const char *ldpaths[LDPATH_MAX + 1];
106
Matt Fischer4fd42c12009-12-31 12:09:10 -0600107static char ldpreloads_buf[LDPRELOAD_BUFSIZE];
108static const char *ldpreload_names[LDPRELOAD_MAX + 1];
109
110static soinfo *preloads[LDPRELOAD_MAX + 1];
111
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -0700112#if LINKER_DEBUG
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113int debug_verbosity;
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -0700114#endif
115
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116static int pid;
117
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100118/* This boolean is set if the program being loaded is setuid */
119static int program_is_setuid;
120
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121#if STATS
122struct _link_stats linker_stats;
123#endif
124
125#if COUNT_PAGES
126unsigned bitmask[4096];
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
David 'Digit' Turner16084162012-06-12 16:25:37 +0200254static soinfo *soinfo_alloc(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255{
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
David 'Digit' Turner16084162012-06-12 16:25:37 +0200263 /* The freelist is populated when we call soinfo_free(), which in turn is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264 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
David 'Digit' Turner16084162012-06-12 16:25:37 +0200290static void soinfo_free(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291{
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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800316const char *addr_to_name(unsigned addr)
317{
318 soinfo *si;
319
320 for(si = solist; si != 0; si = si->next){
321 if((addr >= si->base) && (addr < (si->base + si->size))) {
322 return si->name;
323 }
324 }
325
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800326 return "";
327}
328
329/* For a given PC, find the .so that it belongs to.
330 * Returns the base address of the .ARM.exidx section
331 * for that .so, and the number of 8-byte entries
332 * in that section (via *pcount).
333 *
334 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
335 *
336 * This function is exposed via dlfcn.c and libdl.so.
337 */
338#ifdef ANDROID_ARM_LINKER
339_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
340{
341 soinfo *si;
342 unsigned addr = (unsigned)pc;
343
Nick Kralevich468319c2011-11-11 15:53:17 -0800344 for (si = solist; si != 0; si = si->next){
345 if ((addr >= si->base) && (addr < (si->base + si->size))) {
346 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900347 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800348 }
349 }
350 *pcount = 0;
351 return NULL;
352}
David 'Digit' Turner70b16682012-01-30 17:17:58 +0100353#elif defined(ANDROID_X86_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800354/* Here, we only have to provide a callback to iterate across all the
355 * loaded libraries. gcc_eh does the rest. */
356int
357dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
358 void *data)
359{
360 soinfo *si;
361 struct dl_phdr_info dl_info;
362 int rv = 0;
363
364 for (si = solist; si != NULL; si = si->next) {
365 dl_info.dlpi_addr = si->linkmap.l_addr;
366 dl_info.dlpi_name = si->linkmap.l_name;
367 dl_info.dlpi_phdr = si->phdr;
368 dl_info.dlpi_phnum = si->phnum;
369 rv = cb(&dl_info, sizeof (struct dl_phdr_info), data);
370 if (rv != 0)
371 break;
372 }
373 return rv;
374}
375#endif
376
David 'Digit' Turner16084162012-06-12 16:25:37 +0200377static Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800378{
379 Elf32_Sym *s;
380 Elf32_Sym *symtab = si->symtab;
381 const char *strtab = si->strtab;
382 unsigned n;
383
384 TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
385 name, si->name, si->base, hash, hash % si->nbucket);
386 n = hash % si->nbucket;
387
388 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
389 s = symtab + n;
390 if(strcmp(strtab + s->st_name, name)) continue;
391
Doug Kwane8238072009-10-26 12:05:23 -0700392 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393 switch(ELF32_ST_BIND(s->st_info)){
394 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700395 case STB_WEAK:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396 /* no section == undefined */
397 if(s->st_shndx == 0) continue;
398
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800399 TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
400 name, si->name, s->st_value, s->st_size);
401 return s;
402 }
403 }
404
Doug Kwan94304352009-10-23 18:11:40 -0700405 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406}
407
408static unsigned elfhash(const char *_name)
409{
410 const unsigned char *name = (const unsigned char *) _name;
411 unsigned h = 0, g;
412
413 while(*name) {
414 h = (h << 4) + *name++;
415 g = h & 0xf0000000;
416 h ^= g;
417 h ^= g >> 24;
418 }
419 return h;
420}
421
422static Elf32_Sym *
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200423soinfo_do_lookup(soinfo *si, const char *name, Elf32_Addr *offset)
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700424{
Doug Kwan94304352009-10-23 18:11:40 -0700425 unsigned elf_hash = elfhash(name);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700426 Elf32_Sym *s;
427 unsigned *d;
428 soinfo *lsi = si;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600429 int i;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700430
Nick Kralevich468319c2011-11-11 15:53:17 -0800431 /* Look for symbols in the local scope (the object who is
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700432 * searching). This happens with C++ templates on i386 for some
Doug Kwane8238072009-10-26 12:05:23 -0700433 * reason.
434 *
435 * Notes on weak symbols:
436 * The ELF specs are ambigious about treatment of weak definitions in
437 * dynamic linking. Some systems return the first definition found
438 * and some the first non-weak definition. This is system dependent.
439 * Here we return the first definition found for simplicity. */
Nick Kralevich468319c2011-11-11 15:53:17 -0800440
David 'Digit' Turner16084162012-06-12 16:25:37 +0200441 s = soinfo_elf_lookup(si, elf_hash, name);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700442 if(s != NULL)
443 goto done;
444
Matt Fischer4fd42c12009-12-31 12:09:10 -0600445 /* Next, look for it in the preloads list */
446 for(i = 0; preloads[i] != NULL; i++) {
447 lsi = preloads[i];
David 'Digit' Turner16084162012-06-12 16:25:37 +0200448 s = soinfo_elf_lookup(lsi, elf_hash, name);
Matt Fischer4fd42c12009-12-31 12:09:10 -0600449 if(s != NULL)
450 goto done;
451 }
452
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700453 for(d = si->dynamic; *d; d += 2) {
454 if(d[0] == DT_NEEDED){
455 lsi = (soinfo *)d[1];
456 if (!validate_soinfo(lsi)) {
457 DL_ERR("%5d bad DT_NEEDED pointer in %s",
458 pid, si->name);
Doug Kwan94304352009-10-23 18:11:40 -0700459 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700460 }
461
462 DEBUG("%5d %s: looking up %s in %s\n",
463 pid, si->name, name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200464 s = soinfo_elf_lookup(lsi, elf_hash, name);
Min-su, Kim3cab22c2010-01-19 10:05:33 +0900465 if ((s != NULL) && (s->st_shndx != SHN_UNDEF))
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700466 goto done;
467 }
468 }
469
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -0700470#if ALLOW_SYMBOLS_FROM_MAIN
471 /* If we are resolving relocations while dlopen()ing a library, it's OK for
472 * the library to resolve a symbol that's defined in the executable itself,
473 * although this is rare and is generally a bad idea.
474 */
475 if (somain) {
476 lsi = somain;
477 DEBUG("%5d %s: looking up %s in executable %s\n",
478 pid, si->name, name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200479 s = soinfo_elf_lookup(lsi, elf_hash, name);
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -0700480 }
481#endif
482
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700483done:
484 if(s != NULL) {
485 TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200486 "found in %s, base = 0x%08x, load bias = 0x%08x\n",
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900487 pid, si->name, name, s->st_value,
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200488 lsi->name, lsi->base, lsi->load_bias);
489 *offset = lsi->load_bias;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700490 return s;
491 }
492
Doug Kwan94304352009-10-23 18:11:40 -0700493 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700494}
495
496/* This is used by dl_sym(). It performs symbol lookup only within the
497 specified soinfo object and not in any of its dependencies.
498 */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200499Elf32_Sym *soinfo_lookup(soinfo *si, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800500{
David 'Digit' Turner16084162012-06-12 16:25:37 +0200501 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800502}
503
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700504/* This is used by dl_sym(). It performs a global symbol lookup.
505 */
Matt Fischer1698d9e2009-12-31 12:17:56 -0600506Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800507{
Doug Kwan94304352009-10-23 18:11:40 -0700508 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800509 Elf32_Sym *s = NULL;
510 soinfo *si;
511
Matt Fischer1698d9e2009-12-31 12:17:56 -0600512 if(start == NULL) {
513 start = solist;
514 }
515
516 for(si = start; (s == NULL) && (si != NULL); si = si->next)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800517 {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700518 if(si->flags & FLAG_ERROR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800519 continue;
David 'Digit' Turner16084162012-06-12 16:25:37 +0200520 s = soinfo_elf_lookup(si, elf_hash, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800521 if (s != NULL) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700522 *found = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800523 break;
524 }
525 }
526
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700527 if(s != NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800528 TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
529 "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
530 return s;
531 }
532
Doug Kwan94304352009-10-23 18:11:40 -0700533 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534}
535
Mathias Agopianbda5da02011-09-27 22:30:19 -0700536soinfo *find_containing_library(const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600537{
538 soinfo *si;
539
540 for(si = solist; si != NULL; si = si->next)
541 {
542 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
543 return si;
544 }
545 }
546
547 return NULL;
548}
549
David 'Digit' Turner16084162012-06-12 16:25:37 +0200550Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600551{
552 unsigned int i;
553 unsigned soaddr = (unsigned)addr - si->base;
554
555 /* Search the library's symbol table for any defined symbol which
556 * contains this address */
557 for(i=0; i<si->nchain; i++) {
558 Elf32_Sym *sym = &si->symtab[i];
559
560 if(sym->st_shndx != SHN_UNDEF &&
561 soaddr >= sym->st_value &&
562 soaddr < sym->st_value + sym->st_size) {
563 return sym;
564 }
565 }
566
567 return NULL;
568}
569
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800570#if 0
571static void dump(soinfo *si)
572{
573 Elf32_Sym *s = si->symtab;
574 unsigned n;
575
576 for(n = 0; n < si->nchain; n++) {
577 TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
578 s->st_info, s->st_shndx, s->st_value, s->st_size,
579 si->strtab + s->st_name);
580 s++;
581 }
582}
583#endif
584
David 'Digit' Turner16084162012-06-12 16:25:37 +0200585static const char * const sopaths[] = {
Brian Swetlandfedbcde2010-09-19 03:39:13 -0700586 "/vendor/lib",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800587 "/system/lib",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800588 0
589};
590
591static int _open_lib(const char *name)
592{
593 int fd;
594 struct stat filestat;
595
596 if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
David 'Digit' Turner16084162012-06-12 16:25:37 +0200597 if ((fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY))) >= 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598 return fd;
599 }
600
601 return -1;
602}
603
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800604static int open_library(const char *name)
605{
606 int fd;
607 char buf[512];
David 'Digit' Turner16084162012-06-12 16:25:37 +0200608 const char * const*path;
David Bartleybc3a5c22009-06-02 18:27:28 -0700609 int n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800610
611 TRACE("[ %5d opening %s ]\n", pid, name);
612
613 if(name == 0) return -1;
614 if(strlen(name) > 256) return -1;
615
616 if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
617 return fd;
618
David Bartleybc3a5c22009-06-02 18:27:28 -0700619 for (path = ldpaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800620 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700621 if (n < 0 || n >= (int)sizeof(buf)) {
622 WARN("Ignoring very long library path: %s/%s\n", *path, name);
623 continue;
624 }
625 if ((fd = _open_lib(buf)) >= 0)
626 return fd;
627 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800628 for (path = sopaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800629 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700630 if (n < 0 || n >= (int)sizeof(buf)) {
631 WARN("Ignoring very long library path: %s/%s\n", *path, name);
632 continue;
633 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800634 if ((fd = _open_lib(buf)) >= 0)
635 return fd;
636 }
637
638 return -1;
639}
640
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800641typedef struct {
642 long mmap_addr;
643 char tag[4]; /* 'P', 'R', 'E', ' ' */
644} prelink_info_t;
645
646/* Returns the requested base address if the library is prelinked,
647 * and 0 otherwise. */
648static unsigned long
649is_prelinked(int fd, const char *name)
650{
651 off_t sz;
652 prelink_info_t info;
653
654 sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
655 if (sz < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700656 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657 return 0;
658 }
659
David 'Digit' Turner16084162012-06-12 16:25:37 +0200660 if (TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)) != sizeof(info))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800661 WARN("Could not read prelink_info_t structure for `%s`\n", name);
662 return 0;
663 }
664
David 'Digit' Turner16084162012-06-12 16:25:37 +0200665 if (memcmp(info.tag, "PRE ", 4)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800666 WARN("`%s` is not a prelinked library\n", name);
667 return 0;
668 }
669
670 return (unsigned long)info.mmap_addr;
671}
672
David 'Digit' Turner16084162012-06-12 16:25:37 +0200673/* verify_elf_header
674 * Verifies the content of an ELF header.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800675 *
676 * Args:
677 *
678 * Returns:
679 * 0 on success
680 * -1 if no valid ELF object is found @ base.
681 */
682static int
David 'Digit' Turner16084162012-06-12 16:25:37 +0200683verify_elf_header(const Elf32_Ehdr* hdr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800684{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800685 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
686 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
687 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
688 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
689
690 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800691#ifdef ANDROID_ARM_LINKER
692 if (hdr->e_machine != EM_ARM) return -1;
693#elif defined(ANDROID_X86_LINKER)
694 if (hdr->e_machine != EM_386) return -1;
695#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800696 return 0;
697}
698
699
Nick Kralevich66259862012-03-16 13:06:12 -0700700/* reserve_mem_region
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800701 *
702 * This function reserves a chunk of memory to be used for mapping in
Nick Kralevich66259862012-03-16 13:06:12 -0700703 * a prelinked shared library. We reserve the entire memory region here, and
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800704 * then the rest of the linker will relocate the individual loadable
705 * segments into the correct locations within this memory range.
706 *
707 * Args:
Nick Kralevich66259862012-03-16 13:06:12 -0700708 * si->base: The requested base of the allocation.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800709 * si->size: The size of the allocation.
710 *
711 * Returns:
712 * -1 on failure, and 0 on success. On success, si->base will contain
713 * the virtual address at which the library will be mapped.
714 */
715
David 'Digit' Turner16084162012-06-12 16:25:37 +0200716static int soinfo_reserve_mem_region(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800717{
Nick Kralevich66259862012-03-16 13:06:12 -0700718 void *base = mmap((void *)si->base, si->size, PROT_NONE,
Chris Dearmandb4bce02011-03-10 10:48:14 -0800719 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800720 if (base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700721 DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700722 "as requested, will try general pool: %d (%s)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800723 pid, (si->base ? "" : "non-"), si->name, si->base,
724 errno, strerror(errno));
725 return -1;
726 } else if (base != (void *)si->base) {
Dima Zavin2e855792009-05-20 18:28:09 -0700727 DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700728 "not at 0x%08x", pid, (si->base ? "" : "non-"),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800729 si->name, (unsigned)base, si->base);
730 munmap(base, si->size);
731 return -1;
732 }
733 return 0;
734}
735
David 'Digit' Turner16084162012-06-12 16:25:37 +0200736static int soinfo_alloc_mem_region(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800737{
738 if (si->base) {
739 /* Attempt to mmap a prelinked library. */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200740 return soinfo_reserve_mem_region(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800741 }
742
Shih-wei Liao48527c32011-07-17 12:32:43 -0700743 /* This is not a prelinked library, so we use the kernel's default
744 allocator.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800745 */
Shih-wei Liao48527c32011-07-17 12:32:43 -0700746
Nick Kralevich66259862012-03-16 13:06:12 -0700747 void *base = mmap(NULL, si->size, PROT_NONE,
Shih-wei Liao48527c32011-07-17 12:32:43 -0700748 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
749 if (base == MAP_FAILED) {
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200750 DL_ERR("%5d mmap of library '%s' failed (%d bytes): %d (%s)\n",
751 pid, si->name, si->size,
Shih-wei Liao48527c32011-07-17 12:32:43 -0700752 errno, strerror(errno));
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200753 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800754 }
Shih-wei Liao48527c32011-07-17 12:32:43 -0700755 si->base = (unsigned) base;
756 PRINT("%5d mapped library '%s' to %08x via kernel allocator.\n",
757 pid, si->name, si->base);
758 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800759}
760
761#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
762#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
763 MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
764 MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
765/* load_segments
766 *
767 * This function loads all the loadable (PT_LOAD) segments into memory
768 * at their appropriate memory offsets off the base address.
769 *
770 * Args:
771 * fd: Open file descriptor to the library to load.
772 * header: Pointer to a header page that contains the ELF header.
773 * This is needed since we haven't mapped in the real file yet.
774 * si: ptr to soinfo struct describing the shared object.
775 *
776 * Returns:
777 * 0 on success, -1 on failure.
778 */
779static int
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200780soinfo_load_segments(soinfo* si, int fd, const Elf32_Phdr* phdr_table, int phdr_count, Elf32_Addr ehdr_phoff)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800781{
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200782 const Elf32_Phdr *phdr = phdr_table;
783 const Elf32_Phdr *phdr0 = 0; /* program header for the first LOAD segment */
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900784 Elf32_Addr base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800785 int cnt;
786 unsigned len;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800787 Elf32_Addr tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800788 unsigned char *pbase;
789 unsigned char *extra_base;
790 unsigned extra_len;
791 unsigned total_sz = 0;
792
793 si->wrprotect_start = 0xffffffff;
794 si->wrprotect_end = 0;
795
796 TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n",
797 pid, si->name, (unsigned)si->base);
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900798
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200799 for (cnt = 0; cnt < phdr_count; ++cnt, ++phdr) {
David 'Digit' Turner16084162012-06-12 16:25:37 +0200800
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900801 if (phdr->p_type == PT_LOAD) {
802 phdr0 = phdr;
803 /*
804 * ELF specification section 2-2.
805 *
806 * PT_LOAD: "Loadable segment entries in the program
807 * header table appear in ascending order, sorted on the
808 * p_vaddr member."
809 */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200810 si->load_bias = si->base - PAGE_START(phdr->p_vaddr);
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900811 break;
812 }
813 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200814
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900815 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200816 base = si->load_bias;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900817
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800818 /* Now go through all the PT_LOAD segments and map them into memory
819 * at the appropriate locations. */
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200820 phdr = phdr_table;
821 for (cnt = 0; cnt < phdr_count; ++cnt, ++phdr) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800822 if (phdr->p_type == PT_LOAD) {
823 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
824 /* we want to map in the segment on a page boundary */
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200825 tmp = base + PAGE_START(phdr->p_vaddr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800826 /* add the # of bytes we masked off above to the total length. */
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200827 len = phdr->p_filesz + PAGE_OFFSET(phdr->p_vaddr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800828
829 TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x "
830 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name,
831 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800832 pbase = mmap((void *)tmp, len, PFLAGS_TO_PROT(phdr->p_flags),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800833 MAP_PRIVATE | MAP_FIXED, fd,
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200834 PAGE_START(phdr->p_offset));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800835 if (pbase == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700836 DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700837 "p_vaddr=0x%08x p_offset=0x%08x", pid, si->name,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800838 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
839 goto fail;
840 }
841
842 /* If 'len' didn't end on page boundary, and it's a writable
843 * segment, zero-fill the rest. */
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200844 if (PAGE_OFFSET(len) > 0 && (phdr->p_flags & PF_W) != 0)
845 memset((void *)(pbase + len), 0, PAGE_SIZE - PAGE_OFFSET(len));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800846
847 /* Check to see if we need to extend the map for this segment to
848 * cover the diff between filesz and memsz (i.e. for bss).
849 *
850 * base _+---------------------+ page boundary
851 * . .
852 * | |
853 * . .
854 * pbase _+---------------------+ page boundary
855 * | |
856 * . .
857 * base + p_vaddr _| |
858 * . \ \ .
859 * . | filesz | .
860 * pbase + len _| / | |
861 * <0 pad> . . .
862 * extra_base _+------------|--------+ page boundary
863 * / . . .
864 * | . . .
865 * | +------------|--------+ page boundary
866 * extra_len-> | | | |
867 * | . | memsz .
868 * | . | .
869 * \ _| / |
870 * . .
871 * | |
872 * _+---------------------+ page boundary
873 */
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200874 tmp = (Elf32_Addr)PAGE_END((unsigned)pbase + len);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800875 if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) {
876 extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp;
877 TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x "
878 "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len);
879 /* map in the extra page(s) as anonymous into the range.
880 * This is probably not necessary as we already mapped in
881 * the entire region previously, but we just want to be
882 * sure. This will also set the right flags on the region
883 * (though we can probably accomplish the same thing with
884 * mprotect).
885 */
886 extra_base = mmap((void *)tmp, extra_len,
887 PFLAGS_TO_PROT(phdr->p_flags),
888 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
889 -1, 0);
890 if (extra_base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700891 DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x"
Erik Gillingd00d23a2009-07-22 17:06:11 -0700892 " (0x%08x) ]", pid, si->name, (unsigned)tmp,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800893 extra_len);
894 goto fail;
895 }
896 /* TODO: Check if we need to memset-0 this region.
897 * Anonymous mappings are zero-filled copy-on-writes, so we
898 * shouldn't need to. */
899 TRACE("[ %5d - Segment from '%s' extended @ 0x%08x "
900 "(0x%08x)\n", pid, si->name, (unsigned)extra_base,
901 extra_len);
902 }
903 /* set the len here to show the full extent of the segment we
904 * just loaded, mostly for debugging */
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200905 len = PAGE_END((unsigned)base + phdr->p_vaddr + phdr->p_memsz) - (unsigned)pbase;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800906 TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x "
907 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
908 (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset);
909 total_sz += len;
910 /* Make the section writable just in case we'll have to write to
911 * it during relocation (i.e. text segment). However, we will
912 * remember what range of addresses should be write protected.
913 *
914 */
915 if (!(phdr->p_flags & PF_W)) {
916 if ((unsigned)pbase < si->wrprotect_start)
917 si->wrprotect_start = (unsigned)pbase;
918 if (((unsigned)pbase + len) > si->wrprotect_end)
919 si->wrprotect_end = (unsigned)pbase + len;
920 mprotect(pbase, len,
921 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
922 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800923 } else if (phdr->p_type == PT_GNU_RELRO) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900924 if (((base + phdr->p_vaddr) >= si->base + si->size)
925 || ((base + phdr->p_vaddr + phdr->p_memsz) > si->base + si->size)
926 || ((base + phdr->p_vaddr + phdr->p_memsz) < si->base)) {
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800927 DL_ERR("%d invalid GNU_RELRO in '%s' "
928 "p_vaddr=0x%08x p_memsz=0x%08x", pid, si->name,
929 phdr->p_vaddr, phdr->p_memsz);
930 goto fail;
931 }
932 si->gnu_relro_start = (Elf32_Addr) (base + phdr->p_vaddr);
933 si->gnu_relro_len = (unsigned) phdr->p_memsz;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800934 }
935
936 }
937
938 /* Sanity check */
939 if (total_sz > si->size) {
Dima Zavin2e855792009-05-20 18:28:09 -0700940 DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700941 "greater than what was allocated (0x%08x). THIS IS BAD!",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800942 pid, total_sz, si->name, si->size);
943 goto fail;
944 }
945
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900946 /* vaddr : Real virtual address in process' address space.
947 * p_vaddr : Relative virtual address in ELF object
948 * p_offset : File offset in ELF object
949 *
950 * vaddr p_vaddr p_offset
951 * ----- ------------ --------
952 * base 0
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200953 * si->base PAGE_START(phdr0->p_vaddr)
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900954 * phdr0->p_vaddr phdr0->p_offset
955 * phdr ehdr->e_phoff
956 */
957 si->phdr = (Elf32_Phdr *)(base + phdr0->p_vaddr +
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200958 ehdr_phoff - phdr0->p_offset);
959 si->phnum = phdr_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900960
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800961 TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. "
962 "Total memory footprint: 0x%08x bytes ]\n", pid, si->name,
963 (unsigned)si->base, si->size);
964 return 0;
965
966fail:
967 /* We can just blindly unmap the entire region even though some things
968 * were mapped in originally with anonymous and others could have been
969 * been mapped in from the file before we failed. The kernel will unmap
970 * all the pages in the range, irrespective of how they got there.
971 */
972 munmap((void *)si->base, si->size);
973 si->flags |= FLAG_ERROR;
974 return -1;
975}
976
977/* TODO: Implement this to take care of the fact that Android ARM
978 * ELF objects shove everything into a single loadable segment that has the
979 * write bit set. wr_offset is then used to set non-(data|bss) pages to be
980 * non-writable.
981 */
982#if 0
983static unsigned
984get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr)
985{
986 Elf32_Shdr *shdr_start;
987 Elf32_Shdr *shdr;
988 int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr);
989 int cnt;
990 unsigned wr_offset = 0xffffffff;
991
992 shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd,
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200993 PAGE_START(ehdr->e_shoff));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800994 if (shdr_start == MAP_FAILED) {
995 WARN("%5d - Could not read section header info from '%s'. Will not "
996 "not be able to determine write-protect offset.\n", pid, name);
997 return (unsigned)-1;
998 }
999
1000 for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) {
1001 if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) &&
1002 (shdr->sh_addr < wr_offset)) {
1003 wr_offset = shdr->sh_addr;
1004 }
1005 }
1006
1007 munmap(shdr_start, shdr_sz);
1008 return wr_offset;
1009}
1010#endif
1011
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001012
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001013static soinfo *
1014load_library(const char *name)
1015{
1016 int fd = open_library(name);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001017 int ret, cnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001018 unsigned ext_sz;
1019 unsigned req_base;
Erik Gillingfde86422009-07-28 20:28:19 -07001020 const char *bname;
Ji-Hwan Lee75917c82012-05-25 22:36:00 +09001021 struct stat sb;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001022 soinfo *si = NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001023 Elf32_Ehdr header[1];
1024 int phdr_count;
1025 void* phdr_mmap = NULL;
1026 Elf32_Addr phdr_size;
1027 const Elf32_Phdr* phdr_table;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001028
Ji-Hwan Lee75917c82012-05-25 22:36:00 +09001029 if (fd == -1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001030 DL_ERR("Library '%s' not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001031 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -07001032 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001033
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001034 /* Read the ELF header first */
1035 ret = TEMP_FAILURE_RETRY(read(fd, (void*)header, sizeof(header)));
1036 if (ret < 0) {
1037 DL_ERR("%5d can't read file %s: %s", pid, name, strerror(errno));
1038 goto fail;
1039 }
1040 if (ret != (int)sizeof(header)) {
1041 DL_ERR("%5d too small to be an ELF executable: %s", pid, name);
1042 goto fail;
1043 }
1044 if (verify_elf_header(header) < 0) {
1045 DL_ERR("%5d not a valid ELF executable: %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001046 goto fail;
1047 }
1048
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001049 /* Then read the program header table */
1050 ret = phdr_table_load(fd, header->e_phoff, header->e_phnum,
1051 &phdr_mmap, &phdr_size, &phdr_table);
1052 if (ret < 0) {
1053 DL_ERR("%5d can't load program header table: %s: %s", pid,
1054 name, strerror(errno));
1055 goto fail;
1056 }
1057 phdr_count = header->e_phnum;
1058
1059 /* Get the load extents and the prelinked load address, if any */
1060 ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
1061 if (ext_sz == 0) {
1062 DL_ERR("%5d no loadable segments in file: %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001063 goto fail;
1064 }
1065
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001066 req_base = (unsigned) is_prelinked(fd, name);
1067 if (req_base == (unsigned)-1) {
1068 DL_ERR("%5d can't read end of library: %s: %s", pid, name,
1069 strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001070 goto fail;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001071 }
1072 if (req_base != 0) {
1073 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
1074 pid, name, req_base);
1075 } else {
1076 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
1077 }
David 'Digit' Turner16084162012-06-12 16:25:37 +02001078
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001079 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
1080 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
1081
1082 /* Now configure the soinfo struct where we'll store all of our data
1083 * for the ELF object. If the loading fails, we waste the entry, but
1084 * same thing would happen if we failed during linking. Configuring the
1085 * soinfo struct here is a lot more convenient.
1086 */
Erik Gillingfde86422009-07-28 20:28:19 -07001087 bname = strrchr(name, '/');
David 'Digit' Turner16084162012-06-12 16:25:37 +02001088 si = soinfo_alloc(bname ? bname + 1 : name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001089 if (si == NULL)
1090 goto fail;
1091
1092 /* Carve out a chunk of memory where we will map in the individual
1093 * segments */
1094 si->base = req_base;
1095 si->size = ext_sz;
1096 si->flags = 0;
1097 si->entry = 0;
1098 si->dynamic = (unsigned *)-1;
David 'Digit' Turner16084162012-06-12 16:25:37 +02001099 if (soinfo_alloc_mem_region(si) < 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001100 goto fail;
1101
1102 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
1103 pid, name, (void *)si->base, (unsigned) ext_sz);
1104
1105 /* Now actually load the library's segments into right places in memory */
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001106 if (soinfo_load_segments(si, fd, phdr_table, phdr_count, header->e_phoff) < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001107 goto fail;
1108 }
1109
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001110 phdr_table_unload(phdr_mmap, phdr_size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001111 close(fd);
1112 return si;
1113
1114fail:
David 'Digit' Turner16084162012-06-12 16:25:37 +02001115 if (si) soinfo_free(si);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001116 if (phdr_mmap != NULL) {
1117 phdr_table_unload(phdr_mmap, phdr_size);
1118 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001119 close(fd);
1120 return NULL;
1121}
1122
1123static soinfo *
1124init_library(soinfo *si)
1125{
1126 unsigned wr_offset = 0xffffffff;
1127
1128 /* At this point we know that whatever is loaded @ base is a valid ELF
1129 * shared library whose segments are properly mapped in. */
1130 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
1131 pid, si->base, si->size, si->name);
1132
David 'Digit' Turner16084162012-06-12 16:25:37 +02001133 if(soinfo_link_image(si, wr_offset)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001134 /* We failed to link. However, we can only restore libbase
1135 ** if no additional libraries have moved it since we updated it.
1136 */
1137 munmap((void *)si->base, si->size);
1138 return NULL;
1139 }
1140
1141 return si;
1142}
1143
1144soinfo *find_library(const char *name)
1145{
1146 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001147 const char *bname;
1148
1149#if ALLOW_SYMBOLS_FROM_MAIN
1150 if (name == NULL)
1151 return somain;
1152#else
1153 if (name == NULL)
1154 return NULL;
1155#endif
1156
1157 bname = strrchr(name, '/');
Erik Gillingfde86422009-07-28 20:28:19 -07001158 bname = bname ? bname + 1 : name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001159
1160 for(si = solist; si != 0; si = si->next){
Erik Gillingfde86422009-07-28 20:28:19 -07001161 if(!strcmp(bname, si->name)) {
Erik Gilling30eb4022009-08-13 16:05:30 -07001162 if(si->flags & FLAG_ERROR) {
1163 DL_ERR("%5d '%s' failed to load previously", pid, bname);
1164 return NULL;
1165 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001166 if(si->flags & FLAG_LINKED) return si;
Erik Gillingd00d23a2009-07-22 17:06:11 -07001167 DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name);
Dima Zavin2e855792009-05-20 18:28:09 -07001168 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001169 }
1170 }
1171
1172 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
1173 si = load_library(name);
1174 if(si == NULL)
1175 return NULL;
1176 return init_library(si);
1177}
1178
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001179/* TODO:
1180 * notify gdb of unload
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001181 * for non-prelinked libraries, find a way to decrement libbase
1182 */
1183static void call_destructors(soinfo *si);
David 'Digit' Turner16084162012-06-12 16:25:37 +02001184unsigned soinfo_unload(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001185{
1186 unsigned *d;
1187 if (si->refcount == 1) {
1188 TRACE("%5d unloading '%s'\n", pid, si->name);
1189 call_destructors(si);
1190
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001191 /*
1192 * Make sure that we undo the PT_GNU_RELRO protections we added
David 'Digit' Turner16084162012-06-12 16:25:37 +02001193 * in soinfo_link_image. This is needed to undo the DT_NEEDED hack below.
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001194 */
1195 if ((si->gnu_relro_start != 0) && (si->gnu_relro_len != 0)) {
David 'Digit' Turnera6545f42012-06-18 11:15:54 +02001196 Elf32_Addr start = PAGE_START(si->gnu_relro_start);
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001197 unsigned len = (si->gnu_relro_start - start) + si->gnu_relro_len;
1198 if (mprotect((void *) start, len, PROT_READ | PROT_WRITE) < 0)
1199 DL_ERR("%5d %s: could not undo GNU_RELRO protections. "
1200 "Expect a crash soon. errno=%d (%s)",
1201 pid, si->name, errno, strerror(errno));
1202
1203 }
1204
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001205 for(d = si->dynamic; *d; d += 2) {
1206 if(d[0] == DT_NEEDED){
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001207 soinfo *lsi = (soinfo *)d[1];
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001208
1209 // The next line will segfault if the we don't undo the
1210 // PT_GNU_RELRO protections (see comments above and in
David 'Digit' Turner16084162012-06-12 16:25:37 +02001211 // soinfo_link_image().
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001212 d[1] = 0;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001213
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001214 if (validate_soinfo(lsi)) {
1215 TRACE("%5d %s needs to unload %s\n", pid,
1216 si->name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +02001217 soinfo_unload(lsi);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001218 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001219 else
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001220 DL_ERR("%5d %s: could not unload dependent library",
1221 pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001222 }
1223 }
1224
1225 munmap((char *)si->base, si->size);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001226 notify_gdb_of_unload(si);
David 'Digit' Turner16084162012-06-12 16:25:37 +02001227 soinfo_free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001228 si->refcount = 0;
1229 }
1230 else {
1231 si->refcount--;
1232 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
1233 pid, si->name, si->refcount);
1234 }
1235 return si->refcount;
1236}
1237
1238/* TODO: don't use unsigned for addrs below. It works, but is not
1239 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1240 * long.
1241 */
David 'Digit' Turner16084162012-06-12 16:25:37 +02001242static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001243{
1244 Elf32_Sym *symtab = si->symtab;
1245 const char *strtab = si->strtab;
1246 Elf32_Sym *s;
1247 unsigned base;
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001248 Elf32_Addr offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001249 Elf32_Rel *start = rel;
1250 unsigned idx;
1251
1252 for (idx = 0; idx < count; ++idx) {
1253 unsigned type = ELF32_R_TYPE(rel->r_info);
1254 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001255 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001256 unsigned sym_addr = 0;
1257 char *sym_name = NULL;
1258
1259 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1260 si->name, idx);
1261 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001262 sym_name = (char *)(strtab + symtab[sym].st_name);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001263 s = soinfo_do_lookup(si, sym_name, &offset);
Doug Kwane8238072009-10-26 12:05:23 -07001264 if(s == NULL) {
1265 /* We only allow an undefined symbol if this is a weak
1266 reference.. */
1267 s = &symtab[sym];
1268 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1269 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
1270 return -1;
1271 }
1272
1273 /* IHI0044C AAELF 4.5.1.1:
1274
1275 Libraries are not searched to resolve weak references.
1276 It is not an error for a weak reference to remain
1277 unsatisfied.
1278
1279 During linking, the value of an undefined weak reference is:
1280 - Zero if the relocation type is absolute
1281 - The address of the place if the relocation is pc-relative
1282 - The address of nominial base address if the relocation
1283 type is base-relative.
1284 */
1285
1286 switch (type) {
1287#if defined(ANDROID_ARM_LINKER)
1288 case R_ARM_JUMP_SLOT:
1289 case R_ARM_GLOB_DAT:
1290 case R_ARM_ABS32:
1291 case R_ARM_RELATIVE: /* Don't care. */
1292 case R_ARM_NONE: /* Don't care. */
1293#elif defined(ANDROID_X86_LINKER)
1294 case R_386_JUMP_SLOT:
1295 case R_386_GLOB_DAT:
1296 case R_386_32:
1297 case R_386_RELATIVE: /* Dont' care. */
1298#endif /* ANDROID_*_LINKER */
1299 /* sym_addr was initialized to be zero above or relocation
1300 code below does not care about value of sym_addr.
1301 No need to do anything. */
1302 break;
1303
1304#if defined(ANDROID_X86_LINKER)
1305 case R_386_PC32:
1306 sym_addr = reloc;
1307 break;
1308#endif /* ANDROID_X86_LINKER */
1309
1310#if defined(ANDROID_ARM_LINKER)
1311 case R_ARM_COPY:
1312 /* Fall through. Can't really copy if weak symbol is
1313 not found in run-time. */
1314#endif /* ANDROID_ARM_LINKER */
1315 default:
1316 DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n",
1317 pid, type, rel, (int) (rel - start));
1318 return -1;
1319 }
1320 } else {
1321 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001322#if 0
1323 if((base == 0) && (si->base != 0)){
1324 /* linking from libraries to main image is bad */
Erik Gillingd00d23a2009-07-22 17:06:11 -07001325 DL_ERR("%5d cannot locate '%s'...",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001326 pid, strtab + symtab[sym].st_name);
1327 return -1;
1328 }
1329#endif
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001330 sym_addr = (unsigned)(s->st_value + offset);
Doug Kwane8238072009-10-26 12:05:23 -07001331 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001332 COUNT_RELOC(RELOC_SYMBOL);
1333 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001334 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001335 }
1336
1337/* TODO: This is ugly. Split up the relocations by arch into
1338 * different files.
1339 */
1340 switch(type){
1341#if defined(ANDROID_ARM_LINKER)
1342 case R_ARM_JUMP_SLOT:
1343 COUNT_RELOC(RELOC_ABSOLUTE);
1344 MARK(rel->r_offset);
1345 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1346 reloc, sym_addr, sym_name);
1347 *((unsigned*)reloc) = sym_addr;
1348 break;
1349 case R_ARM_GLOB_DAT:
1350 COUNT_RELOC(RELOC_ABSOLUTE);
1351 MARK(rel->r_offset);
1352 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1353 reloc, sym_addr, sym_name);
1354 *((unsigned*)reloc) = sym_addr;
1355 break;
1356 case R_ARM_ABS32:
1357 COUNT_RELOC(RELOC_ABSOLUTE);
1358 MARK(rel->r_offset);
1359 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1360 reloc, sym_addr, sym_name);
1361 *((unsigned*)reloc) += sym_addr;
1362 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001363 case R_ARM_REL32:
1364 COUNT_RELOC(RELOC_RELATIVE);
1365 MARK(rel->r_offset);
1366 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1367 reloc, sym_addr, rel->r_offset, sym_name);
1368 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1369 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001370#elif defined(ANDROID_X86_LINKER)
1371 case R_386_JUMP_SLOT:
1372 COUNT_RELOC(RELOC_ABSOLUTE);
1373 MARK(rel->r_offset);
1374 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1375 reloc, sym_addr, sym_name);
1376 *((unsigned*)reloc) = sym_addr;
1377 break;
1378 case R_386_GLOB_DAT:
1379 COUNT_RELOC(RELOC_ABSOLUTE);
1380 MARK(rel->r_offset);
1381 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1382 reloc, sym_addr, sym_name);
1383 *((unsigned*)reloc) = sym_addr;
1384 break;
1385#endif /* ANDROID_*_LINKER */
1386
1387#if defined(ANDROID_ARM_LINKER)
1388 case R_ARM_RELATIVE:
1389#elif defined(ANDROID_X86_LINKER)
1390 case R_386_RELATIVE:
1391#endif /* ANDROID_*_LINKER */
1392 COUNT_RELOC(RELOC_RELATIVE);
1393 MARK(rel->r_offset);
1394 if(sym){
Erik Gillingd00d23a2009-07-22 17:06:11 -07001395 DL_ERR("%5d odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001396 return -1;
1397 }
1398 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1399 reloc, si->base);
1400 *((unsigned*)reloc) += si->base;
1401 break;
1402
1403#if defined(ANDROID_X86_LINKER)
1404 case R_386_32:
1405 COUNT_RELOC(RELOC_RELATIVE);
1406 MARK(rel->r_offset);
1407
1408 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1409 reloc, sym_addr, sym_name);
1410 *((unsigned *)reloc) += (unsigned)sym_addr;
1411 break;
1412
1413 case R_386_PC32:
1414 COUNT_RELOC(RELOC_RELATIVE);
1415 MARK(rel->r_offset);
1416 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1417 "+%08x (%08x - %08x) %s\n", pid, reloc,
1418 (sym_addr - reloc), sym_addr, reloc, sym_name);
1419 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1420 break;
1421#endif /* ANDROID_X86_LINKER */
1422
1423#ifdef ANDROID_ARM_LINKER
1424 case R_ARM_COPY:
1425 COUNT_RELOC(RELOC_COPY);
1426 MARK(rel->r_offset);
1427 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1428 reloc, s->st_size, sym_addr, sym_name);
1429 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1430 break;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001431 case R_ARM_NONE:
1432 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001433#endif /* ANDROID_ARM_LINKER */
1434
1435 default:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001436 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001437 pid, type, rel, (int) (rel - start));
1438 return -1;
1439 }
1440 rel++;
1441 }
1442 return 0;
1443}
1444
David 'Digit' Turner82156792009-05-18 14:37:41 +02001445/* Please read the "Initialization and Termination functions" functions.
1446 * of the linker design note in bionic/linker/README.TXT to understand
1447 * what the following code is doing.
1448 *
1449 * The important things to remember are:
1450 *
1451 * DT_PREINIT_ARRAY must be called first for executables, and should
1452 * not appear in shared libraries.
1453 *
1454 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1455 *
1456 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1457 *
1458 * DT_FINI_ARRAY must be parsed in reverse order.
1459 */
1460
1461static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001462{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001463 int n, inc = 1;
1464
1465 if (reverse) {
1466 ctor += (count-1);
1467 inc = -1;
1468 }
1469
1470 for(n = count; n > 0; n--) {
1471 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1472 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001473 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001474 void (*func)() = (void (*)()) *ctor;
1475 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001476 if(((int) func == 0) || ((int) func == -1)) continue;
1477 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1478 func();
1479 }
1480}
1481
David 'Digit' Turner16084162012-06-12 16:25:37 +02001482void soinfo_call_constructors(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001483{
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001484 if (si->constructors_called)
1485 return;
1486
Jesse Hallf5d16932012-01-30 15:39:57 -08001487 // Set this before actually calling the constructors, otherwise it doesn't
1488 // protect against recursive constructor calls. One simple example of
1489 // constructor recursion is the libc debug malloc, which is implemented in
1490 // libc_malloc_debug_leak.so:
1491 // 1. The program depends on libc, so libc's constructor is called here.
1492 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001493 // 3. dlopen() calls soinfo_call_constructors() with the newly created
Jesse Hallf5d16932012-01-30 15:39:57 -08001494 // soinfo for libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001495 // 4. The debug so depends on libc, so soinfo_call_constructors() is
Jesse Hallf5d16932012-01-30 15:39:57 -08001496 // called again with the libc soinfo. If it doesn't trigger the early-
1497 // out above, the libc constructor will be called again (recursively!).
1498 si->constructors_called = 1;
1499
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001500 if (si->flags & FLAG_EXE) {
1501 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1502 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1503 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001504 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001505 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1506 } else {
1507 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001508 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
Erik Gillingd00d23a2009-07-22 17:06:11 -07001509 " This is INVALID.", pid, si->name,
Dima Zavin2e855792009-05-20 18:28:09 -07001510 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001511 }
1512 }
1513
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001514 if (si->dynamic) {
1515 unsigned *d;
1516 for(d = si->dynamic; *d; d += 2) {
1517 if(d[0] == DT_NEEDED){
1518 soinfo* lsi = (soinfo *)d[1];
1519 if (!validate_soinfo(lsi)) {
1520 DL_ERR("%5d bad DT_NEEDED pointer in %s",
1521 pid, si->name);
1522 } else {
David 'Digit' Turner16084162012-06-12 16:25:37 +02001523 soinfo_call_constructors(lsi);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001524 }
1525 }
1526 }
1527 }
1528
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001529 if (si->init_func) {
1530 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1531 (unsigned)si->init_func, si->name);
1532 si->init_func();
1533 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1534 }
1535
1536 if (si->init_array) {
1537 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1538 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001539 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001540 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1541 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001542
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001543}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001544
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001545static void call_destructors(soinfo *si)
1546{
1547 if (si->fini_array) {
1548 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1549 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001550 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001551 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1552 }
1553
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001554 if (si->fini_func) {
1555 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1556 (unsigned)si->fini_func, si->name);
1557 si->fini_func();
1558 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1559 }
1560}
1561
1562/* Force any of the closed stdin, stdout and stderr to be associated with
1563 /dev/null. */
1564static int nullify_closed_stdio (void)
1565{
1566 int dev_null, i, status;
1567 int return_value = 0;
1568
David 'Digit' Turner16084162012-06-12 16:25:37 +02001569 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001570 if (dev_null < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001571 DL_ERR("Cannot open /dev/null.");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001572 return -1;
1573 }
1574 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1575
1576 /* If any of the stdio file descriptors is valid and not associated
1577 with /dev/null, dup /dev/null to it. */
1578 for (i = 0; i < 3; i++) {
1579 /* If it is /dev/null already, we are done. */
1580 if (i == dev_null)
1581 continue;
1582
1583 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1584 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1585 can be interrupted but we do this just to be safe. */
1586 do {
1587 status = fcntl(i, F_GETFL);
1588 } while (status < 0 && errno == EINTR);
1589
1590 /* If file is openned, we are good. */
1591 if (status >= 0)
1592 continue;
1593
1594 /* The only error we allow is that the file descriptor does not
1595 exist, in which case we dup /dev/null to it. */
1596 if (errno != EBADF) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001597 DL_ERR("nullify_stdio: unhandled error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001598 return_value = -1;
1599 continue;
1600 }
1601
1602 /* Try dupping /dev/null to this stdio file descriptor and
1603 repeat if there is a signal. Note that any errors in closing
1604 the stdio descriptor are lost. */
1605 do {
1606 status = dup2(dev_null, i);
1607 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001608
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001609 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001610 DL_ERR("nullify_stdio: dup2 error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001611 return_value = -1;
1612 continue;
1613 }
1614 }
1615
1616 /* If /dev/null is not one of the stdio file descriptors, close it. */
1617 if (dev_null > 2) {
1618 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001619 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001620 status = close(dev_null);
1621 } while (status < 0 && errno == EINTR);
1622
1623 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001624 DL_ERR("nullify_stdio: close error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001625 return_value = -1;
1626 }
1627 }
1628
1629 return return_value;
1630}
1631
David 'Digit' Turner16084162012-06-12 16:25:37 +02001632static int soinfo_link_image(soinfo *si, unsigned wr_offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001633{
1634 unsigned *d;
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001635 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001636 Elf32_Addr base = si->load_bias;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001637 Elf32_Phdr *phdr = si->phdr;
1638 int phnum = si->phnum;
1639
1640 INFO("[ %5d linking %s ]\n", pid, si->name);
1641 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1642 si->base, si->flags);
1643
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001644 /* Extract dynamic section */
1645 si->dynamic = phdr_table_get_dynamic_section(phdr, phnum, base);
1646 if (si->dynamic == NULL) {
1647 DL_ERR("%5d missing PT_DYNAMIC?!", pid);
1648 goto fail;
1649 } else {
1650 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1651 }
1652
1653#ifdef ANDROID_ARM_LINKER
1654 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1655 &si->ARM_exidx, &si->ARM_exidx_count);
1656#endif
1657
Nick Kralevich468319c2011-11-11 15:53:17 -08001658 if (si->flags & (FLAG_EXE | FLAG_LINKER)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001659 /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for
Ji-Hwan Lee75917c82012-05-25 22:36:00 +09001660 * linkage info if this is the executable or the linker itself.
Nick Kralevich468319c2011-11-11 15:53:17 -08001661 * If this was a dynamic lib, that would have been done at load time.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001662 *
1663 * TODO: It's unfortunate that small pieces of this are
1664 * repeated from the load_library routine. Refactor this just
1665 * slightly to reuse these bits.
1666 */
1667 si->size = 0;
1668 for(; phnum > 0; --phnum, ++phdr) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001669 if (phdr->p_type == PT_LOAD) {
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001670 /* For the executable, we use the si->size field only in
1671 dl_unwind_find_exidx(), so the meaning of si->size
Nick Kralevichd9ad6232011-10-20 14:57:56 -07001672 is not the size of the executable; it is the distance
1673 between the load location of the executable and the last
1674 address of the loadable part of the executable.
1675 We use the range [si->base, si->base + si->size) to
1676 determine whether a PC value falls within the executable
1677 section. Of course, if a value is between si->base and
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001678 (base + phdr->p_vaddr), it's not in the executable
Nick Kralevichd9ad6232011-10-20 14:57:56 -07001679 section, but a) we shouldn't be asking for such a value
1680 anyway, and b) if we have to provide an EXIDX for such a
1681 value, then the executable's EXIDX is probably the better
1682 choice.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001683 */
1684 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
1685 if (phdr->p_vaddr + phdr->p_memsz > si->size)
1686 si->size = phdr->p_vaddr + phdr->p_memsz;
1687 /* try to remember what range of addresses should be write
1688 * protected */
1689 if (!(phdr->p_flags & PF_W)) {
1690 unsigned _end;
1691
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001692 if (base + phdr->p_vaddr < si->wrprotect_start)
1693 si->wrprotect_start = base + phdr->p_vaddr;
David 'Digit' Turnera6545f42012-06-18 11:15:54 +02001694 _end = PAGE_END(base + phdr->p_vaddr + phdr->p_memsz);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001695 if (_end > si->wrprotect_end)
1696 si->wrprotect_end = _end;
Nick Kralevichd9ad6232011-10-20 14:57:56 -07001697 /* Make the section writable just in case we'll have to
1698 * write to it during relocation (i.e. text segment).
1699 * However, we will remember what range of addresses
1700 * should be write protected.
1701 */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001702 mprotect((void *) (base + phdr->p_vaddr),
Nick Kralevichd9ad6232011-10-20 14:57:56 -07001703 phdr->p_memsz,
1704 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001705 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001706 } else if (phdr->p_type == PT_GNU_RELRO) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001707 if ((base + phdr->p_vaddr >= si->base + si->size)
1708 || ((base + phdr->p_vaddr + phdr->p_memsz) > si->base + si->size)
1709 || ((base + phdr->p_vaddr + phdr->p_memsz) < si->base)) {
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001710 DL_ERR("%d invalid GNU_RELRO in '%s' "
1711 "p_vaddr=0x%08x p_memsz=0x%08x", pid, si->name,
1712 phdr->p_vaddr, phdr->p_memsz);
1713 goto fail;
1714 }
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001715 si->gnu_relro_start = (Elf32_Addr) (base + phdr->p_vaddr);
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001716 si->gnu_relro_len = (unsigned) phdr->p_memsz;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001717 }
1718 }
1719 }
1720
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001721 /* extract useful information from dynamic section */
1722 for(d = si->dynamic; *d; d++){
1723 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1724 switch(*d++){
1725 case DT_HASH:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001726 si->nbucket = ((unsigned *) (base + *d))[0];
1727 si->nchain = ((unsigned *) (base + *d))[1];
1728 si->bucket = (unsigned *) (base + *d + 8);
1729 si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001730 break;
1731 case DT_STRTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001732 si->strtab = (const char *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001733 break;
1734 case DT_SYMTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001735 si->symtab = (Elf32_Sym *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001736 break;
1737 case DT_PLTREL:
1738 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001739 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001740 goto fail;
1741 }
1742 break;
1743 case DT_JMPREL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001744 si->plt_rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001745 break;
1746 case DT_PLTRELSZ:
1747 si->plt_rel_count = *d / 8;
1748 break;
1749 case DT_REL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001750 si->rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001751 break;
1752 case DT_RELSZ:
1753 si->rel_count = *d / 8;
1754 break;
1755 case DT_PLTGOT:
1756 /* Save this in case we decide to do lazy binding. We don't yet. */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001757 si->plt_got = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001758 break;
1759 case DT_DEBUG:
1760 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1761 *d = (int) &_r_debug;
1762 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001763 case DT_RELA:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001764 DL_ERR("%5d DT_RELA not supported", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001765 goto fail;
1766 case DT_INIT:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001767 si->init_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001768 DEBUG("%5d %s constructors (init func) found at %p\n",
1769 pid, si->name, si->init_func);
1770 break;
1771 case DT_FINI:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001772 si->fini_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001773 DEBUG("%5d %s destructors (fini func) found at %p\n",
1774 pid, si->name, si->fini_func);
1775 break;
1776 case DT_INIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001777 si->init_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001778 DEBUG("%5d %s constructors (init_array) found at %p\n",
1779 pid, si->name, si->init_array);
1780 break;
1781 case DT_INIT_ARRAYSZ:
1782 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1783 break;
1784 case DT_FINI_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001785 si->fini_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001786 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1787 pid, si->name, si->fini_array);
1788 break;
1789 case DT_FINI_ARRAYSZ:
1790 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1791 break;
1792 case DT_PREINIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001793 si->preinit_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001794 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1795 pid, si->name, si->preinit_array);
1796 break;
1797 case DT_PREINIT_ARRAYSZ:
1798 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1799 break;
1800 case DT_TEXTREL:
1801 /* TODO: make use of this. */
1802 /* this means that we might have to write into where the text
1803 * segment was loaded during relocation... Do something with
1804 * it.
1805 */
1806 DEBUG("%5d Text segment should be writable during relocation.\n",
1807 pid);
1808 break;
1809 }
1810 }
1811
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001812 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001813 pid, si->base, si->strtab, si->symtab);
1814
1815 if((si->strtab == 0) || (si->symtab == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001816 DL_ERR("%5d missing essential tables", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001817 goto fail;
1818 }
1819
Matt Fischer4fd42c12009-12-31 12:09:10 -06001820 /* if this is the main executable, then load all of the preloads now */
1821 if(si->flags & FLAG_EXE) {
1822 int i;
1823 memset(preloads, 0, sizeof(preloads));
1824 for(i = 0; ldpreload_names[i] != NULL; i++) {
1825 soinfo *lsi = find_library(ldpreload_names[i]);
1826 if(lsi == 0) {
1827 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
1828 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
1829 pid, ldpreload_names[i], si->name, tmp_err_buf);
1830 goto fail;
1831 }
1832 lsi->refcount++;
1833 preloads[i] = lsi;
1834 }
1835 }
1836
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001837 for(d = si->dynamic; *d; d += 2) {
1838 if(d[0] == DT_NEEDED){
1839 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001840 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001841 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001842 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Erik Gillingd00d23a2009-07-22 17:06:11 -07001843 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
Dima Zavin03531952009-05-29 17:30:25 -07001844 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001845 goto fail;
1846 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001847 /* Save the soinfo of the loaded DT_NEEDED library in the payload
1848 of the DT_NEEDED entry itself, so that we can retrieve the
1849 soinfo directly later from the dynamic segment. This is a hack,
1850 but it allows us to map from DT_NEEDED to soinfo efficiently
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001851 later on when we resolve relocations, trying to look up a symbol
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001852 with dlsym().
1853 */
1854 d[1] = (unsigned)lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001855 lsi->refcount++;
1856 }
1857 }
1858
1859 if(si->plt_rel) {
1860 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
David 'Digit' Turner16084162012-06-12 16:25:37 +02001861 if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001862 goto fail;
1863 }
1864 if(si->rel) {
1865 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
David 'Digit' Turner16084162012-06-12 16:25:37 +02001866 if(soinfo_relocate(si, si->rel, si->rel_count))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001867 goto fail;
1868 }
1869
1870 si->flags |= FLAG_LINKED;
1871 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1872
1873#if 0
1874 /* This is the way that the old dynamic linker did protection of
1875 * non-writable areas. It would scan section headers and find where
1876 * .text ended (rather where .data/.bss began) and assume that this is
1877 * the upper range of the non-writable area. This is too coarse,
1878 * and is kept here for reference until we fully move away from single
1879 * segment elf objects. See the code in get_wr_offset (also #if'd 0)
1880 * that made this possible.
1881 */
1882 if(wr_offset < 0xffffffff){
1883 mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC);
1884 }
1885#else
1886 /* TODO: Verify that this does the right thing in all cases, as it
1887 * presently probably does not. It is possible that an ELF image will
1888 * come with multiple read-only segments. What we ought to do is scan
1889 * the program headers again and mprotect all the read-only segments.
1890 * To prevent re-scanning the program header, we would have to build a
1891 * list of loadable segments in si, and then scan that instead. */
1892 if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) {
1893 mprotect((void *)si->wrprotect_start,
1894 si->wrprotect_end - si->wrprotect_start,
1895 PROT_READ | PROT_EXEC);
1896 }
1897#endif
1898
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001899 if (si->gnu_relro_start != 0 && si->gnu_relro_len != 0) {
David 'Digit' Turnera6545f42012-06-18 11:15:54 +02001900 Elf32_Addr start = PAGE_START(si->gnu_relro_start);
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001901 unsigned len = (si->gnu_relro_start - start) + si->gnu_relro_len;
1902 if (mprotect((void *) start, len, PROT_READ) < 0) {
1903 DL_ERR("%5d GNU_RELRO mprotect of library '%s' failed: %d (%s)\n",
1904 pid, si->name, errno, strerror(errno));
1905 goto fail;
1906 }
1907 }
1908
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001909 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1910 stdout and stderr to close a security hole described in:
1911
1912 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1913
1914 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001915 if (program_is_setuid)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001916 nullify_closed_stdio ();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001917 notify_gdb_of_load(si);
1918 return 0;
1919
1920fail:
Dima Zavina7161902010-08-17 15:56:40 -07001921 ERROR("failed to link %s\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001922 si->flags |= FLAG_ERROR;
1923 return -1;
1924}
1925
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001926static void parse_library_path(const char *path, char *delim)
David Bartleybc3a5c22009-06-02 18:27:28 -07001927{
1928 size_t len;
1929 char *ldpaths_bufp = ldpaths_buf;
1930 int i = 0;
1931
1932 len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf));
1933
1934 while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) {
1935 if (*ldpaths[i] != '\0')
1936 ++i;
1937 }
1938
1939 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1940 * last char isn't '\0' (i.e. not originally a delim). */
1941 if (i > 0 && len >= sizeof(ldpaths_buf) &&
1942 ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') {
1943 ldpaths[i - 1] = NULL;
1944 } else {
1945 ldpaths[i] = NULL;
1946 }
1947}
1948
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001949static void parse_preloads(const char *path, char *delim)
Matt Fischer4fd42c12009-12-31 12:09:10 -06001950{
1951 size_t len;
1952 char *ldpreloads_bufp = ldpreloads_buf;
1953 int i = 0;
1954
1955 len = strlcpy(ldpreloads_buf, path, sizeof(ldpreloads_buf));
1956
1957 while (i < LDPRELOAD_MAX && (ldpreload_names[i] = strsep(&ldpreloads_bufp, delim))) {
1958 if (*ldpreload_names[i] != '\0') {
1959 ++i;
1960 }
1961 }
1962
1963 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1964 * last char isn't '\0' (i.e. not originally a delim). */
1965 if (i > 0 && len >= sizeof(ldpreloads_buf) &&
1966 ldpreloads_buf[sizeof(ldpreloads_buf) - 2] != '\0') {
1967 ldpreload_names[i - 1] = NULL;
1968 } else {
1969 ldpreload_names[i] = NULL;
1970 }
1971}
1972
Nick Kralevich468319c2011-11-11 15:53:17 -08001973/*
1974 * This code is called after the linker has linked itself and
1975 * fixed it's own GOT. It is safe to make references to externs
1976 * and other non-local data at this point.
1977 */
1978static unsigned __linker_init_post_relocation(unsigned **elfdata)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001979{
1980 static soinfo linker_soinfo;
1981
1982 int argc = (int) *elfdata;
1983 char **argv = (char**) (elfdata + 1);
Stephen Smalleybb440552012-01-20 10:59:15 -08001984 unsigned *vecs = (unsigned*) (argv + argc + 1);
1985 unsigned *v;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001986 soinfo *si;
1987 struct link_map * map;
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001988 const char *ldpath_env = NULL;
1989 const char *ldpreload_env = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001990
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001991 /* NOTE: we store the elfdata pointer on a special location
1992 * of the temporary TLS area in order to pass it to
1993 * the C Library's runtime initializer.
1994 *
1995 * The initializer must clear the slot and reset the TLS
1996 * to point to a different location to ensure that no other
1997 * shared library constructor can access it.
1998 */
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001999 __libc_init_tls(elfdata);
2000
2001 pid = getpid();
2002
2003#if TIMING
2004 struct timeval t0, t1;
2005 gettimeofday(&t0, 0);
2006#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002007
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002008 /* Initialize environment functions, and get to the ELF aux vectors table */
2009 vecs = linker_env_init(vecs);
2010
Stephen Smalley861b42a2012-01-13 07:48:11 -05002011 /* Check auxv for AT_SECURE first to see if program is setuid, setgid,
2012 has file caps, or caused a SELinux/AppArmor domain transition. */
2013 for (v = vecs; v[0]; v += 2) {
2014 if (v[0] == AT_SECURE) {
2015 /* kernel told us whether to enable secure mode */
2016 program_is_setuid = v[1];
2017 goto sanitize;
2018 }
2019 }
2020
2021 /* Kernel did not provide AT_SECURE - fall back on legacy test. */
2022 program_is_setuid = (getuid() != geteuid()) || (getgid() != getegid());
2023
2024sanitize:
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002025 /* Sanitize environment if we're loading a setuid program */
2026 if (program_is_setuid)
2027 linker_env_secure();
2028
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002029 debugger_init();
2030
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002031 /* Get a few environment variables */
2032 {
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07002033#if LINKER_DEBUG
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002034 const char* env;
2035 env = linker_env_get("DEBUG"); /* XXX: TODO: Change to LD_DEBUG */
2036 if (env)
2037 debug_verbosity = atoi(env);
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07002038#endif
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002039
2040 /* Normally, these are cleaned by linker_env_secure, but the test
2041 * against program_is_setuid doesn't cost us anything */
2042 if (!program_is_setuid) {
2043 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2044 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002045 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002046 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002047
2048 INFO("[ android linker & debugger ]\n");
2049 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
2050
David 'Digit' Turner16084162012-06-12 16:25:37 +02002051 si = soinfo_alloc(argv[0]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002052 if(si == 0) {
2053 exit(-1);
2054 }
2055
2056 /* bootstrap the link map, the main exe always needs to be first */
2057 si->flags |= FLAG_EXE;
2058 map = &(si->linkmap);
2059
2060 map->l_addr = 0;
2061 map->l_name = argv[0];
2062 map->l_prev = NULL;
2063 map->l_next = NULL;
2064
2065 _r_debug.r_map = map;
2066 r_debug_tail = map;
2067
2068 /* gdb expects the linker to be in the debug shared object list,
2069 * and we need to make sure that the reported load address is zero.
2070 * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
David 'Digit' Turner16084162012-06-12 16:25:37 +02002071 * is. Don't use soinfo_alloc(), because the linker shouldn't
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002072 * be on the soinfo list.
2073 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002074 strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002075 linker_soinfo.flags = 0;
2076 linker_soinfo.base = 0; // This is the important part; must be zero.
2077 insert_soinfo_into_debug_map(&linker_soinfo);
2078
2079 /* extract information passed from the kernel */
2080 while(vecs[0] != 0){
2081 switch(vecs[0]){
2082 case AT_PHDR:
2083 si->phdr = (Elf32_Phdr*) vecs[1];
2084 break;
2085 case AT_PHNUM:
2086 si->phnum = (int) vecs[1];
2087 break;
2088 case AT_ENTRY:
2089 si->entry = vecs[1];
2090 break;
2091 }
2092 vecs += 2;
2093 }
2094
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002095 /* Compute the value of si->base. We can't rely on the fact that
2096 * the first entry is the PHDR because this will not be true
2097 * for certain executables (e.g. some in the NDK unit test suite)
2098 */
2099 int nn;
2100 si->base = 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002101 si->load_bias = 0;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002102 for ( nn = 0; nn < si->phnum; nn++ ) {
2103 if (si->phdr[nn].p_type == PT_PHDR) {
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002104 si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
2105 si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002106 break;
2107 }
2108 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002109 si->dynamic = (unsigned *)-1;
2110 si->wrprotect_start = 0xffffffff;
2111 si->wrprotect_end = 0;
David 'Digit' Turner67748092010-07-21 16:18:21 -07002112 si->refcount = 1;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002113 si->gnu_relro_start = 0;
2114 si->gnu_relro_len = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002115
David Bartleybc3a5c22009-06-02 18:27:28 -07002116 /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002117 if (ldpath_env)
David Bartleybc3a5c22009-06-02 18:27:28 -07002118 parse_library_path(ldpath_env, ":");
2119
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002120 if (ldpreload_env) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06002121 parse_preloads(ldpreload_env, " :");
2122 }
2123
David 'Digit' Turner16084162012-06-12 16:25:37 +02002124 if(soinfo_link_image(si, 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -07002125 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
2126 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2127 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002128 exit(-1);
2129 }
2130
David 'Digit' Turner16084162012-06-12 16:25:37 +02002131 soinfo_call_constructors(si);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002132
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07002133#if ALLOW_SYMBOLS_FROM_MAIN
2134 /* Set somain after we've loaded all the libraries in order to prevent
2135 * linking of symbols back to the main image, which is not set up at that
2136 * point yet.
2137 */
2138 somain = si;
2139#endif
2140
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002141#if TIMING
2142 gettimeofday(&t1,NULL);
2143 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
2144 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2145 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
2146 ));
2147#endif
2148#if STATS
2149 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
2150 linker_stats.reloc[RELOC_ABSOLUTE],
2151 linker_stats.reloc[RELOC_RELATIVE],
2152 linker_stats.reloc[RELOC_COPY],
2153 linker_stats.reloc[RELOC_SYMBOL]);
2154#endif
2155#if COUNT_PAGES
2156 {
2157 unsigned n;
2158 unsigned i;
2159 unsigned count = 0;
2160 for(n = 0; n < 4096; n++){
2161 if(bitmask[n]){
2162 unsigned x = bitmask[n];
2163 for(i = 0; i < 8; i++){
2164 if(x & 1) count++;
2165 x >>= 1;
2166 }
2167 }
2168 }
2169 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
2170 }
2171#endif
2172
2173#if TIMING || STATS || COUNT_PAGES
2174 fflush(stdout);
2175#endif
2176
2177 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
2178 si->entry);
2179 return si->entry;
2180}
Nick Kralevich468319c2011-11-11 15:53:17 -08002181
2182/*
2183 * Find the value of AT_BASE passed to us by the kernel. This is the load
2184 * location of the linker.
2185 */
2186static unsigned find_linker_base(unsigned **elfdata) {
2187 int argc = (int) *elfdata;
2188 char **argv = (char**) (elfdata + 1);
2189 unsigned *vecs = (unsigned*) (argv + argc + 1);
2190 while (vecs[0] != 0) {
2191 vecs++;
2192 }
2193
2194 /* The end of the environment block is marked by two NULL pointers */
2195 vecs++;
2196
2197 while(vecs[0]) {
2198 if (vecs[0] == AT_BASE) {
2199 return vecs[1];
2200 }
2201 vecs += 2;
2202 }
2203
2204 return 0; // should never happen
2205}
2206
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002207/* Compute the load-bias of an existing executable. This shall only
2208 * be used to compute the load bias of an executable or shared library
2209 * that was loaded by the kernel itself.
2210 *
2211 * Input:
2212 * elf -> address of ELF header, assumed to be at the start of the file.
2213 * Return:
2214 * load bias, i.e. add the value of any p_vaddr in the file to get
2215 * the corresponding address in memory.
2216 */
2217static Elf32_Addr
2218get_elf_exec_load_bias(const Elf32_Ehdr* elf)
2219{
2220 Elf32_Addr offset = elf->e_phoff;
2221 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
2222 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
2223 const Elf32_Phdr* phdr;
2224
2225 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
2226 if (phdr->p_type == PT_LOAD) {
2227 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
2228 }
2229 }
2230 return 0;
2231}
2232
Nick Kralevich468319c2011-11-11 15:53:17 -08002233/*
2234 * This is the entry point for the linker, called from begin.S. This
2235 * method is responsible for fixing the linker's own relocations, and
2236 * then calling __linker_init_post_relocation().
2237 *
2238 * Because this method is called before the linker has fixed it's own
2239 * relocations, any attempt to reference an extern variable, extern
2240 * function, or other GOT reference will generate a segfault.
2241 */
2242unsigned __linker_init(unsigned **elfdata) {
2243 unsigned linker_addr = find_linker_base(elfdata);
2244 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2245 Elf32_Phdr *phdr =
2246 (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2247
2248 soinfo linker_so;
2249 memset(&linker_so, 0, sizeof(soinfo));
2250
2251 linker_so.base = linker_addr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002252 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Nick Kralevich468319c2011-11-11 15:53:17 -08002253 linker_so.dynamic = (unsigned *) -1;
2254 linker_so.phdr = phdr;
2255 linker_so.phnum = elf_hdr->e_phnum;
2256 linker_so.flags |= FLAG_LINKER;
2257 linker_so.wrprotect_start = 0xffffffff;
2258 linker_so.wrprotect_end = 0;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002259 linker_so.gnu_relro_start = 0;
2260 linker_so.gnu_relro_len = 0;
Nick Kralevich468319c2011-11-11 15:53:17 -08002261
David 'Digit' Turner16084162012-06-12 16:25:37 +02002262 if (soinfo_link_image(&linker_so, 0)) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002263 // It would be nice to print an error message, but if the linker
2264 // can't link itself, there's no guarantee that we'll be able to
2265 // call write() (because it involves a GOT reference).
2266 //
2267 // This situation should never occur unless the linker itself
2268 // is corrupt.
2269 exit(-1);
2270 }
2271
2272 // We have successfully fixed our own relocations. It's safe to run
2273 // the main part of the linker now.
2274 return __linker_init_post_relocation(elfdata);
2275}