blob: 2a7ccd5b9c5836fb05a07b3c62920c44deec1213 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Doug Kwan94304352009-10-23 18:11:40 -07002 * Copyright (C) 2008, 2009 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <linux/auxvec.h>
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35#include <fcntl.h>
36#include <errno.h>
37#include <dlfcn.h>
38#include <sys/stat.h>
39
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -070040#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
42#include <sys/mman.h>
43
44#include <sys/atomics.h>
45
46/* special private C library header - see Android.mk */
47#include <bionic_tls.h>
48
49#include "linker.h"
50#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010051#include "linker_environ.h"
David 'Digit' Turner5c734642010-01-20 12:36:51 -080052#include "linker_format.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020053#include "linker_phdr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070055#define ALLOW_SYMBOLS_FROM_MAIN 1
Kenny Root72f9a5c2011-02-10 17:02:21 -080056#define SO_MAX 128
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057
David Bartleybc3a5c22009-06-02 18:27:28 -070058/* Assume average path length of 64 and max 8 paths */
59#define LDPATH_BUFSIZE 512
60#define LDPATH_MAX 8
61
Matt Fischer4fd42c12009-12-31 12:09:10 -060062#define LDPRELOAD_BUFSIZE 512
63#define LDPRELOAD_MAX 8
64
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
66 *
67 * Do NOT use malloc() and friends or pthread_*() code here.
68 * Don't use printf() either; it's caused mysterious memory
69 * corruption in the past.
70 * The linker runs before we bring up libc and it's easiest
71 * to make sure it does not depend on any complex libc features
72 *
73 * open issues / todo:
74 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075 * - are we doing everything we should for ARM_COPY relocations?
76 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077 * - after linking, set as much stuff as possible to READONLY
78 * and NOEXEC
79 * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
80 * headers provide versions that are negative...
81 * - allocate space for soinfo structs dynamically instead of
82 * having a hard limit (64)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083*/
84
85
David 'Digit' Turner16084162012-06-12 16:25:37 +020086static int soinfo_link_image(soinfo *si, unsigned wr_offset);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
88static int socount = 0;
89static soinfo sopool[SO_MAX];
90static soinfo *freelist = NULL;
91static soinfo *solist = &libdl_info;
92static soinfo *sonext = &libdl_info;
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070093#if ALLOW_SYMBOLS_FROM_MAIN
94static soinfo *somain; /* main process, always the one after libdl_info */
95#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096
Iliyan Malchevaf7315a2009-10-16 17:50:42 -070097
Iliyan Malchev6ed80c82009-09-28 19:38:04 -070098static inline int validate_soinfo(soinfo *si)
99{
100 return (si >= sopool && si < sopool + SO_MAX) ||
101 si == &libdl_info;
102}
103
David Bartleybc3a5c22009-06-02 18:27:28 -0700104static char ldpaths_buf[LDPATH_BUFSIZE];
105static const char *ldpaths[LDPATH_MAX + 1];
106
Matt Fischer4fd42c12009-12-31 12:09:10 -0600107static char ldpreloads_buf[LDPRELOAD_BUFSIZE];
108static const char *ldpreload_names[LDPRELOAD_MAX + 1];
109
110static soinfo *preloads[LDPRELOAD_MAX + 1];
111
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -0700112#if LINKER_DEBUG
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113int debug_verbosity;
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -0700114#endif
115
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116static int pid;
117
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100118/* This boolean is set if the program being loaded is setuid */
119static int program_is_setuid;
120
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121#if STATS
122struct _link_stats linker_stats;
123#endif
124
125#if COUNT_PAGES
126unsigned bitmask[4096];
127#endif
128
Dima Zavin2e855792009-05-20 18:28:09 -0700129#define HOODLUM(name, ret, ...) \
130 ret name __VA_ARGS__ \
131 { \
132 char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \
133 write(2, errstr, sizeof(errstr)); \
134 abort(); \
135 }
136HOODLUM(malloc, void *, (size_t size));
137HOODLUM(free, void, (void *ptr));
138HOODLUM(realloc, void *, (void *ptr, size_t size));
139HOODLUM(calloc, void *, (size_t cnt, size_t size));
140
Dima Zavin03531952009-05-29 17:30:25 -0700141static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700142static char __linker_dl_err_buf[768];
143#define DL_ERR(fmt, x...) \
144 do { \
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800145 format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), \
Dima Zavin2e855792009-05-20 18:28:09 -0700146 "%s[%d]: " fmt, __func__, __LINE__, ##x); \
Erik Gillingd00d23a2009-07-22 17:06:11 -0700147 ERROR(fmt "\n", ##x); \
Dima Zavin2e855792009-05-20 18:28:09 -0700148 } while(0)
149
150const char *linker_get_error(void)
151{
152 return (const char *)&__linker_dl_err_buf[0];
153}
154
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155/*
156 * This function is an empty stub where GDB locates a breakpoint to get notified
157 * about linker activity.
158 */
Andrew Hsieh40e7ed52012-07-02 11:17:04 -0700159extern void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
162 RT_CONSISTENT, 0};
163static struct link_map *r_debug_tail = 0;
164
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700165static pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800166
167static void insert_soinfo_into_debug_map(soinfo * info)
168{
169 struct link_map * map;
170
171 /* Copy the necessary fields into the debug structure.
172 */
173 map = &(info->linkmap);
174 map->l_addr = info->base;
175 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800176 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800177
178 /* Stick the new library at the end of the list.
179 * gdb tends to care more about libc than it does
180 * about leaf libraries, and ordering it this way
181 * reduces the back-and-forth over the wire.
182 */
183 if (r_debug_tail) {
184 r_debug_tail->l_next = map;
185 map->l_prev = r_debug_tail;
186 map->l_next = 0;
187 } else {
188 _r_debug.r_map = map;
189 map->l_prev = 0;
190 map->l_next = 0;
191 }
192 r_debug_tail = map;
193}
194
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700195static void remove_soinfo_from_debug_map(soinfo * info)
196{
197 struct link_map * map = &(info->linkmap);
198
199 if (r_debug_tail == map)
200 r_debug_tail = map->l_prev;
201
202 if (map->l_prev) map->l_prev->l_next = map->l_next;
203 if (map->l_next) map->l_next->l_prev = map->l_prev;
204}
205
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206void notify_gdb_of_load(soinfo * info)
207{
208 if (info->flags & FLAG_EXE) {
209 // GDB already knows about the main executable
210 return;
211 }
212
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700213 pthread_mutex_lock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214
215 _r_debug.r_state = RT_ADD;
216 rtld_db_dlactivity();
217
218 insert_soinfo_into_debug_map(info);
219
220 _r_debug.r_state = RT_CONSISTENT;
221 rtld_db_dlactivity();
222
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700223 pthread_mutex_unlock(&_r_debug_lock);
224}
225
226void notify_gdb_of_unload(soinfo * info)
227{
228 if (info->flags & FLAG_EXE) {
229 // GDB already knows about the main executable
230 return;
231 }
232
233 pthread_mutex_lock(&_r_debug_lock);
234
235 _r_debug.r_state = RT_DELETE;
236 rtld_db_dlactivity();
237
238 remove_soinfo_from_debug_map(info);
239
240 _r_debug.r_state = RT_CONSISTENT;
241 rtld_db_dlactivity();
242
243 pthread_mutex_unlock(&_r_debug_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244}
245
246void notify_gdb_of_libraries()
247{
248 _r_debug.r_state = RT_ADD;
249 rtld_db_dlactivity();
250 _r_debug.r_state = RT_CONSISTENT;
251 rtld_db_dlactivity();
252}
253
David 'Digit' Turner16084162012-06-12 16:25:37 +0200254static soinfo *soinfo_alloc(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255{
256 soinfo *si;
257
258 if(strlen(name) >= SOINFO_NAME_LEN) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700259 DL_ERR("%5d library name %s too long", pid, name);
Doug Kwan94304352009-10-23 18:11:40 -0700260 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800261 }
262
David 'Digit' Turner16084162012-06-12 16:25:37 +0200263 /* The freelist is populated when we call soinfo_free(), which in turn is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264 done only by dlclose(), which is not likely to be used.
265 */
266 if (!freelist) {
267 if(socount == SO_MAX) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700268 DL_ERR("%5d too many libraries when loading %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269 return NULL;
270 }
271 freelist = sopool + socount++;
272 freelist->next = NULL;
273 }
274
275 si = freelist;
276 freelist = freelist->next;
277
278 /* Make sure we get a clean block of soinfo */
279 memset(si, 0, sizeof(soinfo));
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100280 strlcpy((char*) si->name, name, sizeof(si->name));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800281 sonext->next = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282 si->next = NULL;
283 si->refcount = 0;
284 sonext = si;
285
286 TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
287 return si;
288}
289
David 'Digit' Turner16084162012-06-12 16:25:37 +0200290static void soinfo_free(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291{
292 soinfo *prev = NULL, *trav;
293
294 TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
295
296 for(trav = solist; trav != NULL; trav = trav->next){
297 if (trav == si)
298 break;
299 prev = trav;
300 }
301 if (trav == NULL) {
302 /* si was not ni solist */
Erik Gillingd00d23a2009-07-22 17:06:11 -0700303 DL_ERR("%5d name %s is not in solist!", pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800304 return;
305 }
306
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100307 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308 always the static libdl_info.
309 */
310 prev->next = si->next;
311 if (si == sonext) sonext = prev;
312 si->next = freelist;
313 freelist = si;
314}
315
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800316const char *addr_to_name(unsigned addr)
317{
318 soinfo *si;
319
320 for(si = solist; si != 0; si = si->next){
321 if((addr >= si->base) && (addr < (si->base + si->size))) {
322 return si->name;
323 }
324 }
325
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800326 return "";
327}
328
329/* For a given PC, find the .so that it belongs to.
330 * Returns the base address of the .ARM.exidx section
331 * for that .so, and the number of 8-byte entries
332 * in that section (via *pcount).
333 *
334 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
335 *
336 * This function is exposed via dlfcn.c and libdl.so.
337 */
338#ifdef ANDROID_ARM_LINKER
339_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
340{
341 soinfo *si;
342 unsigned addr = (unsigned)pc;
343
Nick Kralevich468319c2011-11-11 15:53:17 -0800344 for (si = solist; si != 0; si = si->next){
345 if ((addr >= si->base) && (addr < (si->base + si->size))) {
346 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900347 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800348 }
349 }
350 *pcount = 0;
351 return NULL;
352}
David 'Digit' Turner70b16682012-01-30 17:17:58 +0100353#elif defined(ANDROID_X86_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800354/* Here, we only have to provide a callback to iterate across all the
355 * loaded libraries. gcc_eh does the rest. */
356int
357dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
358 void *data)
359{
360 soinfo *si;
361 struct dl_phdr_info dl_info;
362 int rv = 0;
363
364 for (si = solist; si != NULL; si = si->next) {
365 dl_info.dlpi_addr = si->linkmap.l_addr;
366 dl_info.dlpi_name = si->linkmap.l_name;
367 dl_info.dlpi_phdr = si->phdr;
368 dl_info.dlpi_phnum = si->phnum;
369 rv = cb(&dl_info, sizeof (struct dl_phdr_info), data);
370 if (rv != 0)
371 break;
372 }
373 return rv;
374}
375#endif
376
David 'Digit' Turner16084162012-06-12 16:25:37 +0200377static Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800378{
379 Elf32_Sym *s;
380 Elf32_Sym *symtab = si->symtab;
381 const char *strtab = si->strtab;
382 unsigned n;
383
384 TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
385 name, si->name, si->base, hash, hash % si->nbucket);
386 n = hash % si->nbucket;
387
388 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
389 s = symtab + n;
390 if(strcmp(strtab + s->st_name, name)) continue;
391
Doug Kwane8238072009-10-26 12:05:23 -0700392 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393 switch(ELF32_ST_BIND(s->st_info)){
394 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700395 case STB_WEAK:
Robin Burchell439fa8e2012-07-05 09:21:07 +0200396 if(s->st_shndx == SHN_UNDEF)
397 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800399 TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
400 name, si->name, s->st_value, s->st_size);
401 return s;
402 }
403 }
404
Doug Kwan94304352009-10-23 18:11:40 -0700405 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406}
407
408static unsigned elfhash(const char *_name)
409{
410 const unsigned char *name = (const unsigned char *) _name;
411 unsigned h = 0, g;
412
413 while(*name) {
414 h = (h << 4) + *name++;
415 g = h & 0xf0000000;
416 h ^= g;
417 h ^= g >> 24;
418 }
419 return h;
420}
421
422static Elf32_Sym *
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200423soinfo_do_lookup(soinfo *si, const char *name, Elf32_Addr *offset)
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700424{
Doug Kwan94304352009-10-23 18:11:40 -0700425 unsigned elf_hash = elfhash(name);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700426 Elf32_Sym *s;
427 unsigned *d;
428 soinfo *lsi = si;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600429 int i;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700430
Nick Kralevich468319c2011-11-11 15:53:17 -0800431 /* Look for symbols in the local scope (the object who is
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700432 * searching). This happens with C++ templates on i386 for some
Doug Kwane8238072009-10-26 12:05:23 -0700433 * reason.
434 *
435 * Notes on weak symbols:
436 * The ELF specs are ambigious about treatment of weak definitions in
437 * dynamic linking. Some systems return the first definition found
438 * and some the first non-weak definition. This is system dependent.
439 * Here we return the first definition found for simplicity. */
Nick Kralevich468319c2011-11-11 15:53:17 -0800440
David 'Digit' Turner16084162012-06-12 16:25:37 +0200441 s = soinfo_elf_lookup(si, elf_hash, name);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700442 if(s != NULL)
443 goto done;
444
Matt Fischer4fd42c12009-12-31 12:09:10 -0600445 /* Next, look for it in the preloads list */
446 for(i = 0; preloads[i] != NULL; i++) {
447 lsi = preloads[i];
David 'Digit' Turner16084162012-06-12 16:25:37 +0200448 s = soinfo_elf_lookup(lsi, elf_hash, name);
Matt Fischer4fd42c12009-12-31 12:09:10 -0600449 if(s != NULL)
450 goto done;
451 }
452
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700453 for(d = si->dynamic; *d; d += 2) {
454 if(d[0] == DT_NEEDED){
455 lsi = (soinfo *)d[1];
456 if (!validate_soinfo(lsi)) {
457 DL_ERR("%5d bad DT_NEEDED pointer in %s",
458 pid, si->name);
Doug Kwan94304352009-10-23 18:11:40 -0700459 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700460 }
461
462 DEBUG("%5d %s: looking up %s in %s\n",
463 pid, si->name, name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200464 s = soinfo_elf_lookup(lsi, elf_hash, name);
Robin Burchell8211bc62012-07-05 09:23:19 +0200465 if (s != NULL)
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700466 goto done;
467 }
468 }
469
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -0700470#if ALLOW_SYMBOLS_FROM_MAIN
471 /* If we are resolving relocations while dlopen()ing a library, it's OK for
472 * the library to resolve a symbol that's defined in the executable itself,
473 * although this is rare and is generally a bad idea.
474 */
475 if (somain) {
476 lsi = somain;
477 DEBUG("%5d %s: looking up %s in executable %s\n",
478 pid, si->name, name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200479 s = soinfo_elf_lookup(lsi, elf_hash, name);
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -0700480 }
481#endif
482
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700483done:
484 if(s != NULL) {
485 TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200486 "found in %s, base = 0x%08x, load bias = 0x%08x\n",
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900487 pid, si->name, name, s->st_value,
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200488 lsi->name, lsi->base, lsi->load_bias);
489 *offset = lsi->load_bias;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700490 return s;
491 }
492
Doug Kwan94304352009-10-23 18:11:40 -0700493 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700494}
495
496/* This is used by dl_sym(). It performs symbol lookup only within the
497 specified soinfo object and not in any of its dependencies.
498 */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200499Elf32_Sym *soinfo_lookup(soinfo *si, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800500{
David 'Digit' Turner16084162012-06-12 16:25:37 +0200501 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800502}
503
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700504/* This is used by dl_sym(). It performs a global symbol lookup.
505 */
Matt Fischer1698d9e2009-12-31 12:17:56 -0600506Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800507{
Doug Kwan94304352009-10-23 18:11:40 -0700508 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800509 Elf32_Sym *s = NULL;
510 soinfo *si;
511
Matt Fischer1698d9e2009-12-31 12:17:56 -0600512 if(start == NULL) {
513 start = solist;
514 }
515
516 for(si = start; (s == NULL) && (si != NULL); si = si->next)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800517 {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700518 if(si->flags & FLAG_ERROR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800519 continue;
David 'Digit' Turner16084162012-06-12 16:25:37 +0200520 s = soinfo_elf_lookup(si, elf_hash, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800521 if (s != NULL) {
Iliyan Malchev9ea64da2009-09-28 18:21:30 -0700522 *found = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800523 break;
524 }
525 }
526
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700527 if(s != NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800528 TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
529 "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
530 return s;
531 }
532
Doug Kwan94304352009-10-23 18:11:40 -0700533 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534}
535
Mathias Agopianbda5da02011-09-27 22:30:19 -0700536soinfo *find_containing_library(const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600537{
538 soinfo *si;
539
540 for(si = solist; si != NULL; si = si->next)
541 {
542 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
543 return si;
544 }
545 }
546
547 return NULL;
548}
549
David 'Digit' Turner16084162012-06-12 16:25:37 +0200550Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600551{
552 unsigned int i;
553 unsigned soaddr = (unsigned)addr - si->base;
554
555 /* Search the library's symbol table for any defined symbol which
556 * contains this address */
557 for(i=0; i<si->nchain; i++) {
558 Elf32_Sym *sym = &si->symtab[i];
559
560 if(sym->st_shndx != SHN_UNDEF &&
561 soaddr >= sym->st_value &&
562 soaddr < sym->st_value + sym->st_size) {
563 return sym;
564 }
565 }
566
567 return NULL;
568}
569
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800570#if 0
571static void dump(soinfo *si)
572{
573 Elf32_Sym *s = si->symtab;
574 unsigned n;
575
576 for(n = 0; n < si->nchain; n++) {
577 TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
578 s->st_info, s->st_shndx, s->st_value, s->st_size,
579 si->strtab + s->st_name);
580 s++;
581 }
582}
583#endif
584
David 'Digit' Turner16084162012-06-12 16:25:37 +0200585static const char * const sopaths[] = {
Brian Swetlandfedbcde2010-09-19 03:39:13 -0700586 "/vendor/lib",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800587 "/system/lib",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800588 0
589};
590
591static int _open_lib(const char *name)
592{
593 int fd;
594 struct stat filestat;
595
596 if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
David 'Digit' Turner16084162012-06-12 16:25:37 +0200597 if ((fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY))) >= 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598 return fd;
599 }
600
601 return -1;
602}
603
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800604static int open_library(const char *name)
605{
606 int fd;
607 char buf[512];
David 'Digit' Turner16084162012-06-12 16:25:37 +0200608 const char * const*path;
David Bartleybc3a5c22009-06-02 18:27:28 -0700609 int n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800610
611 TRACE("[ %5d opening %s ]\n", pid, name);
612
613 if(name == 0) return -1;
614 if(strlen(name) > 256) return -1;
615
616 if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
617 return fd;
618
David Bartleybc3a5c22009-06-02 18:27:28 -0700619 for (path = ldpaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800620 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700621 if (n < 0 || n >= (int)sizeof(buf)) {
622 WARN("Ignoring very long library path: %s/%s\n", *path, name);
623 continue;
624 }
625 if ((fd = _open_lib(buf)) >= 0)
626 return fd;
627 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800628 for (path = sopaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800629 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700630 if (n < 0 || n >= (int)sizeof(buf)) {
631 WARN("Ignoring very long library path: %s/%s\n", *path, name);
632 continue;
633 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800634 if ((fd = _open_lib(buf)) >= 0)
635 return fd;
636 }
637
638 return -1;
639}
640
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800641typedef struct {
642 long mmap_addr;
643 char tag[4]; /* 'P', 'R', 'E', ' ' */
644} prelink_info_t;
645
646/* Returns the requested base address if the library is prelinked,
647 * and 0 otherwise. */
648static unsigned long
649is_prelinked(int fd, const char *name)
650{
651 off_t sz;
652 prelink_info_t info;
653
654 sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
655 if (sz < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700656 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657 return 0;
658 }
659
David 'Digit' Turner16084162012-06-12 16:25:37 +0200660 if (TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)) != sizeof(info))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800661 WARN("Could not read prelink_info_t structure for `%s`\n", name);
662 return 0;
663 }
664
David 'Digit' Turner16084162012-06-12 16:25:37 +0200665 if (memcmp(info.tag, "PRE ", 4)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800666 WARN("`%s` is not a prelinked library\n", name);
667 return 0;
668 }
669
670 return (unsigned long)info.mmap_addr;
671}
672
David 'Digit' Turner16084162012-06-12 16:25:37 +0200673/* verify_elf_header
674 * Verifies the content of an ELF header.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800675 *
676 * Args:
677 *
678 * Returns:
679 * 0 on success
680 * -1 if no valid ELF object is found @ base.
681 */
682static int
David 'Digit' Turner16084162012-06-12 16:25:37 +0200683verify_elf_header(const Elf32_Ehdr* hdr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800684{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800685 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
686 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
687 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
688 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
689
690 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800691#ifdef ANDROID_ARM_LINKER
692 if (hdr->e_machine != EM_ARM) return -1;
693#elif defined(ANDROID_X86_LINKER)
694 if (hdr->e_machine != EM_386) return -1;
695#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800696 return 0;
697}
698
699
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800700static soinfo *
701load_library(const char *name)
702{
703 int fd = open_library(name);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200704 int ret, cnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800705 unsigned ext_sz;
706 unsigned req_base;
Erik Gillingfde86422009-07-28 20:28:19 -0700707 const char *bname;
Ji-Hwan Lee75917c82012-05-25 22:36:00 +0900708 struct stat sb;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800709 soinfo *si = NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200710 Elf32_Ehdr header[1];
711 int phdr_count;
712 void* phdr_mmap = NULL;
713 Elf32_Addr phdr_size;
714 const Elf32_Phdr* phdr_table;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800715
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200716 void* load_start = NULL;
717 Elf32_Addr load_size = 0;
718 Elf32_Addr load_bias = 0;
719
Ji-Hwan Lee75917c82012-05-25 22:36:00 +0900720 if (fd == -1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700721 DL_ERR("Library '%s' not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800722 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700723 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800724
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200725 /* Read the ELF header first */
726 ret = TEMP_FAILURE_RETRY(read(fd, (void*)header, sizeof(header)));
727 if (ret < 0) {
728 DL_ERR("%5d can't read file %s: %s", pid, name, strerror(errno));
729 goto fail;
730 }
731 if (ret != (int)sizeof(header)) {
732 DL_ERR("%5d too small to be an ELF executable: %s", pid, name);
733 goto fail;
734 }
735 if (verify_elf_header(header) < 0) {
736 DL_ERR("%5d not a valid ELF executable: %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800737 goto fail;
738 }
739
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200740 /* Then read the program header table */
741 ret = phdr_table_load(fd, header->e_phoff, header->e_phnum,
742 &phdr_mmap, &phdr_size, &phdr_table);
743 if (ret < 0) {
744 DL_ERR("%5d can't load program header table: %s: %s", pid,
745 name, strerror(errno));
746 goto fail;
747 }
748 phdr_count = header->e_phnum;
749
750 /* Get the load extents and the prelinked load address, if any */
751 ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
752 if (ext_sz == 0) {
753 DL_ERR("%5d no loadable segments in file: %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800754 goto fail;
755 }
756
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200757 req_base = (unsigned) is_prelinked(fd, name);
758 if (req_base == (unsigned)-1) {
759 DL_ERR("%5d can't read end of library: %s: %s", pid, name,
760 strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800761 goto fail;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200762 }
763 if (req_base != 0) {
764 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
765 pid, name, req_base);
766 } else {
767 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
768 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200769
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800770 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
771 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
772
773 /* Now configure the soinfo struct where we'll store all of our data
774 * for the ELF object. If the loading fails, we waste the entry, but
775 * same thing would happen if we failed during linking. Configuring the
776 * soinfo struct here is a lot more convenient.
777 */
Erik Gillingfde86422009-07-28 20:28:19 -0700778 bname = strrchr(name, '/');
David 'Digit' Turner16084162012-06-12 16:25:37 +0200779 si = soinfo_alloc(bname ? bname + 1 : name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800780 if (si == NULL)
781 goto fail;
782
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200783 /* Reserve address space for all loadable segments */
784 ret = phdr_table_reserve_memory(phdr_table,
785 phdr_count,
786 req_base,
787 &load_start,
788 &load_size,
789 &load_bias);
790 if (ret < 0) {
791 DL_ERR("%5d Can't reserve %d bytes from 0x%08x in address space for %s: %s",
792 pid, ext_sz, req_base, name, strerror(errno));
793 goto fail;
794 }
795
796 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
797 pid, name, load_start, load_size);
798
799 /* Map all the segments in our address space with default protections */
800 ret = phdr_table_load_segments(phdr_table,
801 phdr_count,
802 load_start,
803 load_size,
804 load_bias,
805 fd);
806 if (ret < 0) {
807 DL_ERR("%5d Can't map loadable segments for %s: %s",
808 pid, name, strerror(errno));
809 goto fail;
810 }
811
812 /* Unprotect the segments, i.e. make them writable, to allow
813 * relocations to work properly. We will later call
814 * phdr_table_protect_segments() after all of them are applied
815 * and all constructors are run.
816 */
817 ret = phdr_table_unprotect_segments(phdr_table,
818 phdr_count,
819 load_bias);
820 if (ret < 0) {
821 DL_ERR("%5d Can't unprotect loadable segments for %s: %s",
822 pid, name, strerror(errno));
823 goto fail;
824 }
825
826 si->base = (Elf32_Addr) load_start;
827 si->size = load_size;
828 si->load_bias = load_bias;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800829 si->flags = 0;
830 si->entry = 0;
831 si->dynamic = (unsigned *)-1;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200832 si->phnum = phdr_count;
833 si->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
834 if (si->phdr == NULL) {
835 DL_ERR("%5d Can't find loaded PHDR for %s",
836 pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800837 goto fail;
838 }
839
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200840 phdr_table_unload(phdr_mmap, phdr_size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800841 close(fd);
842 return si;
843
844fail:
David 'Digit' Turner16084162012-06-12 16:25:37 +0200845 if (si) soinfo_free(si);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200846 if (phdr_mmap != NULL) {
847 phdr_table_unload(phdr_mmap, phdr_size);
848 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800849 close(fd);
850 return NULL;
851}
852
853static soinfo *
854init_library(soinfo *si)
855{
856 unsigned wr_offset = 0xffffffff;
857
858 /* At this point we know that whatever is loaded @ base is a valid ELF
859 * shared library whose segments are properly mapped in. */
860 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
861 pid, si->base, si->size, si->name);
862
David 'Digit' Turner16084162012-06-12 16:25:37 +0200863 if(soinfo_link_image(si, wr_offset)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800864 /* We failed to link. However, we can only restore libbase
865 ** if no additional libraries have moved it since we updated it.
866 */
867 munmap((void *)si->base, si->size);
868 return NULL;
869 }
870
871 return si;
872}
873
874soinfo *find_library(const char *name)
875{
876 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700877 const char *bname;
878
879#if ALLOW_SYMBOLS_FROM_MAIN
880 if (name == NULL)
881 return somain;
882#else
883 if (name == NULL)
884 return NULL;
885#endif
886
887 bname = strrchr(name, '/');
Erik Gillingfde86422009-07-28 20:28:19 -0700888 bname = bname ? bname + 1 : name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800889
890 for(si = solist; si != 0; si = si->next){
Erik Gillingfde86422009-07-28 20:28:19 -0700891 if(!strcmp(bname, si->name)) {
Erik Gilling30eb4022009-08-13 16:05:30 -0700892 if(si->flags & FLAG_ERROR) {
893 DL_ERR("%5d '%s' failed to load previously", pid, bname);
894 return NULL;
895 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800896 if(si->flags & FLAG_LINKED) return si;
Erik Gillingd00d23a2009-07-22 17:06:11 -0700897 DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name);
Dima Zavin2e855792009-05-20 18:28:09 -0700898 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800899 }
900 }
901
902 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
903 si = load_library(name);
904 if(si == NULL)
905 return NULL;
906 return init_library(si);
907}
908
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100909/* TODO:
910 * notify gdb of unload
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800911 * for non-prelinked libraries, find a way to decrement libbase
912 */
913static void call_destructors(soinfo *si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200914unsigned soinfo_unload(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800915{
916 unsigned *d;
917 if (si->refcount == 1) {
918 TRACE("%5d unloading '%s'\n", pid, si->name);
919 call_destructors(si);
920
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800921 /*
922 * Make sure that we undo the PT_GNU_RELRO protections we added
David 'Digit' Turner16084162012-06-12 16:25:37 +0200923 * in soinfo_link_image. This is needed to undo the DT_NEEDED hack below.
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800924 */
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200925 if (phdr_table_unprotect_gnu_relro(si->phdr, si->phnum,
926 si->load_bias) < 0) {
927 DL_ERR("%5d %s: could not undo GNU_RELRO protections. "
928 "Expect a crash soon. errno=%d (%s)",
929 pid, si->name, errno, strerror(errno));
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800930 }
931
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800932 for(d = si->dynamic; *d; d += 2) {
933 if(d[0] == DT_NEEDED){
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700934 soinfo *lsi = (soinfo *)d[1];
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800935
936 // The next line will segfault if the we don't undo the
937 // PT_GNU_RELRO protections (see comments above and in
David 'Digit' Turner16084162012-06-12 16:25:37 +0200938 // soinfo_link_image().
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700939 d[1] = 0;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800940
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700941 if (validate_soinfo(lsi)) {
942 TRACE("%5d %s needs to unload %s\n", pid,
943 si->name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200944 soinfo_unload(lsi);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700945 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800946 else
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700947 DL_ERR("%5d %s: could not unload dependent library",
948 pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800949 }
950 }
951
952 munmap((char *)si->base, si->size);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700953 notify_gdb_of_unload(si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200954 soinfo_free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800955 si->refcount = 0;
956 }
957 else {
958 si->refcount--;
959 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
960 pid, si->name, si->refcount);
961 }
962 return si->refcount;
963}
964
965/* TODO: don't use unsigned for addrs below. It works, but is not
966 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
967 * long.
968 */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200969static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800970{
971 Elf32_Sym *symtab = si->symtab;
972 const char *strtab = si->strtab;
973 Elf32_Sym *s;
974 unsigned base;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900975 Elf32_Addr offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800976 Elf32_Rel *start = rel;
977 unsigned idx;
978
979 for (idx = 0; idx < count; ++idx) {
980 unsigned type = ELF32_R_TYPE(rel->r_info);
981 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200982 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800983 unsigned sym_addr = 0;
984 char *sym_name = NULL;
985
986 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
987 si->name, idx);
988 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -0700989 sym_name = (char *)(strtab + symtab[sym].st_name);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200990 s = soinfo_do_lookup(si, sym_name, &offset);
Doug Kwane8238072009-10-26 12:05:23 -0700991 if(s == NULL) {
992 /* We only allow an undefined symbol if this is a weak
993 reference.. */
994 s = &symtab[sym];
995 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
996 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
997 return -1;
998 }
999
1000 /* IHI0044C AAELF 4.5.1.1:
1001
1002 Libraries are not searched to resolve weak references.
1003 It is not an error for a weak reference to remain
1004 unsatisfied.
1005
1006 During linking, the value of an undefined weak reference is:
1007 - Zero if the relocation type is absolute
1008 - The address of the place if the relocation is pc-relative
1009 - The address of nominial base address if the relocation
1010 type is base-relative.
1011 */
1012
1013 switch (type) {
1014#if defined(ANDROID_ARM_LINKER)
1015 case R_ARM_JUMP_SLOT:
1016 case R_ARM_GLOB_DAT:
1017 case R_ARM_ABS32:
1018 case R_ARM_RELATIVE: /* Don't care. */
1019 case R_ARM_NONE: /* Don't care. */
1020#elif defined(ANDROID_X86_LINKER)
1021 case R_386_JUMP_SLOT:
1022 case R_386_GLOB_DAT:
1023 case R_386_32:
1024 case R_386_RELATIVE: /* Dont' care. */
1025#endif /* ANDROID_*_LINKER */
1026 /* sym_addr was initialized to be zero above or relocation
1027 code below does not care about value of sym_addr.
1028 No need to do anything. */
1029 break;
1030
1031#if defined(ANDROID_X86_LINKER)
1032 case R_386_PC32:
1033 sym_addr = reloc;
1034 break;
1035#endif /* ANDROID_X86_LINKER */
1036
1037#if defined(ANDROID_ARM_LINKER)
1038 case R_ARM_COPY:
1039 /* Fall through. Can't really copy if weak symbol is
1040 not found in run-time. */
1041#endif /* ANDROID_ARM_LINKER */
1042 default:
1043 DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n",
1044 pid, type, rel, (int) (rel - start));
1045 return -1;
1046 }
1047 } else {
1048 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001049#if 0
1050 if((base == 0) && (si->base != 0)){
1051 /* linking from libraries to main image is bad */
Erik Gillingd00d23a2009-07-22 17:06:11 -07001052 DL_ERR("%5d cannot locate '%s'...",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001053 pid, strtab + symtab[sym].st_name);
1054 return -1;
1055 }
1056#endif
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001057 sym_addr = (unsigned)(s->st_value + offset);
Doug Kwane8238072009-10-26 12:05:23 -07001058 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001059 COUNT_RELOC(RELOC_SYMBOL);
1060 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001061 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001062 }
1063
1064/* TODO: This is ugly. Split up the relocations by arch into
1065 * different files.
1066 */
1067 switch(type){
1068#if defined(ANDROID_ARM_LINKER)
1069 case R_ARM_JUMP_SLOT:
1070 COUNT_RELOC(RELOC_ABSOLUTE);
1071 MARK(rel->r_offset);
1072 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1073 reloc, sym_addr, sym_name);
1074 *((unsigned*)reloc) = sym_addr;
1075 break;
1076 case R_ARM_GLOB_DAT:
1077 COUNT_RELOC(RELOC_ABSOLUTE);
1078 MARK(rel->r_offset);
1079 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1080 reloc, sym_addr, sym_name);
1081 *((unsigned*)reloc) = sym_addr;
1082 break;
1083 case R_ARM_ABS32:
1084 COUNT_RELOC(RELOC_ABSOLUTE);
1085 MARK(rel->r_offset);
1086 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1087 reloc, sym_addr, sym_name);
1088 *((unsigned*)reloc) += sym_addr;
1089 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001090 case R_ARM_REL32:
1091 COUNT_RELOC(RELOC_RELATIVE);
1092 MARK(rel->r_offset);
1093 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1094 reloc, sym_addr, rel->r_offset, sym_name);
1095 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1096 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001097#elif defined(ANDROID_X86_LINKER)
1098 case R_386_JUMP_SLOT:
1099 COUNT_RELOC(RELOC_ABSOLUTE);
1100 MARK(rel->r_offset);
1101 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1102 reloc, sym_addr, sym_name);
1103 *((unsigned*)reloc) = sym_addr;
1104 break;
1105 case R_386_GLOB_DAT:
1106 COUNT_RELOC(RELOC_ABSOLUTE);
1107 MARK(rel->r_offset);
1108 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1109 reloc, sym_addr, sym_name);
1110 *((unsigned*)reloc) = sym_addr;
1111 break;
1112#endif /* ANDROID_*_LINKER */
1113
1114#if defined(ANDROID_ARM_LINKER)
1115 case R_ARM_RELATIVE:
1116#elif defined(ANDROID_X86_LINKER)
1117 case R_386_RELATIVE:
1118#endif /* ANDROID_*_LINKER */
1119 COUNT_RELOC(RELOC_RELATIVE);
1120 MARK(rel->r_offset);
1121 if(sym){
Erik Gillingd00d23a2009-07-22 17:06:11 -07001122 DL_ERR("%5d odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001123 return -1;
1124 }
1125 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1126 reloc, si->base);
1127 *((unsigned*)reloc) += si->base;
1128 break;
1129
1130#if defined(ANDROID_X86_LINKER)
1131 case R_386_32:
1132 COUNT_RELOC(RELOC_RELATIVE);
1133 MARK(rel->r_offset);
1134
1135 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1136 reloc, sym_addr, sym_name);
1137 *((unsigned *)reloc) += (unsigned)sym_addr;
1138 break;
1139
1140 case R_386_PC32:
1141 COUNT_RELOC(RELOC_RELATIVE);
1142 MARK(rel->r_offset);
1143 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1144 "+%08x (%08x - %08x) %s\n", pid, reloc,
1145 (sym_addr - reloc), sym_addr, reloc, sym_name);
1146 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1147 break;
1148#endif /* ANDROID_X86_LINKER */
1149
1150#ifdef ANDROID_ARM_LINKER
1151 case R_ARM_COPY:
1152 COUNT_RELOC(RELOC_COPY);
1153 MARK(rel->r_offset);
1154 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1155 reloc, s->st_size, sym_addr, sym_name);
1156 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1157 break;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001158 case R_ARM_NONE:
1159 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001160#endif /* ANDROID_ARM_LINKER */
1161
1162 default:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001163 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001164 pid, type, rel, (int) (rel - start));
1165 return -1;
1166 }
1167 rel++;
1168 }
1169 return 0;
1170}
1171
David 'Digit' Turner82156792009-05-18 14:37:41 +02001172/* Please read the "Initialization and Termination functions" functions.
1173 * of the linker design note in bionic/linker/README.TXT to understand
1174 * what the following code is doing.
1175 *
1176 * The important things to remember are:
1177 *
1178 * DT_PREINIT_ARRAY must be called first for executables, and should
1179 * not appear in shared libraries.
1180 *
1181 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1182 *
1183 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1184 *
1185 * DT_FINI_ARRAY must be parsed in reverse order.
1186 */
1187
1188static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001189{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001190 int n, inc = 1;
1191
1192 if (reverse) {
1193 ctor += (count-1);
1194 inc = -1;
1195 }
1196
1197 for(n = count; n > 0; n--) {
1198 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1199 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001200 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001201 void (*func)() = (void (*)()) *ctor;
1202 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001203 if(((int) func == 0) || ((int) func == -1)) continue;
1204 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1205 func();
1206 }
1207}
1208
David 'Digit' Turner16084162012-06-12 16:25:37 +02001209void soinfo_call_constructors(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001210{
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001211 if (si->constructors_called)
1212 return;
1213
Jesse Hallf5d16932012-01-30 15:39:57 -08001214 // Set this before actually calling the constructors, otherwise it doesn't
1215 // protect against recursive constructor calls. One simple example of
1216 // constructor recursion is the libc debug malloc, which is implemented in
1217 // libc_malloc_debug_leak.so:
1218 // 1. The program depends on libc, so libc's constructor is called here.
1219 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001220 // 3. dlopen() calls soinfo_call_constructors() with the newly created
Jesse Hallf5d16932012-01-30 15:39:57 -08001221 // soinfo for libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001222 // 4. The debug so depends on libc, so soinfo_call_constructors() is
Jesse Hallf5d16932012-01-30 15:39:57 -08001223 // called again with the libc soinfo. If it doesn't trigger the early-
1224 // out above, the libc constructor will be called again (recursively!).
1225 si->constructors_called = 1;
1226
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001227 if (si->flags & FLAG_EXE) {
1228 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1229 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1230 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001231 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001232 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1233 } else {
1234 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001235 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
Erik Gillingd00d23a2009-07-22 17:06:11 -07001236 " This is INVALID.", pid, si->name,
Dima Zavin2e855792009-05-20 18:28:09 -07001237 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001238 }
1239 }
1240
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001241 if (si->dynamic) {
1242 unsigned *d;
1243 for(d = si->dynamic; *d; d += 2) {
1244 if(d[0] == DT_NEEDED){
1245 soinfo* lsi = (soinfo *)d[1];
1246 if (!validate_soinfo(lsi)) {
1247 DL_ERR("%5d bad DT_NEEDED pointer in %s",
1248 pid, si->name);
1249 } else {
David 'Digit' Turner16084162012-06-12 16:25:37 +02001250 soinfo_call_constructors(lsi);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001251 }
1252 }
1253 }
1254 }
1255
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001256 if (si->init_func) {
1257 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1258 (unsigned)si->init_func, si->name);
1259 si->init_func();
1260 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1261 }
1262
1263 if (si->init_array) {
1264 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1265 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001266 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001267 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1268 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001269
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001270}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001271
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001272static void call_destructors(soinfo *si)
1273{
1274 if (si->fini_array) {
1275 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1276 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001277 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001278 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1279 }
1280
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001281 if (si->fini_func) {
1282 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1283 (unsigned)si->fini_func, si->name);
1284 si->fini_func();
1285 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1286 }
1287}
1288
1289/* Force any of the closed stdin, stdout and stderr to be associated with
1290 /dev/null. */
1291static int nullify_closed_stdio (void)
1292{
1293 int dev_null, i, status;
1294 int return_value = 0;
1295
David 'Digit' Turner16084162012-06-12 16:25:37 +02001296 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001297 if (dev_null < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001298 DL_ERR("Cannot open /dev/null.");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001299 return -1;
1300 }
1301 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1302
1303 /* If any of the stdio file descriptors is valid and not associated
1304 with /dev/null, dup /dev/null to it. */
1305 for (i = 0; i < 3; i++) {
1306 /* If it is /dev/null already, we are done. */
1307 if (i == dev_null)
1308 continue;
1309
1310 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1311 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1312 can be interrupted but we do this just to be safe. */
1313 do {
1314 status = fcntl(i, F_GETFL);
1315 } while (status < 0 && errno == EINTR);
1316
1317 /* If file is openned, we are good. */
1318 if (status >= 0)
1319 continue;
1320
1321 /* The only error we allow is that the file descriptor does not
1322 exist, in which case we dup /dev/null to it. */
1323 if (errno != EBADF) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001324 DL_ERR("nullify_stdio: unhandled error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001325 return_value = -1;
1326 continue;
1327 }
1328
1329 /* Try dupping /dev/null to this stdio file descriptor and
1330 repeat if there is a signal. Note that any errors in closing
1331 the stdio descriptor are lost. */
1332 do {
1333 status = dup2(dev_null, i);
1334 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001335
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001336 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001337 DL_ERR("nullify_stdio: dup2 error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001338 return_value = -1;
1339 continue;
1340 }
1341 }
1342
1343 /* If /dev/null is not one of the stdio file descriptors, close it. */
1344 if (dev_null > 2) {
1345 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001346 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001347 status = close(dev_null);
1348 } while (status < 0 && errno == EINTR);
1349
1350 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001351 DL_ERR("nullify_stdio: close error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001352 return_value = -1;
1353 }
1354 }
1355
1356 return return_value;
1357}
1358
David 'Digit' Turner16084162012-06-12 16:25:37 +02001359static int soinfo_link_image(soinfo *si, unsigned wr_offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001360{
1361 unsigned *d;
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001362 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001363 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001364 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001365 int phnum = si->phnum;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001366 int relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001367
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001368 /* We can't debug anything until the linker is relocated */
1369 if (!relocating_linker) {
1370 INFO("[ %5d linking %s ]\n", pid, si->name);
1371 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1372 si->base, si->flags);
1373 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001374
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001375 /* Extract dynamic section */
1376 si->dynamic = phdr_table_get_dynamic_section(phdr, phnum, base);
1377 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001378 if (!relocating_linker) {
1379 DL_ERR("%5d missing PT_DYNAMIC?!", pid);
1380 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001381 goto fail;
1382 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001383 if (!relocating_linker) {
1384 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1385 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001386 }
1387
1388#ifdef ANDROID_ARM_LINKER
1389 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1390 &si->ARM_exidx, &si->ARM_exidx_count);
1391#endif
1392
Nick Kralevich468319c2011-11-11 15:53:17 -08001393 if (si->flags & (FLAG_EXE | FLAG_LINKER)) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001394 if (phdr_table_unprotect_segments(si->phdr,
1395 si->phnum,
1396 si->load_bias) < 0) {
1397 /* We can't call DL_ERR if the linker's relocations haven't
1398 * been performed yet */
1399 if (!relocating_linker) {
1400 DL_ERR("%5d Can't unprotect segments for %s: %s",
1401 pid, si->name, strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001402 }
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001403 goto fail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001404 }
1405 }
1406
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001407 /* extract useful information from dynamic section */
1408 for(d = si->dynamic; *d; d++){
1409 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1410 switch(*d++){
1411 case DT_HASH:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001412 si->nbucket = ((unsigned *) (base + *d))[0];
1413 si->nchain = ((unsigned *) (base + *d))[1];
1414 si->bucket = (unsigned *) (base + *d + 8);
1415 si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001416 break;
1417 case DT_STRTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001418 si->strtab = (const char *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001419 break;
1420 case DT_SYMTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001421 si->symtab = (Elf32_Sym *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001422 break;
1423 case DT_PLTREL:
1424 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001425 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001426 goto fail;
1427 }
1428 break;
1429 case DT_JMPREL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001430 si->plt_rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001431 break;
1432 case DT_PLTRELSZ:
1433 si->plt_rel_count = *d / 8;
1434 break;
1435 case DT_REL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001436 si->rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001437 break;
1438 case DT_RELSZ:
1439 si->rel_count = *d / 8;
1440 break;
1441 case DT_PLTGOT:
1442 /* Save this in case we decide to do lazy binding. We don't yet. */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001443 si->plt_got = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001444 break;
1445 case DT_DEBUG:
1446 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1447 *d = (int) &_r_debug;
1448 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001449 case DT_RELA:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001450 DL_ERR("%5d DT_RELA not supported", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001451 goto fail;
1452 case DT_INIT:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001453 si->init_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001454 DEBUG("%5d %s constructors (init func) found at %p\n",
1455 pid, si->name, si->init_func);
1456 break;
1457 case DT_FINI:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001458 si->fini_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001459 DEBUG("%5d %s destructors (fini func) found at %p\n",
1460 pid, si->name, si->fini_func);
1461 break;
1462 case DT_INIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001463 si->init_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001464 DEBUG("%5d %s constructors (init_array) found at %p\n",
1465 pid, si->name, si->init_array);
1466 break;
1467 case DT_INIT_ARRAYSZ:
1468 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1469 break;
1470 case DT_FINI_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001471 si->fini_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001472 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1473 pid, si->name, si->fini_array);
1474 break;
1475 case DT_FINI_ARRAYSZ:
1476 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1477 break;
1478 case DT_PREINIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001479 si->preinit_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001480 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1481 pid, si->name, si->preinit_array);
1482 break;
1483 case DT_PREINIT_ARRAYSZ:
1484 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1485 break;
1486 case DT_TEXTREL:
1487 /* TODO: make use of this. */
1488 /* this means that we might have to write into where the text
1489 * segment was loaded during relocation... Do something with
1490 * it.
1491 */
1492 DEBUG("%5d Text segment should be writable during relocation.\n",
1493 pid);
1494 break;
1495 }
1496 }
1497
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001498 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001499 pid, si->base, si->strtab, si->symtab);
1500
1501 if((si->strtab == 0) || (si->symtab == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001502 DL_ERR("%5d missing essential tables", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001503 goto fail;
1504 }
1505
Matt Fischer4fd42c12009-12-31 12:09:10 -06001506 /* if this is the main executable, then load all of the preloads now */
1507 if(si->flags & FLAG_EXE) {
1508 int i;
1509 memset(preloads, 0, sizeof(preloads));
1510 for(i = 0; ldpreload_names[i] != NULL; i++) {
1511 soinfo *lsi = find_library(ldpreload_names[i]);
1512 if(lsi == 0) {
1513 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
1514 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
1515 pid, ldpreload_names[i], si->name, tmp_err_buf);
1516 goto fail;
1517 }
1518 lsi->refcount++;
1519 preloads[i] = lsi;
1520 }
1521 }
1522
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001523 for(d = si->dynamic; *d; d += 2) {
1524 if(d[0] == DT_NEEDED){
1525 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001526 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001527 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001528 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Erik Gillingd00d23a2009-07-22 17:06:11 -07001529 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
Dima Zavin03531952009-05-29 17:30:25 -07001530 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001531 goto fail;
1532 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001533 /* Save the soinfo of the loaded DT_NEEDED library in the payload
1534 of the DT_NEEDED entry itself, so that we can retrieve the
1535 soinfo directly later from the dynamic segment. This is a hack,
1536 but it allows us to map from DT_NEEDED to soinfo efficiently
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001537 later on when we resolve relocations, trying to look up a symbol
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001538 with dlsym().
1539 */
1540 d[1] = (unsigned)lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001541 lsi->refcount++;
1542 }
1543 }
1544
1545 if(si->plt_rel) {
1546 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
David 'Digit' Turner16084162012-06-12 16:25:37 +02001547 if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001548 goto fail;
1549 }
1550 if(si->rel) {
1551 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
David 'Digit' Turner16084162012-06-12 16:25:37 +02001552 if(soinfo_relocate(si, si->rel, si->rel_count))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001553 goto fail;
1554 }
1555
1556 si->flags |= FLAG_LINKED;
1557 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1558
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001559 /* All relocations are done, we can protect our segments back to
1560 * read-only. */
1561 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1562 DL_ERR("%5d Can't protect segments for %s: %s",
1563 pid, si->name, strerror(errno));
1564 goto fail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001565 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001566
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001567 /* We can also turn on GNU RELRO protection */
1568 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
1569 DL_ERR("%5d Can't enable GNU RELRO protection for %s: %s",
1570 pid, si->name, strerror(errno));
1571 goto fail;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001572 }
1573
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001574 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1575 stdout and stderr to close a security hole described in:
1576
1577 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1578
1579 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001580 if (program_is_setuid)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001581 nullify_closed_stdio ();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001582 notify_gdb_of_load(si);
1583 return 0;
1584
1585fail:
Dima Zavina7161902010-08-17 15:56:40 -07001586 ERROR("failed to link %s\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001587 si->flags |= FLAG_ERROR;
1588 return -1;
1589}
1590
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001591static void parse_library_path(const char *path, char *delim)
David Bartleybc3a5c22009-06-02 18:27:28 -07001592{
1593 size_t len;
1594 char *ldpaths_bufp = ldpaths_buf;
1595 int i = 0;
1596
1597 len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf));
1598
1599 while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) {
1600 if (*ldpaths[i] != '\0')
1601 ++i;
1602 }
1603
1604 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1605 * last char isn't '\0' (i.e. not originally a delim). */
1606 if (i > 0 && len >= sizeof(ldpaths_buf) &&
1607 ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') {
1608 ldpaths[i - 1] = NULL;
1609 } else {
1610 ldpaths[i] = NULL;
1611 }
1612}
1613
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001614static void parse_preloads(const char *path, char *delim)
Matt Fischer4fd42c12009-12-31 12:09:10 -06001615{
1616 size_t len;
1617 char *ldpreloads_bufp = ldpreloads_buf;
1618 int i = 0;
1619
1620 len = strlcpy(ldpreloads_buf, path, sizeof(ldpreloads_buf));
1621
1622 while (i < LDPRELOAD_MAX && (ldpreload_names[i] = strsep(&ldpreloads_bufp, delim))) {
1623 if (*ldpreload_names[i] != '\0') {
1624 ++i;
1625 }
1626 }
1627
1628 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1629 * last char isn't '\0' (i.e. not originally a delim). */
1630 if (i > 0 && len >= sizeof(ldpreloads_buf) &&
1631 ldpreloads_buf[sizeof(ldpreloads_buf) - 2] != '\0') {
1632 ldpreload_names[i - 1] = NULL;
1633 } else {
1634 ldpreload_names[i] = NULL;
1635 }
1636}
1637
Nick Kralevich468319c2011-11-11 15:53:17 -08001638/*
1639 * This code is called after the linker has linked itself and
1640 * fixed it's own GOT. It is safe to make references to externs
1641 * and other non-local data at this point.
1642 */
1643static unsigned __linker_init_post_relocation(unsigned **elfdata)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001644{
1645 static soinfo linker_soinfo;
1646
1647 int argc = (int) *elfdata;
1648 char **argv = (char**) (elfdata + 1);
Stephen Smalleybb440552012-01-20 10:59:15 -08001649 unsigned *vecs = (unsigned*) (argv + argc + 1);
1650 unsigned *v;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001651 soinfo *si;
1652 struct link_map * map;
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001653 const char *ldpath_env = NULL;
1654 const char *ldpreload_env = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001655
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001656 /* NOTE: we store the elfdata pointer on a special location
1657 * of the temporary TLS area in order to pass it to
1658 * the C Library's runtime initializer.
1659 *
1660 * The initializer must clear the slot and reset the TLS
1661 * to point to a different location to ensure that no other
1662 * shared library constructor can access it.
1663 */
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001664 __libc_init_tls(elfdata);
1665
1666 pid = getpid();
1667
1668#if TIMING
1669 struct timeval t0, t1;
1670 gettimeofday(&t0, 0);
1671#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001672
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001673 /* Initialize environment functions, and get to the ELF aux vectors table */
1674 vecs = linker_env_init(vecs);
1675
Stephen Smalley861b42a2012-01-13 07:48:11 -05001676 /* Check auxv for AT_SECURE first to see if program is setuid, setgid,
1677 has file caps, or caused a SELinux/AppArmor domain transition. */
1678 for (v = vecs; v[0]; v += 2) {
1679 if (v[0] == AT_SECURE) {
1680 /* kernel told us whether to enable secure mode */
1681 program_is_setuid = v[1];
1682 goto sanitize;
1683 }
1684 }
1685
1686 /* Kernel did not provide AT_SECURE - fall back on legacy test. */
1687 program_is_setuid = (getuid() != geteuid()) || (getgid() != getegid());
1688
1689sanitize:
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001690 /* Sanitize environment if we're loading a setuid program */
1691 if (program_is_setuid)
1692 linker_env_secure();
1693
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001694 debugger_init();
1695
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001696 /* Get a few environment variables */
1697 {
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07001698#if LINKER_DEBUG
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001699 const char* env;
1700 env = linker_env_get("DEBUG"); /* XXX: TODO: Change to LD_DEBUG */
1701 if (env)
1702 debug_verbosity = atoi(env);
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07001703#endif
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001704
1705 /* Normally, these are cleaned by linker_env_secure, but the test
1706 * against program_is_setuid doesn't cost us anything */
1707 if (!program_is_setuid) {
1708 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1709 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001710 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001711 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001712
1713 INFO("[ android linker & debugger ]\n");
1714 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
1715
David 'Digit' Turner16084162012-06-12 16:25:37 +02001716 si = soinfo_alloc(argv[0]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001717 if(si == 0) {
1718 exit(-1);
1719 }
1720
1721 /* bootstrap the link map, the main exe always needs to be first */
1722 si->flags |= FLAG_EXE;
1723 map = &(si->linkmap);
1724
1725 map->l_addr = 0;
1726 map->l_name = argv[0];
1727 map->l_prev = NULL;
1728 map->l_next = NULL;
1729
1730 _r_debug.r_map = map;
1731 r_debug_tail = map;
1732
1733 /* gdb expects the linker to be in the debug shared object list,
1734 * and we need to make sure that the reported load address is zero.
1735 * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
David 'Digit' Turner16084162012-06-12 16:25:37 +02001736 * is. Don't use soinfo_alloc(), because the linker shouldn't
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001737 * be on the soinfo list.
1738 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001739 strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001740 linker_soinfo.flags = 0;
1741 linker_soinfo.base = 0; // This is the important part; must be zero.
1742 insert_soinfo_into_debug_map(&linker_soinfo);
1743
1744 /* extract information passed from the kernel */
1745 while(vecs[0] != 0){
1746 switch(vecs[0]){
1747 case AT_PHDR:
1748 si->phdr = (Elf32_Phdr*) vecs[1];
1749 break;
1750 case AT_PHNUM:
1751 si->phnum = (int) vecs[1];
1752 break;
1753 case AT_ENTRY:
1754 si->entry = vecs[1];
1755 break;
1756 }
1757 vecs += 2;
1758 }
1759
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001760 /* Compute the value of si->base. We can't rely on the fact that
1761 * the first entry is the PHDR because this will not be true
1762 * for certain executables (e.g. some in the NDK unit test suite)
1763 */
1764 int nn;
1765 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001766 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001767 si->load_bias = 0;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001768 for ( nn = 0; nn < si->phnum; nn++ ) {
1769 if (si->phdr[nn].p_type == PT_PHDR) {
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001770 si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
1771 si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001772 break;
1773 }
1774 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001775 si->dynamic = (unsigned *)-1;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001776 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001777
David Bartleybc3a5c22009-06-02 18:27:28 -07001778 /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001779 if (ldpath_env)
David Bartleybc3a5c22009-06-02 18:27:28 -07001780 parse_library_path(ldpath_env, ":");
1781
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001782 if (ldpreload_env) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001783 parse_preloads(ldpreload_env, " :");
1784 }
1785
David 'Digit' Turner16084162012-06-12 16:25:37 +02001786 if(soinfo_link_image(si, 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -07001787 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
1788 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
1789 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001790 exit(-1);
1791 }
1792
David 'Digit' Turner16084162012-06-12 16:25:37 +02001793 soinfo_call_constructors(si);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001794
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07001795#if ALLOW_SYMBOLS_FROM_MAIN
1796 /* Set somain after we've loaded all the libraries in order to prevent
1797 * linking of symbols back to the main image, which is not set up at that
1798 * point yet.
1799 */
1800 somain = si;
1801#endif
1802
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001803#if TIMING
1804 gettimeofday(&t1,NULL);
1805 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
1806 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1807 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1808 ));
1809#endif
1810#if STATS
1811 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
1812 linker_stats.reloc[RELOC_ABSOLUTE],
1813 linker_stats.reloc[RELOC_RELATIVE],
1814 linker_stats.reloc[RELOC_COPY],
1815 linker_stats.reloc[RELOC_SYMBOL]);
1816#endif
1817#if COUNT_PAGES
1818 {
1819 unsigned n;
1820 unsigned i;
1821 unsigned count = 0;
1822 for(n = 0; n < 4096; n++){
1823 if(bitmask[n]){
1824 unsigned x = bitmask[n];
1825 for(i = 0; i < 8; i++){
1826 if(x & 1) count++;
1827 x >>= 1;
1828 }
1829 }
1830 }
1831 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
1832 }
1833#endif
1834
1835#if TIMING || STATS || COUNT_PAGES
1836 fflush(stdout);
1837#endif
1838
1839 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
1840 si->entry);
1841 return si->entry;
1842}
Nick Kralevich468319c2011-11-11 15:53:17 -08001843
1844/*
1845 * Find the value of AT_BASE passed to us by the kernel. This is the load
1846 * location of the linker.
1847 */
1848static unsigned find_linker_base(unsigned **elfdata) {
1849 int argc = (int) *elfdata;
1850 char **argv = (char**) (elfdata + 1);
1851 unsigned *vecs = (unsigned*) (argv + argc + 1);
1852 while (vecs[0] != 0) {
1853 vecs++;
1854 }
1855
1856 /* The end of the environment block is marked by two NULL pointers */
1857 vecs++;
1858
1859 while(vecs[0]) {
1860 if (vecs[0] == AT_BASE) {
1861 return vecs[1];
1862 }
1863 vecs += 2;
1864 }
1865
1866 return 0; // should never happen
1867}
1868
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001869/* Compute the load-bias of an existing executable. This shall only
1870 * be used to compute the load bias of an executable or shared library
1871 * that was loaded by the kernel itself.
1872 *
1873 * Input:
1874 * elf -> address of ELF header, assumed to be at the start of the file.
1875 * Return:
1876 * load bias, i.e. add the value of any p_vaddr in the file to get
1877 * the corresponding address in memory.
1878 */
1879static Elf32_Addr
1880get_elf_exec_load_bias(const Elf32_Ehdr* elf)
1881{
1882 Elf32_Addr offset = elf->e_phoff;
1883 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
1884 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
1885 const Elf32_Phdr* phdr;
1886
1887 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
1888 if (phdr->p_type == PT_LOAD) {
1889 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
1890 }
1891 }
1892 return 0;
1893}
1894
Nick Kralevich468319c2011-11-11 15:53:17 -08001895/*
1896 * This is the entry point for the linker, called from begin.S. This
1897 * method is responsible for fixing the linker's own relocations, and
1898 * then calling __linker_init_post_relocation().
1899 *
1900 * Because this method is called before the linker has fixed it's own
1901 * relocations, any attempt to reference an extern variable, extern
1902 * function, or other GOT reference will generate a segfault.
1903 */
1904unsigned __linker_init(unsigned **elfdata) {
1905 unsigned linker_addr = find_linker_base(elfdata);
1906 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
1907 Elf32_Phdr *phdr =
1908 (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
1909
1910 soinfo linker_so;
1911 memset(&linker_so, 0, sizeof(soinfo));
1912
1913 linker_so.base = linker_addr;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001914 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001915 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Nick Kralevich468319c2011-11-11 15:53:17 -08001916 linker_so.dynamic = (unsigned *) -1;
1917 linker_so.phdr = phdr;
1918 linker_so.phnum = elf_hdr->e_phnum;
1919 linker_so.flags |= FLAG_LINKER;
Nick Kralevich468319c2011-11-11 15:53:17 -08001920
David 'Digit' Turner16084162012-06-12 16:25:37 +02001921 if (soinfo_link_image(&linker_so, 0)) {
Nick Kralevich468319c2011-11-11 15:53:17 -08001922 // It would be nice to print an error message, but if the linker
1923 // can't link itself, there's no guarantee that we'll be able to
1924 // call write() (because it involves a GOT reference).
1925 //
1926 // This situation should never occur unless the linker itself
1927 // is corrupt.
1928 exit(-1);
1929 }
1930
1931 // We have successfully fixed our own relocations. It's safe to run
1932 // the main part of the linker now.
1933 return __linker_init_post_relocation(elfdata);
1934}