blob: cbdca8e9de92a2efbdf4dd273a91f322fc84f068 [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>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037#include <sys/atomics.h>
Elliott Hughes46882792012-08-03 16:49:39 -070038#include <sys/mman.h>
39#include <sys/stat.h>
40#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Elliott Hughes46882792012-08-03 16:49:39 -070042// Private C library headers.
43#include <private/bionic_tls.h>
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080044#include <private/KernelArgumentBlock.h>
Elliott Hughes46882792012-08-03 16:49:39 -070045#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' Turner23363ed2012-06-18 18:13:49 +020051#include "linker_phdr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
David Bartleybc3a5c22009-06-02 18:27:28 -070053/* Assume average path length of 64 and max 8 paths */
54#define LDPATH_BUFSIZE 512
55#define LDPATH_MAX 8
56
Matt Fischer4fd42c12009-12-31 12:09:10 -060057#define LDPRELOAD_BUFSIZE 512
58#define LDPRELOAD_MAX 8
59
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
61 *
62 * Do NOT use malloc() and friends or pthread_*() code here.
63 * Don't use printf() either; it's caused mysterious memory
64 * corruption in the past.
65 * The linker runs before we bring up libc and it's easiest
66 * to make sure it does not depend on any complex libc features
67 *
68 * open issues / todo:
69 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070 * - are we doing everything we should for ARM_COPY relocations?
71 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072 * - after linking, set as much stuff as possible to READONLY
73 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070074 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075
Elliott Hughes124fae92012-10-31 14:20:03 -070076static bool soinfo_link_image(soinfo* si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077
Magnus Malmbornba98d922012-09-12 13:00:55 +020078// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
79// maps, each a single page in size. The pages are broken up into as many struct soinfo
80// objects as will fit, and they're all threaded together on a free list.
81#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
82struct soinfo_pool_t {
83 soinfo_pool_t* next;
84 soinfo info[SOINFO_PER_POOL];
85};
86static struct soinfo_pool_t* gSoInfoPools = NULL;
87static soinfo* gSoInfoFreeList = NULL;
88
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080089static soinfo* solist = &libdl_info;
90static soinfo* sonext = &libdl_info;
91static soinfo* somain; /* main process, always the one after libdl_info */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092
Elliott Hughes124fae92012-10-31 14:20:03 -070093static const char* const gSoPaths[] = {
94 "/vendor/lib",
95 "/system/lib",
96 NULL
97};
David Bartleybc3a5c22009-06-02 18:27:28 -070098
Elliott Hughes124fae92012-10-31 14:20:03 -070099static char gLdPathsBuffer[LDPATH_BUFSIZE];
100static const char* gLdPaths[LDPATH_MAX + 1];
101
102static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
103static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600104
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800105static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600106
Elliott Hughes650be4e2013-03-05 18:47:58 -0800107__LIBC_HIDDEN__ int gLdDebugVerbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108
Elliott Hughesbedfe382012-08-14 14:07:59 -0700109enum RelocationKind {
110 kRelocAbsolute = 0,
111 kRelocRelative,
112 kRelocCopy,
113 kRelocSymbol,
114 kRelocMax
115};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100116
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700118struct linker_stats_t {
119 int count[kRelocMax];
120};
121
122static linker_stats_t linker_stats;
123
124static void count_relocation(RelocationKind kind) {
125 ++linker_stats.count[kind];
126}
127#else
128static void count_relocation(RelocationKind) {
129}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130#endif
131
132#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700133static unsigned bitmask[4096];
134#define MARK(offset) \
135 do { \
136 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
137 } while(0)
138#else
139#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800140#endif
141
Elliott Hughes46882792012-08-03 16:49:39 -0700142// You shouldn't try to call memory-allocating functions in the dynamic linker.
143// Guard against the most obvious ones.
144#define DISALLOW_ALLOCATION(return_type, name, ...) \
145 return_type name __VA_ARGS__ \
146 { \
147 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
148 __libc_android_log_write(ANDROID_LOG_FATAL, "linker", msg); \
Chris Dearman20a24402012-10-31 05:39:27 -0700149 write(2, msg, strlen(msg)); \
Elliott Hughes46882792012-08-03 16:49:39 -0700150 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700151 }
Elliott Hughes46882792012-08-03 16:49:39 -0700152#define UNUSED __attribute__((unused))
153DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
154DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
155DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
156DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
Dima Zavin2e855792009-05-20 18:28:09 -0700157
Dima Zavin03531952009-05-29 17:30:25 -0700158static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700159static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700160
Elliott Hughes650be4e2013-03-05 18:47:58 -0800161char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700162 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700163}
164
Elliott Hughes650be4e2013-03-05 18:47:58 -0800165size_t linker_get_error_buffer_size() {
166 return sizeof(__linker_dl_err_buf);
167}
168
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169/*
170 * This function is an empty stub where GDB locates a breakpoint to get notified
171 * about linker activity.
172 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700173extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174
Elliott Hughesbedfe382012-08-14 14:07:59 -0700175static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176 RT_CONSISTENT, 0};
Elliott Hughesbedfe382012-08-14 14:07:59 -0700177static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178
Elliott Hughes3b297c42012-10-11 16:08:51 -0700179static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180
Elliott Hughesbedfe382012-08-14 14:07:59 -0700181static void insert_soinfo_into_debug_map(soinfo * info) {
182 // Copy the necessary fields into the debug structure.
183 link_map* map = &(info->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184 map->l_addr = info->base;
185 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800186 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800187
188 /* Stick the new library at the end of the list.
189 * gdb tends to care more about libc than it does
190 * about leaf libraries, and ordering it this way
191 * reduces the back-and-forth over the wire.
192 */
193 if (r_debug_tail) {
194 r_debug_tail->l_next = map;
195 map->l_prev = r_debug_tail;
196 map->l_next = 0;
197 } else {
198 _r_debug.r_map = map;
199 map->l_prev = 0;
200 map->l_next = 0;
201 }
202 r_debug_tail = map;
203}
204
Elliott Hughesbedfe382012-08-14 14:07:59 -0700205static void remove_soinfo_from_debug_map(soinfo* info) {
206 link_map* map = &(info->linkmap);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700207
Elliott Hughesbedfe382012-08-14 14:07:59 -0700208 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700209 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700210 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700211
Elliott Hughesbedfe382012-08-14 14:07:59 -0700212 if (map->l_prev) {
213 map->l_prev->l_next = map->l_next;
214 }
215 if (map->l_next) {
216 map->l_next->l_prev = map->l_prev;
217 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700218}
219
Elliott Hughesbedfe382012-08-14 14:07:59 -0700220static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221 if (info->flags & FLAG_EXE) {
222 // GDB already knows about the main executable
223 return;
224 }
225
Elliott Hughes3b297c42012-10-11 16:08:51 -0700226 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227
228 _r_debug.r_state = RT_ADD;
229 rtld_db_dlactivity();
230
231 insert_soinfo_into_debug_map(info);
232
233 _r_debug.r_state = RT_CONSISTENT;
234 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700235}
236
Elliott Hughesbedfe382012-08-14 14:07:59 -0700237static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700238 if (info->flags & FLAG_EXE) {
239 // GDB already knows about the main executable
240 return;
241 }
242
Elliott Hughes3b297c42012-10-11 16:08:51 -0700243 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700244
245 _r_debug.r_state = RT_DELETE;
246 rtld_db_dlactivity();
247
248 remove_soinfo_from_debug_map(info);
249
250 _r_debug.r_state = RT_CONSISTENT;
251 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800252}
253
Elliott Hughes18a206c2012-10-29 17:37:13 -0700254void notify_gdb_of_libraries() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255 _r_debug.r_state = RT_ADD;
256 rtld_db_dlactivity();
257 _r_debug.r_state = RT_CONSISTENT;
258 rtld_db_dlactivity();
259}
260
Magnus Malmbornba98d922012-09-12 13:00:55 +0200261static bool ensure_free_list_non_empty() {
262 if (gSoInfoFreeList != NULL) {
263 return true;
264 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800265
Magnus Malmbornba98d922012-09-12 13:00:55 +0200266 // Allocate a new pool.
267 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
268 PROT_READ|PROT_WRITE,
269 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
270 if (pool == MAP_FAILED) {
271 return false;
272 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273
Magnus Malmbornba98d922012-09-12 13:00:55 +0200274 // Add the pool to our list of pools.
275 pool->next = gSoInfoPools;
276 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277
Magnus Malmbornba98d922012-09-12 13:00:55 +0200278 // Chain the entries in the new pool onto the free list.
279 gSoInfoFreeList = &pool->info[0];
280 soinfo* next = NULL;
281 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
282 pool->info[i].next = next;
283 next = &pool->info[i];
284 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285
Magnus Malmbornba98d922012-09-12 13:00:55 +0200286 return true;
287}
288
Elliott Hughesd23736e2012-11-01 15:16:56 -0700289static void set_soinfo_pool_protection(int protection) {
290 for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
291 if (mprotect(p, sizeof(*p), protection) == -1) {
292 abort(); // Can't happen.
293 }
294 }
295}
296
Magnus Malmbornba98d922012-09-12 13:00:55 +0200297static soinfo* soinfo_alloc(const char* name) {
298 if (strlen(name) >= SOINFO_NAME_LEN) {
299 DL_ERR("library name \"%s\" too long", name);
300 return NULL;
301 }
302
303 if (!ensure_free_list_non_empty()) {
304 DL_ERR("out of memory when loading \"%s\"", name);
305 return NULL;
306 }
307
308 // Take the head element off the free list.
309 soinfo* si = gSoInfoFreeList;
310 gSoInfoFreeList = gSoInfoFreeList->next;
311
312 // Initialize the new element.
313 memset(si, 0, sizeof(soinfo));
314 strlcpy(si->name, name, sizeof(si->name));
315 sonext->next = si;
316 sonext = si;
317
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700318 TRACE("name %s: allocated soinfo @ %p\n", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200319 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800320}
321
Elliott Hughes46882792012-08-03 16:49:39 -0700322static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800323{
Elliott Hughes46882792012-08-03 16:49:39 -0700324 if (si == NULL) {
325 return;
326 }
327
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800328 soinfo *prev = NULL, *trav;
329
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700330 TRACE("name %s: freeing soinfo @ %p\n", si->name, si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800331
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800332 for (trav = solist; trav != NULL; trav = trav->next) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333 if (trav == si)
334 break;
335 prev = trav;
336 }
337 if (trav == NULL) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800338 /* si was not in solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700339 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800340 return;
341 }
342
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100343 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800344 always the static libdl_info.
345 */
346 prev->next = si->next;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800347 if (si == sonext) {
348 sonext = prev;
349 }
Magnus Malmbornba98d922012-09-12 13:00:55 +0200350 si->next = gSoInfoFreeList;
351 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800352}
353
Elliott Hughescade4c32012-12-20 14:42:14 -0800354
355static void parse_path(const char* path, const char* delimiters,
356 const char** array, char* buf, size_t buf_size, size_t max_count) {
357 if (path == NULL) {
358 return;
359 }
360
361 size_t len = strlcpy(buf, path, buf_size);
362
363 size_t i = 0;
364 char* buf_p = buf;
365 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
366 if (*array[i] != '\0') {
367 ++i;
368 }
369 }
370
371 // Forget the last path if we had to truncate; this occurs if the 2nd to
372 // last char isn't '\0' (i.e. wasn't originally a delimiter).
373 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
374 array[i - 1] = NULL;
375 } else {
376 array[i] = NULL;
377 }
378}
379
380static void parse_LD_LIBRARY_PATH(const char* path) {
381 parse_path(path, ":", gLdPaths,
382 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
383}
384
385static void parse_LD_PRELOAD(const char* path) {
386 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
387 parse_path(path, " :", gLdPreloadNames,
388 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
389}
390
Elliott Hughes46882792012-08-03 16:49:39 -0700391#ifdef ANDROID_ARM_LINKER
392
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393/* For a given PC, find the .so that it belongs to.
394 * Returns the base address of the .ARM.exidx section
395 * for that .so, and the number of 8-byte entries
396 * in that section (via *pcount).
397 *
398 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
399 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700400 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800401 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
403{
404 soinfo *si;
405 unsigned addr = (unsigned)pc;
406
Nick Kralevich468319c2011-11-11 15:53:17 -0800407 for (si = solist; si != 0; si = si->next){
408 if ((addr >= si->base) && (addr < (si->base + si->size))) {
409 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900410 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411 }
412 }
413 *pcount = 0;
414 return NULL;
415}
Elliott Hughes46882792012-08-03 16:49:39 -0700416
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700417#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
Elliott Hughes46882792012-08-03 16:49:39 -0700418
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800419/* Here, we only have to provide a callback to iterate across all the
420 * loaded libraries. gcc_eh does the rest. */
421int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700422dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800423 void *data)
424{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800425 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700426 for (soinfo* si = solist; si != NULL; si = si->next) {
427 dl_phdr_info dl_info;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428 dl_info.dlpi_addr = si->linkmap.l_addr;
429 dl_info.dlpi_name = si->linkmap.l_name;
430 dl_info.dlpi_phdr = si->phdr;
431 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700432 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
433 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800434 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700435 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800436 }
437 return rv;
438}
Elliott Hughes46882792012-08-03 16:49:39 -0700439
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800440#endif
441
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800442static Elf32_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
443 Elf32_Sym* s;
444 Elf32_Sym* symtab = si->symtab;
445 const char* strtab = si->strtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800446 unsigned n;
447
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700448 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800449 name, si->name, si->base, hash, hash % si->nbucket);
450 n = hash % si->nbucket;
451
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800452 for (n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800453 s = symtab + n;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800454 if (strcmp(strtab + s->st_name, name)) continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800455
Doug Kwane8238072009-10-26 12:05:23 -0700456 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800457 switch(ELF32_ST_BIND(s->st_info)){
458 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700459 case STB_WEAK:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800460 if (s->st_shndx == SHN_UNDEF) {
Robin Burchell439fa8e2012-07-05 09:21:07 +0200461 continue;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800462 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800463
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700464 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800465 name, si->name, s->st_value, s->st_size);
466 return s;
467 }
468 }
469
Doug Kwan94304352009-10-23 18:11:40 -0700470 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800471}
472
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800473static unsigned elfhash(const char* _name) {
474 const unsigned char* name = (const unsigned char*) _name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800475 unsigned h = 0, g;
476
477 while(*name) {
478 h = (h << 4) + *name++;
479 g = h & 0xf0000000;
480 h ^= g;
481 h ^= g >> 24;
482 }
483 return h;
484}
485
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800486static Elf32_Sym* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
Doug Kwan94304352009-10-23 18:11:40 -0700487 unsigned elf_hash = elfhash(name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800488 Elf32_Sym* s = NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700489
Pavel Chupinc77c4342012-10-31 13:55:51 +0400490 if (si != NULL && somain != NULL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200491
492 /*
Pavel Chupinc77c4342012-10-31 13:55:51 +0400493 * Local scope is executable scope. Just start looking into it right away
494 * for the shortcut.
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200495 */
496
Pavel Chupinc77c4342012-10-31 13:55:51 +0400497 if (si == somain) {
498 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200499 if (s != NULL) {
Pavel Chupinc77c4342012-10-31 13:55:51 +0400500 *lsi = si;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200501 goto done;
502 }
Pavel Chupinc77c4342012-10-31 13:55:51 +0400503 } else {
504 /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200505
Pavel Chupinc77c4342012-10-31 13:55:51 +0400506 /*
507 * If this object was built with symbolic relocations disabled, the
508 * first place to look to resolve external references is the main
509 * executable.
510 */
Nick Kralevich468319c2011-11-11 15:53:17 -0800511
Pavel Chupinc77c4342012-10-31 13:55:51 +0400512 if (!si->has_DT_SYMBOLIC) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700513 DEBUG("%s: looking up %s in executable %s\n",
514 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400515 s = soinfo_elf_lookup(somain, elf_hash, name);
516 if (s != NULL) {
517 *lsi = somain;
518 goto done;
519 }
520 }
521
522 /* Look for symbols in the local scope (the object who is
523 * searching). This happens with C++ templates on i386 for some
524 * reason.
525 *
526 * Notes on weak symbols:
527 * The ELF specs are ambiguous about treatment of weak definitions in
528 * dynamic linking. Some systems return the first definition found
529 * and some the first non-weak definition. This is system dependent.
530 * Here we return the first definition found for simplicity. */
531
532 s = soinfo_elf_lookup(si, elf_hash, name);
533 if (s != NULL) {
534 *lsi = si;
535 goto done;
536 }
537
538 /*
539 * If this object was built with -Bsymbolic and symbol is not found
540 * in the local scope, try to find the symbol in the main executable.
541 */
542
543 if (si->has_DT_SYMBOLIC) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700544 DEBUG("%s: looking up %s in executable %s after local scope\n",
545 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400546 s = soinfo_elf_lookup(somain, elf_hash, name);
547 if (s != NULL) {
548 *lsi = somain;
549 goto done;
550 }
551 }
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200552 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700553 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700554
Matt Fischer4fd42c12009-12-31 12:09:10 -0600555 /* Next, look for it in the preloads list */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800556 for (int i = 0; gLdPreloads[i] != NULL; i++) {
557 s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
558 if (s != NULL) {
559 *lsi = gLdPreloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600560 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200561 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600562 }
563
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800564 for (int i = 0; needed[i] != NULL; i++) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700565 DEBUG("%s: looking up %s in %s\n",
566 si->name, name, needed[i]->name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200567 s = soinfo_elf_lookup(needed[i], elf_hash, name);
568 if (s != NULL) {
569 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200570 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200571 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700572 }
573
574done:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800575 if (s != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700576 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = 0x%08x, "
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200577 "found in %s, base = 0x%08x, load bias = 0x%08x\n",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700578 si->name, name, s->st_value,
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200579 (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700580 return s;
581 }
582
Doug Kwan94304352009-10-23 18:11:40 -0700583 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700584}
585
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800586/* This is used by dlsym(3). It performs symbol lookup only within the
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700587 specified soinfo object and not in any of its dependencies.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800588
589 TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
590 that it should do a breadth first search through the dependency
591 tree. This agrees with the ELF spec (aka System V Application
592 Binary Interface) where in Chapter 5 it discuss resolving "Shared
593 Object Dependencies" in breadth first search order.
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700594 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800595Elf32_Sym* dlsym_handle_lookup(soinfo* si, const char* name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800596{
David 'Digit' Turner16084162012-06-12 16:25:37 +0200597 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598}
599
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800600/* This is used by dlsym(3) to performs a global symbol lookup. If the
601 start value is null (for RTLD_DEFAULT), the search starts at the
602 beginning of the global solist. Otherwise the search starts at the
603 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700604 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800605Elf32_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800606 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800607
Elliott Hughescade4c32012-12-20 14:42:14 -0800608 if (start == NULL) {
609 start = solist;
610 }
611
612 Elf32_Sym* s = NULL;
613 for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
614 s = soinfo_elf_lookup(si, elf_hash, name);
615 if (s != NULL) {
616 *found = si;
617 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600618 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800619 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600620
Elliott Hughescade4c32012-12-20 14:42:14 -0800621 if (s != NULL) {
622 TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, found->base = 0x%08x\n",
623 name, s->st_value, (*found)->base);
624 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800625
Elliott Hughescade4c32012-12-20 14:42:14 -0800626 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800627}
628
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800629soinfo* find_containing_library(const void* addr) {
630 for (soinfo* si = solist; si != NULL; si = si->next) {
631 if ((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600632 return si;
633 }
634 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600635 return NULL;
636}
637
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800638Elf32_Sym* dladdr_find_symbol(soinfo* si, const void* addr) {
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600639 unsigned int i;
640 unsigned soaddr = (unsigned)addr - si->base;
641
642 /* Search the library's symbol table for any defined symbol which
643 * contains this address */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800644 for (i=0; i<si->nchain; i++) {
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600645 Elf32_Sym *sym = &si->symtab[i];
646
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800647 if (sym->st_shndx != SHN_UNDEF &&
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600648 soaddr >= sym->st_value &&
649 soaddr < sym->st_value + sym->st_size) {
650 return sym;
651 }
652 }
653
654 return NULL;
655}
656
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800658static void dump(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800659{
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800660 Elf32_Sym* s = si->symtab;
661 for (unsigned n = 0; n < si->nchain; n++) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700662 TRACE("%04d> %08x: %02x %04x %08x %08x %s\n", n, s,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800663 s->st_info, s->st_shndx, s->st_value, s->st_size,
664 si->strtab + s->st_name);
665 s++;
666 }
667}
668#endif
669
Elliott Hughes124fae92012-10-31 14:20:03 -0700670static int open_library_on_path(const char* name, const char* const paths[]) {
671 char buf[512];
672 for (size_t i = 0; paths[i] != NULL; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800673 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700674 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughes9c94fc92012-11-05 09:11:43 -0800675 PRINT("Warning: ignoring very long library path: %s/%s\n", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700676 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800677 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700678 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
679 if (fd != -1) {
680 return fd;
681 }
682 }
683 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800684}
685
Elliott Hughes124fae92012-10-31 14:20:03 -0700686static int open_library(const char* name) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700687 TRACE("[ opening %s ]\n", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800688
Elliott Hughes124fae92012-10-31 14:20:03 -0700689 // If the name contains a slash, we should attempt to open it directly and not search the paths.
690 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700691 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
692 if (fd != -1) {
693 return fd;
694 }
695 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700696 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800697
Elliott Hughes124fae92012-10-31 14:20:03 -0700698 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
699 int fd = open_library_on_path(name, gLdPaths);
700 if (fd == -1) {
701 fd = open_library_on_path(name, gSoPaths);
702 }
703 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800704}
705
Elliott Hughes124fae92012-10-31 14:20:03 -0700706static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700707 // Open the file.
Elliott Hughes650be4e2013-03-05 18:47:58 -0800708 int fd = open_library(name);
709 if (fd == -1) {
Elliott Hughes46882792012-08-03 16:49:39 -0700710 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800711 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700712 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800713
Elliott Hughes650be4e2013-03-05 18:47:58 -0800714 // Read the ELF header and load the segments.
715 ElfReader elf_reader(name, fd);
716 if (!elf_reader.Load()) {
Elliott Hughes46882792012-08-03 16:49:39 -0700717 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800718 }
719
Elliott Hughes650be4e2013-03-05 18:47:58 -0800720 const char* bname = strrchr(name, '/');
721 soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
722 if (si == NULL) {
Elliott Hughes46882792012-08-03 16:49:39 -0700723 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200724 }
Elliott Hughes650be4e2013-03-05 18:47:58 -0800725 si->base = elf_reader.load_start();
726 si->size = elf_reader.load_size();
727 si->load_bias = elf_reader.load_bias();
728 si->flags = 0;
729 si->entry = 0;
730 si->dynamic = NULL;
731 si->phnum = elf_reader.phdr_count();
732 si->phdr = elf_reader.loaded_phdr();
733 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800734}
735
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200736static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800737{
738 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700739 const char *bname;
740
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200741 // TODO: don't use basename only for determining libraries
742 // http://code.google.com/p/android/issues/detail?id=6670
743
744 bname = strrchr(name, '/');
745 bname = bname ? bname + 1 : name;
746
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800747 for (si = solist; si != NULL; si = si->next) {
748 if (!strcmp(bname, si->name)) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200749 return si;
750 }
751 }
752 return NULL;
753}
754
Elliott Hughesd23736e2012-11-01 15:16:56 -0700755static soinfo* find_library_internal(const char* name) {
756 if (name == NULL) {
757 return somain;
758 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200759
Elliott Hughesd23736e2012-11-01 15:16:56 -0700760 soinfo* si = find_loaded_library(name);
761 if (si != NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700762 if (si->flags & FLAG_LINKED) {
763 return si;
764 }
765 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
766 return NULL;
767 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800768
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700769 TRACE("[ '%s' has not been loaded yet. Locating...]\n", name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700770 si = load_library(name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800771 if (si == NULL) {
772 return NULL;
773 }
774
775 // At this point we know that whatever is loaded @ base is a valid ELF
776 // shared library whose segments are properly mapped in.
777 TRACE("[ init_library base=0x%08x sz=0x%08x name='%s') ]\n",
778 si->base, si->size, si->name);
779
780 if (!soinfo_link_image(si)) {
781 munmap(reinterpret_cast<void*>(si->base), si->size);
782 soinfo_free(si);
783 return NULL;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700784 }
785
786 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800787}
788
Elliott Hughesd23736e2012-11-01 15:16:56 -0700789static soinfo* find_library(const char* name) {
790 soinfo* si = find_library_internal(name);
791 if (si != NULL) {
792 si->refcount++;
793 }
794 return si;
795}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700796
Elliott Hughesd23736e2012-11-01 15:16:56 -0700797static int soinfo_unload(soinfo* si) {
798 if (si->refcount == 1) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700799 TRACE("unloading '%s'\n", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700800 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800801
Brian Carlstrom2d4b9b72013-03-06 15:32:16 -0800802 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800803 if (d->d_tag == DT_NEEDED) {
804 const char* library_name = si->strtab + d->d_un.d_val;
805 soinfo* lsi = find_loaded_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700806 if (lsi != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700807 TRACE("%s needs to unload %s\n", si->name, lsi->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700808 soinfo_unload(lsi);
809 } else {
810 // TODO: should we return -1 in this case?
811 DL_ERR("\"%s\": could not unload dependent library", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800812 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700813 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800814 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700815
816 munmap(reinterpret_cast<void*>(si->base), si->size);
817 notify_gdb_of_unload(si);
818 soinfo_free(si);
819 si->refcount = 0;
820 } else {
821 si->refcount--;
Elliott Hughes9c94fc92012-11-05 09:11:43 -0800822 TRACE("not unloading '%s', decrementing refcount to %d\n", si->name, si->refcount);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700823 }
824 return 0;
825}
826
Elliott Hughescade4c32012-12-20 14:42:14 -0800827void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
828 if (!get_AT_SECURE()) {
829 parse_LD_LIBRARY_PATH(ld_library_path);
830 }
831}
832
Elliott Hughese66190d2012-12-18 15:57:55 -0800833soinfo* do_dlopen(const char* name, int flags) {
834 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
835 DL_ERR("invalid flags to dlopen: %x", flags);
836 return NULL;
837 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700838 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
839 soinfo* si = find_library(name);
840 if (si != NULL) {
841 si->CallConstructors();
842 }
843 set_soinfo_pool_protection(PROT_READ);
844 return si;
845}
846
847int do_dlclose(soinfo* si) {
848 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
849 int result = soinfo_unload(si);
850 set_soinfo_pool_protection(PROT_READ);
851 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800852}
853
854/* TODO: don't use unsigned for addrs below. It works, but is not
855 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
856 * long.
857 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800858static int soinfo_relocate(soinfo* si, Elf32_Rel* rel, unsigned count,
859 soinfo* needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800860{
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800861 Elf32_Sym* symtab = si->symtab;
862 const char* strtab = si->strtab;
863 Elf32_Sym* s;
864 Elf32_Rel* start = rel;
865 soinfo* lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800866
Elliott Hughes46882792012-08-03 16:49:39 -0700867 for (size_t idx = 0; idx < count; ++idx, ++rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800868 unsigned type = ELF32_R_TYPE(rel->r_info);
869 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200870 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800871 unsigned sym_addr = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800872 char* sym_name = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800873
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700874 DEBUG("Processing '%s' relocation at index %d\n", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700875 if (type == 0) { // R_*_NONE
876 continue;
877 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800878 if (sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -0700879 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200880 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800881 if (s == NULL) {
Doug Kwane8238072009-10-26 12:05:23 -0700882 /* We only allow an undefined symbol if this is a weak
883 reference.. */
884 s = &symtab[sym];
885 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700886 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -0700887 return -1;
888 }
889
890 /* IHI0044C AAELF 4.5.1.1:
891
892 Libraries are not searched to resolve weak references.
893 It is not an error for a weak reference to remain
894 unsatisfied.
895
896 During linking, the value of an undefined weak reference is:
897 - Zero if the relocation type is absolute
898 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -0700899 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -0700900 type is base-relative.
901 */
902
903 switch (type) {
904#if defined(ANDROID_ARM_LINKER)
905 case R_ARM_JUMP_SLOT:
906 case R_ARM_GLOB_DAT:
907 case R_ARM_ABS32:
908 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -0700909#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700910 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -0700911 case R_386_GLOB_DAT:
912 case R_386_32:
913 case R_386_RELATIVE: /* Dont' care. */
914#endif /* ANDROID_*_LINKER */
915 /* sym_addr was initialized to be zero above or relocation
916 code below does not care about value of sym_addr.
917 No need to do anything. */
918 break;
919
920#if defined(ANDROID_X86_LINKER)
921 case R_386_PC32:
922 sym_addr = reloc;
923 break;
924#endif /* ANDROID_X86_LINKER */
925
926#if defined(ANDROID_ARM_LINKER)
927 case R_ARM_COPY:
928 /* Fall through. Can't really copy if weak symbol is
929 not found in run-time. */
930#endif /* ANDROID_ARM_LINKER */
931 default:
Elliott Hughes46882792012-08-03 16:49:39 -0700932 DL_ERR("unknown weak reloc type %d @ %p (%d)",
933 type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -0700934 return -1;
935 }
936 } else {
937 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800938#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800939 if ((base == 0) && (si->base != 0)) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700940 /* linking from libraries to main image is bad */
Elliott Hughes46882792012-08-03 16:49:39 -0700941 DL_ERR("cannot locate \"%s\"...",
942 strtab + symtab[sym].st_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700943 return -1;
944 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800945#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200946 sym_addr = (unsigned)(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700947 }
Elliott Hughesbedfe382012-08-14 14:07:59 -0700948 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800949 } else {
Doug Kwane8238072009-10-26 12:05:23 -0700950 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800951 }
952
953/* TODO: This is ugly. Split up the relocations by arch into
954 * different files.
955 */
956 switch(type){
957#if defined(ANDROID_ARM_LINKER)
958 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700959 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800960 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700961 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800962 *((unsigned*)reloc) = sym_addr;
963 break;
964 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700965 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800966 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700967 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800968 *((unsigned*)reloc) = sym_addr;
969 break;
970 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700971 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800972 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700973 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800974 *((unsigned*)reloc) += sym_addr;
975 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -0800976 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700977 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -0800978 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700979 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s\n",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -0800980 reloc, sym_addr, rel->r_offset, sym_name);
981 *((unsigned*)reloc) += sym_addr - rel->r_offset;
982 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800983#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700984 case R_386_JMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700985 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800986 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700987 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800988 *((unsigned*)reloc) = sym_addr;
989 break;
990 case R_386_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700991 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800992 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700993 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800994 *((unsigned*)reloc) = sym_addr;
995 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700996#elif defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700997 case R_MIPS_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700998 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700999 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001000 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s\n",
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001001 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1002 if (s) {
1003 *((unsigned*)reloc) += sym_addr;
1004 } else {
1005 *((unsigned*)reloc) += si->base;
1006 }
1007 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001008#endif /* ANDROID_*_LINKER */
1009
1010#if defined(ANDROID_ARM_LINKER)
1011 case R_ARM_RELATIVE:
1012#elif defined(ANDROID_X86_LINKER)
1013 case R_386_RELATIVE:
1014#endif /* ANDROID_*_LINKER */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001015 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001016 MARK(rel->r_offset);
Elliott Hughes46882792012-08-03 16:49:39 -07001017 if (sym) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001018 DL_ERR("odd RELATIVE form...");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001019 return -1;
1020 }
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001021 TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x\n", reloc, si->base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001022 *((unsigned*)reloc) += si->base;
1023 break;
1024
1025#if defined(ANDROID_X86_LINKER)
1026 case R_386_32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001027 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001028 MARK(rel->r_offset);
1029
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001030 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001031 *((unsigned *)reloc) += (unsigned)sym_addr;
1032 break;
1033
1034 case R_386_PC32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001035 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001036 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001037 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s\n",
1038 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001039 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1040 break;
1041#endif /* ANDROID_X86_LINKER */
1042
1043#ifdef ANDROID_ARM_LINKER
1044 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001045 if ((si->flags & FLAG_EXE) == 0) {
1046 /*
1047 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1048 *
1049 * Section 4.7.1.10 "Dynamic relocations"
1050 * R_ARM_COPY may only appear in executable objects where e_type is
1051 * set to ET_EXEC.
1052 *
1053 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1054 * We should explicitly disallow ET_DYN executables from having
1055 * R_ARM_COPY relocations.
1056 */
1057 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1058 return -1;
1059 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001060 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001061 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001062 TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s\n", reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001063 if (reloc == sym_addr) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001064 Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1065
1066 if (src == NULL) {
1067 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1068 return -1;
1069 }
1070 if (lsi->has_DT_SYMBOLIC) {
1071 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1072 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1073 return -1;
1074 }
1075 if (s->st_size < src->st_size) {
1076 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1077 si->name, s->st_size, src->st_size);
1078 return -1;
1079 }
1080 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1081 } else {
1082 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001083 return -1;
1084 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001085 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001086#endif /* ANDROID_ARM_LINKER */
1087
1088 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001089 DL_ERR("unknown reloc type %d @ %p (%d)",
1090 type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001091 return -1;
1092 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001093 }
1094 return 0;
1095}
1096
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001097#ifdef ANDROID_MIPS_LINKER
Elliott Hughesbedfe382012-08-14 14:07:59 -07001098static int mips_relocate_got(soinfo* si, soinfo* needed[]) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001099 unsigned *got;
1100 unsigned local_gotno, gotsym, symtabno;
1101 Elf32_Sym *symtab, *sym;
1102 unsigned g;
1103
1104 got = si->plt_got;
1105 local_gotno = si->mips_local_gotno;
1106 gotsym = si->mips_gotsym;
1107 symtabno = si->mips_symtabno;
1108 symtab = si->symtab;
1109
1110 /*
1111 * got[0] is address of lazy resolver function
1112 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001113 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001114 * (should be _rtld_bind_start)
1115 * FIXME: maybe this should be in a separate routine
1116 */
1117
1118 if ((si->flags & FLAG_LINKER) == 0) {
1119 g = 0;
1120 got[g++] = 0xdeadbeef;
1121 if (got[g] & 0x80000000) {
1122 got[g++] = 0xdeadfeed;
1123 }
1124 /*
1125 * Relocate the local GOT entries need to be relocated
1126 */
1127 for (; g < local_gotno; g++) {
1128 got[g] += si->load_bias;
1129 }
1130 }
1131
1132 /* Now for the global GOT entries */
1133 sym = symtab + gotsym;
1134 got = si->plt_got + local_gotno;
1135 for (g = gotsym; g < symtabno; g++, sym++, got++) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001136 const char* sym_name;
1137 Elf32_Sym* s;
1138 soinfo* lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001139
1140 /* This is an undefined reference... try to locate it */
1141 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001142 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001143 if (s == NULL) {
1144 /* We only allow an undefined symbol if this is a weak
1145 reference.. */
1146 s = &symtab[g];
1147 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001148 DL_ERR("cannot locate \"%s\"...", sym_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001149 return -1;
1150 }
1151 *got = 0;
1152 }
1153 else {
1154 /* FIXME: is this sufficient?
1155 * For reference see NetBSD link loader
1156 * 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
1157 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001158 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001159 }
1160 }
1161 return 0;
1162}
1163#endif
1164
David 'Digit' Turner82156792009-05-18 14:37:41 +02001165/* Please read the "Initialization and Termination functions" functions.
1166 * of the linker design note in bionic/linker/README.TXT to understand
1167 * what the following code is doing.
1168 *
1169 * The important things to remember are:
1170 *
1171 * DT_PREINIT_ARRAY must be called first for executables, and should
1172 * not appear in shared libraries.
1173 *
1174 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1175 *
1176 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1177 *
1178 * DT_FINI_ARRAY must be parsed in reverse order.
1179 */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001180void soinfo::CallArray(const char* array_name UNUSED, unsigned* array, int count, bool reverse) {
1181 if (array == NULL) {
1182 return;
1183 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001184
Elliott Hughesd23736e2012-11-01 15:16:56 -07001185 int step = 1;
1186 if (reverse) {
1187 array += (count-1);
1188 step = -1;
1189 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001190
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001191 TRACE("[ Calling %s @ %p [%d] for '%s' ]\n", array_name, array, count, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001192
1193 for (int n = count; n > 0; n--) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001194 TRACE("[ Looking at %s[%d] *%p == 0x%08x ]\n", array_name, n, array, *array);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001195 void (*func)() = (void (*)()) *array;
1196 array += step;
Elliott Hughesdb492b32013-01-03 15:44:03 -08001197 CallFunction("function", func);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001198 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001199
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001200 TRACE("[ Done calling %s for '%s' ]\n", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001201}
1202
Elliott Hughesd23736e2012-11-01 15:16:56 -07001203void soinfo::CallFunction(const char* function_name UNUSED, void (*function)()) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001204 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001205 return;
1206 }
1207
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001208 TRACE("[ Calling %s @ %p for '%s' ]\n", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001209 function();
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001210 TRACE("[ Done calling %s for '%s' ]\n", function_name, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001211
1212 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1213 // are still writable. This happens with our debug malloc (see http://b/7941716).
1214 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001215}
1216
Elliott Hughesd23736e2012-11-01 15:16:56 -07001217void soinfo::CallPreInitConstructors() {
1218 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1219}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001220
Elliott Hughesd23736e2012-11-01 15:16:56 -07001221void soinfo::CallConstructors() {
1222 if (constructors_called) {
1223 return;
1224 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001225
Elliott Hughesd23736e2012-11-01 15:16:56 -07001226 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1227 // protect against recursive constructor calls. One simple example of constructor recursion
1228 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1229 // 1. The program depends on libc, so libc's constructor is called here.
1230 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1231 // 3. dlopen() calls the constructors on the newly created
1232 // soinfo for libc_malloc_debug_leak.so.
1233 // 4. The debug .so depends on libc, so CallConstructors is
1234 // called again with the libc soinfo. If it doesn't trigger the early-
1235 // out above, the libc constructor will be called again (recursively!).
1236 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001237
Elliott Hughesd23736e2012-11-01 15:16:56 -07001238 if (!(flags & FLAG_EXE) && preinit_array) {
1239 DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array);
1240 return;
1241 }
1242
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001243 if (dynamic != NULL) {
1244 for (Elf32_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) {
1245 if (d->d_tag == DT_NEEDED) {
1246 const char* library_name = strtab + d->d_un.d_val;
1247 soinfo* lsi = find_loaded_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001248 if (lsi == NULL) {
1249 DL_ERR("\"%s\": could not initialize dependent library", name);
1250 } else {
1251 lsi->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001252 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001253 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001254 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001255 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001256
Elliott Hughesd23736e2012-11-01 15:16:56 -07001257 CallFunction("DT_INIT", init_func);
1258 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001259}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001260
Elliott Hughesd23736e2012-11-01 15:16:56 -07001261void soinfo::CallDestructors() {
1262 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
1263 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001264}
1265
1266/* Force any of the closed stdin, stdout and stderr to be associated with
1267 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001268static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001269 int dev_null, i, status;
1270 int return_value = 0;
1271
David 'Digit' Turner16084162012-06-12 16:25:37 +02001272 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001273 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001274 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001275 return -1;
1276 }
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001277 TRACE("[ Opened /dev/null file-descriptor=%d]\n", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001278
1279 /* If any of the stdio file descriptors is valid and not associated
1280 with /dev/null, dup /dev/null to it. */
1281 for (i = 0; i < 3; i++) {
1282 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001283 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001284 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001285 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001286
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001287 TRACE("[ Nullifying stdio file descriptor %d]\n", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001288 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001289
Elliott Hughes46882792012-08-03 16:49:39 -07001290 /* If file is opened, we are good. */
1291 if (status != -1) {
1292 continue;
1293 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001294
1295 /* The only error we allow is that the file descriptor does not
1296 exist, in which case we dup /dev/null to it. */
1297 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001298 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001299 return_value = -1;
1300 continue;
1301 }
1302
1303 /* Try dupping /dev/null to this stdio file descriptor and
1304 repeat if there is a signal. Note that any errors in closing
1305 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001306 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001307 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001308 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001309 return_value = -1;
1310 continue;
1311 }
1312 }
1313
1314 /* If /dev/null is not one of the stdio file descriptors, close it. */
1315 if (dev_null > 2) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001316 TRACE("[ Closing /dev/null file-descriptor=%d]\n", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001317 status = TEMP_FAILURE_RETRY(close(dev_null));
1318 if (status == -1) {
1319 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001320 return_value = -1;
1321 }
1322 }
1323
1324 return return_value;
1325}
1326
Elliott Hughes124fae92012-10-31 14:20:03 -07001327static bool soinfo_link_image(soinfo* si) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001328 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001329 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001330 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001331 int phnum = si->phnum;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001332 bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001333
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001334 /* We can't debug anything until the linker is relocated */
1335 if (!relocating_linker) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001336 INFO("[ linking %s ]\n", si->name);
1337 DEBUG("si->base = 0x%08x si->flags = 0x%08x\n", si->base, si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001338 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001339
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001340 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001341 size_t dynamic_count;
Chris Dearmancf239052013-01-11 15:32:20 -08001342 Elf32_Word dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001343 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001344 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001345 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001346 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001347 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001348 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001349 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001350 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001351 if (!relocating_linker) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001352 DEBUG("dynamic = %p\n", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001353 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001354 }
1355
1356#ifdef ANDROID_ARM_LINKER
1357 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1358 &si->ARM_exidx, &si->ARM_exidx_count);
1359#endif
1360
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001361 /* extract useful information from dynamic section */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001362 uint32_t needed_count = 0;
1363 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1364 DEBUG("d = %p, d[0](tag) = 0x%08x d[1](val) = 0x%08x\n", d, d->d_tag, d->d_un.d_val);
1365 switch(d->d_tag){
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001366 case DT_HASH:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001367 si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0];
1368 si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1];
1369 si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8);
1370 si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001371 break;
1372 case DT_STRTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001373 si->strtab = (const char *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001374 break;
1375 case DT_SYMTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001376 si->symtab = (Elf32_Sym *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001377 break;
1378 case DT_PLTREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001379 if (d->d_un.d_val != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001380 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1381 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001382 }
1383 break;
1384 case DT_JMPREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001385 si->plt_rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001386 break;
1387 case DT_PLTRELSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001388 si->plt_rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001389 break;
1390 case DT_REL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001391 si->rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001392 break;
1393 case DT_RELSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001394 si->rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001395 break;
1396 case DT_PLTGOT:
1397 /* Save this in case we decide to do lazy binding. We don't yet. */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001398 si->plt_got = (unsigned *)(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001399 break;
1400 case DT_DEBUG:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001401 // Set the DT_DEBUG entry to the address of _r_debug for GDB
Chris Dearmancf239052013-01-11 15:32:20 -08001402 // if the dynamic table is writable
Elliott Hughes99c32052013-01-14 09:56:21 -08001403 if ((dynamic_flags & PF_W) != 0) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001404 d->d_un.d_val = (int) &_r_debug;
Elliott Hughes99c32052013-01-14 09:56:21 -08001405 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001406 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001407 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001408 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1409 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001410 case DT_INIT:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001411 si->init_func = (void (*)(void))(base + d->d_un.d_ptr);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001412 DEBUG("%s constructors (init func) found at %p\n", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001413 break;
1414 case DT_FINI:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001415 si->fini_func = (void (*)(void))(base + d->d_un.d_ptr);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001416 DEBUG("%s destructors (fini func) found at %p\n", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001417 break;
1418 case DT_INIT_ARRAY:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001419 si->init_array = (unsigned *)(base + d->d_un.d_ptr);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001420 DEBUG("%s constructors (init_array) found at %p\n", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001421 break;
1422 case DT_INIT_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001423 si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001424 break;
1425 case DT_FINI_ARRAY:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001426 si->fini_array = (unsigned *)(base + d->d_un.d_ptr);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001427 DEBUG("%s destructors (fini_array) found at %p\n", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001428 break;
1429 case DT_FINI_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001430 si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001431 break;
1432 case DT_PREINIT_ARRAY:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001433 si->preinit_array = (unsigned *)(base + d->d_un.d_ptr);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001434 DEBUG("%s constructors (preinit_array) found at %p\n", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001435 break;
1436 case DT_PREINIT_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001437 si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001438 break;
1439 case DT_TEXTREL:
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001440 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001441 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001442 case DT_SYMBOLIC:
1443 si->has_DT_SYMBOLIC = true;
1444 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001445 case DT_NEEDED:
1446 ++needed_count;
1447 break;
1448#if defined DT_FLAGS
1449 // TODO: why is DT_FLAGS not defined?
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001450 case DT_FLAGS:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001451 if (d->d_un.d_val & DF_TEXTREL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001452 si->has_text_relocations = true;
1453 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001454 if (d->d_un.d_val & DF_SYMBOLIC) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001455 si->has_DT_SYMBOLIC = true;
1456 }
1457 break;
1458#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001459#if defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001460 case DT_STRSZ:
1461 case DT_SYMENT:
1462 case DT_RELENT:
1463 break;
1464 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001465 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001466 {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001467 r_debug** dp = (r_debug**) d->d_un.d_ptr;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001468 *dp = &_r_debug;
1469 }
1470 break;
1471 case DT_MIPS_RLD_VERSION:
1472 case DT_MIPS_FLAGS:
1473 case DT_MIPS_BASE_ADDRESS:
1474 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001475 break;
1476
1477 case DT_MIPS_SYMTABNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001478 si->mips_symtabno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001479 break;
1480
1481 case DT_MIPS_LOCAL_GOTNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001482 si->mips_local_gotno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001483 break;
1484
1485 case DT_MIPS_GOTSYM:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001486 si->mips_gotsym = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001487 break;
1488
1489 default:
Brian Carlstrom43cc7f72013-03-06 01:03:25 -08001490 DEBUG("Unused DT entry: type 0x%08x arg 0x%08x\n", d->d_tag, d->d_un.d_val);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001491 break;
1492#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001493 }
1494 }
1495
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001496 DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
1497 si->base, si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001498
Elliott Hughes124fae92012-10-31 14:20:03 -07001499 // Sanity checks.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001500 if (relocating_linker && needed_count != 0) {
1501 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1502 return false;
1503 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001504 if (si->nbucket == 0) {
1505 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1506 return false;
1507 }
1508 if (si->strtab == 0) {
1509 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1510 return false;
1511 }
1512 if (si->symtab == 0) {
1513 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1514 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001515 }
1516
Matt Fischer4fd42c12009-12-31 12:09:10 -06001517 /* if this is the main executable, then load all of the preloads now */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001518 if (si->flags & FLAG_EXE) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001519 memset(gLdPreloads, 0, sizeof(gLdPreloads));
Elliott Hughesd23736e2012-11-01 15:16:56 -07001520 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1521 soinfo* lsi = find_library(gLdPreloadNames[i]);
1522 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001523 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001524 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Elliott Hughes124fae92012-10-31 14:20:03 -07001525 gLdPreloadNames[i], si->name, tmp_err_buf);
1526 return false;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001527 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001528 gLdPreloads[i] = lsi;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001529 }
1530 }
1531
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001532 soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*));
1533 soinfo** pneeded = needed;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001534
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001535 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1536 if (d->d_tag == DT_NEEDED) {
1537 const char* library_name = si->strtab + d->d_un.d_val;
1538 DEBUG("%s needs %s\n", si->name, library_name);
1539 soinfo* lsi = find_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001540 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001541 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001542 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001543 library_name, si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001544 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001545 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001546 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001547 }
1548 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001549 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001550
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001551 if (si->has_text_relocations) {
1552 /* Unprotect the segments, i.e. make them writable, to allow
1553 * text relocations to work properly. We will later call
1554 * phdr_table_protect_segments() after all of them are applied
1555 * and all constructors are run.
1556 */
1557 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1558 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1559 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001560 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001561 }
1562 }
1563
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001564 if (si->plt_rel != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001565 DEBUG("[ relocating %s plt ]\n", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001566 if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001567 return false;
1568 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001569 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001570 if (si->rel != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001571 DEBUG("[ relocating %s ]\n", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001572 if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001573 return false;
1574 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001575 }
1576
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001577#ifdef ANDROID_MIPS_LINKER
Elliott Hughes124fae92012-10-31 14:20:03 -07001578 if (mips_relocate_got(si, needed)) {
1579 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001580 }
1581#endif
1582
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001583 si->flags |= FLAG_LINKED;
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001584 DEBUG("[ finished linking %s ]\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001585
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001586 if (si->has_text_relocations) {
1587 /* All relocations are done, we can protect our segments back to
1588 * read-only. */
1589 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1590 DL_ERR("can't protect segments for \"%s\": %s",
1591 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001592 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001593 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001594 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001595
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001596 /* We can also turn on GNU RELRO protection */
1597 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001598 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1599 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001600 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001601 }
1602
Elliott Hughes124fae92012-10-31 14:20:03 -07001603 // If this is a setuid/setgid program, close the security hole described in
1604 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
Elliott Hughes18a206c2012-10-29 17:37:13 -07001605 if (get_AT_SECURE()) {
Elliott Hughes46882792012-08-03 16:49:39 -07001606 nullify_closed_stdio();
1607 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001608 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001609 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001610}
1611
Nick Kralevich468319c2011-11-11 15:53:17 -08001612/*
1613 * This code is called after the linker has linked itself and
1614 * fixed it's own GOT. It is safe to make references to externs
1615 * and other non-local data at this point.
1616 */
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001617static unsigned __linker_init_post_relocation(KernelArgumentBlock& args, unsigned linker_base) {
1618 /* NOTE: we store the args pointer on a special location
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001619 * of the temporary TLS area in order to pass it to
1620 * the C Library's runtime initializer.
1621 *
1622 * The initializer must clear the slot and reset the TLS
1623 * to point to a different location to ensure that no other
1624 * shared library constructor can access it.
1625 */
Elliott Hughesd3920b32013-02-07 18:39:34 -08001626 __libc_init_tls(args);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001627
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001628#if TIMING
1629 struct timeval t0, t1;
1630 gettimeofday(&t0, 0);
1631#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001632
Elliott Hughes18a206c2012-10-29 17:37:13 -07001633 // Initialize environment functions, and get to the ELF aux vectors table.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001634 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001635
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001636 debuggerd_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001637
Elliott Hughes18a206c2012-10-29 17:37:13 -07001638 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001639 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1640 if (LD_DEBUG != NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001641 gLdDebugVerbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001642 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001643
Elliott Hughes18a206c2012-10-29 17:37:13 -07001644 // Normally, these are cleaned by linker_env_init, but the test
1645 // doesn't cost us anything.
1646 const char* ldpath_env = NULL;
1647 const char* ldpreload_env = NULL;
1648 if (!get_AT_SECURE()) {
1649 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1650 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001651 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652
1653 INFO("[ android linker & debugger ]\n");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001654
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001655 soinfo* si = soinfo_alloc(args.argv[0]);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001656 if (si == NULL) {
1657 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001658 }
1659
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001660 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001661 si->flags |= FLAG_EXE;
Elliott Hughesbedfe382012-08-14 14:07:59 -07001662 link_map* map = &(si->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001663
1664 map->l_addr = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001665 map->l_name = args.argv[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001666 map->l_prev = NULL;
1667 map->l_next = NULL;
1668
1669 _r_debug.r_map = map;
1670 r_debug_tail = map;
1671
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001672 /* gdb expects the linker to be in the debug shared object list.
1673 * Without this, gdb has trouble locating the linker's ".text"
1674 * and ".plt" sections. Gdb could also potentially use this to
1675 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1676 * Don't use soinfo_alloc(), because the linker shouldn't
1677 * be on the soinfo list.
Ben Cheng06f0e742012-08-10 16:07:02 -07001678 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001679 {
1680 static soinfo linker_soinfo;
1681 strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
1682 linker_soinfo.flags = 0;
1683 linker_soinfo.base = linker_base;
1684
1685 /*
1686 * Set the dynamic field in the link map otherwise gdb will complain with
1687 * the following:
1688 * warning: .dynamic section for "/system/bin/linker" is not at the
1689 * expected address (wrong library or version mismatch?)
1690 */
1691 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
1692 Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
1693 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1694 &linker_soinfo.dynamic, NULL, NULL);
1695 insert_soinfo_into_debug_map(&linker_soinfo);
1696 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001697
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001698 // Extract information passed from the kernel.
1699 si->phdr = reinterpret_cast<Elf32_Phdr*>(args.getauxval(AT_PHDR));
1700 si->phnum = args.getauxval(AT_PHNUM);
1701 si->entry = args.getauxval(AT_ENTRY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001702
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001703 /* Compute the value of si->base. We can't rely on the fact that
1704 * the first entry is the PHDR because this will not be true
1705 * for certain executables (e.g. some in the NDK unit test suite)
1706 */
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001707 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001708 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001709 si->load_bias = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001710 for (int i = 0; i < si->phnum; ++i) {
1711 if (si->phdr[i].p_type == PT_PHDR) {
1712 si->load_bias = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_vaddr;
1713 si->base = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_offset;
1714 break;
1715 }
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001716 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001717 si->dynamic = NULL;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001718 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001719
Elliott Hughes46882792012-08-03 16:49:39 -07001720 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1721 parse_LD_LIBRARY_PATH(ldpath_env);
1722 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001723
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001724 somain = si;
1725
Elliott Hughes124fae92012-10-31 14:20:03 -07001726 if (!soinfo_link_image(si)) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001727 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
Elliott Hughes18a206c2012-10-29 17:37:13 -07001728 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001729 }
1730
Elliott Hughesd23736e2012-11-01 15:16:56 -07001731 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001732
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001733 for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
1734 gLdPreloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08001735 }
1736
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001737 /* After the link_image, the si->load_bias is initialized.
1738 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1739 * We need to update this value for so exe here. So Unwind_Backtrace
1740 * for some arch like x86 could work correctly within so exe.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08001741 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08001742 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001743 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001744
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001745#if TIMING
1746 gettimeofday(&t1,NULL);
Kito Cheng5e2492e2013-03-06 23:52:45 +08001747 PRINT("LINKER TIME: %s: %d microseconds\n", args.argv[0], (int) (
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001748 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1749 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1750 ));
1751#endif
1752#if STATS
Kito Cheng5e2492e2013-03-06 23:52:45 +08001753 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", args.argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07001754 linker_stats.count[kRelocAbsolute],
1755 linker_stats.count[kRelocRelative],
1756 linker_stats.count[kRelocCopy],
1757 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001758#endif
1759#if COUNT_PAGES
1760 {
1761 unsigned n;
1762 unsigned i;
1763 unsigned count = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001764 for (n = 0; n < 4096; n++) {
1765 if (bitmask[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001766 unsigned x = bitmask[n];
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001767 for (i = 0; i < 8; i++) {
1768 if (x & 1) {
1769 count++;
1770 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001771 x >>= 1;
1772 }
1773 }
1774 }
Kito Cheng5e2492e2013-03-06 23:52:45 +08001775 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", args.argv[0], count, count * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001776 }
1777#endif
1778
1779#if TIMING || STATS || COUNT_PAGES
1780 fflush(stdout);
1781#endif
1782
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001783 TRACE("[ Ready to execute '%s' @ 0x%08x ]\n", si->name, si->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001784 return si->entry;
1785}
Nick Kralevich468319c2011-11-11 15:53:17 -08001786
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001787/* Compute the load-bias of an existing executable. This shall only
1788 * be used to compute the load bias of an executable or shared library
1789 * that was loaded by the kernel itself.
1790 *
1791 * Input:
1792 * elf -> address of ELF header, assumed to be at the start of the file.
1793 * Return:
1794 * load bias, i.e. add the value of any p_vaddr in the file to get
1795 * the corresponding address in memory.
1796 */
1797static Elf32_Addr
1798get_elf_exec_load_bias(const Elf32_Ehdr* elf)
1799{
1800 Elf32_Addr offset = elf->e_phoff;
1801 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
1802 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
1803 const Elf32_Phdr* phdr;
1804
1805 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
1806 if (phdr->p_type == PT_LOAD) {
1807 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
1808 }
1809 }
1810 return 0;
1811}
1812
Nick Kralevich468319c2011-11-11 15:53:17 -08001813/*
1814 * This is the entry point for the linker, called from begin.S. This
1815 * method is responsible for fixing the linker's own relocations, and
1816 * then calling __linker_init_post_relocation().
1817 *
1818 * Because this method is called before the linker has fixed it's own
1819 * relocations, any attempt to reference an extern variable, extern
1820 * function, or other GOT reference will generate a segfault.
1821 */
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001822extern "C" unsigned __linker_init(void* raw_args) {
1823 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08001824
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001825 unsigned linker_addr = args.getauxval(AT_BASE);
Nick Kralevich468319c2011-11-11 15:53:17 -08001826
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001827 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr*) linker_addr;
1828 Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08001829
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001830 soinfo linker_so;
1831 memset(&linker_so, 0, sizeof(soinfo));
Nick Kralevich468319c2011-11-11 15:53:17 -08001832
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001833 linker_so.base = linker_addr;
1834 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
1835 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001836 linker_so.dynamic = NULL;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001837 linker_so.phdr = phdr;
1838 linker_so.phnum = elf_hdr->e_phnum;
1839 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07001840
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001841 if (!soinfo_link_image(&linker_so)) {
1842 // It would be nice to print an error message, but if the linker
1843 // can't link itself, there's no guarantee that we'll be able to
1844 // call write() (because it involves a GOT reference).
1845 //
1846 // This situation should never occur unless the linker itself
1847 // is corrupt.
1848 exit(EXIT_FAILURE);
1849 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001850
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001851 // We have successfully fixed our own relocations. It's safe to run
1852 // the main part of the linker now.
1853 unsigned start_address = __linker_init_post_relocation(args, linker_addr);
1854
1855 set_soinfo_pool_protection(PROT_READ);
1856
1857 // Return the address that the calling assembly stub should jump to.
1858 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08001859}