blob: 0a931308b4e6f4e9626cd2dd50fd838cc706683f [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{
Elliott Hughes8dfc0732012-07-27 15:30:51 -0700651 off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800652 if (sz < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700653 DL_ERR("lseek() failed!");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800654 return 0;
655 }
656
Elliott Hughes8dfc0732012-07-27 15:30:51 -0700657 prelink_info_t info;
658 int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
659 if (rc != sizeof(info)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800660 WARN("Could not read prelink_info_t structure for `%s`\n", name);
661 return 0;
662 }
663
David 'Digit' Turner16084162012-06-12 16:25:37 +0200664 if (memcmp(info.tag, "PRE ", 4)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800665 WARN("`%s` is not a prelinked library\n", name);
666 return 0;
667 }
668
669 return (unsigned long)info.mmap_addr;
670}
671
David 'Digit' Turner16084162012-06-12 16:25:37 +0200672/* verify_elf_header
673 * Verifies the content of an ELF header.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800674 *
675 * Args:
676 *
677 * Returns:
678 * 0 on success
679 * -1 if no valid ELF object is found @ base.
680 */
681static int
David 'Digit' Turner16084162012-06-12 16:25:37 +0200682verify_elf_header(const Elf32_Ehdr* hdr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800683{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800684 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
685 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
686 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
687 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
688
689 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800690#ifdef ANDROID_ARM_LINKER
691 if (hdr->e_machine != EM_ARM) return -1;
692#elif defined(ANDROID_X86_LINKER)
693 if (hdr->e_machine != EM_386) return -1;
694#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800695 return 0;
696}
697
698
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800699static soinfo *
700load_library(const char *name)
701{
702 int fd = open_library(name);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200703 int ret, cnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800704 unsigned ext_sz;
705 unsigned req_base;
Erik Gillingfde86422009-07-28 20:28:19 -0700706 const char *bname;
Ji-Hwan Lee75917c82012-05-25 22:36:00 +0900707 struct stat sb;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800708 soinfo *si = NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200709 Elf32_Ehdr header[1];
710 int phdr_count;
711 void* phdr_mmap = NULL;
712 Elf32_Addr phdr_size;
713 const Elf32_Phdr* phdr_table;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800714
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200715 void* load_start = NULL;
716 Elf32_Addr load_size = 0;
717 Elf32_Addr load_bias = 0;
718
Ji-Hwan Lee75917c82012-05-25 22:36:00 +0900719 if (fd == -1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700720 DL_ERR("Library '%s' not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800721 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700722 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800723
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200724 /* Read the ELF header first */
725 ret = TEMP_FAILURE_RETRY(read(fd, (void*)header, sizeof(header)));
726 if (ret < 0) {
727 DL_ERR("%5d can't read file %s: %s", pid, name, strerror(errno));
728 goto fail;
729 }
730 if (ret != (int)sizeof(header)) {
731 DL_ERR("%5d too small to be an ELF executable: %s", pid, name);
732 goto fail;
733 }
734 if (verify_elf_header(header) < 0) {
735 DL_ERR("%5d not a valid ELF executable: %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800736 goto fail;
737 }
738
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200739 /* Then read the program header table */
740 ret = phdr_table_load(fd, header->e_phoff, header->e_phnum,
741 &phdr_mmap, &phdr_size, &phdr_table);
742 if (ret < 0) {
743 DL_ERR("%5d can't load program header table: %s: %s", pid,
744 name, strerror(errno));
745 goto fail;
746 }
747 phdr_count = header->e_phnum;
748
749 /* Get the load extents and the prelinked load address, if any */
750 ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
751 if (ext_sz == 0) {
752 DL_ERR("%5d no loadable segments in file: %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800753 goto fail;
754 }
755
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200756 req_base = (unsigned) is_prelinked(fd, name);
757 if (req_base == (unsigned)-1) {
758 DL_ERR("%5d can't read end of library: %s: %s", pid, name,
759 strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800760 goto fail;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200761 }
762 if (req_base != 0) {
763 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
764 pid, name, req_base);
765 } else {
766 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
767 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200768
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800769 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
770 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
771
772 /* Now configure the soinfo struct where we'll store all of our data
773 * for the ELF object. If the loading fails, we waste the entry, but
774 * same thing would happen if we failed during linking. Configuring the
775 * soinfo struct here is a lot more convenient.
776 */
Erik Gillingfde86422009-07-28 20:28:19 -0700777 bname = strrchr(name, '/');
David 'Digit' Turner16084162012-06-12 16:25:37 +0200778 si = soinfo_alloc(bname ? bname + 1 : name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800779 if (si == NULL)
780 goto fail;
781
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200782 /* Reserve address space for all loadable segments */
783 ret = phdr_table_reserve_memory(phdr_table,
784 phdr_count,
785 req_base,
786 &load_start,
787 &load_size,
788 &load_bias);
789 if (ret < 0) {
790 DL_ERR("%5d Can't reserve %d bytes from 0x%08x in address space for %s: %s",
791 pid, ext_sz, req_base, name, strerror(errno));
792 goto fail;
793 }
794
795 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
796 pid, name, load_start, load_size);
797
798 /* Map all the segments in our address space with default protections */
799 ret = phdr_table_load_segments(phdr_table,
800 phdr_count,
801 load_start,
802 load_size,
803 load_bias,
804 fd);
805 if (ret < 0) {
806 DL_ERR("%5d Can't map loadable segments for %s: %s",
807 pid, name, strerror(errno));
808 goto fail;
809 }
810
811 /* Unprotect the segments, i.e. make them writable, to allow
812 * relocations to work properly. We will later call
813 * phdr_table_protect_segments() after all of them are applied
814 * and all constructors are run.
815 */
816 ret = phdr_table_unprotect_segments(phdr_table,
817 phdr_count,
818 load_bias);
819 if (ret < 0) {
820 DL_ERR("%5d Can't unprotect loadable segments for %s: %s",
821 pid, name, strerror(errno));
822 goto fail;
823 }
824
825 si->base = (Elf32_Addr) load_start;
826 si->size = load_size;
827 si->load_bias = load_bias;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800828 si->flags = 0;
829 si->entry = 0;
830 si->dynamic = (unsigned *)-1;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200831 si->phnum = phdr_count;
832 si->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
833 if (si->phdr == NULL) {
834 DL_ERR("%5d Can't find loaded PHDR for %s",
835 pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800836 goto fail;
837 }
838
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200839 phdr_table_unload(phdr_mmap, phdr_size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800840 close(fd);
841 return si;
842
843fail:
David 'Digit' Turner16084162012-06-12 16:25:37 +0200844 if (si) soinfo_free(si);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200845 if (phdr_mmap != NULL) {
846 phdr_table_unload(phdr_mmap, phdr_size);
847 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800848 close(fd);
849 return NULL;
850}
851
852static soinfo *
853init_library(soinfo *si)
854{
855 unsigned wr_offset = 0xffffffff;
856
857 /* At this point we know that whatever is loaded @ base is a valid ELF
858 * shared library whose segments are properly mapped in. */
859 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
860 pid, si->base, si->size, si->name);
861
David 'Digit' Turner16084162012-06-12 16:25:37 +0200862 if(soinfo_link_image(si, wr_offset)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800863 /* We failed to link. However, we can only restore libbase
864 ** if no additional libraries have moved it since we updated it.
865 */
866 munmap((void *)si->base, si->size);
867 return NULL;
868 }
869
870 return si;
871}
872
873soinfo *find_library(const char *name)
874{
875 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700876 const char *bname;
877
878#if ALLOW_SYMBOLS_FROM_MAIN
879 if (name == NULL)
880 return somain;
881#else
882 if (name == NULL)
883 return NULL;
884#endif
885
886 bname = strrchr(name, '/');
Erik Gillingfde86422009-07-28 20:28:19 -0700887 bname = bname ? bname + 1 : name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800888
889 for(si = solist; si != 0; si = si->next){
Erik Gillingfde86422009-07-28 20:28:19 -0700890 if(!strcmp(bname, si->name)) {
Erik Gilling30eb4022009-08-13 16:05:30 -0700891 if(si->flags & FLAG_ERROR) {
892 DL_ERR("%5d '%s' failed to load previously", pid, bname);
893 return NULL;
894 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800895 if(si->flags & FLAG_LINKED) return si;
Erik Gillingd00d23a2009-07-22 17:06:11 -0700896 DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name);
Dima Zavin2e855792009-05-20 18:28:09 -0700897 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800898 }
899 }
900
901 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
902 si = load_library(name);
903 if(si == NULL)
904 return NULL;
905 return init_library(si);
906}
907
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100908/* TODO:
909 * notify gdb of unload
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800910 * for non-prelinked libraries, find a way to decrement libbase
911 */
912static void call_destructors(soinfo *si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200913unsigned soinfo_unload(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800914{
915 unsigned *d;
916 if (si->refcount == 1) {
917 TRACE("%5d unloading '%s'\n", pid, si->name);
918 call_destructors(si);
919
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800920 /*
921 * Make sure that we undo the PT_GNU_RELRO protections we added
David 'Digit' Turner16084162012-06-12 16:25:37 +0200922 * in soinfo_link_image. This is needed to undo the DT_NEEDED hack below.
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800923 */
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200924 if (phdr_table_unprotect_gnu_relro(si->phdr, si->phnum,
925 si->load_bias) < 0) {
926 DL_ERR("%5d %s: could not undo GNU_RELRO protections. "
927 "Expect a crash soon. errno=%d (%s)",
928 pid, si->name, errno, strerror(errno));
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800929 }
930
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800931 for(d = si->dynamic; *d; d += 2) {
932 if(d[0] == DT_NEEDED){
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700933 soinfo *lsi = (soinfo *)d[1];
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800934
935 // The next line will segfault if the we don't undo the
936 // PT_GNU_RELRO protections (see comments above and in
David 'Digit' Turner16084162012-06-12 16:25:37 +0200937 // soinfo_link_image().
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700938 d[1] = 0;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800939
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700940 if (validate_soinfo(lsi)) {
941 TRACE("%5d %s needs to unload %s\n", pid,
942 si->name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200943 soinfo_unload(lsi);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700944 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800945 else
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700946 DL_ERR("%5d %s: could not unload dependent library",
947 pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800948 }
949 }
950
951 munmap((char *)si->base, si->size);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700952 notify_gdb_of_unload(si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200953 soinfo_free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800954 si->refcount = 0;
955 }
956 else {
957 si->refcount--;
958 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
959 pid, si->name, si->refcount);
960 }
961 return si->refcount;
962}
963
964/* TODO: don't use unsigned for addrs below. It works, but is not
965 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
966 * long.
967 */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200968static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800969{
970 Elf32_Sym *symtab = si->symtab;
971 const char *strtab = si->strtab;
972 Elf32_Sym *s;
973 unsigned base;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900974 Elf32_Addr offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800975 Elf32_Rel *start = rel;
976 unsigned idx;
977
978 for (idx = 0; idx < count; ++idx) {
979 unsigned type = ELF32_R_TYPE(rel->r_info);
980 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200981 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800982 unsigned sym_addr = 0;
983 char *sym_name = NULL;
984
985 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
986 si->name, idx);
987 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -0700988 sym_name = (char *)(strtab + symtab[sym].st_name);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200989 s = soinfo_do_lookup(si, sym_name, &offset);
Doug Kwane8238072009-10-26 12:05:23 -0700990 if(s == NULL) {
991 /* We only allow an undefined symbol if this is a weak
992 reference.. */
993 s = &symtab[sym];
994 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
995 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
996 return -1;
997 }
998
999 /* IHI0044C AAELF 4.5.1.1:
1000
1001 Libraries are not searched to resolve weak references.
1002 It is not an error for a weak reference to remain
1003 unsatisfied.
1004
1005 During linking, the value of an undefined weak reference is:
1006 - Zero if the relocation type is absolute
1007 - The address of the place if the relocation is pc-relative
1008 - The address of nominial base address if the relocation
1009 type is base-relative.
1010 */
1011
1012 switch (type) {
1013#if defined(ANDROID_ARM_LINKER)
1014 case R_ARM_JUMP_SLOT:
1015 case R_ARM_GLOB_DAT:
1016 case R_ARM_ABS32:
1017 case R_ARM_RELATIVE: /* Don't care. */
1018 case R_ARM_NONE: /* Don't care. */
1019#elif defined(ANDROID_X86_LINKER)
1020 case R_386_JUMP_SLOT:
1021 case R_386_GLOB_DAT:
1022 case R_386_32:
1023 case R_386_RELATIVE: /* Dont' care. */
1024#endif /* ANDROID_*_LINKER */
1025 /* sym_addr was initialized to be zero above or relocation
1026 code below does not care about value of sym_addr.
1027 No need to do anything. */
1028 break;
1029
1030#if defined(ANDROID_X86_LINKER)
1031 case R_386_PC32:
1032 sym_addr = reloc;
1033 break;
1034#endif /* ANDROID_X86_LINKER */
1035
1036#if defined(ANDROID_ARM_LINKER)
1037 case R_ARM_COPY:
1038 /* Fall through. Can't really copy if weak symbol is
1039 not found in run-time. */
1040#endif /* ANDROID_ARM_LINKER */
1041 default:
1042 DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n",
1043 pid, type, rel, (int) (rel - start));
1044 return -1;
1045 }
1046 } else {
1047 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001048#if 0
1049 if((base == 0) && (si->base != 0)){
1050 /* linking from libraries to main image is bad */
Erik Gillingd00d23a2009-07-22 17:06:11 -07001051 DL_ERR("%5d cannot locate '%s'...",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001052 pid, strtab + symtab[sym].st_name);
1053 return -1;
1054 }
1055#endif
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001056 sym_addr = (unsigned)(s->st_value + offset);
Doug Kwane8238072009-10-26 12:05:23 -07001057 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001058 COUNT_RELOC(RELOC_SYMBOL);
1059 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001060 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001061 }
1062
1063/* TODO: This is ugly. Split up the relocations by arch into
1064 * different files.
1065 */
1066 switch(type){
1067#if defined(ANDROID_ARM_LINKER)
1068 case R_ARM_JUMP_SLOT:
1069 COUNT_RELOC(RELOC_ABSOLUTE);
1070 MARK(rel->r_offset);
1071 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1072 reloc, sym_addr, sym_name);
1073 *((unsigned*)reloc) = sym_addr;
1074 break;
1075 case R_ARM_GLOB_DAT:
1076 COUNT_RELOC(RELOC_ABSOLUTE);
1077 MARK(rel->r_offset);
1078 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1079 reloc, sym_addr, sym_name);
1080 *((unsigned*)reloc) = sym_addr;
1081 break;
1082 case R_ARM_ABS32:
1083 COUNT_RELOC(RELOC_ABSOLUTE);
1084 MARK(rel->r_offset);
1085 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1086 reloc, sym_addr, sym_name);
1087 *((unsigned*)reloc) += sym_addr;
1088 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001089 case R_ARM_REL32:
1090 COUNT_RELOC(RELOC_RELATIVE);
1091 MARK(rel->r_offset);
1092 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1093 reloc, sym_addr, rel->r_offset, sym_name);
1094 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1095 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001096#elif defined(ANDROID_X86_LINKER)
1097 case R_386_JUMP_SLOT:
1098 COUNT_RELOC(RELOC_ABSOLUTE);
1099 MARK(rel->r_offset);
1100 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1101 reloc, sym_addr, sym_name);
1102 *((unsigned*)reloc) = sym_addr;
1103 break;
1104 case R_386_GLOB_DAT:
1105 COUNT_RELOC(RELOC_ABSOLUTE);
1106 MARK(rel->r_offset);
1107 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1108 reloc, sym_addr, sym_name);
1109 *((unsigned*)reloc) = sym_addr;
1110 break;
1111#endif /* ANDROID_*_LINKER */
1112
1113#if defined(ANDROID_ARM_LINKER)
1114 case R_ARM_RELATIVE:
1115#elif defined(ANDROID_X86_LINKER)
1116 case R_386_RELATIVE:
1117#endif /* ANDROID_*_LINKER */
1118 COUNT_RELOC(RELOC_RELATIVE);
1119 MARK(rel->r_offset);
1120 if(sym){
Erik Gillingd00d23a2009-07-22 17:06:11 -07001121 DL_ERR("%5d odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001122 return -1;
1123 }
1124 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1125 reloc, si->base);
1126 *((unsigned*)reloc) += si->base;
1127 break;
1128
1129#if defined(ANDROID_X86_LINKER)
1130 case R_386_32:
1131 COUNT_RELOC(RELOC_RELATIVE);
1132 MARK(rel->r_offset);
1133
1134 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1135 reloc, sym_addr, sym_name);
1136 *((unsigned *)reloc) += (unsigned)sym_addr;
1137 break;
1138
1139 case R_386_PC32:
1140 COUNT_RELOC(RELOC_RELATIVE);
1141 MARK(rel->r_offset);
1142 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1143 "+%08x (%08x - %08x) %s\n", pid, reloc,
1144 (sym_addr - reloc), sym_addr, reloc, sym_name);
1145 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1146 break;
1147#endif /* ANDROID_X86_LINKER */
1148
1149#ifdef ANDROID_ARM_LINKER
1150 case R_ARM_COPY:
1151 COUNT_RELOC(RELOC_COPY);
1152 MARK(rel->r_offset);
1153 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1154 reloc, s->st_size, sym_addr, sym_name);
1155 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1156 break;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -07001157 case R_ARM_NONE:
1158 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001159#endif /* ANDROID_ARM_LINKER */
1160
1161 default:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001162 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001163 pid, type, rel, (int) (rel - start));
1164 return -1;
1165 }
1166 rel++;
1167 }
1168 return 0;
1169}
1170
David 'Digit' Turner82156792009-05-18 14:37:41 +02001171/* Please read the "Initialization and Termination functions" functions.
1172 * of the linker design note in bionic/linker/README.TXT to understand
1173 * what the following code is doing.
1174 *
1175 * The important things to remember are:
1176 *
1177 * DT_PREINIT_ARRAY must be called first for executables, and should
1178 * not appear in shared libraries.
1179 *
1180 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1181 *
1182 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1183 *
1184 * DT_FINI_ARRAY must be parsed in reverse order.
1185 */
1186
1187static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001188{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001189 int n, inc = 1;
1190
1191 if (reverse) {
1192 ctor += (count-1);
1193 inc = -1;
1194 }
1195
1196 for(n = count; n > 0; n--) {
1197 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1198 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001199 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001200 void (*func)() = (void (*)()) *ctor;
1201 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001202 if(((int) func == 0) || ((int) func == -1)) continue;
1203 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1204 func();
1205 }
1206}
1207
David 'Digit' Turner16084162012-06-12 16:25:37 +02001208void soinfo_call_constructors(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001209{
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001210 if (si->constructors_called)
1211 return;
1212
Jesse Hallf5d16932012-01-30 15:39:57 -08001213 // Set this before actually calling the constructors, otherwise it doesn't
1214 // protect against recursive constructor calls. One simple example of
1215 // constructor recursion is the libc debug malloc, which is implemented in
1216 // libc_malloc_debug_leak.so:
1217 // 1. The program depends on libc, so libc's constructor is called here.
1218 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001219 // 3. dlopen() calls soinfo_call_constructors() with the newly created
Jesse Hallf5d16932012-01-30 15:39:57 -08001220 // soinfo for libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001221 // 4. The debug so depends on libc, so soinfo_call_constructors() is
Jesse Hallf5d16932012-01-30 15:39:57 -08001222 // called again with the libc soinfo. If it doesn't trigger the early-
1223 // out above, the libc constructor will be called again (recursively!).
1224 si->constructors_called = 1;
1225
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001226 if (si->flags & FLAG_EXE) {
1227 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1228 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1229 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001230 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001231 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1232 } else {
1233 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001234 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
Erik Gillingd00d23a2009-07-22 17:06:11 -07001235 " This is INVALID.", pid, si->name,
Dima Zavin2e855792009-05-20 18:28:09 -07001236 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001237 }
1238 }
1239
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001240 if (si->dynamic) {
1241 unsigned *d;
1242 for(d = si->dynamic; *d; d += 2) {
1243 if(d[0] == DT_NEEDED){
1244 soinfo* lsi = (soinfo *)d[1];
1245 if (!validate_soinfo(lsi)) {
1246 DL_ERR("%5d bad DT_NEEDED pointer in %s",
1247 pid, si->name);
1248 } else {
David 'Digit' Turner16084162012-06-12 16:25:37 +02001249 soinfo_call_constructors(lsi);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001250 }
1251 }
1252 }
1253 }
1254
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001255 if (si->init_func) {
1256 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1257 (unsigned)si->init_func, si->name);
1258 si->init_func();
1259 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1260 }
1261
1262 if (si->init_array) {
1263 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1264 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001265 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001266 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1267 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001268
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001269}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001270
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001271static void call_destructors(soinfo *si)
1272{
1273 if (si->fini_array) {
1274 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1275 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001276 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001277 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1278 }
1279
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001280 if (si->fini_func) {
1281 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1282 (unsigned)si->fini_func, si->name);
1283 si->fini_func();
1284 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1285 }
1286}
1287
1288/* Force any of the closed stdin, stdout and stderr to be associated with
1289 /dev/null. */
1290static int nullify_closed_stdio (void)
1291{
1292 int dev_null, i, status;
1293 int return_value = 0;
1294
David 'Digit' Turner16084162012-06-12 16:25:37 +02001295 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001296 if (dev_null < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001297 DL_ERR("Cannot open /dev/null.");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001298 return -1;
1299 }
1300 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1301
1302 /* If any of the stdio file descriptors is valid and not associated
1303 with /dev/null, dup /dev/null to it. */
1304 for (i = 0; i < 3; i++) {
1305 /* If it is /dev/null already, we are done. */
1306 if (i == dev_null)
1307 continue;
1308
1309 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1310 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1311 can be interrupted but we do this just to be safe. */
1312 do {
1313 status = fcntl(i, F_GETFL);
1314 } while (status < 0 && errno == EINTR);
1315
1316 /* If file is openned, we are good. */
1317 if (status >= 0)
1318 continue;
1319
1320 /* The only error we allow is that the file descriptor does not
1321 exist, in which case we dup /dev/null to it. */
1322 if (errno != EBADF) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001323 DL_ERR("nullify_stdio: unhandled error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001324 return_value = -1;
1325 continue;
1326 }
1327
1328 /* Try dupping /dev/null to this stdio file descriptor and
1329 repeat if there is a signal. Note that any errors in closing
1330 the stdio descriptor are lost. */
1331 do {
1332 status = dup2(dev_null, i);
1333 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001334
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001335 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001336 DL_ERR("nullify_stdio: dup2 error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001337 return_value = -1;
1338 continue;
1339 }
1340 }
1341
1342 /* If /dev/null is not one of the stdio file descriptors, close it. */
1343 if (dev_null > 2) {
1344 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001345 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001346 status = close(dev_null);
1347 } while (status < 0 && errno == EINTR);
1348
1349 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001350 DL_ERR("nullify_stdio: close error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001351 return_value = -1;
1352 }
1353 }
1354
1355 return return_value;
1356}
1357
David 'Digit' Turner16084162012-06-12 16:25:37 +02001358static int soinfo_link_image(soinfo *si, unsigned wr_offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001359{
1360 unsigned *d;
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001361 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001362 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001363 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001364 int phnum = si->phnum;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001365 int relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001366
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001367 /* We can't debug anything until the linker is relocated */
1368 if (!relocating_linker) {
1369 INFO("[ %5d linking %s ]\n", pid, si->name);
1370 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1371 si->base, si->flags);
1372 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001373
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001374 /* Extract dynamic section */
1375 si->dynamic = phdr_table_get_dynamic_section(phdr, phnum, base);
1376 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001377 if (!relocating_linker) {
1378 DL_ERR("%5d missing PT_DYNAMIC?!", pid);
1379 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001380 goto fail;
1381 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001382 if (!relocating_linker) {
1383 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1384 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001385 }
1386
1387#ifdef ANDROID_ARM_LINKER
1388 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1389 &si->ARM_exidx, &si->ARM_exidx_count);
1390#endif
1391
Nick Kralevich468319c2011-11-11 15:53:17 -08001392 if (si->flags & (FLAG_EXE | FLAG_LINKER)) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001393 if (phdr_table_unprotect_segments(si->phdr,
1394 si->phnum,
1395 si->load_bias) < 0) {
1396 /* We can't call DL_ERR if the linker's relocations haven't
1397 * been performed yet */
1398 if (!relocating_linker) {
1399 DL_ERR("%5d Can't unprotect segments for %s: %s",
1400 pid, si->name, strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001401 }
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001402 goto fail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001403 }
1404 }
1405
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001406 /* extract useful information from dynamic section */
1407 for(d = si->dynamic; *d; d++){
1408 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1409 switch(*d++){
1410 case DT_HASH:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001411 si->nbucket = ((unsigned *) (base + *d))[0];
1412 si->nchain = ((unsigned *) (base + *d))[1];
1413 si->bucket = (unsigned *) (base + *d + 8);
1414 si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001415 break;
1416 case DT_STRTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001417 si->strtab = (const char *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001418 break;
1419 case DT_SYMTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001420 si->symtab = (Elf32_Sym *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001421 break;
1422 case DT_PLTREL:
1423 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001424 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001425 goto fail;
1426 }
1427 break;
1428 case DT_JMPREL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001429 si->plt_rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001430 break;
1431 case DT_PLTRELSZ:
1432 si->plt_rel_count = *d / 8;
1433 break;
1434 case DT_REL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001435 si->rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001436 break;
1437 case DT_RELSZ:
1438 si->rel_count = *d / 8;
1439 break;
1440 case DT_PLTGOT:
1441 /* Save this in case we decide to do lazy binding. We don't yet. */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001442 si->plt_got = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001443 break;
1444 case DT_DEBUG:
1445 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1446 *d = (int) &_r_debug;
1447 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001448 case DT_RELA:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001449 DL_ERR("%5d DT_RELA not supported", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001450 goto fail;
1451 case DT_INIT:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001452 si->init_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001453 DEBUG("%5d %s constructors (init func) found at %p\n",
1454 pid, si->name, si->init_func);
1455 break;
1456 case DT_FINI:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001457 si->fini_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001458 DEBUG("%5d %s destructors (fini func) found at %p\n",
1459 pid, si->name, si->fini_func);
1460 break;
1461 case DT_INIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001462 si->init_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001463 DEBUG("%5d %s constructors (init_array) found at %p\n",
1464 pid, si->name, si->init_array);
1465 break;
1466 case DT_INIT_ARRAYSZ:
1467 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1468 break;
1469 case DT_FINI_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001470 si->fini_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001471 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1472 pid, si->name, si->fini_array);
1473 break;
1474 case DT_FINI_ARRAYSZ:
1475 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1476 break;
1477 case DT_PREINIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001478 si->preinit_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001479 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1480 pid, si->name, si->preinit_array);
1481 break;
1482 case DT_PREINIT_ARRAYSZ:
1483 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1484 break;
1485 case DT_TEXTREL:
1486 /* TODO: make use of this. */
1487 /* this means that we might have to write into where the text
1488 * segment was loaded during relocation... Do something with
1489 * it.
1490 */
1491 DEBUG("%5d Text segment should be writable during relocation.\n",
1492 pid);
1493 break;
1494 }
1495 }
1496
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001497 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001498 pid, si->base, si->strtab, si->symtab);
1499
1500 if((si->strtab == 0) || (si->symtab == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001501 DL_ERR("%5d missing essential tables", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001502 goto fail;
1503 }
1504
Matt Fischer4fd42c12009-12-31 12:09:10 -06001505 /* if this is the main executable, then load all of the preloads now */
1506 if(si->flags & FLAG_EXE) {
1507 int i;
1508 memset(preloads, 0, sizeof(preloads));
1509 for(i = 0; ldpreload_names[i] != NULL; i++) {
1510 soinfo *lsi = find_library(ldpreload_names[i]);
1511 if(lsi == 0) {
1512 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
1513 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
1514 pid, ldpreload_names[i], si->name, tmp_err_buf);
1515 goto fail;
1516 }
1517 lsi->refcount++;
1518 preloads[i] = lsi;
1519 }
1520 }
1521
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001522 for(d = si->dynamic; *d; d += 2) {
1523 if(d[0] == DT_NEEDED){
1524 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001525 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001526 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001527 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Erik Gillingd00d23a2009-07-22 17:06:11 -07001528 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
Dima Zavin03531952009-05-29 17:30:25 -07001529 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001530 goto fail;
1531 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001532 /* Save the soinfo of the loaded DT_NEEDED library in the payload
1533 of the DT_NEEDED entry itself, so that we can retrieve the
1534 soinfo directly later from the dynamic segment. This is a hack,
1535 but it allows us to map from DT_NEEDED to soinfo efficiently
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001536 later on when we resolve relocations, trying to look up a symbol
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001537 with dlsym().
1538 */
1539 d[1] = (unsigned)lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001540 lsi->refcount++;
1541 }
1542 }
1543
1544 if(si->plt_rel) {
1545 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
David 'Digit' Turner16084162012-06-12 16:25:37 +02001546 if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001547 goto fail;
1548 }
1549 if(si->rel) {
1550 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
David 'Digit' Turner16084162012-06-12 16:25:37 +02001551 if(soinfo_relocate(si, si->rel, si->rel_count))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001552 goto fail;
1553 }
1554
1555 si->flags |= FLAG_LINKED;
1556 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1557
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001558 /* All relocations are done, we can protect our segments back to
1559 * read-only. */
1560 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1561 DL_ERR("%5d Can't protect segments for %s: %s",
1562 pid, si->name, strerror(errno));
1563 goto fail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001564 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001565
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001566 /* We can also turn on GNU RELRO protection */
1567 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
1568 DL_ERR("%5d Can't enable GNU RELRO protection for %s: %s",
1569 pid, si->name, strerror(errno));
1570 goto fail;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001571 }
1572
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001573 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1574 stdout and stderr to close a security hole described in:
1575
1576 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1577
1578 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001579 if (program_is_setuid)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001580 nullify_closed_stdio ();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001581 notify_gdb_of_load(si);
1582 return 0;
1583
1584fail:
Dima Zavina7161902010-08-17 15:56:40 -07001585 ERROR("failed to link %s\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001586 si->flags |= FLAG_ERROR;
1587 return -1;
1588}
1589
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001590static void parse_library_path(const char *path, char *delim)
David Bartleybc3a5c22009-06-02 18:27:28 -07001591{
1592 size_t len;
1593 char *ldpaths_bufp = ldpaths_buf;
1594 int i = 0;
1595
1596 len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf));
1597
1598 while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) {
1599 if (*ldpaths[i] != '\0')
1600 ++i;
1601 }
1602
1603 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1604 * last char isn't '\0' (i.e. not originally a delim). */
1605 if (i > 0 && len >= sizeof(ldpaths_buf) &&
1606 ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') {
1607 ldpaths[i - 1] = NULL;
1608 } else {
1609 ldpaths[i] = NULL;
1610 }
1611}
1612
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001613static void parse_preloads(const char *path, char *delim)
Matt Fischer4fd42c12009-12-31 12:09:10 -06001614{
1615 size_t len;
1616 char *ldpreloads_bufp = ldpreloads_buf;
1617 int i = 0;
1618
1619 len = strlcpy(ldpreloads_buf, path, sizeof(ldpreloads_buf));
1620
1621 while (i < LDPRELOAD_MAX && (ldpreload_names[i] = strsep(&ldpreloads_bufp, delim))) {
1622 if (*ldpreload_names[i] != '\0') {
1623 ++i;
1624 }
1625 }
1626
1627 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1628 * last char isn't '\0' (i.e. not originally a delim). */
1629 if (i > 0 && len >= sizeof(ldpreloads_buf) &&
1630 ldpreloads_buf[sizeof(ldpreloads_buf) - 2] != '\0') {
1631 ldpreload_names[i - 1] = NULL;
1632 } else {
1633 ldpreload_names[i] = NULL;
1634 }
1635}
1636
Nick Kralevich468319c2011-11-11 15:53:17 -08001637/*
1638 * This code is called after the linker has linked itself and
1639 * fixed it's own GOT. It is safe to make references to externs
1640 * and other non-local data at this point.
1641 */
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001642static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001643{
1644 static soinfo linker_soinfo;
1645
1646 int argc = (int) *elfdata;
1647 char **argv = (char**) (elfdata + 1);
Stephen Smalleybb440552012-01-20 10:59:15 -08001648 unsigned *vecs = (unsigned*) (argv + argc + 1);
1649 unsigned *v;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001650 soinfo *si;
Kito Cheng326e85e2012-07-15 00:49:27 +08001651 int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652 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
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001733 /* gdb expects the linker to be in the debug shared object list.
1734 * Without this, gdb has trouble locating the linker's ".text"
1735 * and ".plt" sections. Gdb could also potentially use this to
1736 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1737 * Don't use soinfo_alloc(), because the linker shouldn't
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001738 * be on the soinfo list.
1739 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001740 strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001741 linker_soinfo.flags = 0;
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001742 linker_soinfo.base = linker_base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001743 insert_soinfo_into_debug_map(&linker_soinfo);
1744
1745 /* extract information passed from the kernel */
1746 while(vecs[0] != 0){
1747 switch(vecs[0]){
1748 case AT_PHDR:
1749 si->phdr = (Elf32_Phdr*) vecs[1];
1750 break;
1751 case AT_PHNUM:
1752 si->phnum = (int) vecs[1];
1753 break;
1754 case AT_ENTRY:
1755 si->entry = vecs[1];
1756 break;
1757 }
1758 vecs += 2;
1759 }
1760
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001761 /* Compute the value of si->base. We can't rely on the fact that
1762 * the first entry is the PHDR because this will not be true
1763 * for certain executables (e.g. some in the NDK unit test suite)
1764 */
1765 int nn;
1766 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001767 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001768 si->load_bias = 0;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001769 for ( nn = 0; nn < si->phnum; nn++ ) {
1770 if (si->phdr[nn].p_type == PT_PHDR) {
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001771 si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
1772 si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001773 break;
1774 }
1775 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001776 si->dynamic = (unsigned *)-1;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001777 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001778
David Bartleybc3a5c22009-06-02 18:27:28 -07001779 /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001780 if (ldpath_env)
David Bartleybc3a5c22009-06-02 18:27:28 -07001781 parse_library_path(ldpath_env, ":");
1782
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001783 if (ldpreload_env) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001784 parse_preloads(ldpreload_env, " :");
1785 }
1786
David 'Digit' Turner16084162012-06-12 16:25:37 +02001787 if(soinfo_link_image(si, 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -07001788 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
1789 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
1790 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001791 exit(-1);
1792 }
1793
Kito Cheng326e85e2012-07-15 00:49:27 +08001794 for(i = 0; preloads[i] != NULL; i++) {
1795 soinfo_call_constructors(preloads[i]);
1796 }
1797
David 'Digit' Turner16084162012-06-12 16:25:37 +02001798 soinfo_call_constructors(si);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001799
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07001800#if ALLOW_SYMBOLS_FROM_MAIN
1801 /* Set somain after we've loaded all the libraries in order to prevent
1802 * linking of symbols back to the main image, which is not set up at that
1803 * point yet.
1804 */
1805 somain = si;
1806#endif
1807
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001808#if TIMING
1809 gettimeofday(&t1,NULL);
1810 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
1811 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1812 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1813 ));
1814#endif
1815#if STATS
1816 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
1817 linker_stats.reloc[RELOC_ABSOLUTE],
1818 linker_stats.reloc[RELOC_RELATIVE],
1819 linker_stats.reloc[RELOC_COPY],
1820 linker_stats.reloc[RELOC_SYMBOL]);
1821#endif
1822#if COUNT_PAGES
1823 {
1824 unsigned n;
1825 unsigned i;
1826 unsigned count = 0;
1827 for(n = 0; n < 4096; n++){
1828 if(bitmask[n]){
1829 unsigned x = bitmask[n];
1830 for(i = 0; i < 8; i++){
1831 if(x & 1) count++;
1832 x >>= 1;
1833 }
1834 }
1835 }
1836 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
1837 }
1838#endif
1839
1840#if TIMING || STATS || COUNT_PAGES
1841 fflush(stdout);
1842#endif
1843
1844 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
1845 si->entry);
1846 return si->entry;
1847}
Nick Kralevich468319c2011-11-11 15:53:17 -08001848
1849/*
1850 * Find the value of AT_BASE passed to us by the kernel. This is the load
1851 * location of the linker.
1852 */
1853static unsigned find_linker_base(unsigned **elfdata) {
1854 int argc = (int) *elfdata;
1855 char **argv = (char**) (elfdata + 1);
1856 unsigned *vecs = (unsigned*) (argv + argc + 1);
1857 while (vecs[0] != 0) {
1858 vecs++;
1859 }
1860
1861 /* The end of the environment block is marked by two NULL pointers */
1862 vecs++;
1863
1864 while(vecs[0]) {
1865 if (vecs[0] == AT_BASE) {
1866 return vecs[1];
1867 }
1868 vecs += 2;
1869 }
1870
1871 return 0; // should never happen
1872}
1873
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001874/* Compute the load-bias of an existing executable. This shall only
1875 * be used to compute the load bias of an executable or shared library
1876 * that was loaded by the kernel itself.
1877 *
1878 * Input:
1879 * elf -> address of ELF header, assumed to be at the start of the file.
1880 * Return:
1881 * load bias, i.e. add the value of any p_vaddr in the file to get
1882 * the corresponding address in memory.
1883 */
1884static Elf32_Addr
1885get_elf_exec_load_bias(const Elf32_Ehdr* elf)
1886{
1887 Elf32_Addr offset = elf->e_phoff;
1888 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
1889 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
1890 const Elf32_Phdr* phdr;
1891
1892 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
1893 if (phdr->p_type == PT_LOAD) {
1894 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
1895 }
1896 }
1897 return 0;
1898}
1899
Nick Kralevich468319c2011-11-11 15:53:17 -08001900/*
1901 * This is the entry point for the linker, called from begin.S. This
1902 * method is responsible for fixing the linker's own relocations, and
1903 * then calling __linker_init_post_relocation().
1904 *
1905 * Because this method is called before the linker has fixed it's own
1906 * relocations, any attempt to reference an extern variable, extern
1907 * function, or other GOT reference will generate a segfault.
1908 */
1909unsigned __linker_init(unsigned **elfdata) {
1910 unsigned linker_addr = find_linker_base(elfdata);
1911 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
1912 Elf32_Phdr *phdr =
1913 (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
1914
1915 soinfo linker_so;
1916 memset(&linker_so, 0, sizeof(soinfo));
1917
1918 linker_so.base = linker_addr;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001919 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001920 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Nick Kralevich468319c2011-11-11 15:53:17 -08001921 linker_so.dynamic = (unsigned *) -1;
1922 linker_so.phdr = phdr;
1923 linker_so.phnum = elf_hdr->e_phnum;
1924 linker_so.flags |= FLAG_LINKER;
Nick Kralevich468319c2011-11-11 15:53:17 -08001925
David 'Digit' Turner16084162012-06-12 16:25:37 +02001926 if (soinfo_link_image(&linker_so, 0)) {
Nick Kralevich468319c2011-11-11 15:53:17 -08001927 // It would be nice to print an error message, but if the linker
1928 // can't link itself, there's no guarantee that we'll be able to
1929 // call write() (because it involves a GOT reference).
1930 //
1931 // This situation should never occur unless the linker itself
1932 // is corrupt.
1933 exit(-1);
1934 }
1935
1936 // We have successfully fixed our own relocations. It's safe to run
1937 // the main part of the linker now.
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001938 return __linker_init_post_relocation(elfdata, linker_addr);
Nick Kralevich468319c2011-11-11 15:53:17 -08001939}