blob: 1bf017f6b0a2e4fe2166f97a5c2a1c3003fd6835 [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
129#ifndef PT_ARM_EXIDX
130#define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */
131#endif
132
Dima Zavin2e855792009-05-20 18:28:09 -0700133#define HOODLUM(name, ret, ...) \
134 ret name __VA_ARGS__ \
135 { \
136 char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \
137 write(2, errstr, sizeof(errstr)); \
138 abort(); \
139 }
140HOODLUM(malloc, void *, (size_t size));
141HOODLUM(free, void, (void *ptr));
142HOODLUM(realloc, void *, (void *ptr, size_t size));
143HOODLUM(calloc, void *, (size_t cnt, size_t size));
144
Dima Zavin03531952009-05-29 17:30:25 -0700145static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700146static char __linker_dl_err_buf[768];
147#define DL_ERR(fmt, x...) \
148 do { \
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800149 format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), \
Dima Zavin2e855792009-05-20 18:28:09 -0700150 "%s[%d]: " fmt, __func__, __LINE__, ##x); \
Erik Gillingd00d23a2009-07-22 17:06:11 -0700151 ERROR(fmt "\n", ##x); \
Dima Zavin2e855792009-05-20 18:28:09 -0700152 } while(0)
153
154const char *linker_get_error(void)
155{
156 return (const char *)&__linker_dl_err_buf[0];
157}
158
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159/*
160 * This function is an empty stub where GDB locates a breakpoint to get notified
161 * about linker activity.
162 */
163extern void __attribute__((noinline)) rtld_db_dlactivity(void);
164
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
166 RT_CONSISTENT, 0};
167static struct link_map *r_debug_tail = 0;
168
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700169static pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170
171static void insert_soinfo_into_debug_map(soinfo * info)
172{
173 struct link_map * map;
174
175 /* Copy the necessary fields into the debug structure.
176 */
177 map = &(info->linkmap);
178 map->l_addr = info->base;
179 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800180 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181
182 /* Stick the new library at the end of the list.
183 * gdb tends to care more about libc than it does
184 * about leaf libraries, and ordering it this way
185 * reduces the back-and-forth over the wire.
186 */
187 if (r_debug_tail) {
188 r_debug_tail->l_next = map;
189 map->l_prev = r_debug_tail;
190 map->l_next = 0;
191 } else {
192 _r_debug.r_map = map;
193 map->l_prev = 0;
194 map->l_next = 0;
195 }
196 r_debug_tail = map;
197}
198
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700199static void remove_soinfo_from_debug_map(soinfo * info)
200{
201 struct link_map * map = &(info->linkmap);
202
203 if (r_debug_tail == map)
204 r_debug_tail = map->l_prev;
205
206 if (map->l_prev) map->l_prev->l_next = map->l_next;
207 if (map->l_next) map->l_next->l_prev = map->l_prev;
208}
209
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800210void notify_gdb_of_load(soinfo * info)
211{
212 if (info->flags & FLAG_EXE) {
213 // GDB already knows about the main executable
214 return;
215 }
216
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700217 pthread_mutex_lock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218
219 _r_debug.r_state = RT_ADD;
220 rtld_db_dlactivity();
221
222 insert_soinfo_into_debug_map(info);
223
224 _r_debug.r_state = RT_CONSISTENT;
225 rtld_db_dlactivity();
226
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700227 pthread_mutex_unlock(&_r_debug_lock);
228}
229
230void notify_gdb_of_unload(soinfo * info)
231{
232 if (info->flags & FLAG_EXE) {
233 // GDB already knows about the main executable
234 return;
235 }
236
237 pthread_mutex_lock(&_r_debug_lock);
238
239 _r_debug.r_state = RT_DELETE;
240 rtld_db_dlactivity();
241
242 remove_soinfo_from_debug_map(info);
243
244 _r_debug.r_state = RT_CONSISTENT;
245 rtld_db_dlactivity();
246
247 pthread_mutex_unlock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248}
249
250void notify_gdb_of_libraries()
251{
252 _r_debug.r_state = RT_ADD;
253 rtld_db_dlactivity();
254 _r_debug.r_state = RT_CONSISTENT;
255 rtld_db_dlactivity();
256}
257
David 'Digit' Turner16084162012-06-12 16:25:37 +0200258static soinfo *soinfo_alloc(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800259{
260 soinfo *si;
261
262 if(strlen(name) >= SOINFO_NAME_LEN) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700263 DL_ERR("%5d library name %s too long", pid, name);
Doug Kwan94304352009-10-23 18:11:40 -0700264 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800265 }
266
David 'Digit' Turner16084162012-06-12 16:25:37 +0200267 /* The freelist is populated when we call soinfo_free(), which in turn is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800268 done only by dlclose(), which is not likely to be used.
269 */
270 if (!freelist) {
271 if(socount == SO_MAX) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700272 DL_ERR("%5d too many libraries when loading %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273 return NULL;
274 }
275 freelist = sopool + socount++;
276 freelist->next = NULL;
277 }
278
279 si = freelist;
280 freelist = freelist->next;
281
282 /* Make sure we get a clean block of soinfo */
283 memset(si, 0, sizeof(soinfo));
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100284 strlcpy((char*) si->name, name, sizeof(si->name));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285 sonext->next = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286 si->next = NULL;
287 si->refcount = 0;
288 sonext = si;
289
290 TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
291 return si;
292}
293
David 'Digit' Turner16084162012-06-12 16:25:37 +0200294static void soinfo_free(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800295{
296 soinfo *prev = NULL, *trav;
297
298 TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
299
300 for(trav = solist; trav != NULL; trav = trav->next){
301 if (trav == si)
302 break;
303 prev = trav;
304 }
305 if (trav == NULL) {
306 /* si was not ni solist */
Erik Gillingd00d23a2009-07-22 17:06:11 -0700307 DL_ERR("%5d name %s is not in solist!", pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308 return;
309 }
310
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100311 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800312 always the static libdl_info.
313 */
314 prev->next = si->next;
315 if (si == sonext) sonext = prev;
316 si->next = freelist;
317 freelist = si;
318}
319
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800320const char *addr_to_name(unsigned addr)
321{
322 soinfo *si;
323
324 for(si = solist; si != 0; si = si->next){
325 if((addr >= si->base) && (addr < (si->base + si->size))) {
326 return si->name;
327 }
328 }
329
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800330 return "";
331}
332
333/* For a given PC, find the .so that it belongs to.
334 * Returns the base address of the .ARM.exidx section
335 * for that .so, and the number of 8-byte entries
336 * in that section (via *pcount).
337 *
338 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
339 *
340 * This function is exposed via dlfcn.c and libdl.so.
341 */
342#ifdef ANDROID_ARM_LINKER
343_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
344{
345 soinfo *si;
346 unsigned addr = (unsigned)pc;
347
Nick Kralevich468319c2011-11-11 15:53:17 -0800348 for (si = solist; si != 0; si = si->next){
349 if ((addr >= si->base) && (addr < (si->base + si->size))) {
350 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900351 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800352 }
353 }
354 *pcount = 0;
355 return NULL;
356}
David 'Digit' Turner70b16682012-01-30 17:17:58 +0100357#elif defined(ANDROID_X86_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800358/* Here, we only have to provide a callback to iterate across all the
359 * loaded libraries. gcc_eh does the rest. */
360int
361dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
362 void *data)
363{
364 soinfo *si;
365 struct dl_phdr_info dl_info;
366 int rv = 0;
367
368 for (si = solist; si != NULL; si = si->next) {
369 dl_info.dlpi_addr = si->linkmap.l_addr;
370 dl_info.dlpi_name = si->linkmap.l_name;
371 dl_info.dlpi_phdr = si->phdr;
372 dl_info.dlpi_phnum = si->phnum;
373 rv = cb(&dl_info, sizeof (struct dl_phdr_info), data);
374 if (rv != 0)
375 break;
376 }
377 return rv;
378}
379#endif
380
David 'Digit' Turner16084162012-06-12 16:25:37 +0200381static Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800382{
383 Elf32_Sym *s;
384 Elf32_Sym *symtab = si->symtab;
385 const char *strtab = si->strtab;
386 unsigned n;
387
388 TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
389 name, si->name, si->base, hash, hash % si->nbucket);
390 n = hash % si->nbucket;
391
392 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
393 s = symtab + n;
394 if(strcmp(strtab + s->st_name, name)) continue;
395
Doug Kwane8238072009-10-26 12:05:23 -0700396 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800397 switch(ELF32_ST_BIND(s->st_info)){
398 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700399 case STB_WEAK:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800400 /* no section == undefined */
401 if(s->st_shndx == 0) continue;
402
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800403 TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
404 name, si->name, s->st_value, s->st_size);
405 return s;
406 }
407 }
408
Doug Kwan94304352009-10-23 18:11:40 -0700409 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800410}
411
412static unsigned elfhash(const char *_name)
413{
414 const unsigned char *name = (const unsigned char *) _name;
415 unsigned h = 0, g;
416
417 while(*name) {
418 h = (h << 4) + *name++;
419 g = h & 0xf0000000;
420 h ^= g;
421 h ^= g >> 24;
422 }
423 return h;
424}
425
426static Elf32_Sym *
David 'Digit' Turner16084162012-06-12 16:25:37 +0200427soinfo_do_lookup(soinfo *si, const char *name, unsigned *base, Elf32_Addr *offset)
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700428{
Doug Kwan94304352009-10-23 18:11:40 -0700429 unsigned elf_hash = elfhash(name);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700430 Elf32_Sym *s;
431 unsigned *d;
432 soinfo *lsi = si;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600433 int i;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700434
Nick Kralevich468319c2011-11-11 15:53:17 -0800435 /* Look for symbols in the local scope (the object who is
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700436 * searching). This happens with C++ templates on i386 for some
Doug Kwane8238072009-10-26 12:05:23 -0700437 * reason.
438 *
439 * Notes on weak symbols:
440 * The ELF specs are ambigious about treatment of weak definitions in
441 * dynamic linking. Some systems return the first definition found
442 * and some the first non-weak definition. This is system dependent.
443 * Here we return the first definition found for simplicity. */
Nick Kralevich468319c2011-11-11 15:53:17 -0800444
David 'Digit' Turner16084162012-06-12 16:25:37 +0200445 s = soinfo_elf_lookup(si, elf_hash, name);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700446 if(s != NULL)
447 goto done;
448
Matt Fischer4fd42c12009-12-31 12:09:10 -0600449 /* Next, look for it in the preloads list */
450 for(i = 0; preloads[i] != NULL; i++) {
451 lsi = preloads[i];
David 'Digit' Turner16084162012-06-12 16:25:37 +0200452 s = soinfo_elf_lookup(lsi, elf_hash, name);
Matt Fischer4fd42c12009-12-31 12:09:10 -0600453 if(s != NULL)
454 goto done;
455 }
456
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700457 for(d = si->dynamic; *d; d += 2) {
458 if(d[0] == DT_NEEDED){
459 lsi = (soinfo *)d[1];
460 if (!validate_soinfo(lsi)) {
461 DL_ERR("%5d bad DT_NEEDED pointer in %s",
462 pid, si->name);
Doug Kwan94304352009-10-23 18:11:40 -0700463 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700464 }
465
466 DEBUG("%5d %s: looking up %s in %s\n",
467 pid, si->name, name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200468 s = soinfo_elf_lookup(lsi, elf_hash, name);
Min-su, Kim3cab22c2010-01-19 10:05:33 +0900469 if ((s != NULL) && (s->st_shndx != SHN_UNDEF))
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700470 goto done;
471 }
472 }
473
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -0700474#if ALLOW_SYMBOLS_FROM_MAIN
475 /* If we are resolving relocations while dlopen()ing a library, it's OK for
476 * the library to resolve a symbol that's defined in the executable itself,
477 * although this is rare and is generally a bad idea.
478 */
479 if (somain) {
480 lsi = somain;
481 DEBUG("%5d %s: looking up %s in executable %s\n",
482 pid, si->name, name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200483 s = soinfo_elf_lookup(lsi, elf_hash, name);
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -0700484 }
485#endif
486
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700487done:
488 if(s != NULL) {
489 TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900490 "found in %s, base = 0x%08x, load offset = 0x%08x\n",
491 pid, si->name, name, s->st_value,
492 lsi->name, lsi->base, lsi->load_offset);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700493 *base = lsi->base;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900494 *offset = lsi->load_offset;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700495 return s;
496 }
497
Doug Kwan94304352009-10-23 18:11:40 -0700498 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700499}
500
501/* This is used by dl_sym(). It performs symbol lookup only within the
502 specified soinfo object and not in any of its dependencies.
503 */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200504Elf32_Sym *soinfo_lookup(soinfo *si, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800505{
David 'Digit' Turner16084162012-06-12 16:25:37 +0200506 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800507}
508
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700509/* This is used by dl_sym(). It performs a global symbol lookup.
510 */
Matt Fischer1698d9e2009-12-31 12:17:56 -0600511Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800512{
Doug Kwan94304352009-10-23 18:11:40 -0700513 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800514 Elf32_Sym *s = NULL;
515 soinfo *si;
516
Matt Fischer1698d9e2009-12-31 12:17:56 -0600517 if(start == NULL) {
518 start = solist;
519 }
520
521 for(si = start; (s == NULL) && (si != NULL); si = si->next)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800522 {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700523 if(si->flags & FLAG_ERROR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800524 continue;
David 'Digit' Turner16084162012-06-12 16:25:37 +0200525 s = soinfo_elf_lookup(si, elf_hash, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800526 if (s != NULL) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700527 *found = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800528 break;
529 }
530 }
531
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700532 if(s != NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800533 TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
534 "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
535 return s;
536 }
537
Doug Kwan94304352009-10-23 18:11:40 -0700538 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800539}
540
Mathias Agopianbda5da02011-09-27 22:30:19 -0700541soinfo *find_containing_library(const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600542{
543 soinfo *si;
544
545 for(si = solist; si != NULL; si = si->next)
546 {
547 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
548 return si;
549 }
550 }
551
552 return NULL;
553}
554
David 'Digit' Turner16084162012-06-12 16:25:37 +0200555Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600556{
557 unsigned int i;
558 unsigned soaddr = (unsigned)addr - si->base;
559
560 /* Search the library's symbol table for any defined symbol which
561 * contains this address */
562 for(i=0; i<si->nchain; i++) {
563 Elf32_Sym *sym = &si->symtab[i];
564
565 if(sym->st_shndx != SHN_UNDEF &&
566 soaddr >= sym->st_value &&
567 soaddr < sym->st_value + sym->st_size) {
568 return sym;
569 }
570 }
571
572 return NULL;
573}
574
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800575#if 0
576static void dump(soinfo *si)
577{
578 Elf32_Sym *s = si->symtab;
579 unsigned n;
580
581 for(n = 0; n < si->nchain; n++) {
582 TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
583 s->st_info, s->st_shndx, s->st_value, s->st_size,
584 si->strtab + s->st_name);
585 s++;
586 }
587}
588#endif
589
David 'Digit' Turner16084162012-06-12 16:25:37 +0200590static const char * const sopaths[] = {
Brian Swetlandfedbcde2010-09-19 03:39:13 -0700591 "/vendor/lib",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800592 "/system/lib",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800593 0
594};
595
596static int _open_lib(const char *name)
597{
598 int fd;
599 struct stat filestat;
600
601 if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
David 'Digit' Turner16084162012-06-12 16:25:37 +0200602 if ((fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY))) >= 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800603 return fd;
604 }
605
606 return -1;
607}
608
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800609static int open_library(const char *name)
610{
611 int fd;
612 char buf[512];
David 'Digit' Turner16084162012-06-12 16:25:37 +0200613 const char * const*path;
David Bartleybc3a5c22009-06-02 18:27:28 -0700614 int n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800615
616 TRACE("[ %5d opening %s ]\n", pid, name);
617
618 if(name == 0) return -1;
619 if(strlen(name) > 256) return -1;
620
621 if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
622 return fd;
623
David Bartleybc3a5c22009-06-02 18:27:28 -0700624 for (path = ldpaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800625 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700626 if (n < 0 || n >= (int)sizeof(buf)) {
627 WARN("Ignoring very long library path: %s/%s\n", *path, name);
628 continue;
629 }
630 if ((fd = _open_lib(buf)) >= 0)
631 return fd;
632 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800633 for (path = sopaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800634 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700635 if (n < 0 || n >= (int)sizeof(buf)) {
636 WARN("Ignoring very long library path: %s/%s\n", *path, name);
637 continue;
638 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800639 if ((fd = _open_lib(buf)) >= 0)
640 return fd;
641 }
642
643 return -1;
644}
645
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800646typedef struct {
647 long mmap_addr;
648 char tag[4]; /* 'P', 'R', 'E', ' ' */
649} prelink_info_t;
650
651/* Returns the requested base address if the library is prelinked,
652 * and 0 otherwise. */
653static unsigned long
654is_prelinked(int fd, const char *name)
655{
656 off_t sz;
657 prelink_info_t info;
658
659 sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
660 if (sz < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700661 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800662 return 0;
663 }
664
David 'Digit' Turner16084162012-06-12 16:25:37 +0200665 if (TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)) != sizeof(info))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800666 WARN("Could not read prelink_info_t structure for `%s`\n", name);
667 return 0;
668 }
669
David 'Digit' Turner16084162012-06-12 16:25:37 +0200670 if (memcmp(info.tag, "PRE ", 4)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800671 WARN("`%s` is not a prelinked library\n", name);
672 return 0;
673 }
674
675 return (unsigned long)info.mmap_addr;
676}
677
David 'Digit' Turner16084162012-06-12 16:25:37 +0200678/* verify_elf_header
679 * Verifies the content of an ELF header.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800680 *
681 * Args:
682 *
683 * Returns:
684 * 0 on success
685 * -1 if no valid ELF object is found @ base.
686 */
687static int
David 'Digit' Turner16084162012-06-12 16:25:37 +0200688verify_elf_header(const Elf32_Ehdr* hdr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800689{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800690 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
691 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
692 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
693 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
694
695 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800696#ifdef ANDROID_ARM_LINKER
697 if (hdr->e_machine != EM_ARM) return -1;
698#elif defined(ANDROID_X86_LINKER)
699 if (hdr->e_machine != EM_386) return -1;
700#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800701 return 0;
702}
703
704
Nick Kralevich66259862012-03-16 13:06:12 -0700705/* reserve_mem_region
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800706 *
707 * This function reserves a chunk of memory to be used for mapping in
Nick Kralevich66259862012-03-16 13:06:12 -0700708 * a prelinked shared library. We reserve the entire memory region here, and
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800709 * then the rest of the linker will relocate the individual loadable
710 * segments into the correct locations within this memory range.
711 *
712 * Args:
Nick Kralevich66259862012-03-16 13:06:12 -0700713 * si->base: The requested base of the allocation.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800714 * si->size: The size of the allocation.
715 *
716 * Returns:
717 * -1 on failure, and 0 on success. On success, si->base will contain
718 * the virtual address at which the library will be mapped.
719 */
720
David 'Digit' Turner16084162012-06-12 16:25:37 +0200721static int soinfo_reserve_mem_region(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800722{
Nick Kralevich66259862012-03-16 13:06:12 -0700723 void *base = mmap((void *)si->base, si->size, PROT_NONE,
Chris Dearmandb4bce02011-03-10 10:48:14 -0800724 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800725 if (base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700726 DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700727 "as requested, will try general pool: %d (%s)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800728 pid, (si->base ? "" : "non-"), si->name, si->base,
729 errno, strerror(errno));
730 return -1;
731 } else if (base != (void *)si->base) {
Dima Zavin2e855792009-05-20 18:28:09 -0700732 DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700733 "not at 0x%08x", pid, (si->base ? "" : "non-"),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800734 si->name, (unsigned)base, si->base);
735 munmap(base, si->size);
736 return -1;
737 }
738 return 0;
739}
740
David 'Digit' Turner16084162012-06-12 16:25:37 +0200741static int soinfo_alloc_mem_region(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800742{
743 if (si->base) {
744 /* Attempt to mmap a prelinked library. */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200745 return soinfo_reserve_mem_region(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800746 }
747
Shih-wei Liao48527c32011-07-17 12:32:43 -0700748 /* This is not a prelinked library, so we use the kernel's default
749 allocator.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800750 */
Shih-wei Liao48527c32011-07-17 12:32:43 -0700751
Nick Kralevich66259862012-03-16 13:06:12 -0700752 void *base = mmap(NULL, si->size, PROT_NONE,
Shih-wei Liao48527c32011-07-17 12:32:43 -0700753 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
754 if (base == MAP_FAILED) {
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200755 DL_ERR("%5d mmap of library '%s' failed (%d bytes): %d (%s)\n",
756 pid, si->name, si->size,
Shih-wei Liao48527c32011-07-17 12:32:43 -0700757 errno, strerror(errno));
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200758 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800759 }
Shih-wei Liao48527c32011-07-17 12:32:43 -0700760 si->base = (unsigned) base;
761 PRINT("%5d mapped library '%s' to %08x via kernel allocator.\n",
762 pid, si->name, si->base);
763 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800764}
765
766#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
767#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
768 MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
769 MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
770/* load_segments
771 *
772 * This function loads all the loadable (PT_LOAD) segments into memory
773 * at their appropriate memory offsets off the base address.
774 *
775 * Args:
776 * fd: Open file descriptor to the library to load.
777 * header: Pointer to a header page that contains the ELF header.
778 * This is needed since we haven't mapped in the real file yet.
779 * si: ptr to soinfo struct describing the shared object.
780 *
781 * Returns:
782 * 0 on success, -1 on failure.
783 */
784static int
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200785soinfo_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 -0800786{
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200787 const Elf32_Phdr *phdr = phdr_table;
788 const Elf32_Phdr *phdr0 = 0; /* program header for the first LOAD segment */
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900789 Elf32_Addr base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800790 int cnt;
791 unsigned len;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800792 Elf32_Addr tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800793 unsigned char *pbase;
794 unsigned char *extra_base;
795 unsigned extra_len;
796 unsigned total_sz = 0;
797
798 si->wrprotect_start = 0xffffffff;
799 si->wrprotect_end = 0;
800
801 TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n",
802 pid, si->name, (unsigned)si->base);
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900803
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200804 for (cnt = 0; cnt < phdr_count; ++cnt, ++phdr) {
David 'Digit' Turner16084162012-06-12 16:25:37 +0200805
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900806 if (phdr->p_type == PT_LOAD) {
807 phdr0 = phdr;
808 /*
809 * ELF specification section 2-2.
810 *
811 * PT_LOAD: "Loadable segment entries in the program
812 * header table appear in ascending order, sorted on the
813 * p_vaddr member."
814 */
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200815 si->load_offset = PAGE_START(phdr->p_vaddr);
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900816 break;
817 }
818 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200819
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900820 /* "base" might wrap around UINT32_MAX. */
821 base = (Elf32_Addr)(si->base - si->load_offset);
822
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800823 /* Now go through all the PT_LOAD segments and map them into memory
824 * at the appropriate locations. */
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200825 phdr = phdr_table;
826 for (cnt = 0; cnt < phdr_count; ++cnt, ++phdr) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800827 if (phdr->p_type == PT_LOAD) {
828 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
829 /* we want to map in the segment on a page boundary */
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200830 tmp = base + PAGE_START(phdr->p_vaddr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800831 /* add the # of bytes we masked off above to the total length. */
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200832 len = phdr->p_filesz + PAGE_OFFSET(phdr->p_vaddr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800833
834 TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x "
835 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name,
836 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800837 pbase = mmap((void *)tmp, len, PFLAGS_TO_PROT(phdr->p_flags),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800838 MAP_PRIVATE | MAP_FIXED, fd,
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200839 PAGE_START(phdr->p_offset));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800840 if (pbase == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700841 DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700842 "p_vaddr=0x%08x p_offset=0x%08x", pid, si->name,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800843 (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
844 goto fail;
845 }
846
847 /* If 'len' didn't end on page boundary, and it's a writable
848 * segment, zero-fill the rest. */
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200849 if (PAGE_OFFSET(len) > 0 && (phdr->p_flags & PF_W) != 0)
850 memset((void *)(pbase + len), 0, PAGE_SIZE - PAGE_OFFSET(len));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800851
852 /* Check to see if we need to extend the map for this segment to
853 * cover the diff between filesz and memsz (i.e. for bss).
854 *
855 * base _+---------------------+ page boundary
856 * . .
857 * | |
858 * . .
859 * pbase _+---------------------+ page boundary
860 * | |
861 * . .
862 * base + p_vaddr _| |
863 * . \ \ .
864 * . | filesz | .
865 * pbase + len _| / | |
866 * <0 pad> . . .
867 * extra_base _+------------|--------+ page boundary
868 * / . . .
869 * | . . .
870 * | +------------|--------+ page boundary
871 * extra_len-> | | | |
872 * | . | memsz .
873 * | . | .
874 * \ _| / |
875 * . .
876 * | |
877 * _+---------------------+ page boundary
878 */
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200879 tmp = (Elf32_Addr)PAGE_END((unsigned)pbase + len);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800880 if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) {
881 extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp;
882 TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x "
883 "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len);
884 /* map in the extra page(s) as anonymous into the range.
885 * This is probably not necessary as we already mapped in
886 * the entire region previously, but we just want to be
887 * sure. This will also set the right flags on the region
888 * (though we can probably accomplish the same thing with
889 * mprotect).
890 */
891 extra_base = mmap((void *)tmp, extra_len,
892 PFLAGS_TO_PROT(phdr->p_flags),
893 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
894 -1, 0);
895 if (extra_base == MAP_FAILED) {
Dima Zavin2e855792009-05-20 18:28:09 -0700896 DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x"
Erik Gillingd00d23a2009-07-22 17:06:11 -0700897 " (0x%08x) ]", pid, si->name, (unsigned)tmp,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800898 extra_len);
899 goto fail;
900 }
901 /* TODO: Check if we need to memset-0 this region.
902 * Anonymous mappings are zero-filled copy-on-writes, so we
903 * shouldn't need to. */
904 TRACE("[ %5d - Segment from '%s' extended @ 0x%08x "
905 "(0x%08x)\n", pid, si->name, (unsigned)extra_base,
906 extra_len);
907 }
908 /* set the len here to show the full extent of the segment we
909 * just loaded, mostly for debugging */
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200910 len = PAGE_END((unsigned)base + phdr->p_vaddr + phdr->p_memsz) - (unsigned)pbase;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800911 TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x "
912 "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
913 (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset);
914 total_sz += len;
915 /* Make the section writable just in case we'll have to write to
916 * it during relocation (i.e. text segment). However, we will
917 * remember what range of addresses should be write protected.
918 *
919 */
920 if (!(phdr->p_flags & PF_W)) {
921 if ((unsigned)pbase < si->wrprotect_start)
922 si->wrprotect_start = (unsigned)pbase;
923 if (((unsigned)pbase + len) > si->wrprotect_end)
924 si->wrprotect_end = (unsigned)pbase + len;
925 mprotect(pbase, len,
926 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
927 }
928 } else if (phdr->p_type == PT_DYNAMIC) {
929 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
930 /* this segment contains the dynamic linking information */
931 si->dynamic = (unsigned *)(base + phdr->p_vaddr);
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800932 } else if (phdr->p_type == PT_GNU_RELRO) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900933 if (((base + phdr->p_vaddr) >= si->base + si->size)
934 || ((base + phdr->p_vaddr + phdr->p_memsz) > si->base + si->size)
935 || ((base + phdr->p_vaddr + phdr->p_memsz) < si->base)) {
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800936 DL_ERR("%d invalid GNU_RELRO in '%s' "
937 "p_vaddr=0x%08x p_memsz=0x%08x", pid, si->name,
938 phdr->p_vaddr, phdr->p_memsz);
939 goto fail;
940 }
941 si->gnu_relro_start = (Elf32_Addr) (base + phdr->p_vaddr);
942 si->gnu_relro_len = (unsigned) phdr->p_memsz;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800943 } else {
944#ifdef ANDROID_ARM_LINKER
945 if (phdr->p_type == PT_ARM_EXIDX) {
946 DEBUG_DUMP_PHDR(phdr, "PT_ARM_EXIDX", pid);
947 /* exidx entries (used for stack unwinding) are 8 bytes each.
948 */
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900949 si->ARM_exidx = (unsigned *)(base + phdr->p_vaddr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800950 si->ARM_exidx_count = phdr->p_memsz / 8;
951 }
952#endif
953 }
954
955 }
956
957 /* Sanity check */
958 if (total_sz > si->size) {
Dima Zavin2e855792009-05-20 18:28:09 -0700959 DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
Erik Gillingd00d23a2009-07-22 17:06:11 -0700960 "greater than what was allocated (0x%08x). THIS IS BAD!",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800961 pid, total_sz, si->name, si->size);
962 goto fail;
963 }
964
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900965 /* vaddr : Real virtual address in process' address space.
966 * p_vaddr : Relative virtual address in ELF object
967 * p_offset : File offset in ELF object
968 *
969 * vaddr p_vaddr p_offset
970 * ----- ------------ --------
971 * base 0
David 'Digit' Turnera6545f42012-06-18 11:15:54 +0200972 * si->base PAGE_START(phdr0->p_vaddr)
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900973 * phdr0->p_vaddr phdr0->p_offset
974 * phdr ehdr->e_phoff
975 */
976 si->phdr = (Elf32_Phdr *)(base + phdr0->p_vaddr +
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200977 ehdr_phoff - phdr0->p_offset);
978 si->phnum = phdr_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900979
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800980 TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. "
981 "Total memory footprint: 0x%08x bytes ]\n", pid, si->name,
982 (unsigned)si->base, si->size);
983 return 0;
984
985fail:
986 /* We can just blindly unmap the entire region even though some things
987 * were mapped in originally with anonymous and others could have been
988 * been mapped in from the file before we failed. The kernel will unmap
989 * all the pages in the range, irrespective of how they got there.
990 */
991 munmap((void *)si->base, si->size);
992 si->flags |= FLAG_ERROR;
993 return -1;
994}
995
996/* TODO: Implement this to take care of the fact that Android ARM
997 * ELF objects shove everything into a single loadable segment that has the
998 * write bit set. wr_offset is then used to set non-(data|bss) pages to be
999 * non-writable.
1000 */
1001#if 0
1002static unsigned
1003get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr)
1004{
1005 Elf32_Shdr *shdr_start;
1006 Elf32_Shdr *shdr;
1007 int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr);
1008 int cnt;
1009 unsigned wr_offset = 0xffffffff;
1010
1011 shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd,
David 'Digit' Turnera6545f42012-06-18 11:15:54 +02001012 PAGE_START(ehdr->e_shoff));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001013 if (shdr_start == MAP_FAILED) {
1014 WARN("%5d - Could not read section header info from '%s'. Will not "
1015 "not be able to determine write-protect offset.\n", pid, name);
1016 return (unsigned)-1;
1017 }
1018
1019 for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) {
1020 if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) &&
1021 (shdr->sh_addr < wr_offset)) {
1022 wr_offset = shdr->sh_addr;
1023 }
1024 }
1025
1026 munmap(shdr_start, shdr_sz);
1027 return wr_offset;
1028}
1029#endif
1030
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001031
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001032static soinfo *
1033load_library(const char *name)
1034{
1035 int fd = open_library(name);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001036 int ret, cnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001037 unsigned ext_sz;
1038 unsigned req_base;
Erik Gillingfde86422009-07-28 20:28:19 -07001039 const char *bname;
Ji-Hwan Lee75917c82012-05-25 22:36:00 +09001040 struct stat sb;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001041 soinfo *si = NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001042 Elf32_Ehdr header[1];
1043 int phdr_count;
1044 void* phdr_mmap = NULL;
1045 Elf32_Addr phdr_size;
1046 const Elf32_Phdr* phdr_table;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001047
Ji-Hwan Lee75917c82012-05-25 22:36:00 +09001048 if (fd == -1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001049 DL_ERR("Library '%s' not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001050 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -07001051 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001052
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001053 /* Read the ELF header first */
1054 ret = TEMP_FAILURE_RETRY(read(fd, (void*)header, sizeof(header)));
1055 if (ret < 0) {
1056 DL_ERR("%5d can't read file %s: %s", pid, name, strerror(errno));
1057 goto fail;
1058 }
1059 if (ret != (int)sizeof(header)) {
1060 DL_ERR("%5d too small to be an ELF executable: %s", pid, name);
1061 goto fail;
1062 }
1063 if (verify_elf_header(header) < 0) {
1064 DL_ERR("%5d not a valid ELF executable: %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001065 goto fail;
1066 }
1067
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001068 /* Then read the program header table */
1069 ret = phdr_table_load(fd, header->e_phoff, header->e_phnum,
1070 &phdr_mmap, &phdr_size, &phdr_table);
1071 if (ret < 0) {
1072 DL_ERR("%5d can't load program header table: %s: %s", pid,
1073 name, strerror(errno));
1074 goto fail;
1075 }
1076 phdr_count = header->e_phnum;
1077
1078 /* Get the load extents and the prelinked load address, if any */
1079 ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
1080 if (ext_sz == 0) {
1081 DL_ERR("%5d no loadable segments in file: %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001082 goto fail;
1083 }
1084
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001085 req_base = (unsigned) is_prelinked(fd, name);
1086 if (req_base == (unsigned)-1) {
1087 DL_ERR("%5d can't read end of library: %s: %s", pid, name,
1088 strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001089 goto fail;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001090 }
1091 if (req_base != 0) {
1092 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
1093 pid, name, req_base);
1094 } else {
1095 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
1096 }
David 'Digit' Turner16084162012-06-12 16:25:37 +02001097
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001098 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
1099 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
1100
1101 /* Now configure the soinfo struct where we'll store all of our data
1102 * for the ELF object. If the loading fails, we waste the entry, but
1103 * same thing would happen if we failed during linking. Configuring the
1104 * soinfo struct here is a lot more convenient.
1105 */
Erik Gillingfde86422009-07-28 20:28:19 -07001106 bname = strrchr(name, '/');
David 'Digit' Turner16084162012-06-12 16:25:37 +02001107 si = soinfo_alloc(bname ? bname + 1 : name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001108 if (si == NULL)
1109 goto fail;
1110
1111 /* Carve out a chunk of memory where we will map in the individual
1112 * segments */
1113 si->base = req_base;
1114 si->size = ext_sz;
1115 si->flags = 0;
1116 si->entry = 0;
1117 si->dynamic = (unsigned *)-1;
David 'Digit' Turner16084162012-06-12 16:25:37 +02001118 if (soinfo_alloc_mem_region(si) < 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001119 goto fail;
1120
1121 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
1122 pid, name, (void *)si->base, (unsigned) ext_sz);
1123
1124 /* Now actually load the library's segments into right places in memory */
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001125 if (soinfo_load_segments(si, fd, phdr_table, phdr_count, header->e_phoff) < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001126 goto fail;
1127 }
1128
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001129 phdr_table_unload(phdr_mmap, phdr_size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001130 close(fd);
1131 return si;
1132
1133fail:
David 'Digit' Turner16084162012-06-12 16:25:37 +02001134 if (si) soinfo_free(si);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +02001135 if (phdr_mmap != NULL) {
1136 phdr_table_unload(phdr_mmap, phdr_size);
1137 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001138 close(fd);
1139 return NULL;
1140}
1141
1142static soinfo *
1143init_library(soinfo *si)
1144{
1145 unsigned wr_offset = 0xffffffff;
1146
1147 /* At this point we know that whatever is loaded @ base is a valid ELF
1148 * shared library whose segments are properly mapped in. */
1149 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
1150 pid, si->base, si->size, si->name);
1151
David 'Digit' Turner16084162012-06-12 16:25:37 +02001152 if(soinfo_link_image(si, wr_offset)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001153 /* We failed to link. However, we can only restore libbase
1154 ** if no additional libraries have moved it since we updated it.
1155 */
1156 munmap((void *)si->base, si->size);
1157 return NULL;
1158 }
1159
1160 return si;
1161}
1162
1163soinfo *find_library(const char *name)
1164{
1165 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001166 const char *bname;
1167
1168#if ALLOW_SYMBOLS_FROM_MAIN
1169 if (name == NULL)
1170 return somain;
1171#else
1172 if (name == NULL)
1173 return NULL;
1174#endif
1175
1176 bname = strrchr(name, '/');
Erik Gillingfde86422009-07-28 20:28:19 -07001177 bname = bname ? bname + 1 : name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001178
1179 for(si = solist; si != 0; si = si->next){
Erik Gillingfde86422009-07-28 20:28:19 -07001180 if(!strcmp(bname, si->name)) {
Erik Gilling30eb4022009-08-13 16:05:30 -07001181 if(si->flags & FLAG_ERROR) {
1182 DL_ERR("%5d '%s' failed to load previously", pid, bname);
1183 return NULL;
1184 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001185 if(si->flags & FLAG_LINKED) return si;
Erik Gillingd00d23a2009-07-22 17:06:11 -07001186 DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name);
Dima Zavin2e855792009-05-20 18:28:09 -07001187 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001188 }
1189 }
1190
1191 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
1192 si = load_library(name);
1193 if(si == NULL)
1194 return NULL;
1195 return init_library(si);
1196}
1197
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001198/* TODO:
1199 * notify gdb of unload
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001200 * for non-prelinked libraries, find a way to decrement libbase
1201 */
1202static void call_destructors(soinfo *si);
David 'Digit' Turner16084162012-06-12 16:25:37 +02001203unsigned soinfo_unload(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001204{
1205 unsigned *d;
1206 if (si->refcount == 1) {
1207 TRACE("%5d unloading '%s'\n", pid, si->name);
1208 call_destructors(si);
1209
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001210 /*
1211 * Make sure that we undo the PT_GNU_RELRO protections we added
David 'Digit' Turner16084162012-06-12 16:25:37 +02001212 * in soinfo_link_image. This is needed to undo the DT_NEEDED hack below.
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001213 */
1214 if ((si->gnu_relro_start != 0) && (si->gnu_relro_len != 0)) {
David 'Digit' Turnera6545f42012-06-18 11:15:54 +02001215 Elf32_Addr start = PAGE_START(si->gnu_relro_start);
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001216 unsigned len = (si->gnu_relro_start - start) + si->gnu_relro_len;
1217 if (mprotect((void *) start, len, PROT_READ | PROT_WRITE) < 0)
1218 DL_ERR("%5d %s: could not undo GNU_RELRO protections. "
1219 "Expect a crash soon. errno=%d (%s)",
1220 pid, si->name, errno, strerror(errno));
1221
1222 }
1223
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001224 for(d = si->dynamic; *d; d += 2) {
1225 if(d[0] == DT_NEEDED){
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001226 soinfo *lsi = (soinfo *)d[1];
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001227
1228 // The next line will segfault if the we don't undo the
1229 // PT_GNU_RELRO protections (see comments above and in
David 'Digit' Turner16084162012-06-12 16:25:37 +02001230 // soinfo_link_image().
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001231 d[1] = 0;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001232
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001233 if (validate_soinfo(lsi)) {
1234 TRACE("%5d %s needs to unload %s\n", pid,
1235 si->name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +02001236 soinfo_unload(lsi);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001237 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001238 else
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001239 DL_ERR("%5d %s: could not unload dependent library",
1240 pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001241 }
1242 }
1243
1244 munmap((char *)si->base, si->size);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001245 notify_gdb_of_unload(si);
David 'Digit' Turner16084162012-06-12 16:25:37 +02001246 soinfo_free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001247 si->refcount = 0;
1248 }
1249 else {
1250 si->refcount--;
1251 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
1252 pid, si->name, si->refcount);
1253 }
1254 return si->refcount;
1255}
1256
1257/* TODO: don't use unsigned for addrs below. It works, but is not
1258 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1259 * long.
1260 */
David 'Digit' Turner16084162012-06-12 16:25:37 +02001261static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001262{
1263 Elf32_Sym *symtab = si->symtab;
1264 const char *strtab = si->strtab;
1265 Elf32_Sym *s;
1266 unsigned base;
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001267 Elf32_Addr offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001268 Elf32_Rel *start = rel;
1269 unsigned idx;
1270
1271 for (idx = 0; idx < count; ++idx) {
1272 unsigned type = ELF32_R_TYPE(rel->r_info);
1273 unsigned sym = ELF32_R_SYM(rel->r_info);
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001274 unsigned reloc = (unsigned)(rel->r_offset + si->base - si->load_offset);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001275 unsigned sym_addr = 0;
1276 char *sym_name = NULL;
1277
1278 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
1279 si->name, idx);
1280 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001281 sym_name = (char *)(strtab + symtab[sym].st_name);
David 'Digit' Turner16084162012-06-12 16:25:37 +02001282 s = soinfo_do_lookup(si, sym_name, &base, &offset);
Doug Kwane8238072009-10-26 12:05:23 -07001283 if(s == NULL) {
1284 /* We only allow an undefined symbol if this is a weak
1285 reference.. */
1286 s = &symtab[sym];
1287 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1288 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
1289 return -1;
1290 }
1291
1292 /* IHI0044C AAELF 4.5.1.1:
1293
1294 Libraries are not searched to resolve weak references.
1295 It is not an error for a weak reference to remain
1296 unsatisfied.
1297
1298 During linking, the value of an undefined weak reference is:
1299 - Zero if the relocation type is absolute
1300 - The address of the place if the relocation is pc-relative
1301 - The address of nominial base address if the relocation
1302 type is base-relative.
1303 */
1304
1305 switch (type) {
1306#if defined(ANDROID_ARM_LINKER)
1307 case R_ARM_JUMP_SLOT:
1308 case R_ARM_GLOB_DAT:
1309 case R_ARM_ABS32:
1310 case R_ARM_RELATIVE: /* Don't care. */
1311 case R_ARM_NONE: /* Don't care. */
1312#elif defined(ANDROID_X86_LINKER)
1313 case R_386_JUMP_SLOT:
1314 case R_386_GLOB_DAT:
1315 case R_386_32:
1316 case R_386_RELATIVE: /* Dont' care. */
1317#endif /* ANDROID_*_LINKER */
1318 /* sym_addr was initialized to be zero above or relocation
1319 code below does not care about value of sym_addr.
1320 No need to do anything. */
1321 break;
1322
1323#if defined(ANDROID_X86_LINKER)
1324 case R_386_PC32:
1325 sym_addr = reloc;
1326 break;
1327#endif /* ANDROID_X86_LINKER */
1328
1329#if defined(ANDROID_ARM_LINKER)
1330 case R_ARM_COPY:
1331 /* Fall through. Can't really copy if weak symbol is
1332 not found in run-time. */
1333#endif /* ANDROID_ARM_LINKER */
1334 default:
1335 DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n",
1336 pid, type, rel, (int) (rel - start));
1337 return -1;
1338 }
1339 } else {
1340 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001341#if 0
1342 if((base == 0) && (si->base != 0)){
1343 /* linking from libraries to main image is bad */
Erik Gillingd00d23a2009-07-22 17:06:11 -07001344 DL_ERR("%5d cannot locate '%s'...",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001345 pid, strtab + symtab[sym].st_name);
1346 return -1;
1347 }
1348#endif
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001349 sym_addr = (unsigned)(s->st_value + base - offset);
Doug Kwane8238072009-10-26 12:05:23 -07001350 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001351 COUNT_RELOC(RELOC_SYMBOL);
1352 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001353 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001354 }
1355
1356/* TODO: This is ugly. Split up the relocations by arch into
1357 * different files.
1358 */
1359 switch(type){
1360#if defined(ANDROID_ARM_LINKER)
1361 case R_ARM_JUMP_SLOT:
1362 COUNT_RELOC(RELOC_ABSOLUTE);
1363 MARK(rel->r_offset);
1364 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1365 reloc, sym_addr, sym_name);
1366 *((unsigned*)reloc) = sym_addr;
1367 break;
1368 case R_ARM_GLOB_DAT:
1369 COUNT_RELOC(RELOC_ABSOLUTE);
1370 MARK(rel->r_offset);
1371 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1372 reloc, sym_addr, sym_name);
1373 *((unsigned*)reloc) = sym_addr;
1374 break;
1375 case R_ARM_ABS32:
1376 COUNT_RELOC(RELOC_ABSOLUTE);
1377 MARK(rel->r_offset);
1378 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1379 reloc, sym_addr, sym_name);
1380 *((unsigned*)reloc) += sym_addr;
1381 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001382 case R_ARM_REL32:
1383 COUNT_RELOC(RELOC_RELATIVE);
1384 MARK(rel->r_offset);
1385 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1386 reloc, sym_addr, rel->r_offset, sym_name);
1387 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1388 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001389#elif defined(ANDROID_X86_LINKER)
1390 case R_386_JUMP_SLOT:
1391 COUNT_RELOC(RELOC_ABSOLUTE);
1392 MARK(rel->r_offset);
1393 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1394 reloc, sym_addr, sym_name);
1395 *((unsigned*)reloc) = sym_addr;
1396 break;
1397 case R_386_GLOB_DAT:
1398 COUNT_RELOC(RELOC_ABSOLUTE);
1399 MARK(rel->r_offset);
1400 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1401 reloc, sym_addr, sym_name);
1402 *((unsigned*)reloc) = sym_addr;
1403 break;
1404#endif /* ANDROID_*_LINKER */
1405
1406#if defined(ANDROID_ARM_LINKER)
1407 case R_ARM_RELATIVE:
1408#elif defined(ANDROID_X86_LINKER)
1409 case R_386_RELATIVE:
1410#endif /* ANDROID_*_LINKER */
1411 COUNT_RELOC(RELOC_RELATIVE);
1412 MARK(rel->r_offset);
1413 if(sym){
Erik Gillingd00d23a2009-07-22 17:06:11 -07001414 DL_ERR("%5d odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001415 return -1;
1416 }
1417 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1418 reloc, si->base);
1419 *((unsigned*)reloc) += si->base;
1420 break;
1421
1422#if defined(ANDROID_X86_LINKER)
1423 case R_386_32:
1424 COUNT_RELOC(RELOC_RELATIVE);
1425 MARK(rel->r_offset);
1426
1427 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1428 reloc, sym_addr, sym_name);
1429 *((unsigned *)reloc) += (unsigned)sym_addr;
1430 break;
1431
1432 case R_386_PC32:
1433 COUNT_RELOC(RELOC_RELATIVE);
1434 MARK(rel->r_offset);
1435 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1436 "+%08x (%08x - %08x) %s\n", pid, reloc,
1437 (sym_addr - reloc), sym_addr, reloc, sym_name);
1438 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1439 break;
1440#endif /* ANDROID_X86_LINKER */
1441
1442#ifdef ANDROID_ARM_LINKER
1443 case R_ARM_COPY:
1444 COUNT_RELOC(RELOC_COPY);
1445 MARK(rel->r_offset);
1446 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1447 reloc, s->st_size, sym_addr, sym_name);
1448 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1449 break;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001450 case R_ARM_NONE:
1451 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001452#endif /* ANDROID_ARM_LINKER */
1453
1454 default:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001455 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001456 pid, type, rel, (int) (rel - start));
1457 return -1;
1458 }
1459 rel++;
1460 }
1461 return 0;
1462}
1463
David 'Digit' Turner82156792009-05-18 14:37:41 +02001464/* Please read the "Initialization and Termination functions" functions.
1465 * of the linker design note in bionic/linker/README.TXT to understand
1466 * what the following code is doing.
1467 *
1468 * The important things to remember are:
1469 *
1470 * DT_PREINIT_ARRAY must be called first for executables, and should
1471 * not appear in shared libraries.
1472 *
1473 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1474 *
1475 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1476 *
1477 * DT_FINI_ARRAY must be parsed in reverse order.
1478 */
1479
1480static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001481{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001482 int n, inc = 1;
1483
1484 if (reverse) {
1485 ctor += (count-1);
1486 inc = -1;
1487 }
1488
1489 for(n = count; n > 0; n--) {
1490 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1491 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001492 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001493 void (*func)() = (void (*)()) *ctor;
1494 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001495 if(((int) func == 0) || ((int) func == -1)) continue;
1496 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1497 func();
1498 }
1499}
1500
David 'Digit' Turner16084162012-06-12 16:25:37 +02001501void soinfo_call_constructors(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001502{
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001503 if (si->constructors_called)
1504 return;
1505
Jesse Hallf5d16932012-01-30 15:39:57 -08001506 // Set this before actually calling the constructors, otherwise it doesn't
1507 // protect against recursive constructor calls. One simple example of
1508 // constructor recursion is the libc debug malloc, which is implemented in
1509 // libc_malloc_debug_leak.so:
1510 // 1. The program depends on libc, so libc's constructor is called here.
1511 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001512 // 3. dlopen() calls soinfo_call_constructors() with the newly created
Jesse Hallf5d16932012-01-30 15:39:57 -08001513 // soinfo for libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001514 // 4. The debug so depends on libc, so soinfo_call_constructors() is
Jesse Hallf5d16932012-01-30 15:39:57 -08001515 // called again with the libc soinfo. If it doesn't trigger the early-
1516 // out above, the libc constructor will be called again (recursively!).
1517 si->constructors_called = 1;
1518
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001519 if (si->flags & FLAG_EXE) {
1520 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1521 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1522 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001523 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001524 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1525 } else {
1526 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001527 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
Erik Gillingd00d23a2009-07-22 17:06:11 -07001528 " This is INVALID.", pid, si->name,
Dima Zavin2e855792009-05-20 18:28:09 -07001529 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001530 }
1531 }
1532
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001533 if (si->dynamic) {
1534 unsigned *d;
1535 for(d = si->dynamic; *d; d += 2) {
1536 if(d[0] == DT_NEEDED){
1537 soinfo* lsi = (soinfo *)d[1];
1538 if (!validate_soinfo(lsi)) {
1539 DL_ERR("%5d bad DT_NEEDED pointer in %s",
1540 pid, si->name);
1541 } else {
David 'Digit' Turner16084162012-06-12 16:25:37 +02001542 soinfo_call_constructors(lsi);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001543 }
1544 }
1545 }
1546 }
1547
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001548 if (si->init_func) {
1549 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1550 (unsigned)si->init_func, si->name);
1551 si->init_func();
1552 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1553 }
1554
1555 if (si->init_array) {
1556 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1557 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001558 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001559 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1560 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001561
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001562}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001563
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001564static void call_destructors(soinfo *si)
1565{
1566 if (si->fini_array) {
1567 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1568 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001569 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001570 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1571 }
1572
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001573 if (si->fini_func) {
1574 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1575 (unsigned)si->fini_func, si->name);
1576 si->fini_func();
1577 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1578 }
1579}
1580
1581/* Force any of the closed stdin, stdout and stderr to be associated with
1582 /dev/null. */
1583static int nullify_closed_stdio (void)
1584{
1585 int dev_null, i, status;
1586 int return_value = 0;
1587
David 'Digit' Turner16084162012-06-12 16:25:37 +02001588 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001589 if (dev_null < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001590 DL_ERR("Cannot open /dev/null.");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001591 return -1;
1592 }
1593 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1594
1595 /* If any of the stdio file descriptors is valid and not associated
1596 with /dev/null, dup /dev/null to it. */
1597 for (i = 0; i < 3; i++) {
1598 /* If it is /dev/null already, we are done. */
1599 if (i == dev_null)
1600 continue;
1601
1602 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1603 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1604 can be interrupted but we do this just to be safe. */
1605 do {
1606 status = fcntl(i, F_GETFL);
1607 } while (status < 0 && errno == EINTR);
1608
1609 /* If file is openned, we are good. */
1610 if (status >= 0)
1611 continue;
1612
1613 /* The only error we allow is that the file descriptor does not
1614 exist, in which case we dup /dev/null to it. */
1615 if (errno != EBADF) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001616 DL_ERR("nullify_stdio: unhandled error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001617 return_value = -1;
1618 continue;
1619 }
1620
1621 /* Try dupping /dev/null to this stdio file descriptor and
1622 repeat if there is a signal. Note that any errors in closing
1623 the stdio descriptor are lost. */
1624 do {
1625 status = dup2(dev_null, i);
1626 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001627
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001628 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001629 DL_ERR("nullify_stdio: dup2 error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001630 return_value = -1;
1631 continue;
1632 }
1633 }
1634
1635 /* If /dev/null is not one of the stdio file descriptors, close it. */
1636 if (dev_null > 2) {
1637 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001638 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001639 status = close(dev_null);
1640 } while (status < 0 && errno == EINTR);
1641
1642 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001643 DL_ERR("nullify_stdio: close error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001644 return_value = -1;
1645 }
1646 }
1647
1648 return return_value;
1649}
1650
David 'Digit' Turner16084162012-06-12 16:25:37 +02001651static int soinfo_link_image(soinfo *si, unsigned wr_offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652{
1653 unsigned *d;
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001654 /* "base" might wrap around UINT32_MAX. */
1655 Elf32_Addr base = si->base - si->load_offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001656 Elf32_Phdr *phdr = si->phdr;
1657 int phnum = si->phnum;
1658
1659 INFO("[ %5d linking %s ]\n", pid, si->name);
1660 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1661 si->base, si->flags);
1662
Nick Kralevich468319c2011-11-11 15:53:17 -08001663 if (si->flags & (FLAG_EXE | FLAG_LINKER)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001664 /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for
Ji-Hwan Lee75917c82012-05-25 22:36:00 +09001665 * linkage info if this is the executable or the linker itself.
Nick Kralevich468319c2011-11-11 15:53:17 -08001666 * If this was a dynamic lib, that would have been done at load time.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001667 *
1668 * TODO: It's unfortunate that small pieces of this are
1669 * repeated from the load_library routine. Refactor this just
1670 * slightly to reuse these bits.
1671 */
1672 si->size = 0;
1673 for(; phnum > 0; --phnum, ++phdr) {
1674#ifdef ANDROID_ARM_LINKER
1675 if(phdr->p_type == PT_ARM_EXIDX) {
1676 /* exidx entries (used for stack unwinding) are 8 bytes each.
1677 */
Evgeniy Stepanov20bc0612012-06-22 14:52:52 +04001678 si->ARM_exidx = (unsigned *)(base + phdr->p_vaddr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001679 si->ARM_exidx_count = phdr->p_memsz / 8;
1680 }
1681#endif
1682 if (phdr->p_type == PT_LOAD) {
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001683 /* For the executable, we use the si->size field only in
1684 dl_unwind_find_exidx(), so the meaning of si->size
Nick Kralevichd9ad6232011-10-20 14:57:56 -07001685 is not the size of the executable; it is the distance
1686 between the load location of the executable and the last
1687 address of the loadable part of the executable.
1688 We use the range [si->base, si->base + si->size) to
1689 determine whether a PC value falls within the executable
1690 section. Of course, if a value is between si->base and
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001691 (base + phdr->p_vaddr), it's not in the executable
Nick Kralevichd9ad6232011-10-20 14:57:56 -07001692 section, but a) we shouldn't be asking for such a value
1693 anyway, and b) if we have to provide an EXIDX for such a
1694 value, then the executable's EXIDX is probably the better
1695 choice.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001696 */
1697 DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
1698 if (phdr->p_vaddr + phdr->p_memsz > si->size)
1699 si->size = phdr->p_vaddr + phdr->p_memsz;
1700 /* try to remember what range of addresses should be write
1701 * protected */
1702 if (!(phdr->p_flags & PF_W)) {
1703 unsigned _end;
1704
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001705 if (base + phdr->p_vaddr < si->wrprotect_start)
1706 si->wrprotect_start = base + phdr->p_vaddr;
David 'Digit' Turnera6545f42012-06-18 11:15:54 +02001707 _end = PAGE_END(base + phdr->p_vaddr + phdr->p_memsz);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001708 if (_end > si->wrprotect_end)
1709 si->wrprotect_end = _end;
Nick Kralevichd9ad6232011-10-20 14:57:56 -07001710 /* Make the section writable just in case we'll have to
1711 * write to it during relocation (i.e. text segment).
1712 * However, we will remember what range of addresses
1713 * should be write protected.
1714 */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001715 mprotect((void *) (base + phdr->p_vaddr),
Nick Kralevichd9ad6232011-10-20 14:57:56 -07001716 phdr->p_memsz,
1717 PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001718 }
1719 } else if (phdr->p_type == PT_DYNAMIC) {
1720 if (si->dynamic != (unsigned *)-1) {
Dima Zavin2e855792009-05-20 18:28:09 -07001721 DL_ERR("%5d multiple PT_DYNAMIC segments found in '%s'. "
Erik Gillingd00d23a2009-07-22 17:06:11 -07001722 "Segment at 0x%08x, previously one found at 0x%08x",
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001723 pid, si->name, base + phdr->p_vaddr,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001724 (unsigned)si->dynamic);
1725 goto fail;
1726 }
1727 DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001728 si->dynamic = (unsigned *) (base + phdr->p_vaddr);
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001729 } else if (phdr->p_type == PT_GNU_RELRO) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001730 if ((base + phdr->p_vaddr >= si->base + si->size)
1731 || ((base + phdr->p_vaddr + phdr->p_memsz) > si->base + si->size)
1732 || ((base + phdr->p_vaddr + phdr->p_memsz) < si->base)) {
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001733 DL_ERR("%d invalid GNU_RELRO in '%s' "
1734 "p_vaddr=0x%08x p_memsz=0x%08x", pid, si->name,
1735 phdr->p_vaddr, phdr->p_memsz);
1736 goto fail;
1737 }
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001738 si->gnu_relro_start = (Elf32_Addr) (base + phdr->p_vaddr);
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001739 si->gnu_relro_len = (unsigned) phdr->p_memsz;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001740 }
1741 }
1742 }
1743
1744 if (si->dynamic == (unsigned *)-1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001745 DL_ERR("%5d missing PT_DYNAMIC?!", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001746 goto fail;
1747 }
1748
1749 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1750
1751 /* extract useful information from dynamic section */
1752 for(d = si->dynamic; *d; d++){
1753 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1754 switch(*d++){
1755 case DT_HASH:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001756 si->nbucket = ((unsigned *) (base + *d))[0];
1757 si->nchain = ((unsigned *) (base + *d))[1];
1758 si->bucket = (unsigned *) (base + *d + 8);
1759 si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001760 break;
1761 case DT_STRTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001762 si->strtab = (const char *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001763 break;
1764 case DT_SYMTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001765 si->symtab = (Elf32_Sym *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001766 break;
1767 case DT_PLTREL:
1768 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001769 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001770 goto fail;
1771 }
1772 break;
1773 case DT_JMPREL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001774 si->plt_rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001775 break;
1776 case DT_PLTRELSZ:
1777 si->plt_rel_count = *d / 8;
1778 break;
1779 case DT_REL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001780 si->rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001781 break;
1782 case DT_RELSZ:
1783 si->rel_count = *d / 8;
1784 break;
1785 case DT_PLTGOT:
1786 /* Save this in case we decide to do lazy binding. We don't yet. */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001787 si->plt_got = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001788 break;
1789 case DT_DEBUG:
1790 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1791 *d = (int) &_r_debug;
1792 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001793 case DT_RELA:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001794 DL_ERR("%5d DT_RELA not supported", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001795 goto fail;
1796 case DT_INIT:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001797 si->init_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001798 DEBUG("%5d %s constructors (init func) found at %p\n",
1799 pid, si->name, si->init_func);
1800 break;
1801 case DT_FINI:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001802 si->fini_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001803 DEBUG("%5d %s destructors (fini func) found at %p\n",
1804 pid, si->name, si->fini_func);
1805 break;
1806 case DT_INIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001807 si->init_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001808 DEBUG("%5d %s constructors (init_array) found at %p\n",
1809 pid, si->name, si->init_array);
1810 break;
1811 case DT_INIT_ARRAYSZ:
1812 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1813 break;
1814 case DT_FINI_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001815 si->fini_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001816 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1817 pid, si->name, si->fini_array);
1818 break;
1819 case DT_FINI_ARRAYSZ:
1820 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1821 break;
1822 case DT_PREINIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001823 si->preinit_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001824 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1825 pid, si->name, si->preinit_array);
1826 break;
1827 case DT_PREINIT_ARRAYSZ:
1828 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1829 break;
1830 case DT_TEXTREL:
1831 /* TODO: make use of this. */
1832 /* this means that we might have to write into where the text
1833 * segment was loaded during relocation... Do something with
1834 * it.
1835 */
1836 DEBUG("%5d Text segment should be writable during relocation.\n",
1837 pid);
1838 break;
1839 }
1840 }
1841
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001842 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001843 pid, si->base, si->strtab, si->symtab);
1844
1845 if((si->strtab == 0) || (si->symtab == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001846 DL_ERR("%5d missing essential tables", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001847 goto fail;
1848 }
1849
Matt Fischer4fd42c12009-12-31 12:09:10 -06001850 /* if this is the main executable, then load all of the preloads now */
1851 if(si->flags & FLAG_EXE) {
1852 int i;
1853 memset(preloads, 0, sizeof(preloads));
1854 for(i = 0; ldpreload_names[i] != NULL; i++) {
1855 soinfo *lsi = find_library(ldpreload_names[i]);
1856 if(lsi == 0) {
1857 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
1858 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
1859 pid, ldpreload_names[i], si->name, tmp_err_buf);
1860 goto fail;
1861 }
1862 lsi->refcount++;
1863 preloads[i] = lsi;
1864 }
1865 }
1866
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001867 for(d = si->dynamic; *d; d += 2) {
1868 if(d[0] == DT_NEEDED){
1869 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001870 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001871 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001872 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Erik Gillingd00d23a2009-07-22 17:06:11 -07001873 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
Dima Zavin03531952009-05-29 17:30:25 -07001874 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001875 goto fail;
1876 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001877 /* Save the soinfo of the loaded DT_NEEDED library in the payload
1878 of the DT_NEEDED entry itself, so that we can retrieve the
1879 soinfo directly later from the dynamic segment. This is a hack,
1880 but it allows us to map from DT_NEEDED to soinfo efficiently
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001881 later on when we resolve relocations, trying to look up a symbol
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001882 with dlsym().
1883 */
1884 d[1] = (unsigned)lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001885 lsi->refcount++;
1886 }
1887 }
1888
1889 if(si->plt_rel) {
1890 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
David 'Digit' Turner16084162012-06-12 16:25:37 +02001891 if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001892 goto fail;
1893 }
1894 if(si->rel) {
1895 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
David 'Digit' Turner16084162012-06-12 16:25:37 +02001896 if(soinfo_relocate(si, si->rel, si->rel_count))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001897 goto fail;
1898 }
1899
1900 si->flags |= FLAG_LINKED;
1901 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1902
1903#if 0
1904 /* This is the way that the old dynamic linker did protection of
1905 * non-writable areas. It would scan section headers and find where
1906 * .text ended (rather where .data/.bss began) and assume that this is
1907 * the upper range of the non-writable area. This is too coarse,
1908 * and is kept here for reference until we fully move away from single
1909 * segment elf objects. See the code in get_wr_offset (also #if'd 0)
1910 * that made this possible.
1911 */
1912 if(wr_offset < 0xffffffff){
1913 mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC);
1914 }
1915#else
1916 /* TODO: Verify that this does the right thing in all cases, as it
1917 * presently probably does not. It is possible that an ELF image will
1918 * come with multiple read-only segments. What we ought to do is scan
1919 * the program headers again and mprotect all the read-only segments.
1920 * To prevent re-scanning the program header, we would have to build a
1921 * list of loadable segments in si, and then scan that instead. */
1922 if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) {
1923 mprotect((void *)si->wrprotect_start,
1924 si->wrprotect_end - si->wrprotect_start,
1925 PROT_READ | PROT_EXEC);
1926 }
1927#endif
1928
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001929 if (si->gnu_relro_start != 0 && si->gnu_relro_len != 0) {
David 'Digit' Turnera6545f42012-06-18 11:15:54 +02001930 Elf32_Addr start = PAGE_START(si->gnu_relro_start);
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001931 unsigned len = (si->gnu_relro_start - start) + si->gnu_relro_len;
1932 if (mprotect((void *) start, len, PROT_READ) < 0) {
1933 DL_ERR("%5d GNU_RELRO mprotect of library '%s' failed: %d (%s)\n",
1934 pid, si->name, errno, strerror(errno));
1935 goto fail;
1936 }
1937 }
1938
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001939 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1940 stdout and stderr to close a security hole described in:
1941
1942 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1943
1944 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001945 if (program_is_setuid)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001946 nullify_closed_stdio ();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001947 notify_gdb_of_load(si);
1948 return 0;
1949
1950fail:
Dima Zavina7161902010-08-17 15:56:40 -07001951 ERROR("failed to link %s\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001952 si->flags |= FLAG_ERROR;
1953 return -1;
1954}
1955
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001956static void parse_library_path(const char *path, char *delim)
David Bartleybc3a5c22009-06-02 18:27:28 -07001957{
1958 size_t len;
1959 char *ldpaths_bufp = ldpaths_buf;
1960 int i = 0;
1961
1962 len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf));
1963
1964 while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) {
1965 if (*ldpaths[i] != '\0')
1966 ++i;
1967 }
1968
1969 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1970 * last char isn't '\0' (i.e. not originally a delim). */
1971 if (i > 0 && len >= sizeof(ldpaths_buf) &&
1972 ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') {
1973 ldpaths[i - 1] = NULL;
1974 } else {
1975 ldpaths[i] = NULL;
1976 }
1977}
1978
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001979static void parse_preloads(const char *path, char *delim)
Matt Fischer4fd42c12009-12-31 12:09:10 -06001980{
1981 size_t len;
1982 char *ldpreloads_bufp = ldpreloads_buf;
1983 int i = 0;
1984
1985 len = strlcpy(ldpreloads_buf, path, sizeof(ldpreloads_buf));
1986
1987 while (i < LDPRELOAD_MAX && (ldpreload_names[i] = strsep(&ldpreloads_bufp, delim))) {
1988 if (*ldpreload_names[i] != '\0') {
1989 ++i;
1990 }
1991 }
1992
1993 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1994 * last char isn't '\0' (i.e. not originally a delim). */
1995 if (i > 0 && len >= sizeof(ldpreloads_buf) &&
1996 ldpreloads_buf[sizeof(ldpreloads_buf) - 2] != '\0') {
1997 ldpreload_names[i - 1] = NULL;
1998 } else {
1999 ldpreload_names[i] = NULL;
2000 }
2001}
2002
Nick Kralevich468319c2011-11-11 15:53:17 -08002003/*
2004 * This code is called after the linker has linked itself and
2005 * fixed it's own GOT. It is safe to make references to externs
2006 * and other non-local data at this point.
2007 */
2008static unsigned __linker_init_post_relocation(unsigned **elfdata)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002009{
2010 static soinfo linker_soinfo;
2011
2012 int argc = (int) *elfdata;
2013 char **argv = (char**) (elfdata + 1);
Stephen Smalleybb440552012-01-20 10:59:15 -08002014 unsigned *vecs = (unsigned*) (argv + argc + 1);
2015 unsigned *v;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002016 soinfo *si;
2017 struct link_map * map;
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002018 const char *ldpath_env = NULL;
2019 const char *ldpreload_env = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002020
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02002021 /* NOTE: we store the elfdata pointer on a special location
2022 * of the temporary TLS area in order to pass it to
2023 * the C Library's runtime initializer.
2024 *
2025 * The initializer must clear the slot and reset the TLS
2026 * to point to a different location to ensure that no other
2027 * shared library constructor can access it.
2028 */
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002029 __libc_init_tls(elfdata);
2030
2031 pid = getpid();
2032
2033#if TIMING
2034 struct timeval t0, t1;
2035 gettimeofday(&t0, 0);
2036#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002037
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002038 /* Initialize environment functions, and get to the ELF aux vectors table */
2039 vecs = linker_env_init(vecs);
2040
Stephen Smalley861b42a2012-01-13 07:48:11 -05002041 /* Check auxv for AT_SECURE first to see if program is setuid, setgid,
2042 has file caps, or caused a SELinux/AppArmor domain transition. */
2043 for (v = vecs; v[0]; v += 2) {
2044 if (v[0] == AT_SECURE) {
2045 /* kernel told us whether to enable secure mode */
2046 program_is_setuid = v[1];
2047 goto sanitize;
2048 }
2049 }
2050
2051 /* Kernel did not provide AT_SECURE - fall back on legacy test. */
2052 program_is_setuid = (getuid() != geteuid()) || (getgid() != getegid());
2053
2054sanitize:
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002055 /* Sanitize environment if we're loading a setuid program */
2056 if (program_is_setuid)
2057 linker_env_secure();
2058
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002059 debugger_init();
2060
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002061 /* Get a few environment variables */
2062 {
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07002063#if LINKER_DEBUG
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002064 const char* env;
2065 env = linker_env_get("DEBUG"); /* XXX: TODO: Change to LD_DEBUG */
2066 if (env)
2067 debug_verbosity = atoi(env);
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07002068#endif
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002069
2070 /* Normally, these are cleaned by linker_env_secure, but the test
2071 * against program_is_setuid doesn't cost us anything */
2072 if (!program_is_setuid) {
2073 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2074 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002075 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002076 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002077
2078 INFO("[ android linker & debugger ]\n");
2079 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
2080
David 'Digit' Turner16084162012-06-12 16:25:37 +02002081 si = soinfo_alloc(argv[0]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002082 if(si == 0) {
2083 exit(-1);
2084 }
2085
2086 /* bootstrap the link map, the main exe always needs to be first */
2087 si->flags |= FLAG_EXE;
2088 map = &(si->linkmap);
2089
2090 map->l_addr = 0;
2091 map->l_name = argv[0];
2092 map->l_prev = NULL;
2093 map->l_next = NULL;
2094
2095 _r_debug.r_map = map;
2096 r_debug_tail = map;
2097
2098 /* gdb expects the linker to be in the debug shared object list,
2099 * and we need to make sure that the reported load address is zero.
2100 * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
David 'Digit' Turner16084162012-06-12 16:25:37 +02002101 * is. Don't use soinfo_alloc(), because the linker shouldn't
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002102 * be on the soinfo list.
2103 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002104 strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002105 linker_soinfo.flags = 0;
2106 linker_soinfo.base = 0; // This is the important part; must be zero.
2107 insert_soinfo_into_debug_map(&linker_soinfo);
2108
2109 /* extract information passed from the kernel */
2110 while(vecs[0] != 0){
2111 switch(vecs[0]){
2112 case AT_PHDR:
2113 si->phdr = (Elf32_Phdr*) vecs[1];
2114 break;
2115 case AT_PHNUM:
2116 si->phnum = (int) vecs[1];
2117 break;
2118 case AT_ENTRY:
2119 si->entry = vecs[1];
2120 break;
2121 }
2122 vecs += 2;
2123 }
2124
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002125 /* Compute the value of si->base. We can't rely on the fact that
2126 * the first entry is the PHDR because this will not be true
2127 * for certain executables (e.g. some in the NDK unit test suite)
2128 */
2129 int nn;
2130 si->base = 0;
2131 for ( nn = 0; nn < si->phnum; nn++ ) {
2132 if (si->phdr[nn].p_type == PT_PHDR) {
2133 si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_vaddr;
2134 break;
2135 }
2136 }
Ji-Hwan Leef186a182012-05-31 20:20:36 +09002137 si->load_offset = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002138 si->dynamic = (unsigned *)-1;
2139 si->wrprotect_start = 0xffffffff;
2140 si->wrprotect_end = 0;
David 'Digit' Turner67748092010-07-21 16:18:21 -07002141 si->refcount = 1;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002142 si->gnu_relro_start = 0;
2143 si->gnu_relro_len = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002144
David Bartleybc3a5c22009-06-02 18:27:28 -07002145 /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002146 if (ldpath_env)
David Bartleybc3a5c22009-06-02 18:27:28 -07002147 parse_library_path(ldpath_env, ":");
2148
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002149 if (ldpreload_env) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06002150 parse_preloads(ldpreload_env, " :");
2151 }
2152
David 'Digit' Turner16084162012-06-12 16:25:37 +02002153 if(soinfo_link_image(si, 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -07002154 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
2155 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2156 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002157 exit(-1);
2158 }
2159
David 'Digit' Turner16084162012-06-12 16:25:37 +02002160 soinfo_call_constructors(si);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002161
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07002162#if ALLOW_SYMBOLS_FROM_MAIN
2163 /* Set somain after we've loaded all the libraries in order to prevent
2164 * linking of symbols back to the main image, which is not set up at that
2165 * point yet.
2166 */
2167 somain = si;
2168#endif
2169
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002170#if TIMING
2171 gettimeofday(&t1,NULL);
2172 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
2173 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2174 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
2175 ));
2176#endif
2177#if STATS
2178 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
2179 linker_stats.reloc[RELOC_ABSOLUTE],
2180 linker_stats.reloc[RELOC_RELATIVE],
2181 linker_stats.reloc[RELOC_COPY],
2182 linker_stats.reloc[RELOC_SYMBOL]);
2183#endif
2184#if COUNT_PAGES
2185 {
2186 unsigned n;
2187 unsigned i;
2188 unsigned count = 0;
2189 for(n = 0; n < 4096; n++){
2190 if(bitmask[n]){
2191 unsigned x = bitmask[n];
2192 for(i = 0; i < 8; i++){
2193 if(x & 1) count++;
2194 x >>= 1;
2195 }
2196 }
2197 }
2198 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
2199 }
2200#endif
2201
2202#if TIMING || STATS || COUNT_PAGES
2203 fflush(stdout);
2204#endif
2205
2206 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
2207 si->entry);
2208 return si->entry;
2209}
Nick Kralevich468319c2011-11-11 15:53:17 -08002210
2211/*
2212 * Find the value of AT_BASE passed to us by the kernel. This is the load
2213 * location of the linker.
2214 */
2215static unsigned find_linker_base(unsigned **elfdata) {
2216 int argc = (int) *elfdata;
2217 char **argv = (char**) (elfdata + 1);
2218 unsigned *vecs = (unsigned*) (argv + argc + 1);
2219 while (vecs[0] != 0) {
2220 vecs++;
2221 }
2222
2223 /* The end of the environment block is marked by two NULL pointers */
2224 vecs++;
2225
2226 while(vecs[0]) {
2227 if (vecs[0] == AT_BASE) {
2228 return vecs[1];
2229 }
2230 vecs += 2;
2231 }
2232
2233 return 0; // should never happen
2234}
2235
2236/*
2237 * This is the entry point for the linker, called from begin.S. This
2238 * method is responsible for fixing the linker's own relocations, and
2239 * then calling __linker_init_post_relocation().
2240 *
2241 * Because this method is called before the linker has fixed it's own
2242 * relocations, any attempt to reference an extern variable, extern
2243 * function, or other GOT reference will generate a segfault.
2244 */
2245unsigned __linker_init(unsigned **elfdata) {
2246 unsigned linker_addr = find_linker_base(elfdata);
2247 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2248 Elf32_Phdr *phdr =
2249 (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2250
2251 soinfo linker_so;
2252 memset(&linker_so, 0, sizeof(soinfo));
2253
2254 linker_so.base = linker_addr;
Ji-Hwan Leef186a182012-05-31 20:20:36 +09002255 linker_so.load_offset = 0;
Nick Kralevich468319c2011-11-11 15:53:17 -08002256 linker_so.dynamic = (unsigned *) -1;
2257 linker_so.phdr = phdr;
2258 linker_so.phnum = elf_hdr->e_phnum;
2259 linker_so.flags |= FLAG_LINKER;
2260 linker_so.wrprotect_start = 0xffffffff;
2261 linker_so.wrprotect_end = 0;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002262 linker_so.gnu_relro_start = 0;
2263 linker_so.gnu_relro_len = 0;
Nick Kralevich468319c2011-11-11 15:53:17 -08002264
David 'Digit' Turner16084162012-06-12 16:25:37 +02002265 if (soinfo_link_image(&linker_so, 0)) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002266 // It would be nice to print an error message, but if the linker
2267 // can't link itself, there's no guarantee that we'll be able to
2268 // call write() (because it involves a GOT reference).
2269 //
2270 // This situation should never occur unless the linker itself
2271 // is corrupt.
2272 exit(-1);
2273 }
2274
2275 // We have successfully fixed our own relocations. It's safe to run
2276 // the main part of the linker now.
2277 return __linker_init_post_relocation(elfdata);
2278}