blob: e0c3bceeb59c3219ea8f0151dbb160a794ab32f4 [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 { \
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700145 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); \
Raghu Gandhamd7daacb2012-07-31 12:07:22 -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}
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700353#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_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",
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700458 pid, lsi->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;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700694#elif defined(ANDROID_MIPS_LINKER)
695 if (hdr->e_machine != EM_MIPS) return -1;
Zhenghua Wang897815a2011-10-19 00:29:14 +0800696#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800697 return 0;
698}
699
700
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800701static soinfo *
702load_library(const char *name)
703{
704 int fd = open_library(name);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200705 int ret, cnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800706 unsigned ext_sz;
707 unsigned req_base;
Erik Gillingfde86422009-07-28 20:28:19 -0700708 const char *bname;
Ji-Hwan Lee75917c82012-05-25 22:36:00 +0900709 struct stat sb;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800710 soinfo *si = NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200711 Elf32_Ehdr header[1];
712 int phdr_count;
713 void* phdr_mmap = NULL;
714 Elf32_Addr phdr_size;
715 const Elf32_Phdr* phdr_table;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800716
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200717 void* load_start = NULL;
718 Elf32_Addr load_size = 0;
719 Elf32_Addr load_bias = 0;
720
Ji-Hwan Lee75917c82012-05-25 22:36:00 +0900721 if (fd == -1) {
Erik Gillingd00d23a2009-07-22 17:06:11 -0700722 DL_ERR("Library '%s' not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800723 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700724 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800725
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200726 /* Read the ELF header first */
727 ret = TEMP_FAILURE_RETRY(read(fd, (void*)header, sizeof(header)));
728 if (ret < 0) {
729 DL_ERR("%5d can't read file %s: %s", pid, name, strerror(errno));
730 goto fail;
731 }
732 if (ret != (int)sizeof(header)) {
733 DL_ERR("%5d too small to be an ELF executable: %s", pid, name);
734 goto fail;
735 }
736 if (verify_elf_header(header) < 0) {
737 DL_ERR("%5d not a valid ELF executable: %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800738 goto fail;
739 }
740
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200741 /* Then read the program header table */
742 ret = phdr_table_load(fd, header->e_phoff, header->e_phnum,
743 &phdr_mmap, &phdr_size, &phdr_table);
744 if (ret < 0) {
745 DL_ERR("%5d can't load program header table: %s: %s", pid,
746 name, strerror(errno));
747 goto fail;
748 }
749 phdr_count = header->e_phnum;
750
751 /* Get the load extents and the prelinked load address, if any */
752 ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
753 if (ext_sz == 0) {
754 DL_ERR("%5d no loadable segments in file: %s", pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800755 goto fail;
756 }
757
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200758 req_base = (unsigned) is_prelinked(fd, name);
759 if (req_base == (unsigned)-1) {
760 DL_ERR("%5d can't read end of library: %s: %s", pid, name,
761 strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800762 goto fail;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200763 }
764 if (req_base != 0) {
765 TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
766 pid, name, req_base);
767 } else {
768 TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
769 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200770
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800771 TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
772 (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
773
774 /* Now configure the soinfo struct where we'll store all of our data
775 * for the ELF object. If the loading fails, we waste the entry, but
776 * same thing would happen if we failed during linking. Configuring the
777 * soinfo struct here is a lot more convenient.
778 */
Erik Gillingfde86422009-07-28 20:28:19 -0700779 bname = strrchr(name, '/');
David 'Digit' Turner16084162012-06-12 16:25:37 +0200780 si = soinfo_alloc(bname ? bname + 1 : name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800781 if (si == NULL)
782 goto fail;
783
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200784 /* Reserve address space for all loadable segments */
785 ret = phdr_table_reserve_memory(phdr_table,
786 phdr_count,
787 req_base,
788 &load_start,
789 &load_size,
790 &load_bias);
791 if (ret < 0) {
792 DL_ERR("%5d Can't reserve %d bytes from 0x%08x in address space for %s: %s",
793 pid, ext_sz, req_base, name, strerror(errno));
794 goto fail;
795 }
796
797 TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
798 pid, name, load_start, load_size);
799
800 /* Map all the segments in our address space with default protections */
801 ret = phdr_table_load_segments(phdr_table,
802 phdr_count,
803 load_start,
804 load_size,
805 load_bias,
806 fd);
807 if (ret < 0) {
808 DL_ERR("%5d Can't map loadable segments for %s: %s",
809 pid, name, strerror(errno));
810 goto fail;
811 }
812
813 /* Unprotect the segments, i.e. make them writable, to allow
814 * relocations to work properly. We will later call
815 * phdr_table_protect_segments() after all of them are applied
816 * and all constructors are run.
817 */
818 ret = phdr_table_unprotect_segments(phdr_table,
819 phdr_count,
820 load_bias);
821 if (ret < 0) {
822 DL_ERR("%5d Can't unprotect loadable segments for %s: %s",
823 pid, name, strerror(errno));
824 goto fail;
825 }
826
827 si->base = (Elf32_Addr) load_start;
828 si->size = load_size;
829 si->load_bias = load_bias;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800830 si->flags = 0;
831 si->entry = 0;
832 si->dynamic = (unsigned *)-1;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200833 si->phnum = phdr_count;
834 si->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
835 if (si->phdr == NULL) {
836 DL_ERR("%5d Can't find loaded PHDR for %s",
837 pid, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800838 goto fail;
839 }
840
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200841 phdr_table_unload(phdr_mmap, phdr_size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800842 close(fd);
843 return si;
844
845fail:
David 'Digit' Turner16084162012-06-12 16:25:37 +0200846 if (si) soinfo_free(si);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200847 if (phdr_mmap != NULL) {
848 phdr_table_unload(phdr_mmap, phdr_size);
849 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800850 close(fd);
851 return NULL;
852}
853
854static soinfo *
855init_library(soinfo *si)
856{
857 unsigned wr_offset = 0xffffffff;
858
859 /* At this point we know that whatever is loaded @ base is a valid ELF
860 * shared library whose segments are properly mapped in. */
861 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
862 pid, si->base, si->size, si->name);
863
David 'Digit' Turner16084162012-06-12 16:25:37 +0200864 if(soinfo_link_image(si, wr_offset)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800865 /* We failed to link. However, we can only restore libbase
866 ** if no additional libraries have moved it since we updated it.
867 */
868 munmap((void *)si->base, si->size);
869 return NULL;
870 }
871
872 return si;
873}
874
875soinfo *find_library(const char *name)
876{
877 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700878 const char *bname;
879
880#if ALLOW_SYMBOLS_FROM_MAIN
881 if (name == NULL)
882 return somain;
883#else
884 if (name == NULL)
885 return NULL;
886#endif
887
888 bname = strrchr(name, '/');
Erik Gillingfde86422009-07-28 20:28:19 -0700889 bname = bname ? bname + 1 : name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800890
891 for(si = solist; si != 0; si = si->next){
Erik Gillingfde86422009-07-28 20:28:19 -0700892 if(!strcmp(bname, si->name)) {
Erik Gilling30eb4022009-08-13 16:05:30 -0700893 if(si->flags & FLAG_ERROR) {
894 DL_ERR("%5d '%s' failed to load previously", pid, bname);
895 return NULL;
896 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800897 if(si->flags & FLAG_LINKED) return si;
Erik Gillingd00d23a2009-07-22 17:06:11 -0700898 DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name);
Dima Zavin2e855792009-05-20 18:28:09 -0700899 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800900 }
901 }
902
903 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
904 si = load_library(name);
905 if(si == NULL)
906 return NULL;
907 return init_library(si);
908}
909
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100910/* TODO:
911 * notify gdb of unload
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800912 * for non-prelinked libraries, find a way to decrement libbase
913 */
914static void call_destructors(soinfo *si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200915unsigned soinfo_unload(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800916{
917 unsigned *d;
918 if (si->refcount == 1) {
919 TRACE("%5d unloading '%s'\n", pid, si->name);
920 call_destructors(si);
921
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800922 /*
923 * Make sure that we undo the PT_GNU_RELRO protections we added
David 'Digit' Turner16084162012-06-12 16:25:37 +0200924 * in soinfo_link_image. This is needed to undo the DT_NEEDED hack below.
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800925 */
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200926 if (phdr_table_unprotect_gnu_relro(si->phdr, si->phnum,
927 si->load_bias) < 0) {
928 DL_ERR("%5d %s: could not undo GNU_RELRO protections. "
929 "Expect a crash soon. errno=%d (%s)",
930 pid, si->name, errno, strerror(errno));
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800931 }
932
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800933 for(d = si->dynamic; *d; d += 2) {
934 if(d[0] == DT_NEEDED){
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700935 soinfo *lsi = (soinfo *)d[1];
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800936
937 // The next line will segfault if the we don't undo the
938 // PT_GNU_RELRO protections (see comments above and in
David 'Digit' Turner16084162012-06-12 16:25:37 +0200939 // soinfo_link_image().
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700940 d[1] = 0;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800941
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700942 if (validate_soinfo(lsi)) {
943 TRACE("%5d %s needs to unload %s\n", pid,
944 si->name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200945 soinfo_unload(lsi);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700946 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800947 else
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700948 DL_ERR("%5d %s: could not unload dependent library",
949 pid, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800950 }
951 }
952
953 munmap((char *)si->base, si->size);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700954 notify_gdb_of_unload(si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200955 soinfo_free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800956 si->refcount = 0;
957 }
958 else {
959 si->refcount--;
960 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
961 pid, si->name, si->refcount);
962 }
963 return si->refcount;
964}
965
966/* TODO: don't use unsigned for addrs below. It works, but is not
967 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
968 * long.
969 */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200970static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800971{
972 Elf32_Sym *symtab = si->symtab;
973 const char *strtab = si->strtab;
974 Elf32_Sym *s;
975 unsigned base;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900976 Elf32_Addr offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800977 Elf32_Rel *start = rel;
978 unsigned idx;
979
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700980 for (idx = 0; idx < count; ++idx, ++rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800981 unsigned type = ELF32_R_TYPE(rel->r_info);
982 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200983 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800984 unsigned sym_addr = 0;
985 char *sym_name = NULL;
986
987 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
988 si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700989 if (type == 0) { // R_*_NONE
990 continue;
991 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800992 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -0700993 sym_name = (char *)(strtab + symtab[sym].st_name);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200994 s = soinfo_do_lookup(si, sym_name, &offset);
Doug Kwane8238072009-10-26 12:05:23 -0700995 if(s == NULL) {
996 /* We only allow an undefined symbol if this is a weak
997 reference.. */
998 s = &symtab[sym];
999 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1000 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
1001 return -1;
1002 }
1003
1004 /* IHI0044C AAELF 4.5.1.1:
1005
1006 Libraries are not searched to resolve weak references.
1007 It is not an error for a weak reference to remain
1008 unsatisfied.
1009
1010 During linking, the value of an undefined weak reference is:
1011 - Zero if the relocation type is absolute
1012 - The address of the place if the relocation is pc-relative
1013 - The address of nominial base address if the relocation
1014 type is base-relative.
1015 */
1016
1017 switch (type) {
1018#if defined(ANDROID_ARM_LINKER)
1019 case R_ARM_JUMP_SLOT:
1020 case R_ARM_GLOB_DAT:
1021 case R_ARM_ABS32:
1022 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -07001023#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001024 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001025 case R_386_GLOB_DAT:
1026 case R_386_32:
1027 case R_386_RELATIVE: /* Dont' care. */
1028#endif /* ANDROID_*_LINKER */
1029 /* sym_addr was initialized to be zero above or relocation
1030 code below does not care about value of sym_addr.
1031 No need to do anything. */
1032 break;
1033
1034#if defined(ANDROID_X86_LINKER)
1035 case R_386_PC32:
1036 sym_addr = reloc;
1037 break;
1038#endif /* ANDROID_X86_LINKER */
1039
1040#if defined(ANDROID_ARM_LINKER)
1041 case R_ARM_COPY:
1042 /* Fall through. Can't really copy if weak symbol is
1043 not found in run-time. */
1044#endif /* ANDROID_ARM_LINKER */
1045 default:
1046 DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n",
1047 pid, type, rel, (int) (rel - start));
1048 return -1;
1049 }
1050 } else {
1051 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001052#if 0
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001053 if((base == 0) && (si->base != 0)){
1054 /* linking from libraries to main image is bad */
1055 DL_ERR("%5d cannot locate '%s'...",
1056 pid, strtab + symtab[sym].st_name);
1057 return -1;
1058 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001059#endif
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001060 sym_addr = (unsigned)(s->st_value + offset);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001061 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001062 COUNT_RELOC(RELOC_SYMBOL);
1063 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001064 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001065 }
1066
1067/* TODO: This is ugly. Split up the relocations by arch into
1068 * different files.
1069 */
1070 switch(type){
1071#if defined(ANDROID_ARM_LINKER)
1072 case R_ARM_JUMP_SLOT:
1073 COUNT_RELOC(RELOC_ABSOLUTE);
1074 MARK(rel->r_offset);
1075 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1076 reloc, sym_addr, sym_name);
1077 *((unsigned*)reloc) = sym_addr;
1078 break;
1079 case R_ARM_GLOB_DAT:
1080 COUNT_RELOC(RELOC_ABSOLUTE);
1081 MARK(rel->r_offset);
1082 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1083 reloc, sym_addr, sym_name);
1084 *((unsigned*)reloc) = sym_addr;
1085 break;
1086 case R_ARM_ABS32:
1087 COUNT_RELOC(RELOC_ABSOLUTE);
1088 MARK(rel->r_offset);
1089 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1090 reloc, sym_addr, sym_name);
1091 *((unsigned*)reloc) += sym_addr;
1092 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001093 case R_ARM_REL32:
1094 COUNT_RELOC(RELOC_RELATIVE);
1095 MARK(rel->r_offset);
1096 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1097 reloc, sym_addr, rel->r_offset, sym_name);
1098 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1099 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001100#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001101 case R_386_JMP_SLOT:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001102 COUNT_RELOC(RELOC_ABSOLUTE);
1103 MARK(rel->r_offset);
1104 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1105 reloc, sym_addr, sym_name);
1106 *((unsigned*)reloc) = sym_addr;
1107 break;
1108 case R_386_GLOB_DAT:
1109 COUNT_RELOC(RELOC_ABSOLUTE);
1110 MARK(rel->r_offset);
1111 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1112 reloc, sym_addr, sym_name);
1113 *((unsigned*)reloc) = sym_addr;
1114 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001115#elif defined(ANDROID_MIPS_LINKER)
1116 case R_MIPS_JUMP_SLOT:
1117 COUNT_RELOC(RELOC_ABSOLUTE);
1118 MARK(rel->r_offset);
1119 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1120 reloc, sym_addr, sym_name);
1121 *((unsigned*)reloc) = sym_addr;
1122 break;
1123 case R_MIPS_REL32:
1124 COUNT_RELOC(RELOC_ABSOLUTE);
1125 MARK(rel->r_offset);
1126 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x %s\n", pid,
1127 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1128 if (s) {
1129 *((unsigned*)reloc) += sym_addr;
1130 } else {
1131 *((unsigned*)reloc) += si->base;
1132 }
1133 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001134#endif /* ANDROID_*_LINKER */
1135
1136#if defined(ANDROID_ARM_LINKER)
1137 case R_ARM_RELATIVE:
1138#elif defined(ANDROID_X86_LINKER)
1139 case R_386_RELATIVE:
1140#endif /* ANDROID_*_LINKER */
1141 COUNT_RELOC(RELOC_RELATIVE);
1142 MARK(rel->r_offset);
1143 if(sym){
Erik Gillingd00d23a2009-07-22 17:06:11 -07001144 DL_ERR("%5d odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001145 return -1;
1146 }
1147 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1148 reloc, si->base);
1149 *((unsigned*)reloc) += si->base;
1150 break;
1151
1152#if defined(ANDROID_X86_LINKER)
1153 case R_386_32:
1154 COUNT_RELOC(RELOC_RELATIVE);
1155 MARK(rel->r_offset);
1156
1157 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1158 reloc, sym_addr, sym_name);
1159 *((unsigned *)reloc) += (unsigned)sym_addr;
1160 break;
1161
1162 case R_386_PC32:
1163 COUNT_RELOC(RELOC_RELATIVE);
1164 MARK(rel->r_offset);
1165 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1166 "+%08x (%08x - %08x) %s\n", pid, reloc,
1167 (sym_addr - reloc), sym_addr, reloc, sym_name);
1168 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1169 break;
1170#endif /* ANDROID_X86_LINKER */
1171
1172#ifdef ANDROID_ARM_LINKER
1173 case R_ARM_COPY:
1174 COUNT_RELOC(RELOC_COPY);
1175 MARK(rel->r_offset);
1176 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1177 reloc, s->st_size, sym_addr, sym_name);
1178 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1179 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001180#endif /* ANDROID_ARM_LINKER */
1181
1182 default:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001183 DL_ERR("%5d unknown reloc type %d @ %p (%d)",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001184 pid, type, rel, (int) (rel - start));
1185 return -1;
1186 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001187 }
1188 return 0;
1189}
1190
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001191#ifdef ANDROID_MIPS_LINKER
1192int mips_relocate_got(struct soinfo *si)
1193{
1194 unsigned *got;
1195 unsigned local_gotno, gotsym, symtabno;
1196 Elf32_Sym *symtab, *sym;
1197 unsigned g;
1198
1199 got = si->plt_got;
1200 local_gotno = si->mips_local_gotno;
1201 gotsym = si->mips_gotsym;
1202 symtabno = si->mips_symtabno;
1203 symtab = si->symtab;
1204
1205 /*
1206 * got[0] is address of lazy resolver function
1207 * got[1] may be used for a GNU extension
1208 * set it to a recognisable address in case someone calls it
1209 * (should be _rtld_bind_start)
1210 * FIXME: maybe this should be in a separate routine
1211 */
1212
1213 if ((si->flags & FLAG_LINKER) == 0) {
1214 g = 0;
1215 got[g++] = 0xdeadbeef;
1216 if (got[g] & 0x80000000) {
1217 got[g++] = 0xdeadfeed;
1218 }
1219 /*
1220 * Relocate the local GOT entries need to be relocated
1221 */
1222 for (; g < local_gotno; g++) {
1223 got[g] += si->load_bias;
1224 }
1225 }
1226
1227 /* Now for the global GOT entries */
1228 sym = symtab + gotsym;
1229 got = si->plt_got + local_gotno;
1230 for (g = gotsym; g < symtabno; g++, sym++, got++) {
1231 const char *sym_name;
1232 unsigned base;
1233 Elf32_Sym *s;
1234
1235 /* This is an undefined reference... try to locate it */
1236 sym_name = si->strtab + sym->st_name;
1237 s = soinfo_do_lookup(si, sym_name, &base);
1238 if (s == NULL) {
1239 /* We only allow an undefined symbol if this is a weak
1240 reference.. */
1241 s = &symtab[g];
1242 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1243 DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
1244 return -1;
1245 }
1246 *got = 0;
1247 }
1248 else {
1249 /* FIXME: is this sufficient?
1250 * For reference see NetBSD link loader
1251 * http://cvsweb.netbsd.org/bsdweb.cgi/src/libexec/ld.elf_so/arch/mips/mips_reloc.c?rev=1.53&content-type=text/x-cvsweb-markup
1252 */
1253 *got = base + s->st_value;
1254 }
1255 }
1256 return 0;
1257}
1258#endif
1259
David 'Digit' Turner82156792009-05-18 14:37:41 +02001260/* Please read the "Initialization and Termination functions" functions.
1261 * of the linker design note in bionic/linker/README.TXT to understand
1262 * what the following code is doing.
1263 *
1264 * The important things to remember are:
1265 *
1266 * DT_PREINIT_ARRAY must be called first for executables, and should
1267 * not appear in shared libraries.
1268 *
1269 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1270 *
1271 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1272 *
1273 * DT_FINI_ARRAY must be parsed in reverse order.
1274 */
1275
1276static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001277{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001278 int n, inc = 1;
1279
1280 if (reverse) {
1281 ctor += (count-1);
1282 inc = -1;
1283 }
1284
1285 for(n = count; n > 0; n--) {
1286 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1287 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001288 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001289 void (*func)() = (void (*)()) *ctor;
1290 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001291 if(((int) func == 0) || ((int) func == -1)) continue;
1292 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1293 func();
1294 }
1295}
1296
David 'Digit' Turner16084162012-06-12 16:25:37 +02001297void soinfo_call_constructors(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001298{
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001299 if (si->constructors_called)
1300 return;
1301
Jesse Hallf5d16932012-01-30 15:39:57 -08001302 // Set this before actually calling the constructors, otherwise it doesn't
1303 // protect against recursive constructor calls. One simple example of
1304 // constructor recursion is the libc debug malloc, which is implemented in
1305 // libc_malloc_debug_leak.so:
1306 // 1. The program depends on libc, so libc's constructor is called here.
1307 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001308 // 3. dlopen() calls soinfo_call_constructors() with the newly created
Jesse Hallf5d16932012-01-30 15:39:57 -08001309 // soinfo for libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001310 // 4. The debug so depends on libc, so soinfo_call_constructors() is
Jesse Hallf5d16932012-01-30 15:39:57 -08001311 // called again with the libc soinfo. If it doesn't trigger the early-
1312 // out above, the libc constructor will be called again (recursively!).
1313 si->constructors_called = 1;
1314
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001315 if (si->flags & FLAG_EXE) {
1316 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1317 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1318 si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001319 call_array(si->preinit_array, si->preinit_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001320 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1321 } else {
1322 if (si->preinit_array) {
Dima Zavin2e855792009-05-20 18:28:09 -07001323 DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
Erik Gillingd00d23a2009-07-22 17:06:11 -07001324 " This is INVALID.", pid, si->name,
Dima Zavin2e855792009-05-20 18:28:09 -07001325 (unsigned)si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001326 }
1327 }
1328
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001329 if (si->dynamic) {
1330 unsigned *d;
1331 for(d = si->dynamic; *d; d += 2) {
1332 if(d[0] == DT_NEEDED){
1333 soinfo* lsi = (soinfo *)d[1];
1334 if (!validate_soinfo(lsi)) {
1335 DL_ERR("%5d bad DT_NEEDED pointer in %s",
1336 pid, si->name);
1337 } else {
David 'Digit' Turner16084162012-06-12 16:25:37 +02001338 soinfo_call_constructors(lsi);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001339 }
1340 }
1341 }
1342 }
1343
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001344 if (si->init_func) {
1345 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1346 (unsigned)si->init_func, si->name);
1347 si->init_func();
1348 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1349 }
1350
1351 if (si->init_array) {
1352 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1353 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001354 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001355 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1356 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001357
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001358}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001359
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001360static void call_destructors(soinfo *si)
1361{
1362 if (si->fini_array) {
1363 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1364 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001365 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001366 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1367 }
1368
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001369 if (si->fini_func) {
1370 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1371 (unsigned)si->fini_func, si->name);
1372 si->fini_func();
1373 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1374 }
1375}
1376
1377/* Force any of the closed stdin, stdout and stderr to be associated with
1378 /dev/null. */
1379static int nullify_closed_stdio (void)
1380{
1381 int dev_null, i, status;
1382 int return_value = 0;
1383
David 'Digit' Turner16084162012-06-12 16:25:37 +02001384 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001385 if (dev_null < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001386 DL_ERR("Cannot open /dev/null.");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001387 return -1;
1388 }
1389 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1390
1391 /* If any of the stdio file descriptors is valid and not associated
1392 with /dev/null, dup /dev/null to it. */
1393 for (i = 0; i < 3; i++) {
1394 /* If it is /dev/null already, we are done. */
1395 if (i == dev_null)
1396 continue;
1397
1398 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
1399 /* The man page of fcntl does not say that fcntl(..,F_GETFL)
1400 can be interrupted but we do this just to be safe. */
1401 do {
1402 status = fcntl(i, F_GETFL);
1403 } while (status < 0 && errno == EINTR);
1404
1405 /* If file is openned, we are good. */
1406 if (status >= 0)
1407 continue;
1408
1409 /* The only error we allow is that the file descriptor does not
1410 exist, in which case we dup /dev/null to it. */
1411 if (errno != EBADF) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001412 DL_ERR("nullify_stdio: unhandled error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001413 return_value = -1;
1414 continue;
1415 }
1416
1417 /* Try dupping /dev/null to this stdio file descriptor and
1418 repeat if there is a signal. Note that any errors in closing
1419 the stdio descriptor are lost. */
1420 do {
1421 status = dup2(dev_null, i);
1422 } while (status < 0 && errno == EINTR);
Dima Zavin2e855792009-05-20 18:28:09 -07001423
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001424 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001425 DL_ERR("nullify_stdio: dup2 error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001426 return_value = -1;
1427 continue;
1428 }
1429 }
1430
1431 /* If /dev/null is not one of the stdio file descriptors, close it. */
1432 if (dev_null > 2) {
1433 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Dima Zavin2e855792009-05-20 18:28:09 -07001434 do {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001435 status = close(dev_null);
1436 } while (status < 0 && errno == EINTR);
1437
1438 if (status < 0) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001439 DL_ERR("nullify_stdio: close error %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001440 return_value = -1;
1441 }
1442 }
1443
1444 return return_value;
1445}
1446
David 'Digit' Turner16084162012-06-12 16:25:37 +02001447static int soinfo_link_image(soinfo *si, unsigned wr_offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001448{
1449 unsigned *d;
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001450 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001451 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001452 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001453 int phnum = si->phnum;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001454 int relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001455
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001456 /* We can't debug anything until the linker is relocated */
1457 if (!relocating_linker) {
1458 INFO("[ %5d linking %s ]\n", pid, si->name);
1459 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1460 si->base, si->flags);
1461 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001462
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001463 /* Extract dynamic section */
1464 si->dynamic = phdr_table_get_dynamic_section(phdr, phnum, base);
1465 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001466 if (!relocating_linker) {
1467 DL_ERR("%5d missing PT_DYNAMIC?!", pid);
1468 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001469 goto fail;
1470 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001471 if (!relocating_linker) {
1472 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1473 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001474 }
1475
1476#ifdef ANDROID_ARM_LINKER
1477 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1478 &si->ARM_exidx, &si->ARM_exidx_count);
1479#endif
1480
Nick Kralevich468319c2011-11-11 15:53:17 -08001481 if (si->flags & (FLAG_EXE | FLAG_LINKER)) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001482 if (phdr_table_unprotect_segments(si->phdr,
1483 si->phnum,
1484 si->load_bias) < 0) {
1485 /* We can't call DL_ERR if the linker's relocations haven't
1486 * been performed yet */
1487 if (!relocating_linker) {
1488 DL_ERR("%5d Can't unprotect segments for %s: %s",
1489 pid, si->name, strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001490 }
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001491 goto fail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001492 }
1493 }
1494
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001495 /* extract useful information from dynamic section */
1496 for(d = si->dynamic; *d; d++){
1497 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1498 switch(*d++){
1499 case DT_HASH:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001500 si->nbucket = ((unsigned *) (base + *d))[0];
1501 si->nchain = ((unsigned *) (base + *d))[1];
1502 si->bucket = (unsigned *) (base + *d + 8);
1503 si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001504 break;
1505 case DT_STRTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001506 si->strtab = (const char *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001507 break;
1508 case DT_SYMTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001509 si->symtab = (Elf32_Sym *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001510 break;
1511 case DT_PLTREL:
1512 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001513 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001514 goto fail;
1515 }
1516 break;
1517 case DT_JMPREL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001518 si->plt_rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001519 break;
1520 case DT_PLTRELSZ:
1521 si->plt_rel_count = *d / 8;
1522 break;
1523 case DT_REL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001524 si->rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001525 break;
1526 case DT_RELSZ:
1527 si->rel_count = *d / 8;
1528 break;
1529 case DT_PLTGOT:
1530 /* Save this in case we decide to do lazy binding. We don't yet. */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001531 si->plt_got = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001532 break;
1533 case DT_DEBUG:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001534#if !defined(ANDROID_MIPS_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001535 // Set the DT_DEBUG entry to the addres of _r_debug for GDB
1536 *d = (int) &_r_debug;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001537#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001538 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001539 case DT_RELA:
Erik Gillingd00d23a2009-07-22 17:06:11 -07001540 DL_ERR("%5d DT_RELA not supported", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001541 goto fail;
1542 case DT_INIT:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001543 si->init_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001544 DEBUG("%5d %s constructors (init func) found at %p\n",
1545 pid, si->name, si->init_func);
1546 break;
1547 case DT_FINI:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001548 si->fini_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001549 DEBUG("%5d %s destructors (fini func) found at %p\n",
1550 pid, si->name, si->fini_func);
1551 break;
1552 case DT_INIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001553 si->init_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001554 DEBUG("%5d %s constructors (init_array) found at %p\n",
1555 pid, si->name, si->init_array);
1556 break;
1557 case DT_INIT_ARRAYSZ:
1558 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1559 break;
1560 case DT_FINI_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001561 si->fini_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001562 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1563 pid, si->name, si->fini_array);
1564 break;
1565 case DT_FINI_ARRAYSZ:
1566 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1567 break;
1568 case DT_PREINIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001569 si->preinit_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001570 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1571 pid, si->name, si->preinit_array);
1572 break;
1573 case DT_PREINIT_ARRAYSZ:
1574 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1575 break;
1576 case DT_TEXTREL:
1577 /* TODO: make use of this. */
1578 /* this means that we might have to write into where the text
1579 * segment was loaded during relocation... Do something with
1580 * it.
1581 */
1582 DEBUG("%5d Text segment should be writable during relocation.\n",
1583 pid);
1584 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001585#if defined(ANDROID_MIPS_LINKER)
1586 case DT_NEEDED:
1587 case DT_STRSZ:
1588 case DT_SYMENT:
1589 case DT_RELENT:
1590 break;
1591 case DT_MIPS_RLD_MAP:
1592 /* Set the DT_MIPS_RLD_MAP entry to the addres of _r_debug for GDB */
1593 {
1594 struct r_debug **dp = (struct r_debug **)*d;
1595 *dp = &_r_debug;
1596 }
1597 break;
1598 case DT_MIPS_RLD_VERSION:
1599 case DT_MIPS_FLAGS:
1600 case DT_MIPS_BASE_ADDRESS:
1601 case DT_MIPS_UNREFEXTNO:
1602 case DT_MIPS_RWPLT:
1603 break;
1604
1605 case DT_MIPS_PLTGOT:
1606#if 0
1607 /* not yet... */
1608 si->mips_pltgot = (unsigned *)(si->base + *d);
1609#endif
1610 break;
1611
1612 case DT_MIPS_SYMTABNO:
1613 si->mips_symtabno = *d;
1614 break;
1615
1616 case DT_MIPS_LOCAL_GOTNO:
1617 si->mips_local_gotno = *d;
1618 break;
1619
1620 case DT_MIPS_GOTSYM:
1621 si->mips_gotsym = *d;
1622 break;
1623
1624 default:
1625 DEBUG("%5d Unused DT entry: type 0x%08x arg 0x%08x\n",
1626 pid, d[-1], d[0]);
1627 break;
1628#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001629 }
1630 }
1631
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001632 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001633 pid, si->base, si->strtab, si->symtab);
1634
1635 if((si->strtab == 0) || (si->symtab == 0)) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001636 DL_ERR("%5d missing essential tables", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001637 goto fail;
1638 }
1639
Matt Fischer4fd42c12009-12-31 12:09:10 -06001640 /* if this is the main executable, then load all of the preloads now */
1641 if(si->flags & FLAG_EXE) {
1642 int i;
1643 memset(preloads, 0, sizeof(preloads));
1644 for(i = 0; ldpreload_names[i] != NULL; i++) {
1645 soinfo *lsi = find_library(ldpreload_names[i]);
1646 if(lsi == 0) {
1647 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
1648 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
1649 pid, ldpreload_names[i], si->name, tmp_err_buf);
1650 goto fail;
1651 }
1652 lsi->refcount++;
1653 preloads[i] = lsi;
1654 }
1655 }
1656
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001657 for(d = si->dynamic; *d; d += 2) {
1658 if(d[0] == DT_NEEDED){
1659 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001660 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001661 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001662 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Erik Gillingd00d23a2009-07-22 17:06:11 -07001663 DL_ERR("%5d could not load needed library '%s' for '%s' (%s)",
Dima Zavin03531952009-05-29 17:30:25 -07001664 pid, si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001665 goto fail;
1666 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001667 /* Save the soinfo of the loaded DT_NEEDED library in the payload
1668 of the DT_NEEDED entry itself, so that we can retrieve the
1669 soinfo directly later from the dynamic segment. This is a hack,
1670 but it allows us to map from DT_NEEDED to soinfo efficiently
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001671 later on when we resolve relocations, trying to look up a symbol
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001672 with dlsym().
1673 */
1674 d[1] = (unsigned)lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001675 lsi->refcount++;
1676 }
1677 }
1678
1679 if(si->plt_rel) {
1680 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
David 'Digit' Turner16084162012-06-12 16:25:37 +02001681 if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001682 goto fail;
1683 }
1684 if(si->rel) {
1685 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
David 'Digit' Turner16084162012-06-12 16:25:37 +02001686 if(soinfo_relocate(si, si->rel, si->rel_count))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001687 goto fail;
1688 }
1689
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001690#ifdef ANDROID_MIPS_LINKER
1691 if(mips_relocate_got(si)) {
1692 goto fail;
1693 }
1694#endif
1695
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001696 si->flags |= FLAG_LINKED;
1697 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1698
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001699 /* All relocations are done, we can protect our segments back to
1700 * read-only. */
1701 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1702 DL_ERR("%5d Can't protect segments for %s: %s",
1703 pid, si->name, strerror(errno));
1704 goto fail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001705 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001706
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001707 /* We can also turn on GNU RELRO protection */
1708 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
1709 DL_ERR("%5d Can't enable GNU RELRO protection for %s: %s",
1710 pid, si->name, strerror(errno));
1711 goto fail;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001712 }
1713
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001714 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1715 stdout and stderr to close a security hole described in:
1716
1717 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1718
1719 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001720 if (program_is_setuid)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001721 nullify_closed_stdio ();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001722 notify_gdb_of_load(si);
1723 return 0;
1724
1725fail:
Dima Zavina7161902010-08-17 15:56:40 -07001726 ERROR("failed to link %s\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001727 si->flags |= FLAG_ERROR;
1728 return -1;
1729}
1730
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001731static void parse_library_path(const char *path, char *delim)
David Bartleybc3a5c22009-06-02 18:27:28 -07001732{
1733 size_t len;
1734 char *ldpaths_bufp = ldpaths_buf;
1735 int i = 0;
1736
1737 len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf));
1738
1739 while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) {
1740 if (*ldpaths[i] != '\0')
1741 ++i;
1742 }
1743
1744 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1745 * last char isn't '\0' (i.e. not originally a delim). */
1746 if (i > 0 && len >= sizeof(ldpaths_buf) &&
1747 ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') {
1748 ldpaths[i - 1] = NULL;
1749 } else {
1750 ldpaths[i] = NULL;
1751 }
1752}
1753
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001754static void parse_preloads(const char *path, char *delim)
Matt Fischer4fd42c12009-12-31 12:09:10 -06001755{
1756 size_t len;
1757 char *ldpreloads_bufp = ldpreloads_buf;
1758 int i = 0;
1759
1760 len = strlcpy(ldpreloads_buf, path, sizeof(ldpreloads_buf));
1761
1762 while (i < LDPRELOAD_MAX && (ldpreload_names[i] = strsep(&ldpreloads_bufp, delim))) {
1763 if (*ldpreload_names[i] != '\0') {
1764 ++i;
1765 }
1766 }
1767
1768 /* Forget the last path if we had to truncate; this occurs if the 2nd to
1769 * last char isn't '\0' (i.e. not originally a delim). */
1770 if (i > 0 && len >= sizeof(ldpreloads_buf) &&
1771 ldpreloads_buf[sizeof(ldpreloads_buf) - 2] != '\0') {
1772 ldpreload_names[i - 1] = NULL;
1773 } else {
1774 ldpreload_names[i] = NULL;
1775 }
1776}
1777
Nick Kralevich468319c2011-11-11 15:53:17 -08001778/*
1779 * This code is called after the linker has linked itself and
1780 * fixed it's own GOT. It is safe to make references to externs
1781 * and other non-local data at this point.
1782 */
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001783static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001784{
1785 static soinfo linker_soinfo;
1786
1787 int argc = (int) *elfdata;
1788 char **argv = (char**) (elfdata + 1);
Stephen Smalleybb440552012-01-20 10:59:15 -08001789 unsigned *vecs = (unsigned*) (argv + argc + 1);
1790 unsigned *v;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001791 soinfo *si;
Kito Cheng326e85e2012-07-15 00:49:27 +08001792 int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001793 struct link_map * map;
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001794 const char *ldpath_env = NULL;
1795 const char *ldpreload_env = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001796
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001797 /* NOTE: we store the elfdata pointer on a special location
1798 * of the temporary TLS area in order to pass it to
1799 * the C Library's runtime initializer.
1800 *
1801 * The initializer must clear the slot and reset the TLS
1802 * to point to a different location to ensure that no other
1803 * shared library constructor can access it.
1804 */
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001805 __libc_init_tls(elfdata);
1806
1807 pid = getpid();
1808
1809#if TIMING
1810 struct timeval t0, t1;
1811 gettimeofday(&t0, 0);
1812#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001813
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001814 /* Initialize environment functions, and get to the ELF aux vectors table */
1815 vecs = linker_env_init(vecs);
1816
Stephen Smalley861b42a2012-01-13 07:48:11 -05001817 /* Check auxv for AT_SECURE first to see if program is setuid, setgid,
1818 has file caps, or caused a SELinux/AppArmor domain transition. */
1819 for (v = vecs; v[0]; v += 2) {
1820 if (v[0] == AT_SECURE) {
1821 /* kernel told us whether to enable secure mode */
1822 program_is_setuid = v[1];
1823 goto sanitize;
1824 }
1825 }
1826
1827 /* Kernel did not provide AT_SECURE - fall back on legacy test. */
1828 program_is_setuid = (getuid() != geteuid()) || (getgid() != getegid());
1829
1830sanitize:
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001831 /* Sanitize environment if we're loading a setuid program */
1832 if (program_is_setuid)
1833 linker_env_secure();
1834
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001835 debugger_init();
1836
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001837 /* Get a few environment variables */
1838 {
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07001839#if LINKER_DEBUG
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001840 const char* env;
1841 env = linker_env_get("DEBUG"); /* XXX: TODO: Change to LD_DEBUG */
1842 if (env)
1843 debug_verbosity = atoi(env);
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07001844#endif
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001845
1846 /* Normally, these are cleaned by linker_env_secure, but the test
1847 * against program_is_setuid doesn't cost us anything */
1848 if (!program_is_setuid) {
1849 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1850 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001851 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001852 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001853
1854 INFO("[ android linker & debugger ]\n");
1855 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
1856
David 'Digit' Turner16084162012-06-12 16:25:37 +02001857 si = soinfo_alloc(argv[0]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001858 if(si == 0) {
1859 exit(-1);
1860 }
1861
1862 /* bootstrap the link map, the main exe always needs to be first */
1863 si->flags |= FLAG_EXE;
1864 map = &(si->linkmap);
1865
1866 map->l_addr = 0;
1867 map->l_name = argv[0];
1868 map->l_prev = NULL;
1869 map->l_next = NULL;
1870
1871 _r_debug.r_map = map;
1872 r_debug_tail = map;
1873
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001874 /* gdb expects the linker to be in the debug shared object list.
1875 * Without this, gdb has trouble locating the linker's ".text"
1876 * and ".plt" sections. Gdb could also potentially use this to
1877 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1878 * Don't use soinfo_alloc(), because the linker shouldn't
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001879 * be on the soinfo list.
1880 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001881 strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001882 linker_soinfo.flags = 0;
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001883 linker_soinfo.base = linker_base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001884 insert_soinfo_into_debug_map(&linker_soinfo);
1885
1886 /* extract information passed from the kernel */
1887 while(vecs[0] != 0){
1888 switch(vecs[0]){
1889 case AT_PHDR:
1890 si->phdr = (Elf32_Phdr*) vecs[1];
1891 break;
1892 case AT_PHNUM:
1893 si->phnum = (int) vecs[1];
1894 break;
1895 case AT_ENTRY:
1896 si->entry = vecs[1];
1897 break;
1898 }
1899 vecs += 2;
1900 }
1901
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001902 /* Compute the value of si->base. We can't rely on the fact that
1903 * the first entry is the PHDR because this will not be true
1904 * for certain executables (e.g. some in the NDK unit test suite)
1905 */
1906 int nn;
1907 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001908 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001909 si->load_bias = 0;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001910 for ( nn = 0; nn < si->phnum; nn++ ) {
1911 if (si->phdr[nn].p_type == PT_PHDR) {
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001912 si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
1913 si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001914 break;
1915 }
1916 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001917 si->dynamic = (unsigned *)-1;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001918 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001919
David Bartleybc3a5c22009-06-02 18:27:28 -07001920 /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001921 if (ldpath_env)
David Bartleybc3a5c22009-06-02 18:27:28 -07001922 parse_library_path(ldpath_env, ":");
1923
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001924 if (ldpreload_env) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001925 parse_preloads(ldpreload_env, " :");
1926 }
1927
David 'Digit' Turner16084162012-06-12 16:25:37 +02001928 if(soinfo_link_image(si, 0)) {
Dima Zavin2e855792009-05-20 18:28:09 -07001929 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
1930 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
1931 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001932 exit(-1);
1933 }
1934
Kito Cheng326e85e2012-07-15 00:49:27 +08001935 for(i = 0; preloads[i] != NULL; i++) {
1936 soinfo_call_constructors(preloads[i]);
1937 }
1938
David 'Digit' Turner16084162012-06-12 16:25:37 +02001939 soinfo_call_constructors(si);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001940
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07001941#if ALLOW_SYMBOLS_FROM_MAIN
1942 /* Set somain after we've loaded all the libraries in order to prevent
1943 * linking of symbols back to the main image, which is not set up at that
1944 * point yet.
1945 */
1946 somain = si;
1947#endif
1948
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001949#if TIMING
1950 gettimeofday(&t1,NULL);
1951 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
1952 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1953 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1954 ));
1955#endif
1956#if STATS
1957 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
1958 linker_stats.reloc[RELOC_ABSOLUTE],
1959 linker_stats.reloc[RELOC_RELATIVE],
1960 linker_stats.reloc[RELOC_COPY],
1961 linker_stats.reloc[RELOC_SYMBOL]);
1962#endif
1963#if COUNT_PAGES
1964 {
1965 unsigned n;
1966 unsigned i;
1967 unsigned count = 0;
1968 for(n = 0; n < 4096; n++){
1969 if(bitmask[n]){
1970 unsigned x = bitmask[n];
1971 for(i = 0; i < 8; i++){
1972 if(x & 1) count++;
1973 x >>= 1;
1974 }
1975 }
1976 }
1977 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
1978 }
1979#endif
1980
1981#if TIMING || STATS || COUNT_PAGES
1982 fflush(stdout);
1983#endif
1984
1985 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
1986 si->entry);
1987 return si->entry;
1988}
Nick Kralevich468319c2011-11-11 15:53:17 -08001989
1990/*
1991 * Find the value of AT_BASE passed to us by the kernel. This is the load
1992 * location of the linker.
1993 */
1994static unsigned find_linker_base(unsigned **elfdata) {
1995 int argc = (int) *elfdata;
1996 char **argv = (char**) (elfdata + 1);
1997 unsigned *vecs = (unsigned*) (argv + argc + 1);
1998 while (vecs[0] != 0) {
1999 vecs++;
2000 }
2001
2002 /* The end of the environment block is marked by two NULL pointers */
2003 vecs++;
2004
2005 while(vecs[0]) {
2006 if (vecs[0] == AT_BASE) {
2007 return vecs[1];
2008 }
2009 vecs += 2;
2010 }
2011
2012 return 0; // should never happen
2013}
2014
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002015/* Compute the load-bias of an existing executable. This shall only
2016 * be used to compute the load bias of an executable or shared library
2017 * that was loaded by the kernel itself.
2018 *
2019 * Input:
2020 * elf -> address of ELF header, assumed to be at the start of the file.
2021 * Return:
2022 * load bias, i.e. add the value of any p_vaddr in the file to get
2023 * the corresponding address in memory.
2024 */
2025static Elf32_Addr
2026get_elf_exec_load_bias(const Elf32_Ehdr* elf)
2027{
2028 Elf32_Addr offset = elf->e_phoff;
2029 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
2030 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
2031 const Elf32_Phdr* phdr;
2032
2033 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
2034 if (phdr->p_type == PT_LOAD) {
2035 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
2036 }
2037 }
2038 return 0;
2039}
2040
Nick Kralevich468319c2011-11-11 15:53:17 -08002041/*
2042 * This is the entry point for the linker, called from begin.S. This
2043 * method is responsible for fixing the linker's own relocations, and
2044 * then calling __linker_init_post_relocation().
2045 *
2046 * Because this method is called before the linker has fixed it's own
2047 * relocations, any attempt to reference an extern variable, extern
2048 * function, or other GOT reference will generate a segfault.
2049 */
2050unsigned __linker_init(unsigned **elfdata) {
2051 unsigned linker_addr = find_linker_base(elfdata);
2052 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2053 Elf32_Phdr *phdr =
2054 (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2055
2056 soinfo linker_so;
2057 memset(&linker_so, 0, sizeof(soinfo));
2058
2059 linker_so.base = linker_addr;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002060 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002061 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Nick Kralevich468319c2011-11-11 15:53:17 -08002062 linker_so.dynamic = (unsigned *) -1;
2063 linker_so.phdr = phdr;
2064 linker_so.phnum = elf_hdr->e_phnum;
2065 linker_so.flags |= FLAG_LINKER;
Nick Kralevich468319c2011-11-11 15:53:17 -08002066
David 'Digit' Turner16084162012-06-12 16:25:37 +02002067 if (soinfo_link_image(&linker_so, 0)) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002068 // It would be nice to print an error message, but if the linker
2069 // can't link itself, there's no guarantee that we'll be able to
2070 // call write() (because it involves a GOT reference).
2071 //
2072 // This situation should never occur unless the linker itself
2073 // is corrupt.
2074 exit(-1);
2075 }
2076
2077 // We have successfully fixed our own relocations. It's safe to run
2078 // the main part of the linker now.
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05002079 return __linker_init_post_relocation(elfdata, linker_addr);
Nick Kralevich468319c2011-11-11 15:53:17 -08002080}