blob: 4ffa1e74f68efe4ec93d5d599f465208ac550935 [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
Elliott Hughes46882792012-08-03 16:49:39 -070029#include <dlfcn.h>
30#include <errno.h>
31#include <fcntl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <linux/auxvec.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
34#include <stdbool.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038#include <sys/atomics.h>
Elliott Hughes46882792012-08-03 16:49:39 -070039#include <sys/mman.h>
40#include <sys/stat.h>
41#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042
Elliott Hughes46882792012-08-03 16:49:39 -070043// Private C library headers.
44#include <private/bionic_tls.h>
45#include <private/logd.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070046#include <private/ScopedPthreadMutexLocker.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047
48#include "linker.h"
49#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010050#include "linker_environ.h"
David 'Digit' Turner5c734642010-01-20 12:36:51 -080051#include "linker_format.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020052#include "linker_phdr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070054#define ALLOW_SYMBOLS_FROM_MAIN 1
Kenny Root72f9a5c2011-02-10 17:02:21 -080055#define SO_MAX 128
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056
David Bartleybc3a5c22009-06-02 18:27:28 -070057/* Assume average path length of 64 and max 8 paths */
58#define LDPATH_BUFSIZE 512
59#define LDPATH_MAX 8
60
Matt Fischer4fd42c12009-12-31 12:09:10 -060061#define LDPRELOAD_BUFSIZE 512
62#define LDPRELOAD_MAX 8
63
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
65 *
66 * Do NOT use malloc() and friends or pthread_*() code here.
67 * Don't use printf() either; it's caused mysterious memory
68 * corruption in the past.
69 * The linker runs before we bring up libc and it's easiest
70 * to make sure it does not depend on any complex libc features
71 *
72 * open issues / todo:
73 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074 * - are we doing everything we should for ARM_COPY relocations?
75 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076 * - after linking, set as much stuff as possible to READONLY
77 * and NOEXEC
78 * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
79 * headers provide versions that are negative...
80 * - allocate space for soinfo structs dynamically instead of
Elliott Hughes46882792012-08-03 16:49:39 -070081 * having a hard limit (SO_MAX)
82 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083
84
Nick Kralevich5135b3a2012-08-10 21:08:42 -070085static int soinfo_link_image(soinfo *si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
87static int socount = 0;
88static soinfo sopool[SO_MAX];
89static soinfo *freelist = NULL;
90static soinfo *solist = &libdl_info;
91static soinfo *sonext = &libdl_info;
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070092#if ALLOW_SYMBOLS_FROM_MAIN
93static soinfo *somain; /* main process, always the one after libdl_info */
94#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
Iliyan Malchevaf7315a2009-10-16 17:50:42 -070096
David Bartleybc3a5c22009-06-02 18:27:28 -070097static char ldpaths_buf[LDPATH_BUFSIZE];
98static const char *ldpaths[LDPATH_MAX + 1];
99
Matt Fischer4fd42c12009-12-31 12:09:10 -0600100static char ldpreloads_buf[LDPRELOAD_BUFSIZE];
101static const char *ldpreload_names[LDPRELOAD_MAX + 1];
102
103static soinfo *preloads[LDPRELOAD_MAX + 1];
104
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -0700105#if LINKER_DEBUG
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106int debug_verbosity;
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -0700107#endif
108
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109static int pid;
110
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100111/* This boolean is set if the program being loaded is setuid */
Elliott Hughesbedfe382012-08-14 14:07:59 -0700112static bool program_is_setuid;
113
114enum RelocationKind {
115 kRelocAbsolute = 0,
116 kRelocRelative,
117 kRelocCopy,
118 kRelocSymbol,
119 kRelocMax
120};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100121
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700123struct linker_stats_t {
124 int count[kRelocMax];
125};
126
127static linker_stats_t linker_stats;
128
129static void count_relocation(RelocationKind kind) {
130 ++linker_stats.count[kind];
131}
132#else
133static void count_relocation(RelocationKind) {
134}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135#endif
136
137#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700138static unsigned bitmask[4096];
139#define MARK(offset) \
140 do { \
141 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
142 } while(0)
143#else
144#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145#endif
146
Elliott Hughes46882792012-08-03 16:49:39 -0700147// You shouldn't try to call memory-allocating functions in the dynamic linker.
148// Guard against the most obvious ones.
149#define DISALLOW_ALLOCATION(return_type, name, ...) \
150 return_type name __VA_ARGS__ \
151 { \
152 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
153 __libc_android_log_write(ANDROID_LOG_FATAL, "linker", msg); \
154 write(2, msg, sizeof(msg)); \
155 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700156 }
Elliott Hughes46882792012-08-03 16:49:39 -0700157#define UNUSED __attribute__((unused))
158DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
159DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
160DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
161DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
Dima Zavin2e855792009-05-20 18:28:09 -0700162
Dima Zavin03531952009-05-29 17:30:25 -0700163static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700164static char __linker_dl_err_buf[768];
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700165#define DL_ERR(fmt, x...) \
166 do { \
Elliott Hughes3b297c42012-10-11 16:08:51 -0700167 format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), fmt, ##x); \
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700168 ERROR(fmt "\n", ##x); \
Dima Zavin2e855792009-05-20 18:28:09 -0700169 } while(0)
170
Elliott Hughes5419b942012-10-16 15:54:46 -0700171const char* linker_get_error() {
172 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700173}
174
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175/*
176 * This function is an empty stub where GDB locates a breakpoint to get notified
177 * about linker activity.
178 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700179extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180
Elliott Hughesbedfe382012-08-14 14:07:59 -0700181static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182 RT_CONSISTENT, 0};
Elliott Hughesbedfe382012-08-14 14:07:59 -0700183static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184
Elliott Hughes3b297c42012-10-11 16:08:51 -0700185static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186
Elliott Hughesbedfe382012-08-14 14:07:59 -0700187static void insert_soinfo_into_debug_map(soinfo * info) {
188 // Copy the necessary fields into the debug structure.
189 link_map* map = &(info->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190 map->l_addr = info->base;
191 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800192 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193
194 /* Stick the new library at the end of the list.
195 * gdb tends to care more about libc than it does
196 * about leaf libraries, and ordering it this way
197 * reduces the back-and-forth over the wire.
198 */
199 if (r_debug_tail) {
200 r_debug_tail->l_next = map;
201 map->l_prev = r_debug_tail;
202 map->l_next = 0;
203 } else {
204 _r_debug.r_map = map;
205 map->l_prev = 0;
206 map->l_next = 0;
207 }
208 r_debug_tail = map;
209}
210
Elliott Hughesbedfe382012-08-14 14:07:59 -0700211static void remove_soinfo_from_debug_map(soinfo* info) {
212 link_map* map = &(info->linkmap);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700213
Elliott Hughesbedfe382012-08-14 14:07:59 -0700214 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700215 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700216 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700217
Elliott Hughesbedfe382012-08-14 14:07:59 -0700218 if (map->l_prev) {
219 map->l_prev->l_next = map->l_next;
220 }
221 if (map->l_next) {
222 map->l_next->l_prev = map->l_prev;
223 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700224}
225
Elliott Hughesbedfe382012-08-14 14:07:59 -0700226static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227 if (info->flags & FLAG_EXE) {
228 // GDB already knows about the main executable
229 return;
230 }
231
Elliott Hughes3b297c42012-10-11 16:08:51 -0700232 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233
234 _r_debug.r_state = RT_ADD;
235 rtld_db_dlactivity();
236
237 insert_soinfo_into_debug_map(info);
238
239 _r_debug.r_state = RT_CONSISTENT;
240 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700241}
242
Elliott Hughesbedfe382012-08-14 14:07:59 -0700243static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700244 if (info->flags & FLAG_EXE) {
245 // GDB already knows about the main executable
246 return;
247 }
248
Elliott Hughes3b297c42012-10-11 16:08:51 -0700249 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700250
251 _r_debug.r_state = RT_DELETE;
252 rtld_db_dlactivity();
253
254 remove_soinfo_from_debug_map(info);
255
256 _r_debug.r_state = RT_CONSISTENT;
257 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800258}
259
Elliott Hughes46882792012-08-03 16:49:39 -0700260extern "C" void notify_gdb_of_libraries()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800261{
262 _r_debug.r_state = RT_ADD;
263 rtld_db_dlactivity();
264 _r_debug.r_state = RT_CONSISTENT;
265 rtld_db_dlactivity();
266}
267
David 'Digit' Turner16084162012-06-12 16:25:37 +0200268static soinfo *soinfo_alloc(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269{
Elliott Hughes46882792012-08-03 16:49:39 -0700270 if (strlen(name) >= SOINFO_NAME_LEN) {
271 DL_ERR("library name \"%s\" too long", name);
Doug Kwan94304352009-10-23 18:11:40 -0700272 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273 }
274
David 'Digit' Turner16084162012-06-12 16:25:37 +0200275 /* The freelist is populated when we call soinfo_free(), which in turn is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800276 done only by dlclose(), which is not likely to be used.
277 */
278 if (!freelist) {
Elliott Hughes46882792012-08-03 16:49:39 -0700279 if (socount == SO_MAX) {
280 DL_ERR("too many libraries when loading \"%s\"", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800281 return NULL;
282 }
283 freelist = sopool + socount++;
284 freelist->next = NULL;
285 }
286
Elliott Hughes46882792012-08-03 16:49:39 -0700287 soinfo* si = freelist;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800288 freelist = freelist->next;
289
290 /* Make sure we get a clean block of soinfo */
291 memset(si, 0, sizeof(soinfo));
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100292 strlcpy((char*) si->name, name, sizeof(si->name));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800293 sonext->next = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800294 si->next = NULL;
295 si->refcount = 0;
296 sonext = si;
297
298 TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
299 return si;
300}
301
Elliott Hughes46882792012-08-03 16:49:39 -0700302static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303{
Elliott Hughes46882792012-08-03 16:49:39 -0700304 if (si == NULL) {
305 return;
306 }
307
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308 soinfo *prev = NULL, *trav;
309
310 TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
311
312 for(trav = solist; trav != NULL; trav = trav->next){
313 if (trav == si)
314 break;
315 prev = trav;
316 }
317 if (trav == NULL) {
318 /* si was not ni solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700319 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800320 return;
321 }
322
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100323 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324 always the static libdl_info.
325 */
326 prev->next = si->next;
327 if (si == sonext) sonext = prev;
328 si->next = freelist;
329 freelist = si;
330}
331
Elliott Hughes46882792012-08-03 16:49:39 -0700332#ifdef ANDROID_ARM_LINKER
333
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334/* For a given PC, find the .so that it belongs to.
335 * Returns the base address of the .ARM.exidx section
336 * for that .so, and the number of 8-byte entries
337 * in that section (via *pcount).
338 *
339 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
340 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700341 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800342 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800343_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
344{
345 soinfo *si;
346 unsigned addr = (unsigned)pc;
347
Nick Kralevich468319c2011-11-11 15:53:17 -0800348 for (si = solist; si != 0; si = si->next){
349 if ((addr >= si->base) && (addr < (si->base + si->size))) {
350 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900351 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800352 }
353 }
354 *pcount = 0;
355 return NULL;
356}
Elliott Hughes46882792012-08-03 16:49:39 -0700357
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700358#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
Elliott Hughes46882792012-08-03 16:49:39 -0700359
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800360/* Here, we only have to provide a callback to iterate across all the
361 * loaded libraries. gcc_eh does the rest. */
362int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700363dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364 void *data)
365{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800366 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700367 for (soinfo* si = solist; si != NULL; si = si->next) {
368 dl_phdr_info dl_info;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800369 dl_info.dlpi_addr = si->linkmap.l_addr;
370 dl_info.dlpi_name = si->linkmap.l_name;
371 dl_info.dlpi_phdr = si->phdr;
372 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700373 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
374 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800375 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700376 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800377 }
378 return rv;
379}
Elliott Hughes46882792012-08-03 16:49:39 -0700380
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800381#endif
382
David 'Digit' Turner16084162012-06-12 16:25:37 +0200383static Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800384{
385 Elf32_Sym *s;
386 Elf32_Sym *symtab = si->symtab;
387 const char *strtab = si->strtab;
388 unsigned n;
389
390 TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
391 name, si->name, si->base, hash, hash % si->nbucket);
392 n = hash % si->nbucket;
393
394 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
395 s = symtab + n;
396 if(strcmp(strtab + s->st_name, name)) continue;
397
Doug Kwane8238072009-10-26 12:05:23 -0700398 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800399 switch(ELF32_ST_BIND(s->st_info)){
400 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700401 case STB_WEAK:
Robin Burchell439fa8e2012-07-05 09:21:07 +0200402 if(s->st_shndx == SHN_UNDEF)
403 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800404
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405 TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
406 name, si->name, s->st_value, s->st_size);
407 return s;
408 }
409 }
410
Doug Kwan94304352009-10-23 18:11:40 -0700411 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800412}
413
414static unsigned elfhash(const char *_name)
415{
416 const unsigned char *name = (const unsigned char *) _name;
417 unsigned h = 0, g;
418
419 while(*name) {
420 h = (h << 4) + *name++;
421 g = h & 0xf0000000;
422 h ^= g;
423 h ^= g >> 24;
424 }
425 return h;
426}
427
428static Elf32_Sym *
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200429soinfo_do_lookup(soinfo *si, const char *name, Elf32_Addr *offset,
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700430 soinfo *needed[], bool ignore_local)
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700431{
Doug Kwan94304352009-10-23 18:11:40 -0700432 unsigned elf_hash = elfhash(name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700433 Elf32_Sym *s = NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700434 soinfo *lsi = si;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600435 int i;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700436
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700437 if (!ignore_local) {
438 /* Look for symbols in the local scope (the object who is
439 * searching). This happens with C++ templates on i386 for some
440 * reason.
441 *
442 * Notes on weak symbols:
443 * The ELF specs are ambiguous about treatment of weak definitions in
444 * dynamic linking. Some systems return the first definition found
445 * and some the first non-weak definition. This is system dependent.
446 * Here we return the first definition found for simplicity. */
Nick Kralevich468319c2011-11-11 15:53:17 -0800447
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700448 s = soinfo_elf_lookup(si, elf_hash, name);
449 if(s != NULL)
450 goto done;
451 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700452
Matt Fischer4fd42c12009-12-31 12:09:10 -0600453 /* Next, look for it in the preloads list */
454 for(i = 0; preloads[i] != NULL; i++) {
455 lsi = preloads[i];
David 'Digit' Turner16084162012-06-12 16:25:37 +0200456 s = soinfo_elf_lookup(lsi, elf_hash, name);
Matt Fischer4fd42c12009-12-31 12:09:10 -0600457 if(s != NULL)
458 goto done;
459 }
460
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200461 for(i = 0; needed[i] != NULL; i++) {
462 lsi = needed[i];
463 DEBUG("%5d %s: looking up %s in %s\n",
464 pid, si->name, name, lsi->name);
465 s = soinfo_elf_lookup(lsi, elf_hash, name);
466 if (s != NULL)
467 goto done;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700468 }
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
Elliott Hughesbedfe382012-08-14 14:07:59 -0700591static int _open_lib(const char* name) {
592 // TODO: why not just call open?
593 struct stat sb;
594 if (stat(name, &sb) == -1 || !S_ISREG(sb.st_mode)) {
595 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800596 }
Elliott Hughesbedfe382012-08-14 14:07:59 -0700597 return TEMP_FAILURE_RETRY(open(name, O_RDONLY));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598}
599
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800600static int open_library(const char *name)
601{
602 int fd;
603 char buf[512];
David 'Digit' Turner16084162012-06-12 16:25:37 +0200604 const char * const*path;
David Bartleybc3a5c22009-06-02 18:27:28 -0700605 int n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800606
607 TRACE("[ %5d opening %s ]\n", pid, name);
608
609 if(name == 0) return -1;
610 if(strlen(name) > 256) return -1;
611
612 if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
613 return fd;
614
David Bartleybc3a5c22009-06-02 18:27:28 -0700615 for (path = ldpaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800616 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700617 if (n < 0 || n >= (int)sizeof(buf)) {
618 WARN("Ignoring very long library path: %s/%s\n", *path, name);
619 continue;
620 }
621 if ((fd = _open_lib(buf)) >= 0)
622 return fd;
623 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800624 for (path = sopaths; *path; path++) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800625 n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
David Bartleybc3a5c22009-06-02 18:27:28 -0700626 if (n < 0 || n >= (int)sizeof(buf)) {
627 WARN("Ignoring very long library path: %s/%s\n", *path, name);
628 continue;
629 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630 if ((fd = _open_lib(buf)) >= 0)
631 return fd;
632 }
633
634 return -1;
635}
636
Elliott Hughes46882792012-08-03 16:49:39 -0700637// Returns 'true' if the library is prelinked or on failure so we error out
638// either way. We no longer support prelinking.
639static bool is_prelinked(int fd, const char* name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800640{
Elliott Hughes46882792012-08-03 16:49:39 -0700641 struct prelink_info_t {
642 long mmap_addr;
643 char tag[4]; // "PRE ".
644 };
645
Elliott Hughesbedfe382012-08-14 14:07:59 -0700646 off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800647 if (sz < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700648 DL_ERR("lseek failed: %s", strerror(errno));
649 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800650 }
651
Elliott Hughesbedfe382012-08-14 14:07:59 -0700652 prelink_info_t info;
Elliott Hughes8dfc0732012-07-27 15:30:51 -0700653 int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
654 if (rc != sizeof(info)) {
Elliott Hughes46882792012-08-03 16:49:39 -0700655 DL_ERR("could not read prelink_info_t structure for \"%s\":", name, strerror(errno));
656 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657 }
658
Elliott Hughes46882792012-08-03 16:49:39 -0700659 if (memcmp(info.tag, "PRE ", 4) == 0) {
660 DL_ERR("prelinked libraries no longer supported: %s", name);
661 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800662 }
Elliott Hughes46882792012-08-03 16:49:39 -0700663 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800664}
665
David 'Digit' Turner16084162012-06-12 16:25:37 +0200666/* verify_elf_header
667 * Verifies the content of an ELF header.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800668 *
669 * Args:
670 *
671 * Returns:
672 * 0 on success
673 * -1 if no valid ELF object is found @ base.
674 */
675static int
David 'Digit' Turner16084162012-06-12 16:25:37 +0200676verify_elf_header(const Elf32_Ehdr* hdr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800677{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800678 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
679 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
680 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
681 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700682 if (hdr->e_type != ET_DYN) return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800683
684 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800685#ifdef ANDROID_ARM_LINKER
686 if (hdr->e_machine != EM_ARM) return -1;
687#elif defined(ANDROID_X86_LINKER)
688 if (hdr->e_machine != EM_386) return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700689#elif defined(ANDROID_MIPS_LINKER)
690 if (hdr->e_machine != EM_MIPS) return -1;
Zhenghua Wang897815a2011-10-19 00:29:14 +0800691#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800692 return 0;
693}
694
Elliott Hughes46882792012-08-03 16:49:39 -0700695struct scoped_fd {
696 ~scoped_fd() {
697 if (fd != -1) {
698 close(fd);
699 }
700 }
701 int fd;
702};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800703
Elliott Hughes46882792012-08-03 16:49:39 -0700704struct soinfo_ptr {
705 soinfo_ptr(const char* name) {
706 const char* bname = strrchr(name, '/');
707 ptr = soinfo_alloc(bname ? bname + 1 : name);
708 }
709 ~soinfo_ptr() {
710 soinfo_free(ptr);
711 }
712 soinfo* release() {
713 soinfo* result = ptr;
714 ptr = NULL;
715 return result;
716 }
717 soinfo* ptr;
718};
719
720// TODO: rewrite linker_phdr.h to use a class, then lose this.
721struct phdr_ptr {
722 phdr_ptr() : phdr_mmap(NULL) {}
723 ~phdr_ptr() {
724 if (phdr_mmap != NULL) {
725 phdr_table_unload(phdr_mmap, phdr_size);
726 }
727 }
728 void* phdr_mmap;
729 Elf32_Addr phdr_size;
730};
731
732static soinfo* load_library(const char* name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800733{
Elliott Hughes46882792012-08-03 16:49:39 -0700734 // Open the file.
735 scoped_fd fd;
736 fd.fd = open_library(name);
737 if (fd.fd == -1) {
738 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800739 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700740 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800741
Elliott Hughes46882792012-08-03 16:49:39 -0700742 // Read the ELF header.
743 Elf32_Ehdr header[1];
744 int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200745 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700746 DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
747 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200748 }
749 if (ret != (int)sizeof(header)) {
Elliott Hughes46882792012-08-03 16:49:39 -0700750 DL_ERR("too small to be an ELF executable: %s", name);
751 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200752 }
753 if (verify_elf_header(header) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700754 DL_ERR("not a valid ELF executable: %s", name);
755 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800756 }
757
Elliott Hughes46882792012-08-03 16:49:39 -0700758 // Read the program header table.
759 const Elf32_Phdr* phdr_table;
760 phdr_ptr phdr_holder;
761 ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
762 &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200763 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700764 DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
765 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200766 }
Elliott Hughes46882792012-08-03 16:49:39 -0700767 size_t phdr_count = header->e_phnum;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200768
Elliott Hughes46882792012-08-03 16:49:39 -0700769 // Get the load extents.
770 Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
771 TRACE("[ %5d - '%s' wants sz=0x%08x ]\n", pid, name, ext_sz);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200772 if (ext_sz == 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700773 DL_ERR("no loadable segments in file: %s", name);
774 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800775 }
776
Elliott Hughes46882792012-08-03 16:49:39 -0700777 // We no longer support pre-linked libraries.
778 if (is_prelinked(fd.fd, name)) {
779 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200780 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200781
Elliott Hughes46882792012-08-03 16:49:39 -0700782 // Reserve address space for all loadable segments.
783 void* load_start = NULL;
784 Elf32_Addr load_size = 0;
785 Elf32_Addr load_bias = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200786 ret = phdr_table_reserve_memory(phdr_table,
787 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200788 &load_start,
789 &load_size,
790 &load_bias);
791 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700792 DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
793 ext_sz, name, strerror(errno));
794 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200795 }
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,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200803 load_bias,
Elliott Hughes46882792012-08-03 16:49:39 -0700804 fd.fd);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200805 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700806 DL_ERR("can't map loadable segments for \"%s\": %s",
807 name, strerror(errno));
808 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200809 }
810
Elliott Hughes46882792012-08-03 16:49:39 -0700811 soinfo_ptr si(name);
812 if (si.ptr == NULL) {
813 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800814 }
815
Elliott Hughes46882792012-08-03 16:49:39 -0700816 si.ptr->base = (Elf32_Addr) load_start;
817 si.ptr->size = load_size;
818 si.ptr->load_bias = load_bias;
819 si.ptr->flags = 0;
820 si.ptr->entry = 0;
821 si.ptr->dynamic = (unsigned *)-1;
822 si.ptr->phnum = phdr_count;
823 si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
824 if (si.ptr->phdr == NULL) {
825 DL_ERR("can't find loaded PHDR for \"%s\"", name);
826 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200827 }
Elliott Hughes46882792012-08-03 16:49:39 -0700828
829 return si.release();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800830}
831
832static soinfo *
833init_library(soinfo *si)
834{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800835 /* At this point we know that whatever is loaded @ base is a valid ELF
836 * shared library whose segments are properly mapped in. */
837 TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
838 pid, si->base, si->size, si->name);
839
Nick Kralevich5135b3a2012-08-10 21:08:42 -0700840 if(soinfo_link_image(si)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800841 munmap((void *)si->base, si->size);
842 return NULL;
843 }
844
845 return si;
846}
847
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200848static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800849{
850 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700851 const char *bname;
852
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200853 // TODO: don't use basename only for determining libraries
854 // http://code.google.com/p/android/issues/detail?id=6670
855
856 bname = strrchr(name, '/');
857 bname = bname ? bname + 1 : name;
858
859 for(si = solist; si != NULL; si = si->next){
860 if(!strcmp(bname, si->name)) {
861 return si;
862 }
863 }
864 return NULL;
865}
866
867soinfo *find_library(const char *name)
868{
869 soinfo *si;
870
David 'Digit' Turner67748092010-07-21 16:18:21 -0700871#if ALLOW_SYMBOLS_FROM_MAIN
872 if (name == NULL)
873 return somain;
874#else
875 if (name == NULL)
876 return NULL;
877#endif
878
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200879 si = find_loaded_library(name);
880 if (si != NULL) {
881 if(si->flags & FLAG_ERROR) {
882 DL_ERR("\"%s\" failed to load previously", name);
Dima Zavin2e855792009-05-20 18:28:09 -0700883 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800884 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200885 if(si->flags & FLAG_LINKED) return si;
886 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
887 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800888 }
889
890 TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
891 si = load_library(name);
892 if(si == NULL)
893 return NULL;
894 return init_library(si);
895}
896
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800897static void call_destructors(soinfo *si);
Elliott Hughesbedfe382012-08-14 14:07:59 -0700898
899int soinfo_unload(soinfo* si) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800900 if (si->refcount == 1) {
901 TRACE("%5d unloading '%s'\n", pid, si->name);
902 call_destructors(si);
903
Elliott Hughesbedfe382012-08-14 14:07:59 -0700904 for (unsigned* d = si->dynamic; *d; d += 2) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800905 if(d[0] == DT_NEEDED){
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200906 soinfo *lsi = find_loaded_library(si->strtab + d[1]);
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200907 if (lsi) {
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700908 TRACE("%5d %s needs to unload %s\n", pid,
909 si->name, lsi->name);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200910 soinfo_unload(lsi);
Elliott Hughesbedfe382012-08-14 14:07:59 -0700911 } else {
912 // TODO: should we return -1 in this case?
Elliott Hughes46882792012-08-03 16:49:39 -0700913 DL_ERR("\"%s\": could not unload dependent library",
914 si->name);
Elliott Hughesbedfe382012-08-14 14:07:59 -0700915 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800916 }
917 }
918
919 munmap((char *)si->base, si->size);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700920 notify_gdb_of_unload(si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200921 soinfo_free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800922 si->refcount = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700923 } else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800924 si->refcount--;
925 PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
926 pid, si->name, si->refcount);
927 }
Elliott Hughesbedfe382012-08-14 14:07:59 -0700928 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800929}
930
931/* TODO: don't use unsigned for addrs below. It works, but is not
932 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
933 * long.
934 */
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200935static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count,
936 soinfo *needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800937{
938 Elf32_Sym *symtab = si->symtab;
939 const char *strtab = si->strtab;
940 Elf32_Sym *s;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900941 Elf32_Addr offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800942 Elf32_Rel *start = rel;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800943
Elliott Hughes46882792012-08-03 16:49:39 -0700944 for (size_t idx = 0; idx < count; ++idx, ++rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800945 unsigned type = ELF32_R_TYPE(rel->r_info);
946 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200947 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800948 unsigned sym_addr = 0;
949 char *sym_name = NULL;
950
951 DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
952 si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700953 if (type == 0) { // R_*_NONE
954 continue;
955 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800956 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -0700957 sym_name = (char *)(strtab + symtab[sym].st_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700958 bool ignore_local = false;
959#if defined(ANDROID_ARM_LINKER)
960 ignore_local = (type == R_ARM_COPY);
961#endif
962 s = soinfo_do_lookup(si, sym_name, &offset, needed, ignore_local);
Doug Kwane8238072009-10-26 12:05:23 -0700963 if(s == NULL) {
964 /* We only allow an undefined symbol if this is a weak
965 reference.. */
966 s = &symtab[sym];
967 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700968 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -0700969 return -1;
970 }
971
972 /* IHI0044C AAELF 4.5.1.1:
973
974 Libraries are not searched to resolve weak references.
975 It is not an error for a weak reference to remain
976 unsatisfied.
977
978 During linking, the value of an undefined weak reference is:
979 - Zero if the relocation type is absolute
980 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -0700981 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -0700982 type is base-relative.
983 */
984
985 switch (type) {
986#if defined(ANDROID_ARM_LINKER)
987 case R_ARM_JUMP_SLOT:
988 case R_ARM_GLOB_DAT:
989 case R_ARM_ABS32:
990 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -0700991#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700992 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -0700993 case R_386_GLOB_DAT:
994 case R_386_32:
995 case R_386_RELATIVE: /* Dont' care. */
996#endif /* ANDROID_*_LINKER */
997 /* sym_addr was initialized to be zero above or relocation
998 code below does not care about value of sym_addr.
999 No need to do anything. */
1000 break;
1001
1002#if defined(ANDROID_X86_LINKER)
1003 case R_386_PC32:
1004 sym_addr = reloc;
1005 break;
1006#endif /* ANDROID_X86_LINKER */
1007
1008#if defined(ANDROID_ARM_LINKER)
1009 case R_ARM_COPY:
1010 /* Fall through. Can't really copy if weak symbol is
1011 not found in run-time. */
1012#endif /* ANDROID_ARM_LINKER */
1013 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001014 DL_ERR("unknown weak reloc type %d @ %p (%d)",
1015 type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -07001016 return -1;
1017 }
1018 } else {
1019 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001020#if 0
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001021 if((base == 0) && (si->base != 0)){
1022 /* linking from libraries to main image is bad */
Elliott Hughes46882792012-08-03 16:49:39 -07001023 DL_ERR("cannot locate \"%s\"...",
1024 strtab + symtab[sym].st_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001025 return -1;
1026 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001027#endif
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001028 sym_addr = (unsigned)(s->st_value + offset);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001029 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001030 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001031 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001032 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001033 }
1034
1035/* TODO: This is ugly. Split up the relocations by arch into
1036 * different files.
1037 */
1038 switch(type){
1039#if defined(ANDROID_ARM_LINKER)
1040 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001041 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001042 MARK(rel->r_offset);
1043 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1044 reloc, sym_addr, sym_name);
1045 *((unsigned*)reloc) = sym_addr;
1046 break;
1047 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001048 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001049 MARK(rel->r_offset);
1050 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1051 reloc, sym_addr, sym_name);
1052 *((unsigned*)reloc) = sym_addr;
1053 break;
1054 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001055 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001056 MARK(rel->r_offset);
1057 TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
1058 reloc, sym_addr, sym_name);
1059 *((unsigned*)reloc) += sym_addr;
1060 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001061 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001062 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001063 MARK(rel->r_offset);
1064 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
1065 reloc, sym_addr, rel->r_offset, sym_name);
1066 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1067 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001068#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001069 case R_386_JMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001070 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001071 MARK(rel->r_offset);
1072 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1073 reloc, sym_addr, sym_name);
1074 *((unsigned*)reloc) = sym_addr;
1075 break;
1076 case R_386_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001077 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001078 MARK(rel->r_offset);
1079 TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
1080 reloc, sym_addr, sym_name);
1081 *((unsigned*)reloc) = sym_addr;
1082 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001083#elif defined(ANDROID_MIPS_LINKER)
1084 case R_MIPS_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001085 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001086 MARK(rel->r_offset);
1087 TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1088 reloc, sym_addr, sym_name);
1089 *((unsigned*)reloc) = sym_addr;
1090 break;
1091 case R_MIPS_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001092 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001093 MARK(rel->r_offset);
1094 TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x %s\n", pid,
1095 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1096 if (s) {
1097 *((unsigned*)reloc) += sym_addr;
1098 } else {
1099 *((unsigned*)reloc) += si->base;
1100 }
1101 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001102#endif /* ANDROID_*_LINKER */
1103
1104#if defined(ANDROID_ARM_LINKER)
1105 case R_ARM_RELATIVE:
1106#elif defined(ANDROID_X86_LINKER)
1107 case R_386_RELATIVE:
1108#endif /* ANDROID_*_LINKER */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001109 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001110 MARK(rel->r_offset);
Elliott Hughes46882792012-08-03 16:49:39 -07001111 if (sym) {
1112 DL_ERR("odd RELATIVE form...", pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001113 return -1;
1114 }
1115 TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
1116 reloc, si->base);
1117 *((unsigned*)reloc) += si->base;
1118 break;
1119
1120#if defined(ANDROID_X86_LINKER)
1121 case R_386_32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001122 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001123 MARK(rel->r_offset);
1124
1125 TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
1126 reloc, sym_addr, sym_name);
1127 *((unsigned *)reloc) += (unsigned)sym_addr;
1128 break;
1129
1130 case R_386_PC32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001131 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001132 MARK(rel->r_offset);
1133 TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
1134 "+%08x (%08x - %08x) %s\n", pid, reloc,
1135 (sym_addr - reloc), sym_addr, reloc, sym_name);
1136 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1137 break;
1138#endif /* ANDROID_X86_LINKER */
1139
1140#ifdef ANDROID_ARM_LINKER
1141 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001142 if ((si->flags & FLAG_EXE) == 0) {
1143 /*
1144 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1145 *
1146 * Section 4.7.1.10 "Dynamic relocations"
1147 * R_ARM_COPY may only appear in executable objects where e_type is
1148 * set to ET_EXEC.
1149 *
1150 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1151 * We should explicitly disallow ET_DYN executables from having
1152 * R_ARM_COPY relocations.
1153 */
1154 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1155 return -1;
1156 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001157 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001158 MARK(rel->r_offset);
1159 TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
1160 reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001161 if (reloc == sym_addr) {
1162 DL_ERR("Internal linker error detected. reloc == symaddr");
1163 return -1;
1164 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001165 memcpy((void*)reloc, (void*)sym_addr, s->st_size);
1166 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001167#endif /* ANDROID_ARM_LINKER */
1168
1169 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001170 DL_ERR("unknown reloc type %d @ %p (%d)",
1171 type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001172 return -1;
1173 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001174 }
1175 return 0;
1176}
1177
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001178#ifdef ANDROID_MIPS_LINKER
Elliott Hughesbedfe382012-08-14 14:07:59 -07001179static int mips_relocate_got(soinfo* si, soinfo* needed[]) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001180 unsigned *got;
1181 unsigned local_gotno, gotsym, symtabno;
1182 Elf32_Sym *symtab, *sym;
1183 unsigned g;
1184
1185 got = si->plt_got;
1186 local_gotno = si->mips_local_gotno;
1187 gotsym = si->mips_gotsym;
1188 symtabno = si->mips_symtabno;
1189 symtab = si->symtab;
1190
1191 /*
1192 * got[0] is address of lazy resolver function
1193 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001194 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001195 * (should be _rtld_bind_start)
1196 * FIXME: maybe this should be in a separate routine
1197 */
1198
1199 if ((si->flags & FLAG_LINKER) == 0) {
1200 g = 0;
1201 got[g++] = 0xdeadbeef;
1202 if (got[g] & 0x80000000) {
1203 got[g++] = 0xdeadfeed;
1204 }
1205 /*
1206 * Relocate the local GOT entries need to be relocated
1207 */
1208 for (; g < local_gotno; g++) {
1209 got[g] += si->load_bias;
1210 }
1211 }
1212
1213 /* Now for the global GOT entries */
1214 sym = symtab + gotsym;
1215 got = si->plt_got + local_gotno;
1216 for (g = gotsym; g < symtabno; g++, sym++, got++) {
1217 const char *sym_name;
1218 unsigned base;
1219 Elf32_Sym *s;
1220
1221 /* This is an undefined reference... try to locate it */
1222 sym_name = si->strtab + sym->st_name;
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001223 s = soinfo_do_lookup(si, sym_name, &base, needed, false);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001224 if (s == NULL) {
1225 /* We only allow an undefined symbol if this is a weak
1226 reference.. */
1227 s = &symtab[g];
1228 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001229 DL_ERR("cannot locate \"%s\"...", sym_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001230 return -1;
1231 }
1232 *got = 0;
1233 }
1234 else {
1235 /* FIXME: is this sufficient?
1236 * For reference see NetBSD link loader
1237 * 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
1238 */
1239 *got = base + s->st_value;
1240 }
1241 }
1242 return 0;
1243}
1244#endif
1245
David 'Digit' Turner82156792009-05-18 14:37:41 +02001246/* Please read the "Initialization and Termination functions" functions.
1247 * of the linker design note in bionic/linker/README.TXT to understand
1248 * what the following code is doing.
1249 *
1250 * The important things to remember are:
1251 *
1252 * DT_PREINIT_ARRAY must be called first for executables, and should
1253 * not appear in shared libraries.
1254 *
1255 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1256 *
1257 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1258 *
1259 * DT_FINI_ARRAY must be parsed in reverse order.
1260 */
1261
1262static void call_array(unsigned *ctor, int count, int reverse)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001263{
David 'Digit' Turner82156792009-05-18 14:37:41 +02001264 int n, inc = 1;
1265
1266 if (reverse) {
1267 ctor += (count-1);
1268 inc = -1;
1269 }
1270
1271 for(n = count; n > 0; n--) {
1272 TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
1273 reverse ? "dtor" : "ctor",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001274 (unsigned)ctor, (unsigned)*ctor);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001275 void (*func)() = (void (*)()) *ctor;
1276 ctor += inc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001277 if(((int) func == 0) || ((int) func == -1)) continue;
1278 TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
1279 func();
1280 }
1281}
1282
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001283static void soinfo_call_preinit_constructors(soinfo *si)
1284{
1285 TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
1286 pid, (unsigned)si->preinit_array, si->preinit_array_count,
1287 si->name);
1288 call_array(si->preinit_array, si->preinit_array_count, 0);
1289 TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
1290}
1291
David 'Digit' Turner16084162012-06-12 16:25:37 +02001292void soinfo_call_constructors(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001293{
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001294 if (si->constructors_called)
1295 return;
1296
Jesse Hallf5d16932012-01-30 15:39:57 -08001297 // Set this before actually calling the constructors, otherwise it doesn't
1298 // protect against recursive constructor calls. One simple example of
1299 // constructor recursion is the libc debug malloc, which is implemented in
1300 // libc_malloc_debug_leak.so:
1301 // 1. The program depends on libc, so libc's constructor is called here.
1302 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001303 // 3. dlopen() calls soinfo_call_constructors() with the newly created
Jesse Hallf5d16932012-01-30 15:39:57 -08001304 // soinfo for libc_malloc_debug_leak.so.
David 'Digit' Turner16084162012-06-12 16:25:37 +02001305 // 4. The debug so depends on libc, so soinfo_call_constructors() is
Jesse Hallf5d16932012-01-30 15:39:57 -08001306 // called again with the libc soinfo. If it doesn't trigger the early-
1307 // out above, the libc constructor will be called again (recursively!).
1308 si->constructors_called = 1;
1309
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001310 if (!(si->flags & FLAG_EXE) && si->preinit_array) {
1311 DL_ERR("shared library \"%s\" has a preinit_array table @ 0x%08x. "
1312 "This is INVALID.", si->name, (unsigned) si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001313 }
1314
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001315 if (si->dynamic) {
1316 unsigned *d;
1317 for(d = si->dynamic; *d; d += 2) {
1318 if(d[0] == DT_NEEDED){
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001319 soinfo* lsi = find_loaded_library(si->strtab + d[1]);
1320 if (!lsi) {
1321 DL_ERR("\"%s\": could not initialize dependent library",
1322 si->name);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001323 } else {
David 'Digit' Turner16084162012-06-12 16:25:37 +02001324 soinfo_call_constructors(lsi);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001325 }
1326 }
1327 }
1328 }
1329
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001330 if (si->init_func) {
1331 TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
1332 (unsigned)si->init_func, si->name);
1333 si->init_func();
1334 TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
1335 }
1336
1337 if (si->init_array) {
1338 TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
1339 (unsigned)si->init_array, si->init_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001340 call_array(si->init_array, si->init_array_count, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001341 TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
1342 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001343
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001344}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001345
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001346static void call_destructors(soinfo *si)
1347{
1348 if (si->fini_array) {
1349 TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
1350 (unsigned)si->fini_array, si->fini_array_count, si->name);
David 'Digit' Turner82156792009-05-18 14:37:41 +02001351 call_array(si->fini_array, si->fini_array_count, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001352 TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
1353 }
1354
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001355 if (si->fini_func) {
1356 TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
1357 (unsigned)si->fini_func, si->name);
1358 si->fini_func();
1359 TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
1360 }
1361}
1362
1363/* Force any of the closed stdin, stdout and stderr to be associated with
1364 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001365static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001366 int dev_null, i, status;
1367 int return_value = 0;
1368
David 'Digit' Turner16084162012-06-12 16:25:37 +02001369 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001370 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001371 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001372 return -1;
1373 }
1374 TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
1375
1376 /* If any of the stdio file descriptors is valid and not associated
1377 with /dev/null, dup /dev/null to it. */
1378 for (i = 0; i < 3; i++) {
1379 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001380 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001381 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001382 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001383
1384 TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
Elliott Hughes46882792012-08-03 16:49:39 -07001385 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001386
Elliott Hughes46882792012-08-03 16:49:39 -07001387 /* If file is opened, we are good. */
1388 if (status != -1) {
1389 continue;
1390 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001391
1392 /* The only error we allow is that the file descriptor does not
1393 exist, in which case we dup /dev/null to it. */
1394 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001395 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001396 return_value = -1;
1397 continue;
1398 }
1399
1400 /* Try dupping /dev/null to this stdio file descriptor and
1401 repeat if there is a signal. Note that any errors in closing
1402 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001403 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001404 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001405 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001406 return_value = -1;
1407 continue;
1408 }
1409 }
1410
1411 /* If /dev/null is not one of the stdio file descriptors, close it. */
1412 if (dev_null > 2) {
1413 TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001414 status = TEMP_FAILURE_RETRY(close(dev_null));
1415 if (status == -1) {
1416 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001417 return_value = -1;
1418 }
1419 }
1420
1421 return return_value;
1422}
1423
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001424static int soinfo_link_image(soinfo *si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001425{
1426 unsigned *d;
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001427 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001428 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001429 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001430 int phnum = si->phnum;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001431 int relocating_linker = (si->flags & FLAG_LINKER) != 0;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001432 soinfo **needed, **pneeded;
1433 size_t dynamic_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001434
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001435 /* We can't debug anything until the linker is relocated */
1436 if (!relocating_linker) {
1437 INFO("[ %5d linking %s ]\n", pid, si->name);
1438 DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1439 si->base, si->flags);
1440 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001441
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001442 /* Extract dynamic section */
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001443 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
1444 &dynamic_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001445 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001446 if (!relocating_linker) {
Elliott Hughes46882792012-08-03 16:49:39 -07001447 DL_ERR("missing PT_DYNAMIC?!");
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001448 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001449 goto fail;
1450 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001451 if (!relocating_linker) {
1452 DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1453 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001454 }
1455
1456#ifdef ANDROID_ARM_LINKER
1457 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1458 &si->ARM_exidx, &si->ARM_exidx_count);
1459#endif
1460
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001461 /* extract useful information from dynamic section */
1462 for(d = si->dynamic; *d; d++){
1463 DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
1464 switch(*d++){
1465 case DT_HASH:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001466 si->nbucket = ((unsigned *) (base + *d))[0];
1467 si->nchain = ((unsigned *) (base + *d))[1];
1468 si->bucket = (unsigned *) (base + *d + 8);
1469 si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001470 break;
1471 case DT_STRTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001472 si->strtab = (const char *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001473 break;
1474 case DT_SYMTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001475 si->symtab = (Elf32_Sym *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001476 break;
1477 case DT_PLTREL:
1478 if(*d != DT_REL) {
Erik Gillingd00d23a2009-07-22 17:06:11 -07001479 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001480 goto fail;
1481 }
1482 break;
1483 case DT_JMPREL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001484 si->plt_rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001485 break;
1486 case DT_PLTRELSZ:
1487 si->plt_rel_count = *d / 8;
1488 break;
1489 case DT_REL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001490 si->rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001491 break;
1492 case DT_RELSZ:
1493 si->rel_count = *d / 8;
1494 break;
1495 case DT_PLTGOT:
1496 /* Save this in case we decide to do lazy binding. We don't yet. */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001497 si->plt_got = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001498 break;
1499 case DT_DEBUG:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001500#if !defined(ANDROID_MIPS_LINKER)
Elliott Hughesbedfe382012-08-14 14:07:59 -07001501 // Set the DT_DEBUG entry to the address of _r_debug for GDB
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001502 *d = (int) &_r_debug;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001503#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001504 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001505 case DT_RELA:
Elliott Hughes46882792012-08-03 16:49:39 -07001506 DL_ERR("DT_RELA not supported");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001507 goto fail;
1508 case DT_INIT:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001509 si->init_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001510 DEBUG("%5d %s constructors (init func) found at %p\n",
1511 pid, si->name, si->init_func);
1512 break;
1513 case DT_FINI:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001514 si->fini_func = (void (*)(void))(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001515 DEBUG("%5d %s destructors (fini func) found at %p\n",
1516 pid, si->name, si->fini_func);
1517 break;
1518 case DT_INIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001519 si->init_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001520 DEBUG("%5d %s constructors (init_array) found at %p\n",
1521 pid, si->name, si->init_array);
1522 break;
1523 case DT_INIT_ARRAYSZ:
1524 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1525 break;
1526 case DT_FINI_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001527 si->fini_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001528 DEBUG("%5d %s destructors (fini_array) found at %p\n",
1529 pid, si->name, si->fini_array);
1530 break;
1531 case DT_FINI_ARRAYSZ:
1532 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1533 break;
1534 case DT_PREINIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001535 si->preinit_array = (unsigned *)(base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001536 DEBUG("%5d %s constructors (preinit_array) found at %p\n",
1537 pid, si->name, si->preinit_array);
1538 break;
1539 case DT_PREINIT_ARRAYSZ:
1540 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1541 break;
1542 case DT_TEXTREL:
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001543 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001544 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001545#if defined(ANDROID_MIPS_LINKER)
1546 case DT_NEEDED:
1547 case DT_STRSZ:
1548 case DT_SYMENT:
1549 case DT_RELENT:
1550 break;
1551 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001552 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001553 {
Elliott Hughesbedfe382012-08-14 14:07:59 -07001554 r_debug** dp = (r_debug**) *d;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001555 *dp = &_r_debug;
1556 }
1557 break;
1558 case DT_MIPS_RLD_VERSION:
1559 case DT_MIPS_FLAGS:
1560 case DT_MIPS_BASE_ADDRESS:
1561 case DT_MIPS_UNREFEXTNO:
1562 case DT_MIPS_RWPLT:
1563 break;
1564
1565 case DT_MIPS_PLTGOT:
1566#if 0
1567 /* not yet... */
1568 si->mips_pltgot = (unsigned *)(si->base + *d);
1569#endif
1570 break;
1571
1572 case DT_MIPS_SYMTABNO:
1573 si->mips_symtabno = *d;
1574 break;
1575
1576 case DT_MIPS_LOCAL_GOTNO:
1577 si->mips_local_gotno = *d;
1578 break;
1579
1580 case DT_MIPS_GOTSYM:
1581 si->mips_gotsym = *d;
1582 break;
1583
1584 default:
1585 DEBUG("%5d Unused DT entry: type 0x%08x arg 0x%08x\n",
1586 pid, d[-1], d[0]);
1587 break;
1588#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001589 }
1590 }
1591
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001592 DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001593 pid, si->base, si->strtab, si->symtab);
1594
1595 if((si->strtab == 0) || (si->symtab == 0)) {
Elliott Hughes46882792012-08-03 16:49:39 -07001596 DL_ERR("missing essential tables");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001597 goto fail;
1598 }
1599
Matt Fischer4fd42c12009-12-31 12:09:10 -06001600 /* if this is the main executable, then load all of the preloads now */
1601 if(si->flags & FLAG_EXE) {
1602 int i;
1603 memset(preloads, 0, sizeof(preloads));
1604 for(i = 0; ldpreload_names[i] != NULL; i++) {
1605 soinfo *lsi = find_library(ldpreload_names[i]);
1606 if(lsi == 0) {
1607 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001608 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1609 ldpreload_names[i], si->name, tmp_err_buf);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001610 goto fail;
1611 }
1612 lsi->refcount++;
1613 preloads[i] = lsi;
1614 }
1615 }
1616
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001617 /* dynamic_count is an upper bound for the number of needed libs */
1618 pneeded = needed = (soinfo**) alloca((1 + dynamic_count) * sizeof(soinfo*));
1619
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001620 for(d = si->dynamic; *d; d += 2) {
1621 if(d[0] == DT_NEEDED){
1622 DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
Dima Zavin2e855792009-05-20 18:28:09 -07001623 soinfo *lsi = find_library(si->strtab + d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001624 if(lsi == 0) {
Dima Zavin03531952009-05-29 17:30:25 -07001625 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001626 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1627 si->strtab + d[1], si->name, tmp_err_buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001628 goto fail;
1629 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001630 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001631 lsi->refcount++;
1632 }
1633 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001634 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001635
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001636 if (si->has_text_relocations) {
1637 /* Unprotect the segments, i.e. make them writable, to allow
1638 * text relocations to work properly. We will later call
1639 * phdr_table_protect_segments() after all of them are applied
1640 * and all constructors are run.
1641 */
1642 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1643 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1644 si->name, strerror(errno));
1645 goto fail;
1646 }
1647 }
1648
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001649 if(si->plt_rel) {
1650 DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001651 if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652 goto fail;
1653 }
1654 if(si->rel) {
1655 DEBUG("[ %5d relocating %s ]\n", pid, si->name );
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001656 if(soinfo_relocate(si, si->rel, si->rel_count, needed))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001657 goto fail;
1658 }
1659
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001660#ifdef ANDROID_MIPS_LINKER
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001661 if(mips_relocate_got(si, needed)) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001662 goto fail;
1663 }
1664#endif
1665
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001666 si->flags |= FLAG_LINKED;
1667 DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
1668
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001669 if (si->has_text_relocations) {
1670 /* All relocations are done, we can protect our segments back to
1671 * read-only. */
1672 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1673 DL_ERR("can't protect segments for \"%s\": %s",
1674 si->name, strerror(errno));
1675 goto fail;
1676 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001677 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001678
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001679 /* We can also turn on GNU RELRO protection */
1680 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001681 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1682 si->name, strerror(errno));
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001683 goto fail;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001684 }
1685
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001686 /* If this is a SET?ID program, dup /dev/null to opened stdin,
1687 stdout and stderr to close a security hole described in:
1688
1689 ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1690
1691 */
Elliott Hughes46882792012-08-03 16:49:39 -07001692 if (program_is_setuid) {
1693 nullify_closed_stdio();
1694 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001695 notify_gdb_of_load(si);
1696 return 0;
1697
1698fail:
Dima Zavina7161902010-08-17 15:56:40 -07001699 ERROR("failed to link %s\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001700 si->flags |= FLAG_ERROR;
1701 return -1;
1702}
1703
Elliott Hughes46882792012-08-03 16:49:39 -07001704static void parse_path(const char* path, const char* delimiters,
1705 const char** array, char* buf, size_t buf_size, size_t max_count)
David Bartleybc3a5c22009-06-02 18:27:28 -07001706{
Elliott Hughes46882792012-08-03 16:49:39 -07001707 if (path == NULL) {
1708 return;
David Bartleybc3a5c22009-06-02 18:27:28 -07001709 }
1710
Elliott Hughes46882792012-08-03 16:49:39 -07001711 size_t len = strlcpy(buf, path, buf_size);
David Bartleybc3a5c22009-06-02 18:27:28 -07001712
Elliott Hughes46882792012-08-03 16:49:39 -07001713 size_t i = 0;
1714 char* buf_p = buf;
1715 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
1716 if (*array[i] != '\0') {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001717 ++i;
1718 }
1719 }
1720
Elliott Hughes46882792012-08-03 16:49:39 -07001721 // Forget the last path if we had to truncate; this occurs if the 2nd to
1722 // last char isn't '\0' (i.e. wasn't originally a delimiter).
1723 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
1724 array[i - 1] = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001725 } else {
Elliott Hughes46882792012-08-03 16:49:39 -07001726 array[i] = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001727 }
1728}
1729
Elliott Hughes46882792012-08-03 16:49:39 -07001730static void parse_LD_LIBRARY_PATH(const char* path) {
1731 parse_path(path, ":", ldpaths,
1732 ldpaths_buf, sizeof(ldpaths_buf), LDPATH_MAX);
1733}
1734
1735static void parse_LD_PRELOAD(const char* path) {
1736 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
1737 parse_path(path, " :", ldpreload_names,
1738 ldpreloads_buf, sizeof(ldpreloads_buf), LDPRELOAD_MAX);
1739}
1740
Nick Kralevich468319c2011-11-11 15:53:17 -08001741/*
1742 * This code is called after the linker has linked itself and
1743 * fixed it's own GOT. It is safe to make references to externs
1744 * and other non-local data at this point.
1745 */
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001746static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001747{
1748 static soinfo linker_soinfo;
1749
1750 int argc = (int) *elfdata;
1751 char **argv = (char**) (elfdata + 1);
Stephen Smalleybb440552012-01-20 10:59:15 -08001752 unsigned *vecs = (unsigned*) (argv + argc + 1);
1753 unsigned *v;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001754 soinfo *si;
Kito Cheng326e85e2012-07-15 00:49:27 +08001755 int i;
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001756 const char *ldpath_env = NULL;
1757 const char *ldpreload_env = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001758
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001759 /* NOTE: we store the elfdata pointer on a special location
1760 * of the temporary TLS area in order to pass it to
1761 * the C Library's runtime initializer.
1762 *
1763 * The initializer must clear the slot and reset the TLS
1764 * to point to a different location to ensure that no other
1765 * shared library constructor can access it.
1766 */
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001767 __libc_init_tls(elfdata);
1768
1769 pid = getpid();
1770
1771#if TIMING
1772 struct timeval t0, t1;
1773 gettimeofday(&t0, 0);
1774#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001775
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001776 /* Initialize environment functions, and get to the ELF aux vectors table */
1777 vecs = linker_env_init(vecs);
1778
Stephen Smalley861b42a2012-01-13 07:48:11 -05001779 /* Check auxv for AT_SECURE first to see if program is setuid, setgid,
1780 has file caps, or caused a SELinux/AppArmor domain transition. */
1781 for (v = vecs; v[0]; v += 2) {
1782 if (v[0] == AT_SECURE) {
1783 /* kernel told us whether to enable secure mode */
1784 program_is_setuid = v[1];
1785 goto sanitize;
1786 }
1787 }
1788
1789 /* Kernel did not provide AT_SECURE - fall back on legacy test. */
1790 program_is_setuid = (getuid() != geteuid()) || (getgid() != getegid());
1791
1792sanitize:
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001793 /* Sanitize environment if we're loading a setuid program */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001794 if (program_is_setuid) {
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001795 linker_env_secure();
Elliott Hughesbedfe382012-08-14 14:07:59 -07001796 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001797
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001798 debugger_init();
1799
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001800 /* Get a few environment variables */
1801 {
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07001802#if LINKER_DEBUG
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001803 const char* env;
1804 env = linker_env_get("DEBUG"); /* XXX: TODO: Change to LD_DEBUG */
1805 if (env)
1806 debug_verbosity = atoi(env);
Nick Kralevich8c4f3ce2012-04-04 12:43:32 -07001807#endif
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001808
1809 /* Normally, these are cleaned by linker_env_secure, but the test
1810 * against program_is_setuid doesn't cost us anything */
1811 if (!program_is_setuid) {
1812 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1813 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001814 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001815 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001816
1817 INFO("[ android linker & debugger ]\n");
1818 DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
1819
David 'Digit' Turner16084162012-06-12 16:25:37 +02001820 si = soinfo_alloc(argv[0]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001821 if(si == 0) {
1822 exit(-1);
1823 }
1824
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001825 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001826 si->flags |= FLAG_EXE;
Elliott Hughesbedfe382012-08-14 14:07:59 -07001827 link_map* map = &(si->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001828
1829 map->l_addr = 0;
1830 map->l_name = argv[0];
1831 map->l_prev = NULL;
1832 map->l_next = NULL;
1833
1834 _r_debug.r_map = map;
1835 r_debug_tail = map;
1836
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001837 /* gdb expects the linker to be in the debug shared object list.
1838 * Without this, gdb has trouble locating the linker's ".text"
1839 * and ".plt" sections. Gdb could also potentially use this to
1840 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1841 * Don't use soinfo_alloc(), because the linker shouldn't
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001842 * be on the soinfo list.
1843 */
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001844 strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001845 linker_soinfo.flags = 0;
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001846 linker_soinfo.base = linker_base;
Ben Cheng06f0e742012-08-10 16:07:02 -07001847 /*
1848 * Set the dynamic field in the link map otherwise gdb will complain with
1849 * the following:
1850 * warning: .dynamic section for "/system/bin/linker" is not at the
1851 * expected address (wrong library or version mismatch?)
1852 */
1853 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
1854 Elf32_Phdr *phdr =
1855 (Elf32_Phdr *)((unsigned char *) linker_base + elf_hdr->e_phoff);
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001856 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1857 &linker_soinfo.dynamic, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001858 insert_soinfo_into_debug_map(&linker_soinfo);
1859
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001860 /* extract information passed from the kernel */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001861 while(vecs[0] != 0){
1862 switch(vecs[0]){
1863 case AT_PHDR:
1864 si->phdr = (Elf32_Phdr*) vecs[1];
1865 break;
1866 case AT_PHNUM:
1867 si->phnum = (int) vecs[1];
1868 break;
1869 case AT_ENTRY:
1870 si->entry = vecs[1];
1871 break;
1872 }
1873 vecs += 2;
1874 }
1875
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001876 /* Compute the value of si->base. We can't rely on the fact that
1877 * the first entry is the PHDR because this will not be true
1878 * for certain executables (e.g. some in the NDK unit test suite)
1879 */
1880 int nn;
1881 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001882 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001883 si->load_bias = 0;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001884 for ( nn = 0; nn < si->phnum; nn++ ) {
1885 if (si->phdr[nn].p_type == PT_PHDR) {
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001886 si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
1887 si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001888 break;
1889 }
1890 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001891 si->dynamic = (unsigned *)-1;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001892 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001893
Elliott Hughes46882792012-08-03 16:49:39 -07001894 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1895 parse_LD_LIBRARY_PATH(ldpath_env);
1896 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001897
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001898 if(soinfo_link_image(si)) {
Dima Zavin2e855792009-05-20 18:28:09 -07001899 char errmsg[] = "CANNOT LINK EXECUTABLE\n";
1900 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
1901 write(2, errmsg, sizeof(errmsg));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001902 exit(-1);
1903 }
1904
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001905 soinfo_call_preinit_constructors(si);
1906
Kito Cheng326e85e2012-07-15 00:49:27 +08001907 for(i = 0; preloads[i] != NULL; i++) {
1908 soinfo_call_constructors(preloads[i]);
1909 }
1910
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08001911 /*After the link_image, the si->base is initialized.
1912 *For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1913 *We need to update this value for so exe here. So Unwind_Backtrace
1914 *for some arch like x86 could work correctly within so exe.
1915 */
1916 map->l_addr = si->base;
David 'Digit' Turner16084162012-06-12 16:25:37 +02001917 soinfo_call_constructors(si);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001918
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -07001919#if ALLOW_SYMBOLS_FROM_MAIN
1920 /* Set somain after we've loaded all the libraries in order to prevent
1921 * linking of symbols back to the main image, which is not set up at that
1922 * point yet.
1923 */
1924 somain = si;
1925#endif
1926
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001927#if TIMING
1928 gettimeofday(&t1,NULL);
1929 PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
1930 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1931 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1932 ));
1933#endif
1934#if STATS
1935 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07001936 linker_stats.count[kRelocAbsolute],
1937 linker_stats.count[kRelocRelative],
1938 linker_stats.count[kRelocCopy],
1939 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001940#endif
1941#if COUNT_PAGES
1942 {
1943 unsigned n;
1944 unsigned i;
1945 unsigned count = 0;
1946 for(n = 0; n < 4096; n++){
1947 if(bitmask[n]){
1948 unsigned x = bitmask[n];
1949 for(i = 0; i < 8; i++){
1950 if(x & 1) count++;
1951 x >>= 1;
1952 }
1953 }
1954 }
1955 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
1956 }
1957#endif
1958
1959#if TIMING || STATS || COUNT_PAGES
1960 fflush(stdout);
1961#endif
1962
1963 TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
1964 si->entry);
1965 return si->entry;
1966}
Nick Kralevich468319c2011-11-11 15:53:17 -08001967
1968/*
1969 * Find the value of AT_BASE passed to us by the kernel. This is the load
1970 * location of the linker.
1971 */
1972static unsigned find_linker_base(unsigned **elfdata) {
1973 int argc = (int) *elfdata;
1974 char **argv = (char**) (elfdata + 1);
1975 unsigned *vecs = (unsigned*) (argv + argc + 1);
1976 while (vecs[0] != 0) {
1977 vecs++;
1978 }
1979
1980 /* The end of the environment block is marked by two NULL pointers */
1981 vecs++;
1982
1983 while(vecs[0]) {
1984 if (vecs[0] == AT_BASE) {
1985 return vecs[1];
1986 }
1987 vecs += 2;
1988 }
1989
1990 return 0; // should never happen
1991}
1992
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001993/* Compute the load-bias of an existing executable. This shall only
1994 * be used to compute the load bias of an executable or shared library
1995 * that was loaded by the kernel itself.
1996 *
1997 * Input:
1998 * elf -> address of ELF header, assumed to be at the start of the file.
1999 * Return:
2000 * load bias, i.e. add the value of any p_vaddr in the file to get
2001 * the corresponding address in memory.
2002 */
2003static Elf32_Addr
2004get_elf_exec_load_bias(const Elf32_Ehdr* elf)
2005{
2006 Elf32_Addr offset = elf->e_phoff;
2007 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
2008 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
2009 const Elf32_Phdr* phdr;
2010
2011 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
2012 if (phdr->p_type == PT_LOAD) {
2013 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
2014 }
2015 }
2016 return 0;
2017}
2018
Nick Kralevich468319c2011-11-11 15:53:17 -08002019/*
2020 * This is the entry point for the linker, called from begin.S. This
2021 * method is responsible for fixing the linker's own relocations, and
2022 * then calling __linker_init_post_relocation().
2023 *
2024 * Because this method is called before the linker has fixed it's own
2025 * relocations, any attempt to reference an extern variable, extern
2026 * function, or other GOT reference will generate a segfault.
2027 */
Elliott Hughes46882792012-08-03 16:49:39 -07002028extern "C" unsigned __linker_init(unsigned **elfdata) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002029 unsigned linker_addr = find_linker_base(elfdata);
2030 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2031 Elf32_Phdr *phdr =
2032 (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2033
2034 soinfo linker_so;
2035 memset(&linker_so, 0, sizeof(soinfo));
2036
2037 linker_so.base = linker_addr;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002038 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002039 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Nick Kralevich468319c2011-11-11 15:53:17 -08002040 linker_so.dynamic = (unsigned *) -1;
2041 linker_so.phdr = phdr;
2042 linker_so.phnum = elf_hdr->e_phnum;
2043 linker_so.flags |= FLAG_LINKER;
Nick Kralevich468319c2011-11-11 15:53:17 -08002044
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002045 if (soinfo_link_image(&linker_so)) {
Nick Kralevich468319c2011-11-11 15:53:17 -08002046 // It would be nice to print an error message, but if the linker
2047 // can't link itself, there's no guarantee that we'll be able to
2048 // call write() (because it involves a GOT reference).
2049 //
2050 // This situation should never occur unless the linker itself
2051 // is corrupt.
2052 exit(-1);
2053 }
2054
2055 // We have successfully fixed our own relocations. It's safe to run
2056 // the main part of the linker now.
Elliott Hughes5419b942012-10-16 15:54:46 -07002057 unsigned start_address = __linker_init_post_relocation(elfdata, linker_addr);
2058
2059 // Return the address that the calling assembly stub should jump to.
2060 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002061}