blob: 2f119e44a5f6482c5fa0c6c695c8775894b77e52 [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 Hughesca0c11b2013-03-12 10:40:45 -0700177static link_map_t* 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.
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700183 link_map_t* map = &(info->link_map);
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) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700206 link_map_t* map = &(info->link_map);
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 Hughesca0c11b2013-03-12 10:40:45 -0700318 TRACE("name %s: allocated soinfo @ %p", 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 Hughesca0c11b2013-03-12 10:40:45 -0700330 TRACE("name %s: freeing soinfo @ %p", 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;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700428 dl_info.dlpi_addr = si->link_map.l_addr;
429 dl_info.dlpi_name = si->link_map.l_name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800430 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 Hughesca0c11b2013-03-12 10:40:45 -0700448 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d",
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 Hughesca0c11b2013-03-12 10:40:45 -0700464 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d",
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 Hughesca0c11b2013-03-12 10:40:45 -0700513 DEBUG("%s: looking up %s in executable %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700514 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 Hughesca0c11b2013-03-12 10:40:45 -0700544 DEBUG("%s: looking up %s in executable %s after local scope",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700545 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 Hughesca0c11b2013-03-12 10:40:45 -0700565 DEBUG("%s: looking up %s in %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700566 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, "
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700577 "found in %s, base = 0x%08x, load bias = 0x%08x",
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) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700622 TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, found->base = 0x%08x",
Elliott Hughescade4c32012-12-20 14:42:14 -0800623 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
Kito Chengfa8c05d2013-03-12 14:58:06 +0800629soinfo* find_containing_library(const void* p) {
630 Elf32_Addr address = reinterpret_cast<Elf32_Addr>(p);
631 for (soinfo* si = solist; si != NULL; si = si->next) {
632 if (address >= si->base && address - si->base < si->size) {
633 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600634 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800635 }
636 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600637}
638
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800639Elf32_Sym* dladdr_find_symbol(soinfo* si, const void* addr) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800640 Elf32_Addr soaddr = reinterpret_cast<Elf32_Addr>(addr) - si->base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600641
Kito Chengfa8c05d2013-03-12 14:58:06 +0800642 // Search the library's symbol table for any defined symbol which
643 // contains this address.
644 for (size_t i = 0; i < si->nchain; ++i) {
645 Elf32_Sym* sym = &si->symtab[i];
646 if (sym->st_shndx != SHN_UNDEF &&
647 soaddr >= sym->st_value &&
648 soaddr < sym->st_value + sym->st_size) {
649 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600650 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800651 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600652
Kito Chengfa8c05d2013-03-12 14:58:06 +0800653 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600654}
655
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800656#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800657static void dump(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800658{
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800659 Elf32_Sym* s = si->symtab;
660 for (unsigned n = 0; n < si->nchain; n++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700661 TRACE("%04d> %08x: %02x %04x %08x %08x %s", n, s,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800662 s->st_info, s->st_shndx, s->st_value, s->st_size,
663 si->strtab + s->st_name);
664 s++;
665 }
666}
667#endif
668
Elliott Hughes124fae92012-10-31 14:20:03 -0700669static int open_library_on_path(const char* name, const char* const paths[]) {
670 char buf[512];
671 for (size_t i = 0; paths[i] != NULL; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800672 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700673 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700674 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700675 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800676 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700677 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
678 if (fd != -1) {
679 return fd;
680 }
681 }
682 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800683}
684
Elliott Hughes124fae92012-10-31 14:20:03 -0700685static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700686 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800687
Elliott Hughes124fae92012-10-31 14:20:03 -0700688 // If the name contains a slash, we should attempt to open it directly and not search the paths.
689 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700690 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
691 if (fd != -1) {
692 return fd;
693 }
694 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700695 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800696
Elliott Hughes124fae92012-10-31 14:20:03 -0700697 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
698 int fd = open_library_on_path(name, gLdPaths);
699 if (fd == -1) {
700 fd = open_library_on_path(name, gSoPaths);
701 }
702 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800703}
704
Elliott Hughes124fae92012-10-31 14:20:03 -0700705static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700706 // Open the file.
Elliott Hughes650be4e2013-03-05 18:47:58 -0800707 int fd = open_library(name);
708 if (fd == -1) {
Elliott Hughes46882792012-08-03 16:49:39 -0700709 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800710 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700711 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800712
Elliott Hughes650be4e2013-03-05 18:47:58 -0800713 // Read the ELF header and load the segments.
714 ElfReader elf_reader(name, fd);
715 if (!elf_reader.Load()) {
Elliott Hughes46882792012-08-03 16:49:39 -0700716 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800717 }
718
Elliott Hughes650be4e2013-03-05 18:47:58 -0800719 const char* bname = strrchr(name, '/');
720 soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
721 if (si == NULL) {
Elliott Hughes46882792012-08-03 16:49:39 -0700722 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200723 }
Elliott Hughes650be4e2013-03-05 18:47:58 -0800724 si->base = elf_reader.load_start();
725 si->size = elf_reader.load_size();
726 si->load_bias = elf_reader.load_bias();
727 si->flags = 0;
728 si->entry = 0;
729 si->dynamic = NULL;
730 si->phnum = elf_reader.phdr_count();
731 si->phdr = elf_reader.loaded_phdr();
732 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800733}
734
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200735static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800736{
737 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700738 const char *bname;
739
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200740 // TODO: don't use basename only for determining libraries
741 // http://code.google.com/p/android/issues/detail?id=6670
742
743 bname = strrchr(name, '/');
744 bname = bname ? bname + 1 : name;
745
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800746 for (si = solist; si != NULL; si = si->next) {
747 if (!strcmp(bname, si->name)) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200748 return si;
749 }
750 }
751 return NULL;
752}
753
Elliott Hughesd23736e2012-11-01 15:16:56 -0700754static soinfo* find_library_internal(const char* name) {
755 if (name == NULL) {
756 return somain;
757 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200758
Elliott Hughesd23736e2012-11-01 15:16:56 -0700759 soinfo* si = find_loaded_library(name);
760 if (si != NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700761 if (si->flags & FLAG_LINKED) {
762 return si;
763 }
764 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
765 return NULL;
766 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800767
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700768 TRACE("[ '%s' has not been loaded yet. Locating...]", name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700769 si = load_library(name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800770 if (si == NULL) {
771 return NULL;
772 }
773
774 // At this point we know that whatever is loaded @ base is a valid ELF
775 // shared library whose segments are properly mapped in.
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700776 TRACE("[ init_library base=0x%08x sz=0x%08x name='%s' ]",
Elliott Hughescade4c32012-12-20 14:42:14 -0800777 si->base, si->size, si->name);
778
779 if (!soinfo_link_image(si)) {
780 munmap(reinterpret_cast<void*>(si->base), si->size);
781 soinfo_free(si);
782 return NULL;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700783 }
784
785 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800786}
787
Elliott Hughesd23736e2012-11-01 15:16:56 -0700788static soinfo* find_library(const char* name) {
789 soinfo* si = find_library_internal(name);
790 if (si != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700791 si->ref_count++;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700792 }
793 return si;
794}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700795
Elliott Hughesd23736e2012-11-01 15:16:56 -0700796static int soinfo_unload(soinfo* si) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700797 if (si->ref_count == 1) {
798 TRACE("unloading '%s'", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700799 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800800
Brian Carlstrom2d4b9b72013-03-06 15:32:16 -0800801 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800802 if (d->d_tag == DT_NEEDED) {
803 const char* library_name = si->strtab + d->d_un.d_val;
804 soinfo* lsi = find_loaded_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700805 if (lsi != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700806 TRACE("%s needs to unload %s", si->name, lsi->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700807 soinfo_unload(lsi);
808 } else {
809 // TODO: should we return -1 in this case?
810 DL_ERR("\"%s\": could not unload dependent library", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800811 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700812 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800813 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700814
815 munmap(reinterpret_cast<void*>(si->base), si->size);
816 notify_gdb_of_unload(si);
817 soinfo_free(si);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700818 si->ref_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700819 } else {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700820 si->ref_count--;
821 TRACE("not unloading '%s', decrementing ref_count to %d", si->name, si->ref_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700822 }
823 return 0;
824}
825
Elliott Hughescade4c32012-12-20 14:42:14 -0800826void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
827 if (!get_AT_SECURE()) {
828 parse_LD_LIBRARY_PATH(ld_library_path);
829 }
830}
831
Elliott Hughese66190d2012-12-18 15:57:55 -0800832soinfo* do_dlopen(const char* name, int flags) {
833 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
834 DL_ERR("invalid flags to dlopen: %x", flags);
835 return NULL;
836 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700837 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
838 soinfo* si = find_library(name);
839 if (si != NULL) {
840 si->CallConstructors();
841 }
842 set_soinfo_pool_protection(PROT_READ);
843 return si;
844}
845
846int do_dlclose(soinfo* si) {
847 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
848 int result = soinfo_unload(si);
849 set_soinfo_pool_protection(PROT_READ);
850 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800851}
852
853/* TODO: don't use unsigned for addrs below. It works, but is not
854 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
855 * long.
856 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800857static int soinfo_relocate(soinfo* si, Elf32_Rel* rel, unsigned count,
858 soinfo* needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800859{
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800860 Elf32_Sym* symtab = si->symtab;
861 const char* strtab = si->strtab;
862 Elf32_Sym* s;
863 Elf32_Rel* start = rel;
864 soinfo* lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800865
Elliott Hughes46882792012-08-03 16:49:39 -0700866 for (size_t idx = 0; idx < count; ++idx, ++rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800867 unsigned type = ELF32_R_TYPE(rel->r_info);
868 unsigned sym = ELF32_R_SYM(rel->r_info);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800869 Elf32_Addr reloc = static_cast<Elf32_Addr>(rel->r_offset + si->load_bias);
870 Elf32_Addr sym_addr = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800871 char* sym_name = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800872
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700873 DEBUG("Processing '%s' relocation at index %d", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700874 if (type == 0) { // R_*_NONE
875 continue;
876 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800877 if (sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -0700878 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200879 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800880 if (s == NULL) {
Doug Kwane8238072009-10-26 12:05:23 -0700881 /* We only allow an undefined symbol if this is a weak
882 reference.. */
883 s = &symtab[sym];
884 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700885 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -0700886 return -1;
887 }
888
889 /* IHI0044C AAELF 4.5.1.1:
890
891 Libraries are not searched to resolve weak references.
892 It is not an error for a weak reference to remain
893 unsatisfied.
894
895 During linking, the value of an undefined weak reference is:
896 - Zero if the relocation type is absolute
897 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -0700898 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -0700899 type is base-relative.
900 */
901
902 switch (type) {
903#if defined(ANDROID_ARM_LINKER)
904 case R_ARM_JUMP_SLOT:
905 case R_ARM_GLOB_DAT:
906 case R_ARM_ABS32:
907 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -0700908#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700909 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -0700910 case R_386_GLOB_DAT:
911 case R_386_32:
912 case R_386_RELATIVE: /* Dont' care. */
913#endif /* ANDROID_*_LINKER */
914 /* sym_addr was initialized to be zero above or relocation
915 code below does not care about value of sym_addr.
916 No need to do anything. */
917 break;
918
919#if defined(ANDROID_X86_LINKER)
920 case R_386_PC32:
921 sym_addr = reloc;
922 break;
923#endif /* ANDROID_X86_LINKER */
924
925#if defined(ANDROID_ARM_LINKER)
926 case R_ARM_COPY:
927 /* Fall through. Can't really copy if weak symbol is
928 not found in run-time. */
929#endif /* ANDROID_ARM_LINKER */
930 default:
Elliott Hughes46882792012-08-03 16:49:39 -0700931 DL_ERR("unknown weak reloc type %d @ %p (%d)",
932 type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -0700933 return -1;
934 }
935 } else {
936 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800937#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800938 if ((base == 0) && (si->base != 0)) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700939 /* linking from libraries to main image is bad */
Elliott Hughes46882792012-08-03 16:49:39 -0700940 DL_ERR("cannot locate \"%s\"...",
941 strtab + symtab[sym].st_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700942 return -1;
943 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800944#endif
Kito Chengfa8c05d2013-03-12 14:58:06 +0800945 sym_addr = static_cast<Elf32_Addr>(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700946 }
Elliott Hughesbedfe382012-08-14 14:07:59 -0700947 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800948 } else {
Doug Kwane8238072009-10-26 12:05:23 -0700949 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800950 }
951
952/* TODO: This is ugly. Split up the relocations by arch into
953 * different files.
954 */
955 switch(type){
956#if defined(ANDROID_ARM_LINKER)
957 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700958 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800959 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700960 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800961 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800962 break;
963 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700964 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800965 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700966 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800967 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800968 break;
969 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700970 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800971 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700972 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800973 *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800974 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -0800975 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700976 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -0800977 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700978 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -0800979 reloc, sym_addr, rel->r_offset, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800980 *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr - rel->r_offset;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -0800981 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800982#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700983 case R_386_JMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700984 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800985 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700986 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800987 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800988 break;
989 case R_386_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700990 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800991 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700992 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800993 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800994 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700995#elif defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700996 case R_MIPS_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700997 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700998 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700999 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s",
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001000 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1001 if (s) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001002 *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001003 } else {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001004 *reinterpret_cast<Elf32_Addr*>(reloc) += si->base;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001005 }
1006 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001007#endif /* ANDROID_*_LINKER */
1008
1009#if defined(ANDROID_ARM_LINKER)
1010 case R_ARM_RELATIVE:
1011#elif defined(ANDROID_X86_LINKER)
1012 case R_386_RELATIVE:
1013#endif /* ANDROID_*_LINKER */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001014 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001015 MARK(rel->r_offset);
Elliott Hughes46882792012-08-03 16:49:39 -07001016 if (sym) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001017 DL_ERR("odd RELATIVE form...");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001018 return -1;
1019 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001020 TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x", reloc, si->base);
Kito Chengfa8c05d2013-03-12 14:58:06 +08001021 *reinterpret_cast<Elf32_Addr*>(reloc) += si->base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001022 break;
1023
1024#if defined(ANDROID_X86_LINKER)
1025 case R_386_32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001026 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001027 MARK(rel->r_offset);
1028
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001029 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +08001030 *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001031 break;
1032
1033 case R_386_PC32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001034 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001035 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001036 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001037 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +08001038 *reinterpret_cast<Elf32_Addr*>(reloc) += (sym_addr - reloc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001039 break;
1040#endif /* ANDROID_X86_LINKER */
1041
1042#ifdef ANDROID_ARM_LINKER
1043 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001044 if ((si->flags & FLAG_EXE) == 0) {
1045 /*
1046 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1047 *
1048 * Section 4.7.1.10 "Dynamic relocations"
1049 * R_ARM_COPY may only appear in executable objects where e_type is
1050 * set to ET_EXEC.
1051 *
1052 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1053 * We should explicitly disallow ET_DYN executables from having
1054 * R_ARM_COPY relocations.
1055 */
1056 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1057 return -1;
1058 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001059 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001060 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001061 TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s", reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001062 if (reloc == sym_addr) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001063 Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1064
1065 if (src == NULL) {
1066 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1067 return -1;
1068 }
1069 if (lsi->has_DT_SYMBOLIC) {
1070 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1071 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1072 return -1;
1073 }
1074 if (s->st_size < src->st_size) {
1075 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1076 si->name, s->st_size, src->st_size);
1077 return -1;
1078 }
1079 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1080 } else {
1081 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001082 return -1;
1083 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001084 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001085#endif /* ANDROID_ARM_LINKER */
1086
1087 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001088 DL_ERR("unknown reloc type %d @ %p (%d)",
1089 type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001090 return -1;
1091 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001092 }
1093 return 0;
1094}
1095
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001096#ifdef ANDROID_MIPS_LINKER
Elliott Hughesbedfe382012-08-14 14:07:59 -07001097static int mips_relocate_got(soinfo* si, soinfo* needed[]) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001098 unsigned *got;
1099 unsigned local_gotno, gotsym, symtabno;
1100 Elf32_Sym *symtab, *sym;
1101 unsigned g;
1102
1103 got = si->plt_got;
1104 local_gotno = si->mips_local_gotno;
1105 gotsym = si->mips_gotsym;
1106 symtabno = si->mips_symtabno;
1107 symtab = si->symtab;
1108
1109 /*
1110 * got[0] is address of lazy resolver function
1111 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001112 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001113 * (should be _rtld_bind_start)
1114 * FIXME: maybe this should be in a separate routine
1115 */
1116
1117 if ((si->flags & FLAG_LINKER) == 0) {
1118 g = 0;
1119 got[g++] = 0xdeadbeef;
1120 if (got[g] & 0x80000000) {
1121 got[g++] = 0xdeadfeed;
1122 }
1123 /*
1124 * Relocate the local GOT entries need to be relocated
1125 */
1126 for (; g < local_gotno; g++) {
1127 got[g] += si->load_bias;
1128 }
1129 }
1130
1131 /* Now for the global GOT entries */
1132 sym = symtab + gotsym;
1133 got = si->plt_got + local_gotno;
1134 for (g = gotsym; g < symtabno; g++, sym++, got++) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001135 const char* sym_name;
1136 Elf32_Sym* s;
1137 soinfo* lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001138
1139 /* This is an undefined reference... try to locate it */
1140 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001141 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001142 if (s == NULL) {
1143 /* We only allow an undefined symbol if this is a weak
1144 reference.. */
1145 s = &symtab[g];
1146 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001147 DL_ERR("cannot locate \"%s\"...", sym_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001148 return -1;
1149 }
1150 *got = 0;
1151 }
1152 else {
1153 /* FIXME: is this sufficient?
1154 * For reference see NetBSD link loader
1155 * 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
1156 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001157 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001158 }
1159 }
1160 return 0;
1161}
1162#endif
1163
David 'Digit' Turner82156792009-05-18 14:37:41 +02001164/* Please read the "Initialization and Termination functions" functions.
1165 * of the linker design note in bionic/linker/README.TXT to understand
1166 * what the following code is doing.
1167 *
1168 * The important things to remember are:
1169 *
1170 * DT_PREINIT_ARRAY must be called first for executables, and should
1171 * not appear in shared libraries.
1172 *
1173 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1174 *
1175 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1176 *
1177 * DT_FINI_ARRAY must be parsed in reverse order.
1178 */
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001179void soinfo::CallArray(const char* array_name UNUSED, linker_function_t* functions, size_t count, bool reverse) {
1180 if (functions == NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001181 return;
1182 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001183
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001184 TRACE("[ Calling %s (size %d) @ %p for '%s' ]", array_name, count, functions, name);
1185
1186 int begin = reverse ? (count - 1) : 0;
1187 int end = reverse ? -1 : count;
1188 int step = reverse ? -1 : 1;
1189
1190 for (int i = begin; i != end; i += step) {
1191 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1192 CallFunction("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001193 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001194
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001195 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001196}
1197
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001198void soinfo::CallFunction(const char* function_name UNUSED, linker_function_t function) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001199 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001200 return;
1201 }
1202
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001203 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001204 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001205 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001206
1207 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1208 // are still writable. This happens with our debug malloc (see http://b/7941716).
1209 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001210}
1211
Elliott Hughesd23736e2012-11-01 15:16:56 -07001212void soinfo::CallPreInitConstructors() {
1213 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1214}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001215
Elliott Hughesd23736e2012-11-01 15:16:56 -07001216void soinfo::CallConstructors() {
1217 if (constructors_called) {
1218 return;
1219 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001220
Elliott Hughesd23736e2012-11-01 15:16:56 -07001221 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1222 // protect against recursive constructor calls. One simple example of constructor recursion
1223 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1224 // 1. The program depends on libc, so libc's constructor is called here.
1225 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1226 // 3. dlopen() calls the constructors on the newly created
1227 // soinfo for libc_malloc_debug_leak.so.
1228 // 4. The debug .so depends on libc, so CallConstructors is
1229 // called again with the libc soinfo. If it doesn't trigger the early-
1230 // out above, the libc constructor will be called again (recursively!).
1231 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001232
Elliott Hughesd23736e2012-11-01 15:16:56 -07001233 if (!(flags & FLAG_EXE) && preinit_array) {
1234 DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array);
1235 return;
1236 }
1237
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001238 if (dynamic != NULL) {
1239 for (Elf32_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) {
1240 if (d->d_tag == DT_NEEDED) {
1241 const char* library_name = strtab + d->d_un.d_val;
1242 soinfo* lsi = find_loaded_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001243 if (lsi == NULL) {
1244 DL_ERR("\"%s\": could not initialize dependent library", name);
1245 } else {
1246 lsi->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001247 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001248 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001249 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001250 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001251
Elliott Hughesd23736e2012-11-01 15:16:56 -07001252 CallFunction("DT_INIT", init_func);
1253 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001254}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001255
Elliott Hughesd23736e2012-11-01 15:16:56 -07001256void soinfo::CallDestructors() {
1257 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
1258 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001259}
1260
1261/* Force any of the closed stdin, stdout and stderr to be associated with
1262 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001263static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001264 int dev_null, i, status;
1265 int return_value = 0;
1266
David 'Digit' Turner16084162012-06-12 16:25:37 +02001267 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001268 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001269 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001270 return -1;
1271 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001272 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001273
1274 /* If any of the stdio file descriptors is valid and not associated
1275 with /dev/null, dup /dev/null to it. */
1276 for (i = 0; i < 3; i++) {
1277 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001278 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001279 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001280 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001281
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001282 TRACE("[ Nullifying stdio file descriptor %d]", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001283 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001284
Elliott Hughes46882792012-08-03 16:49:39 -07001285 /* If file is opened, we are good. */
1286 if (status != -1) {
1287 continue;
1288 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001289
1290 /* The only error we allow is that the file descriptor does not
1291 exist, in which case we dup /dev/null to it. */
1292 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001293 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001294 return_value = -1;
1295 continue;
1296 }
1297
1298 /* Try dupping /dev/null to this stdio file descriptor and
1299 repeat if there is a signal. Note that any errors in closing
1300 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001301 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001302 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001303 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001304 return_value = -1;
1305 continue;
1306 }
1307 }
1308
1309 /* If /dev/null is not one of the stdio file descriptors, close it. */
1310 if (dev_null > 2) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001311 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001312 status = TEMP_FAILURE_RETRY(close(dev_null));
1313 if (status == -1) {
1314 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001315 return_value = -1;
1316 }
1317 }
1318
1319 return return_value;
1320}
1321
Elliott Hughes124fae92012-10-31 14:20:03 -07001322static bool soinfo_link_image(soinfo* si) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001323 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001324 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001325 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001326 int phnum = si->phnum;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001327 bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001328
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001329 /* We can't debug anything until the linker is relocated */
1330 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001331 INFO("[ linking %s ]", si->name);
1332 DEBUG("si->base = 0x%08x si->flags = 0x%08x", si->base, si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001333 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001334
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001335 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001336 size_t dynamic_count;
Chris Dearmancf239052013-01-11 15:32:20 -08001337 Elf32_Word dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001338 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001339 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001340 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001341 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001342 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001343 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001344 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001345 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001346 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001347 DEBUG("dynamic = %p", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001348 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001349 }
1350
1351#ifdef ANDROID_ARM_LINKER
1352 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1353 &si->ARM_exidx, &si->ARM_exidx_count);
1354#endif
1355
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001356 /* extract useful information from dynamic section */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001357 uint32_t needed_count = 0;
1358 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001359 DEBUG("d = %p, d[0](tag) = 0x%08x d[1](val) = 0x%08x", d, d->d_tag, d->d_un.d_val);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001360 switch(d->d_tag){
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001361 case DT_HASH:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001362 si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0];
1363 si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1];
1364 si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8);
1365 si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001366 break;
1367 case DT_STRTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001368 si->strtab = (const char *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001369 break;
1370 case DT_SYMTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001371 si->symtab = (Elf32_Sym *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001372 break;
1373 case DT_PLTREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001374 if (d->d_un.d_val != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001375 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1376 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001377 }
1378 break;
1379 case DT_JMPREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001380 si->plt_rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001381 break;
1382 case DT_PLTRELSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001383 si->plt_rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001384 break;
1385 case DT_REL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001386 si->rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001387 break;
1388 case DT_RELSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001389 si->rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001390 break;
1391 case DT_PLTGOT:
1392 /* Save this in case we decide to do lazy binding. We don't yet. */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001393 si->plt_got = (unsigned *)(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001394 break;
1395 case DT_DEBUG:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001396 // Set the DT_DEBUG entry to the address of _r_debug for GDB
Chris Dearmancf239052013-01-11 15:32:20 -08001397 // if the dynamic table is writable
Elliott Hughes99c32052013-01-14 09:56:21 -08001398 if ((dynamic_flags & PF_W) != 0) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001399 d->d_un.d_val = (int) &_r_debug;
Elliott Hughes99c32052013-01-14 09:56:21 -08001400 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001401 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001402 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001403 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1404 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001405 case DT_INIT:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001406 si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
1407 DEBUG("%s constructors (init func) found at %p", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001408 break;
1409 case DT_FINI:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001410 si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
1411 DEBUG("%s destructors (fini func) found at %p", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001412 break;
1413 case DT_INIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001414 si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1415 DEBUG("%s constructors (init_array) found at %p", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001416 break;
1417 case DT_INIT_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001418 si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001419 break;
1420 case DT_FINI_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001421 si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1422 DEBUG("%s destructors (fini_array) found at %p", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001423 break;
1424 case DT_FINI_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001425 si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001426 break;
1427 case DT_PREINIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001428 si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1429 DEBUG("%s constructors (preinit_array) found at %p", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001430 break;
1431 case DT_PREINIT_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001432 si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001433 break;
1434 case DT_TEXTREL:
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001435 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001436 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001437 case DT_SYMBOLIC:
1438 si->has_DT_SYMBOLIC = true;
1439 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001440 case DT_NEEDED:
1441 ++needed_count;
1442 break;
1443#if defined DT_FLAGS
1444 // TODO: why is DT_FLAGS not defined?
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001445 case DT_FLAGS:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001446 if (d->d_un.d_val & DF_TEXTREL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001447 si->has_text_relocations = true;
1448 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001449 if (d->d_un.d_val & DF_SYMBOLIC) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001450 si->has_DT_SYMBOLIC = true;
1451 }
1452 break;
1453#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001454#if defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001455 case DT_STRSZ:
1456 case DT_SYMENT:
1457 case DT_RELENT:
1458 break;
1459 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001460 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001461 {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001462 r_debug** dp = (r_debug**) d->d_un.d_ptr;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001463 *dp = &_r_debug;
1464 }
1465 break;
1466 case DT_MIPS_RLD_VERSION:
1467 case DT_MIPS_FLAGS:
1468 case DT_MIPS_BASE_ADDRESS:
1469 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001470 break;
1471
1472 case DT_MIPS_SYMTABNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001473 si->mips_symtabno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001474 break;
1475
1476 case DT_MIPS_LOCAL_GOTNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001477 si->mips_local_gotno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001478 break;
1479
1480 case DT_MIPS_GOTSYM:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001481 si->mips_gotsym = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001482 break;
1483
1484 default:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001485 DEBUG("Unused DT entry: type 0x%08x arg 0x%08x", d->d_tag, d->d_un.d_val);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001486 break;
1487#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001488 }
1489 }
1490
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001491 DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001492 si->base, si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001493
Elliott Hughes124fae92012-10-31 14:20:03 -07001494 // Sanity checks.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001495 if (relocating_linker && needed_count != 0) {
1496 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1497 return false;
1498 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001499 if (si->nbucket == 0) {
1500 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1501 return false;
1502 }
1503 if (si->strtab == 0) {
1504 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1505 return false;
1506 }
1507 if (si->symtab == 0) {
1508 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1509 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001510 }
1511
Matt Fischer4fd42c12009-12-31 12:09:10 -06001512 /* if this is the main executable, then load all of the preloads now */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001513 if (si->flags & FLAG_EXE) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001514 memset(gLdPreloads, 0, sizeof(gLdPreloads));
Elliott Hughesd23736e2012-11-01 15:16:56 -07001515 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1516 soinfo* lsi = find_library(gLdPreloadNames[i]);
1517 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001518 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001519 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Elliott Hughes124fae92012-10-31 14:20:03 -07001520 gLdPreloadNames[i], si->name, tmp_err_buf);
1521 return false;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001522 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001523 gLdPreloads[i] = lsi;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001524 }
1525 }
1526
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001527 soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*));
1528 soinfo** pneeded = needed;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001529
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001530 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1531 if (d->d_tag == DT_NEEDED) {
1532 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001533 DEBUG("%s needs %s", si->name, library_name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001534 soinfo* lsi = find_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001535 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001536 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001537 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001538 library_name, si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001539 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001540 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001541 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001542 }
1543 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001544 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001545
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001546 if (si->has_text_relocations) {
1547 /* Unprotect the segments, i.e. make them writable, to allow
1548 * text relocations to work properly. We will later call
1549 * phdr_table_protect_segments() after all of them are applied
1550 * and all constructors are run.
1551 */
1552 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1553 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1554 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001555 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001556 }
1557 }
1558
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001559 if (si->plt_rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001560 DEBUG("[ relocating %s plt ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001561 if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001562 return false;
1563 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001564 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001565 if (si->rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001566 DEBUG("[ relocating %s ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001567 if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001568 return false;
1569 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001570 }
1571
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001572#ifdef ANDROID_MIPS_LINKER
Elliott Hughes124fae92012-10-31 14:20:03 -07001573 if (mips_relocate_got(si, needed)) {
1574 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001575 }
1576#endif
1577
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001578 si->flags |= FLAG_LINKED;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001579 DEBUG("[ finished linking %s ]", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001580
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001581 if (si->has_text_relocations) {
1582 /* All relocations are done, we can protect our segments back to
1583 * read-only. */
1584 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1585 DL_ERR("can't protect segments for \"%s\": %s",
1586 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001587 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001588 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001589 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001590
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001591 /* We can also turn on GNU RELRO protection */
1592 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001593 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1594 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001595 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001596 }
1597
Elliott Hughes124fae92012-10-31 14:20:03 -07001598 // If this is a setuid/setgid program, close the security hole described in
1599 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
Elliott Hughes18a206c2012-10-29 17:37:13 -07001600 if (get_AT_SECURE()) {
Elliott Hughes46882792012-08-03 16:49:39 -07001601 nullify_closed_stdio();
1602 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001603 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001604 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001605}
1606
Nick Kralevich468319c2011-11-11 15:53:17 -08001607/*
1608 * This code is called after the linker has linked itself and
1609 * fixed it's own GOT. It is safe to make references to externs
1610 * and other non-local data at this point.
1611 */
Kito Chengfa8c05d2013-03-12 14:58:06 +08001612static Elf32_Addr __linker_init_post_relocation(KernelArgumentBlock& args, Elf32_Addr linker_base) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001613 /* NOTE: we store the args pointer on a special location
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001614 * of the temporary TLS area in order to pass it to
1615 * the C Library's runtime initializer.
1616 *
1617 * The initializer must clear the slot and reset the TLS
1618 * to point to a different location to ensure that no other
1619 * shared library constructor can access it.
1620 */
Elliott Hughesd3920b32013-02-07 18:39:34 -08001621 __libc_init_tls(args);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001622
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001623#if TIMING
1624 struct timeval t0, t1;
1625 gettimeofday(&t0, 0);
1626#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001627
Elliott Hughes18a206c2012-10-29 17:37:13 -07001628 // Initialize environment functions, and get to the ELF aux vectors table.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001629 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001630
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001631 debuggerd_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001632
Elliott Hughes18a206c2012-10-29 17:37:13 -07001633 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001634 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1635 if (LD_DEBUG != NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001636 gLdDebugVerbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001637 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001638
Elliott Hughes18a206c2012-10-29 17:37:13 -07001639 // Normally, these are cleaned by linker_env_init, but the test
1640 // doesn't cost us anything.
1641 const char* ldpath_env = NULL;
1642 const char* ldpreload_env = NULL;
1643 if (!get_AT_SECURE()) {
1644 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1645 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001646 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001647
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001648 INFO("[ android linker & debugger ]");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001649
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001650 soinfo* si = soinfo_alloc(args.argv[0]);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001651 if (si == NULL) {
1652 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001653 }
1654
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001655 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001656 si->flags |= FLAG_EXE;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001657 link_map_t* map = &(si->link_map);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001658
1659 map->l_addr = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001660 map->l_name = args.argv[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001661 map->l_prev = NULL;
1662 map->l_next = NULL;
1663
1664 _r_debug.r_map = map;
1665 r_debug_tail = map;
1666
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001667 /* gdb expects the linker to be in the debug shared object list.
1668 * Without this, gdb has trouble locating the linker's ".text"
1669 * and ".plt" sections. Gdb could also potentially use this to
1670 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1671 * Don't use soinfo_alloc(), because the linker shouldn't
1672 * be on the soinfo list.
Ben Cheng06f0e742012-08-10 16:07:02 -07001673 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001674 {
1675 static soinfo linker_soinfo;
1676 strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
1677 linker_soinfo.flags = 0;
1678 linker_soinfo.base = linker_base;
1679
1680 /*
1681 * Set the dynamic field in the link map otherwise gdb will complain with
1682 * the following:
1683 * warning: .dynamic section for "/system/bin/linker" is not at the
1684 * expected address (wrong library or version mismatch?)
1685 */
1686 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
1687 Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
1688 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1689 &linker_soinfo.dynamic, NULL, NULL);
1690 insert_soinfo_into_debug_map(&linker_soinfo);
1691 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001692
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001693 // Extract information passed from the kernel.
1694 si->phdr = reinterpret_cast<Elf32_Phdr*>(args.getauxval(AT_PHDR));
1695 si->phnum = args.getauxval(AT_PHNUM);
1696 si->entry = args.getauxval(AT_ENTRY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001697
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001698 /* Compute the value of si->base. We can't rely on the fact that
1699 * the first entry is the PHDR because this will not be true
1700 * for certain executables (e.g. some in the NDK unit test suite)
1701 */
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001702 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001703 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001704 si->load_bias = 0;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001705 for (size_t i = 0; i < si->phnum; ++i) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001706 if (si->phdr[i].p_type == PT_PHDR) {
1707 si->load_bias = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_vaddr;
1708 si->base = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_offset;
1709 break;
1710 }
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001711 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001712 si->dynamic = NULL;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001713 si->ref_count = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001714
Elliott Hughes46882792012-08-03 16:49:39 -07001715 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1716 parse_LD_LIBRARY_PATH(ldpath_env);
1717 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001718
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001719 somain = si;
1720
Elliott Hughes124fae92012-10-31 14:20:03 -07001721 if (!soinfo_link_image(si)) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001722 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
Elliott Hughes18a206c2012-10-29 17:37:13 -07001723 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001724 }
1725
Elliott Hughesd23736e2012-11-01 15:16:56 -07001726 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001727
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001728 for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
1729 gLdPreloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08001730 }
1731
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001732 /* After the link_image, the si->load_bias is initialized.
1733 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1734 * We need to update this value for so exe here. So Unwind_Backtrace
1735 * for some arch like x86 could work correctly within so exe.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08001736 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08001737 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001738 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001739
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001740#if TIMING
1741 gettimeofday(&t1,NULL);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001742 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001743 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1744 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1745 ));
1746#endif
1747#if STATS
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001748 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07001749 linker_stats.count[kRelocAbsolute],
1750 linker_stats.count[kRelocRelative],
1751 linker_stats.count[kRelocCopy],
1752 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001753#endif
1754#if COUNT_PAGES
1755 {
1756 unsigned n;
1757 unsigned i;
1758 unsigned count = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001759 for (n = 0; n < 4096; n++) {
1760 if (bitmask[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001761 unsigned x = bitmask[n];
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001762 for (i = 0; i < 8; i++) {
1763 if (x & 1) {
1764 count++;
1765 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001766 x >>= 1;
1767 }
1768 }
1769 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001770 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001771 }
1772#endif
1773
1774#if TIMING || STATS || COUNT_PAGES
1775 fflush(stdout);
1776#endif
1777
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001778 TRACE("[ Ready to execute '%s' @ 0x%08x ]", si->name, si->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001779 return si->entry;
1780}
Nick Kralevich468319c2011-11-11 15:53:17 -08001781
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001782/* Compute the load-bias of an existing executable. This shall only
1783 * be used to compute the load bias of an executable or shared library
1784 * that was loaded by the kernel itself.
1785 *
1786 * Input:
1787 * elf -> address of ELF header, assumed to be at the start of the file.
1788 * Return:
1789 * load bias, i.e. add the value of any p_vaddr in the file to get
1790 * the corresponding address in memory.
1791 */
Kito Chengfa8c05d2013-03-12 14:58:06 +08001792static Elf32_Addr get_elf_exec_load_bias(const Elf32_Ehdr* elf) {
1793 Elf32_Addr offset = elf->e_phoff;
1794 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
1795 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001796
Kito Chengfa8c05d2013-03-12 14:58:06 +08001797 for (const Elf32_Phdr* phdr = phdr_table; phdr < phdr_end; phdr++) {
1798 if (phdr->p_type == PT_LOAD) {
1799 return reinterpret_cast<Elf32_Addr>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001800 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001801 }
1802 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001803}
1804
Nick Kralevich468319c2011-11-11 15:53:17 -08001805/*
1806 * This is the entry point for the linker, called from begin.S. This
1807 * method is responsible for fixing the linker's own relocations, and
1808 * then calling __linker_init_post_relocation().
1809 *
1810 * Because this method is called before the linker has fixed it's own
1811 * relocations, any attempt to reference an extern variable, extern
1812 * function, or other GOT reference will generate a segfault.
1813 */
Kito Chengfa8c05d2013-03-12 14:58:06 +08001814extern "C" Elf32_Addr __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001815 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08001816
Kito Chengfa8c05d2013-03-12 14:58:06 +08001817 Elf32_Addr linker_addr = args.getauxval(AT_BASE);
Nick Kralevich468319c2011-11-11 15:53:17 -08001818
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001819 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr*) linker_addr;
1820 Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08001821
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001822 soinfo linker_so;
1823 memset(&linker_so, 0, sizeof(soinfo));
Nick Kralevich468319c2011-11-11 15:53:17 -08001824
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001825 linker_so.base = linker_addr;
1826 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
1827 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001828 linker_so.dynamic = NULL;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001829 linker_so.phdr = phdr;
1830 linker_so.phnum = elf_hdr->e_phnum;
1831 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07001832
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001833 if (!soinfo_link_image(&linker_so)) {
1834 // It would be nice to print an error message, but if the linker
1835 // can't link itself, there's no guarantee that we'll be able to
1836 // call write() (because it involves a GOT reference).
1837 //
1838 // This situation should never occur unless the linker itself
1839 // is corrupt.
1840 exit(EXIT_FAILURE);
1841 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001842
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001843 // We have successfully fixed our own relocations. It's safe to run
1844 // the main part of the linker now.
Kito Chengfa8c05d2013-03-12 14:58:06 +08001845 Elf32_Addr start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001846
1847 set_soinfo_pool_protection(PROT_READ);
1848
1849 // Return the address that the calling assembly stub should jump to.
1850 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08001851}