blob: e08eaba43f58d93fe838d3cc4fb67597680932ce [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 Hughes1e980b62013-01-17 18:36:06 -080044#include <private/debug_format.h>
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080045#include <private/KernelArgumentBlock.h>
Elliott Hughes46882792012-08-03 16:49:39 -070046#include <private/logd.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070047#include <private/ScopedPthreadMutexLocker.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048
49#include "linker.h"
50#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010051#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020052#include "linker_phdr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
David Bartleybc3a5c22009-06-02 18:27:28 -070054/* Assume average path length of 64 and max 8 paths */
55#define LDPATH_BUFSIZE 512
56#define LDPATH_MAX 8
57
Matt Fischer4fd42c12009-12-31 12:09:10 -060058#define LDPRELOAD_BUFSIZE 512
59#define LDPRELOAD_MAX 8
60
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
62 *
63 * Do NOT use malloc() and friends or pthread_*() code here.
64 * Don't use printf() either; it's caused mysterious memory
65 * corruption in the past.
66 * The linker runs before we bring up libc and it's easiest
67 * to make sure it does not depend on any complex libc features
68 *
69 * open issues / todo:
70 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071 * - are we doing everything we should for ARM_COPY relocations?
72 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073 * - after linking, set as much stuff as possible to READONLY
74 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070075 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076
Elliott Hughes124fae92012-10-31 14:20:03 -070077static bool soinfo_link_image(soinfo* si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
Magnus Malmbornba98d922012-09-12 13:00:55 +020079// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
80// maps, each a single page in size. The pages are broken up into as many struct soinfo
81// objects as will fit, and they're all threaded together on a free list.
82#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
83struct soinfo_pool_t {
84 soinfo_pool_t* next;
85 soinfo info[SOINFO_PER_POOL];
86};
87static struct soinfo_pool_t* gSoInfoPools = NULL;
88static soinfo* gSoInfoFreeList = NULL;
89
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080090static soinfo* solist = &libdl_info;
91static soinfo* sonext = &libdl_info;
92static soinfo* somain; /* main process, always the one after libdl_info */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
Elliott Hughes124fae92012-10-31 14:20:03 -070094static const char* const gSoPaths[] = {
95 "/vendor/lib",
96 "/system/lib",
97 NULL
98};
David Bartleybc3a5c22009-06-02 18:27:28 -070099
Elliott Hughes124fae92012-10-31 14:20:03 -0700100static char gLdPathsBuffer[LDPATH_BUFSIZE];
101static const char* gLdPaths[LDPATH_MAX + 1];
102
103static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
104static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600105
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800106static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600107
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700108static int debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109
Elliott Hughesbedfe382012-08-14 14:07:59 -0700110enum RelocationKind {
111 kRelocAbsolute = 0,
112 kRelocRelative,
113 kRelocCopy,
114 kRelocSymbol,
115 kRelocMax
116};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100117
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700119struct linker_stats_t {
120 int count[kRelocMax];
121};
122
123static linker_stats_t linker_stats;
124
125static void count_relocation(RelocationKind kind) {
126 ++linker_stats.count[kind];
127}
128#else
129static void count_relocation(RelocationKind) {
130}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131#endif
132
133#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700134static unsigned bitmask[4096];
135#define MARK(offset) \
136 do { \
137 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
138 } while(0)
139#else
140#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141#endif
142
Elliott Hughes46882792012-08-03 16:49:39 -0700143// You shouldn't try to call memory-allocating functions in the dynamic linker.
144// Guard against the most obvious ones.
145#define DISALLOW_ALLOCATION(return_type, name, ...) \
146 return_type name __VA_ARGS__ \
147 { \
148 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
149 __libc_android_log_write(ANDROID_LOG_FATAL, "linker", msg); \
Chris Dearman20a24402012-10-31 05:39:27 -0700150 write(2, msg, strlen(msg)); \
Elliott Hughes46882792012-08-03 16:49:39 -0700151 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700152 }
Elliott Hughes46882792012-08-03 16:49:39 -0700153#define UNUSED __attribute__((unused))
154DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
155DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
156DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
157DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
Dima Zavin2e855792009-05-20 18:28:09 -0700158
Dima Zavin03531952009-05-29 17:30:25 -0700159static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700160static char __linker_dl_err_buf[768];
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700161#define DL_ERR(fmt, x...) \
162 do { \
Elliott Hughes1e980b62013-01-17 18:36:06 -0800163 __libc_format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), fmt, ##x); \
Elliott Hughes9c94fc92012-11-05 09:11:43 -0800164 /* If LD_DEBUG is set high enough, send every dlerror(3) message to the log. */ \
165 DEBUG(fmt "\n", ##x); \
Dima Zavin2e855792009-05-20 18:28:09 -0700166 } while(0)
167
Elliott Hughes5419b942012-10-16 15:54:46 -0700168const char* linker_get_error() {
169 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700170}
171
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172/*
173 * This function is an empty stub where GDB locates a breakpoint to get notified
174 * about linker activity.
175 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700176extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800177
Elliott Hughesbedfe382012-08-14 14:07:59 -0700178static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179 RT_CONSISTENT, 0};
Elliott Hughesbedfe382012-08-14 14:07:59 -0700180static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181
Elliott Hughes3b297c42012-10-11 16:08:51 -0700182static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183
Elliott Hughesbedfe382012-08-14 14:07:59 -0700184static void insert_soinfo_into_debug_map(soinfo * info) {
185 // Copy the necessary fields into the debug structure.
186 link_map* map = &(info->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800187 map->l_addr = info->base;
188 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800189 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190
191 /* Stick the new library at the end of the list.
192 * gdb tends to care more about libc than it does
193 * about leaf libraries, and ordering it this way
194 * reduces the back-and-forth over the wire.
195 */
196 if (r_debug_tail) {
197 r_debug_tail->l_next = map;
198 map->l_prev = r_debug_tail;
199 map->l_next = 0;
200 } else {
201 _r_debug.r_map = map;
202 map->l_prev = 0;
203 map->l_next = 0;
204 }
205 r_debug_tail = map;
206}
207
Elliott Hughesbedfe382012-08-14 14:07:59 -0700208static void remove_soinfo_from_debug_map(soinfo* info) {
209 link_map* map = &(info->linkmap);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700210
Elliott Hughesbedfe382012-08-14 14:07:59 -0700211 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700212 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700213 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700214
Elliott Hughesbedfe382012-08-14 14:07:59 -0700215 if (map->l_prev) {
216 map->l_prev->l_next = map->l_next;
217 }
218 if (map->l_next) {
219 map->l_next->l_prev = map->l_prev;
220 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700221}
222
Elliott Hughesbedfe382012-08-14 14:07:59 -0700223static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224 if (info->flags & FLAG_EXE) {
225 // GDB already knows about the main executable
226 return;
227 }
228
Elliott Hughes3b297c42012-10-11 16:08:51 -0700229 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230
231 _r_debug.r_state = RT_ADD;
232 rtld_db_dlactivity();
233
234 insert_soinfo_into_debug_map(info);
235
236 _r_debug.r_state = RT_CONSISTENT;
237 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700238}
239
Elliott Hughesbedfe382012-08-14 14:07:59 -0700240static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700241 if (info->flags & FLAG_EXE) {
242 // GDB already knows about the main executable
243 return;
244 }
245
Elliott Hughes3b297c42012-10-11 16:08:51 -0700246 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700247
248 _r_debug.r_state = RT_DELETE;
249 rtld_db_dlactivity();
250
251 remove_soinfo_from_debug_map(info);
252
253 _r_debug.r_state = RT_CONSISTENT;
254 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255}
256
Elliott Hughes18a206c2012-10-29 17:37:13 -0700257void notify_gdb_of_libraries() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800258 _r_debug.r_state = RT_ADD;
259 rtld_db_dlactivity();
260 _r_debug.r_state = RT_CONSISTENT;
261 rtld_db_dlactivity();
262}
263
Magnus Malmbornba98d922012-09-12 13:00:55 +0200264static bool ensure_free_list_non_empty() {
265 if (gSoInfoFreeList != NULL) {
266 return true;
267 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800268
Magnus Malmbornba98d922012-09-12 13:00:55 +0200269 // Allocate a new pool.
270 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
271 PROT_READ|PROT_WRITE,
272 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
273 if (pool == MAP_FAILED) {
274 return false;
275 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800276
Magnus Malmbornba98d922012-09-12 13:00:55 +0200277 // Add the pool to our list of pools.
278 pool->next = gSoInfoPools;
279 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800280
Magnus Malmbornba98d922012-09-12 13:00:55 +0200281 // Chain the entries in the new pool onto the free list.
282 gSoInfoFreeList = &pool->info[0];
283 soinfo* next = NULL;
284 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
285 pool->info[i].next = next;
286 next = &pool->info[i];
287 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800288
Magnus Malmbornba98d922012-09-12 13:00:55 +0200289 return true;
290}
291
Elliott Hughesd23736e2012-11-01 15:16:56 -0700292static void set_soinfo_pool_protection(int protection) {
293 for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
294 if (mprotect(p, sizeof(*p), protection) == -1) {
295 abort(); // Can't happen.
296 }
297 }
298}
299
Magnus Malmbornba98d922012-09-12 13:00:55 +0200300static soinfo* soinfo_alloc(const char* name) {
301 if (strlen(name) >= SOINFO_NAME_LEN) {
302 DL_ERR("library name \"%s\" too long", name);
303 return NULL;
304 }
305
306 if (!ensure_free_list_non_empty()) {
307 DL_ERR("out of memory when loading \"%s\"", name);
308 return NULL;
309 }
310
311 // Take the head element off the free list.
312 soinfo* si = gSoInfoFreeList;
313 gSoInfoFreeList = gSoInfoFreeList->next;
314
315 // Initialize the new element.
316 memset(si, 0, sizeof(soinfo));
317 strlcpy(si->name, name, sizeof(si->name));
318 sonext->next = si;
319 sonext = si;
320
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700321 TRACE("name %s: allocated soinfo @ %p\n", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200322 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800323}
324
Elliott Hughes46882792012-08-03 16:49:39 -0700325static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800326{
Elliott Hughes46882792012-08-03 16:49:39 -0700327 if (si == NULL) {
328 return;
329 }
330
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800331 soinfo *prev = NULL, *trav;
332
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700333 TRACE("name %s: freeing soinfo @ %p\n", si->name, si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800335 for (trav = solist; trav != NULL; trav = trav->next) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336 if (trav == si)
337 break;
338 prev = trav;
339 }
340 if (trav == NULL) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800341 /* si was not in solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700342 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800343 return;
344 }
345
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100346 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800347 always the static libdl_info.
348 */
349 prev->next = si->next;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800350 if (si == sonext) {
351 sonext = prev;
352 }
Magnus Malmbornba98d922012-09-12 13:00:55 +0200353 si->next = gSoInfoFreeList;
354 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355}
356
Elliott Hughescade4c32012-12-20 14:42:14 -0800357
358static void parse_path(const char* path, const char* delimiters,
359 const char** array, char* buf, size_t buf_size, size_t max_count) {
360 if (path == NULL) {
361 return;
362 }
363
364 size_t len = strlcpy(buf, path, buf_size);
365
366 size_t i = 0;
367 char* buf_p = buf;
368 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
369 if (*array[i] != '\0') {
370 ++i;
371 }
372 }
373
374 // Forget the last path if we had to truncate; this occurs if the 2nd to
375 // last char isn't '\0' (i.e. wasn't originally a delimiter).
376 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
377 array[i - 1] = NULL;
378 } else {
379 array[i] = NULL;
380 }
381}
382
383static void parse_LD_LIBRARY_PATH(const char* path) {
384 parse_path(path, ":", gLdPaths,
385 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
386}
387
388static void parse_LD_PRELOAD(const char* path) {
389 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
390 parse_path(path, " :", gLdPreloadNames,
391 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
392}
393
Elliott Hughes46882792012-08-03 16:49:39 -0700394#ifdef ANDROID_ARM_LINKER
395
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396/* For a given PC, find the .so that it belongs to.
397 * Returns the base address of the .ARM.exidx section
398 * for that .so, and the number of 8-byte entries
399 * in that section (via *pcount).
400 *
401 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
402 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700403 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800404 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
406{
407 soinfo *si;
408 unsigned addr = (unsigned)pc;
409
Nick Kralevich468319c2011-11-11 15:53:17 -0800410 for (si = solist; si != 0; si = si->next){
411 if ((addr >= si->base) && (addr < (si->base + si->size))) {
412 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900413 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800414 }
415 }
416 *pcount = 0;
417 return NULL;
418}
Elliott Hughes46882792012-08-03 16:49:39 -0700419
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700420#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
Elliott Hughes46882792012-08-03 16:49:39 -0700421
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800422/* Here, we only have to provide a callback to iterate across all the
423 * loaded libraries. gcc_eh does the rest. */
424int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700425dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800426 void *data)
427{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700429 for (soinfo* si = solist; si != NULL; si = si->next) {
430 dl_phdr_info dl_info;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800431 dl_info.dlpi_addr = si->linkmap.l_addr;
432 dl_info.dlpi_name = si->linkmap.l_name;
433 dl_info.dlpi_phdr = si->phdr;
434 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700435 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
436 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800437 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700438 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800439 }
440 return rv;
441}
Elliott Hughes46882792012-08-03 16:49:39 -0700442
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800443#endif
444
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800445static Elf32_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
446 Elf32_Sym* s;
447 Elf32_Sym* symtab = si->symtab;
448 const char* strtab = si->strtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800449 unsigned n;
450
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700451 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800452 name, si->name, si->base, hash, hash % si->nbucket);
453 n = hash % si->nbucket;
454
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800455 for (n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800456 s = symtab + n;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800457 if (strcmp(strtab + s->st_name, name)) continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800458
Doug Kwane8238072009-10-26 12:05:23 -0700459 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800460 switch(ELF32_ST_BIND(s->st_info)){
461 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700462 case STB_WEAK:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800463 if (s->st_shndx == SHN_UNDEF) {
Robin Burchell439fa8e2012-07-05 09:21:07 +0200464 continue;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800465 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800466
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700467 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800468 name, si->name, s->st_value, s->st_size);
469 return s;
470 }
471 }
472
Doug Kwan94304352009-10-23 18:11:40 -0700473 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800474}
475
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800476static unsigned elfhash(const char* _name) {
477 const unsigned char* name = (const unsigned char*) _name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800478 unsigned h = 0, g;
479
480 while(*name) {
481 h = (h << 4) + *name++;
482 g = h & 0xf0000000;
483 h ^= g;
484 h ^= g >> 24;
485 }
486 return h;
487}
488
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800489static Elf32_Sym* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
Doug Kwan94304352009-10-23 18:11:40 -0700490 unsigned elf_hash = elfhash(name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800491 Elf32_Sym* s = NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700492
Pavel Chupinc77c4342012-10-31 13:55:51 +0400493 if (si != NULL && somain != NULL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200494
495 /*
Pavel Chupinc77c4342012-10-31 13:55:51 +0400496 * Local scope is executable scope. Just start looking into it right away
497 * for the shortcut.
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200498 */
499
Pavel Chupinc77c4342012-10-31 13:55:51 +0400500 if (si == somain) {
501 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200502 if (s != NULL) {
Pavel Chupinc77c4342012-10-31 13:55:51 +0400503 *lsi = si;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200504 goto done;
505 }
Pavel Chupinc77c4342012-10-31 13:55:51 +0400506 } else {
507 /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200508
Pavel Chupinc77c4342012-10-31 13:55:51 +0400509 /*
510 * If this object was built with symbolic relocations disabled, the
511 * first place to look to resolve external references is the main
512 * executable.
513 */
Nick Kralevich468319c2011-11-11 15:53:17 -0800514
Pavel Chupinc77c4342012-10-31 13:55:51 +0400515 if (!si->has_DT_SYMBOLIC) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700516 DEBUG("%s: looking up %s in executable %s\n",
517 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400518 s = soinfo_elf_lookup(somain, elf_hash, name);
519 if (s != NULL) {
520 *lsi = somain;
521 goto done;
522 }
523 }
524
525 /* Look for symbols in the local scope (the object who is
526 * searching). This happens with C++ templates on i386 for some
527 * reason.
528 *
529 * Notes on weak symbols:
530 * The ELF specs are ambiguous about treatment of weak definitions in
531 * dynamic linking. Some systems return the first definition found
532 * and some the first non-weak definition. This is system dependent.
533 * Here we return the first definition found for simplicity. */
534
535 s = soinfo_elf_lookup(si, elf_hash, name);
536 if (s != NULL) {
537 *lsi = si;
538 goto done;
539 }
540
541 /*
542 * If this object was built with -Bsymbolic and symbol is not found
543 * in the local scope, try to find the symbol in the main executable.
544 */
545
546 if (si->has_DT_SYMBOLIC) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700547 DEBUG("%s: looking up %s in executable %s after local scope\n",
548 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400549 s = soinfo_elf_lookup(somain, elf_hash, name);
550 if (s != NULL) {
551 *lsi = somain;
552 goto done;
553 }
554 }
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200555 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700556 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700557
Matt Fischer4fd42c12009-12-31 12:09:10 -0600558 /* Next, look for it in the preloads list */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800559 for (int i = 0; gLdPreloads[i] != NULL; i++) {
560 s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
561 if (s != NULL) {
562 *lsi = gLdPreloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600563 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200564 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600565 }
566
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800567 for (int i = 0; needed[i] != NULL; i++) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700568 DEBUG("%s: looking up %s in %s\n",
569 si->name, name, needed[i]->name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200570 s = soinfo_elf_lookup(needed[i], elf_hash, name);
571 if (s != NULL) {
572 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200573 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200574 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700575 }
576
577done:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800578 if (s != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700579 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = 0x%08x, "
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200580 "found in %s, base = 0x%08x, load bias = 0x%08x\n",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700581 si->name, name, s->st_value,
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200582 (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700583 return s;
584 }
585
Doug Kwan94304352009-10-23 18:11:40 -0700586 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700587}
588
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800589/* This is used by dlsym(3). It performs symbol lookup only within the
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700590 specified soinfo object and not in any of its dependencies.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800591
592 TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
593 that it should do a breadth first search through the dependency
594 tree. This agrees with the ELF spec (aka System V Application
595 Binary Interface) where in Chapter 5 it discuss resolving "Shared
596 Object Dependencies" in breadth first search order.
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700597 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800598Elf32_Sym* dlsym_handle_lookup(soinfo* si, const char* name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800599{
David 'Digit' Turner16084162012-06-12 16:25:37 +0200600 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800601}
602
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800603/* This is used by dlsym(3) to performs a global symbol lookup. If the
604 start value is null (for RTLD_DEFAULT), the search starts at the
605 beginning of the global solist. Otherwise the search starts at the
606 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700607 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800608Elf32_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800609 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800610
Elliott Hughescade4c32012-12-20 14:42:14 -0800611 if (start == NULL) {
612 start = solist;
613 }
614
615 Elf32_Sym* s = NULL;
616 for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
617 s = soinfo_elf_lookup(si, elf_hash, name);
618 if (s != NULL) {
619 *found = si;
620 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600621 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800622 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600623
Elliott Hughescade4c32012-12-20 14:42:14 -0800624 if (s != NULL) {
625 TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, found->base = 0x%08x\n",
626 name, s->st_value, (*found)->base);
627 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800628
Elliott Hughescade4c32012-12-20 14:42:14 -0800629 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630}
631
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800632soinfo* find_containing_library(const void* addr) {
633 for (soinfo* si = solist; si != NULL; si = si->next) {
634 if ((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600635 return si;
636 }
637 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600638 return NULL;
639}
640
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800641Elf32_Sym* dladdr_find_symbol(soinfo* si, const void* addr) {
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600642 unsigned int i;
643 unsigned soaddr = (unsigned)addr - si->base;
644
645 /* Search the library's symbol table for any defined symbol which
646 * contains this address */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800647 for (i=0; i<si->nchain; i++) {
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600648 Elf32_Sym *sym = &si->symtab[i];
649
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800650 if (sym->st_shndx != SHN_UNDEF &&
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600651 soaddr >= sym->st_value &&
652 soaddr < sym->st_value + sym->st_size) {
653 return sym;
654 }
655 }
656
657 return NULL;
658}
659
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800660#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800661static void dump(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800662{
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800663 Elf32_Sym* s = si->symtab;
664 for (unsigned n = 0; n < si->nchain; n++) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700665 TRACE("%04d> %08x: %02x %04x %08x %08x %s\n", n, s,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800666 s->st_info, s->st_shndx, s->st_value, s->st_size,
667 si->strtab + s->st_name);
668 s++;
669 }
670}
671#endif
672
Elliott Hughes124fae92012-10-31 14:20:03 -0700673static int open_library_on_path(const char* name, const char* const paths[]) {
674 char buf[512];
675 for (size_t i = 0; paths[i] != NULL; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800676 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700677 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughes9c94fc92012-11-05 09:11:43 -0800678 PRINT("Warning: ignoring very long library path: %s/%s\n", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700679 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800680 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700681 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
682 if (fd != -1) {
683 return fd;
684 }
685 }
686 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800687}
688
Elliott Hughes124fae92012-10-31 14:20:03 -0700689static int open_library(const char* name) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700690 TRACE("[ opening %s ]\n", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800691
Elliott Hughes124fae92012-10-31 14:20:03 -0700692 // If the name contains a slash, we should attempt to open it directly and not search the paths.
693 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700694 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
695 if (fd != -1) {
696 return fd;
697 }
698 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700699 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800700
Elliott Hughes124fae92012-10-31 14:20:03 -0700701 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
702 int fd = open_library_on_path(name, gLdPaths);
703 if (fd == -1) {
704 fd = open_library_on_path(name, gSoPaths);
705 }
706 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800707}
708
Elliott Hughes46882792012-08-03 16:49:39 -0700709// Returns 'true' if the library is prelinked or on failure so we error out
710// either way. We no longer support prelinking.
711static bool is_prelinked(int fd, const char* name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800712{
Elliott Hughes46882792012-08-03 16:49:39 -0700713 struct prelink_info_t {
714 long mmap_addr;
715 char tag[4]; // "PRE ".
716 };
717
Elliott Hughesbedfe382012-08-14 14:07:59 -0700718 off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800719 if (sz < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700720 DL_ERR("lseek failed: %s", strerror(errno));
721 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800722 }
723
Elliott Hughesbedfe382012-08-14 14:07:59 -0700724 prelink_info_t info;
Elliott Hughes8dfc0732012-07-27 15:30:51 -0700725 int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
726 if (rc != sizeof(info)) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700727 DL_ERR("could not read prelink_info_t structure for \"%s\": %s", name, strerror(errno));
Elliott Hughes46882792012-08-03 16:49:39 -0700728 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800729 }
730
Elliott Hughes46882792012-08-03 16:49:39 -0700731 if (memcmp(info.tag, "PRE ", 4) == 0) {
732 DL_ERR("prelinked libraries no longer supported: %s", name);
733 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800734 }
Elliott Hughes46882792012-08-03 16:49:39 -0700735 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800736}
737
David 'Digit' Turner16084162012-06-12 16:25:37 +0200738/* verify_elf_header
739 * Verifies the content of an ELF header.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800740 *
741 * Args:
742 *
743 * Returns:
744 * 0 on success
745 * -1 if no valid ELF object is found @ base.
746 */
747static int
David 'Digit' Turner16084162012-06-12 16:25:37 +0200748verify_elf_header(const Elf32_Ehdr* hdr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800749{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800750 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
751 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
752 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
753 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700754 if (hdr->e_type != ET_DYN) return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800755
756 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800757#ifdef ANDROID_ARM_LINKER
758 if (hdr->e_machine != EM_ARM) return -1;
759#elif defined(ANDROID_X86_LINKER)
760 if (hdr->e_machine != EM_386) return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700761#elif defined(ANDROID_MIPS_LINKER)
762 if (hdr->e_machine != EM_MIPS) return -1;
Zhenghua Wang897815a2011-10-19 00:29:14 +0800763#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800764 return 0;
765}
766
Elliott Hughes46882792012-08-03 16:49:39 -0700767struct scoped_fd {
768 ~scoped_fd() {
769 if (fd != -1) {
770 close(fd);
771 }
772 }
773 int fd;
774};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800775
Elliott Hughes46882792012-08-03 16:49:39 -0700776struct soinfo_ptr {
777 soinfo_ptr(const char* name) {
778 const char* bname = strrchr(name, '/');
779 ptr = soinfo_alloc(bname ? bname + 1 : name);
780 }
781 ~soinfo_ptr() {
782 soinfo_free(ptr);
783 }
784 soinfo* release() {
785 soinfo* result = ptr;
786 ptr = NULL;
787 return result;
788 }
789 soinfo* ptr;
790};
791
792// TODO: rewrite linker_phdr.h to use a class, then lose this.
793struct phdr_ptr {
794 phdr_ptr() : phdr_mmap(NULL) {}
795 ~phdr_ptr() {
796 if (phdr_mmap != NULL) {
797 phdr_table_unload(phdr_mmap, phdr_size);
798 }
799 }
800 void* phdr_mmap;
801 Elf32_Addr phdr_size;
802};
803
Elliott Hughes124fae92012-10-31 14:20:03 -0700804static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700805 // Open the file.
806 scoped_fd fd;
807 fd.fd = open_library(name);
808 if (fd.fd == -1) {
809 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800810 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700811 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800812
Elliott Hughes46882792012-08-03 16:49:39 -0700813 // Read the ELF header.
814 Elf32_Ehdr header[1];
815 int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200816 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700817 DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
818 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200819 }
820 if (ret != (int)sizeof(header)) {
Elliott Hughes46882792012-08-03 16:49:39 -0700821 DL_ERR("too small to be an ELF executable: %s", name);
822 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200823 }
824 if (verify_elf_header(header) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700825 DL_ERR("not a valid ELF executable: %s", name);
826 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800827 }
828
Elliott Hughes46882792012-08-03 16:49:39 -0700829 // Read the program header table.
830 const Elf32_Phdr* phdr_table;
831 phdr_ptr phdr_holder;
832 ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
833 &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200834 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700835 DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
836 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200837 }
Elliott Hughes46882792012-08-03 16:49:39 -0700838 size_t phdr_count = header->e_phnum;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200839
Elliott Hughes46882792012-08-03 16:49:39 -0700840 // Get the load extents.
841 Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700842 TRACE("[ '%s' wants sz=0x%08x ]\n", name, ext_sz);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200843 if (ext_sz == 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700844 DL_ERR("no loadable segments in file: %s", name);
845 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800846 }
847
Elliott Hughes46882792012-08-03 16:49:39 -0700848 // We no longer support pre-linked libraries.
849 if (is_prelinked(fd.fd, name)) {
850 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200851 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200852
Elliott Hughes46882792012-08-03 16:49:39 -0700853 // Reserve address space for all loadable segments.
854 void* load_start = NULL;
855 Elf32_Addr load_size = 0;
856 Elf32_Addr load_bias = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200857 ret = phdr_table_reserve_memory(phdr_table,
858 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200859 &load_start,
860 &load_size,
861 &load_bias);
862 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700863 DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
864 ext_sz, name, strerror(errno));
865 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200866 }
867
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700868 TRACE("[ allocated memory for %s @ %p (0x%08x) ]\n", name, load_start, load_size);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200869
870 /* Map all the segments in our address space with default protections */
871 ret = phdr_table_load_segments(phdr_table,
872 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200873 load_bias,
Elliott Hughes46882792012-08-03 16:49:39 -0700874 fd.fd);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200875 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700876 DL_ERR("can't map loadable segments for \"%s\": %s",
877 name, strerror(errno));
878 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200879 }
880
Elliott Hughes46882792012-08-03 16:49:39 -0700881 soinfo_ptr si(name);
882 if (si.ptr == NULL) {
883 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800884 }
885
Elliott Hughes46882792012-08-03 16:49:39 -0700886 si.ptr->base = (Elf32_Addr) load_start;
887 si.ptr->size = load_size;
888 si.ptr->load_bias = load_bias;
889 si.ptr->flags = 0;
890 si.ptr->entry = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800891 si.ptr->dynamic = NULL;
Elliott Hughes46882792012-08-03 16:49:39 -0700892 si.ptr->phnum = phdr_count;
893 si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
894 if (si.ptr->phdr == NULL) {
895 DL_ERR("can't find loaded PHDR for \"%s\"", name);
896 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200897 }
Elliott Hughes46882792012-08-03 16:49:39 -0700898
899 return si.release();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800900}
901
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200902static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800903{
904 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700905 const char *bname;
906
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200907 // TODO: don't use basename only for determining libraries
908 // http://code.google.com/p/android/issues/detail?id=6670
909
910 bname = strrchr(name, '/');
911 bname = bname ? bname + 1 : name;
912
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800913 for (si = solist; si != NULL; si = si->next) {
914 if (!strcmp(bname, si->name)) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200915 return si;
916 }
917 }
918 return NULL;
919}
920
Elliott Hughesd23736e2012-11-01 15:16:56 -0700921static soinfo* find_library_internal(const char* name) {
922 if (name == NULL) {
923 return somain;
924 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200925
Elliott Hughesd23736e2012-11-01 15:16:56 -0700926 soinfo* si = find_loaded_library(name);
927 if (si != NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700928 if (si->flags & FLAG_LINKED) {
929 return si;
930 }
931 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
932 return NULL;
933 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800934
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700935 TRACE("[ '%s' has not been loaded yet. Locating...]\n", name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700936 si = load_library(name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800937 if (si == NULL) {
938 return NULL;
939 }
940
941 // At this point we know that whatever is loaded @ base is a valid ELF
942 // shared library whose segments are properly mapped in.
943 TRACE("[ init_library base=0x%08x sz=0x%08x name='%s') ]\n",
944 si->base, si->size, si->name);
945
946 if (!soinfo_link_image(si)) {
947 munmap(reinterpret_cast<void*>(si->base), si->size);
948 soinfo_free(si);
949 return NULL;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700950 }
951
952 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800953}
954
Elliott Hughesd23736e2012-11-01 15:16:56 -0700955static soinfo* find_library(const char* name) {
956 soinfo* si = find_library_internal(name);
957 if (si != NULL) {
958 si->refcount++;
959 }
960 return si;
961}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700962
Elliott Hughesd23736e2012-11-01 15:16:56 -0700963static int soinfo_unload(soinfo* si) {
964 if (si->refcount == 1) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700965 TRACE("unloading '%s'\n", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700966 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800967
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800968 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NEEDED; ++d) {
969 if (d->d_tag == DT_NEEDED) {
970 const char* library_name = si->strtab + d->d_un.d_val;
971 soinfo* lsi = find_loaded_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700972 if (lsi != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700973 TRACE("%s needs to unload %s\n", si->name, lsi->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700974 soinfo_unload(lsi);
975 } else {
976 // TODO: should we return -1 in this case?
977 DL_ERR("\"%s\": could not unload dependent library", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800978 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700979 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800980 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700981
982 munmap(reinterpret_cast<void*>(si->base), si->size);
983 notify_gdb_of_unload(si);
984 soinfo_free(si);
985 si->refcount = 0;
986 } else {
987 si->refcount--;
Elliott Hughes9c94fc92012-11-05 09:11:43 -0800988 TRACE("not unloading '%s', decrementing refcount to %d\n", si->name, si->refcount);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700989 }
990 return 0;
991}
992
Elliott Hughescade4c32012-12-20 14:42:14 -0800993void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
994 if (!get_AT_SECURE()) {
995 parse_LD_LIBRARY_PATH(ld_library_path);
996 }
997}
998
Elliott Hughese66190d2012-12-18 15:57:55 -0800999soinfo* do_dlopen(const char* name, int flags) {
1000 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
1001 DL_ERR("invalid flags to dlopen: %x", flags);
1002 return NULL;
1003 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001004 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
1005 soinfo* si = find_library(name);
1006 if (si != NULL) {
1007 si->CallConstructors();
1008 }
1009 set_soinfo_pool_protection(PROT_READ);
1010 return si;
1011}
1012
1013int do_dlclose(soinfo* si) {
1014 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
1015 int result = soinfo_unload(si);
1016 set_soinfo_pool_protection(PROT_READ);
1017 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001018}
1019
1020/* TODO: don't use unsigned for addrs below. It works, but is not
1021 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1022 * long.
1023 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001024static int soinfo_relocate(soinfo* si, Elf32_Rel* rel, unsigned count,
1025 soinfo* needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001026{
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001027 Elf32_Sym* symtab = si->symtab;
1028 const char* strtab = si->strtab;
1029 Elf32_Sym* s;
1030 Elf32_Rel* start = rel;
1031 soinfo* lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001032
Elliott Hughes46882792012-08-03 16:49:39 -07001033 for (size_t idx = 0; idx < count; ++idx, ++rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001034 unsigned type = ELF32_R_TYPE(rel->r_info);
1035 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001036 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001037 unsigned sym_addr = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001038 char* sym_name = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001039
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001040 DEBUG("Processing '%s' relocation at index %d\n", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001041 if (type == 0) { // R_*_NONE
1042 continue;
1043 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001044 if (sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001045 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001046 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001047 if (s == NULL) {
Doug Kwane8238072009-10-26 12:05:23 -07001048 /* We only allow an undefined symbol if this is a weak
1049 reference.. */
1050 s = &symtab[sym];
1051 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -07001052 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -07001053 return -1;
1054 }
1055
1056 /* IHI0044C AAELF 4.5.1.1:
1057
1058 Libraries are not searched to resolve weak references.
1059 It is not an error for a weak reference to remain
1060 unsatisfied.
1061
1062 During linking, the value of an undefined weak reference is:
1063 - Zero if the relocation type is absolute
1064 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -07001065 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -07001066 type is base-relative.
1067 */
1068
1069 switch (type) {
1070#if defined(ANDROID_ARM_LINKER)
1071 case R_ARM_JUMP_SLOT:
1072 case R_ARM_GLOB_DAT:
1073 case R_ARM_ABS32:
1074 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -07001075#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001076 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001077 case R_386_GLOB_DAT:
1078 case R_386_32:
1079 case R_386_RELATIVE: /* Dont' care. */
1080#endif /* ANDROID_*_LINKER */
1081 /* sym_addr was initialized to be zero above or relocation
1082 code below does not care about value of sym_addr.
1083 No need to do anything. */
1084 break;
1085
1086#if defined(ANDROID_X86_LINKER)
1087 case R_386_PC32:
1088 sym_addr = reloc;
1089 break;
1090#endif /* ANDROID_X86_LINKER */
1091
1092#if defined(ANDROID_ARM_LINKER)
1093 case R_ARM_COPY:
1094 /* Fall through. Can't really copy if weak symbol is
1095 not found in run-time. */
1096#endif /* ANDROID_ARM_LINKER */
1097 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001098 DL_ERR("unknown weak reloc type %d @ %p (%d)",
1099 type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -07001100 return -1;
1101 }
1102 } else {
1103 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001104#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001105 if ((base == 0) && (si->base != 0)) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001106 /* linking from libraries to main image is bad */
Elliott Hughes46882792012-08-03 16:49:39 -07001107 DL_ERR("cannot locate \"%s\"...",
1108 strtab + symtab[sym].st_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001109 return -1;
1110 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001111#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001112 sym_addr = (unsigned)(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001113 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001114 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001115 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001116 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001117 }
1118
1119/* TODO: This is ugly. Split up the relocations by arch into
1120 * different files.
1121 */
1122 switch(type){
1123#if defined(ANDROID_ARM_LINKER)
1124 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001125 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001126 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001127 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001128 *((unsigned*)reloc) = sym_addr;
1129 break;
1130 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001131 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001132 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001133 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001134 *((unsigned*)reloc) = sym_addr;
1135 break;
1136 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001137 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001138 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001139 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001140 *((unsigned*)reloc) += sym_addr;
1141 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001142 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001143 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001144 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001145 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s\n",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001146 reloc, sym_addr, rel->r_offset, sym_name);
1147 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1148 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001149#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001150 case R_386_JMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001151 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001152 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001153 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001154 *((unsigned*)reloc) = sym_addr;
1155 break;
1156 case R_386_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001157 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001158 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001159 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001160 *((unsigned*)reloc) = sym_addr;
1161 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001162#elif defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001163 case R_MIPS_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001164 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001165 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001166 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s\n",
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001167 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1168 if (s) {
1169 *((unsigned*)reloc) += sym_addr;
1170 } else {
1171 *((unsigned*)reloc) += si->base;
1172 }
1173 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001174#endif /* ANDROID_*_LINKER */
1175
1176#if defined(ANDROID_ARM_LINKER)
1177 case R_ARM_RELATIVE:
1178#elif defined(ANDROID_X86_LINKER)
1179 case R_386_RELATIVE:
1180#endif /* ANDROID_*_LINKER */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001181 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001182 MARK(rel->r_offset);
Elliott Hughes46882792012-08-03 16:49:39 -07001183 if (sym) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001184 DL_ERR("odd RELATIVE form...");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001185 return -1;
1186 }
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001187 TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x\n", reloc, si->base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001188 *((unsigned*)reloc) += si->base;
1189 break;
1190
1191#if defined(ANDROID_X86_LINKER)
1192 case R_386_32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001193 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001194 MARK(rel->r_offset);
1195
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001196 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001197 *((unsigned *)reloc) += (unsigned)sym_addr;
1198 break;
1199
1200 case R_386_PC32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001201 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001202 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001203 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s\n",
1204 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001205 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1206 break;
1207#endif /* ANDROID_X86_LINKER */
1208
1209#ifdef ANDROID_ARM_LINKER
1210 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001211 if ((si->flags & FLAG_EXE) == 0) {
1212 /*
1213 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1214 *
1215 * Section 4.7.1.10 "Dynamic relocations"
1216 * R_ARM_COPY may only appear in executable objects where e_type is
1217 * set to ET_EXEC.
1218 *
1219 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1220 * We should explicitly disallow ET_DYN executables from having
1221 * R_ARM_COPY relocations.
1222 */
1223 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1224 return -1;
1225 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001226 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001227 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001228 TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s\n", reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001229 if (reloc == sym_addr) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001230 Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1231
1232 if (src == NULL) {
1233 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1234 return -1;
1235 }
1236 if (lsi->has_DT_SYMBOLIC) {
1237 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1238 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1239 return -1;
1240 }
1241 if (s->st_size < src->st_size) {
1242 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1243 si->name, s->st_size, src->st_size);
1244 return -1;
1245 }
1246 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1247 } else {
1248 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001249 return -1;
1250 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001251 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001252#endif /* ANDROID_ARM_LINKER */
1253
1254 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001255 DL_ERR("unknown reloc type %d @ %p (%d)",
1256 type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001257 return -1;
1258 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001259 }
1260 return 0;
1261}
1262
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001263#ifdef ANDROID_MIPS_LINKER
Elliott Hughesbedfe382012-08-14 14:07:59 -07001264static int mips_relocate_got(soinfo* si, soinfo* needed[]) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001265 unsigned *got;
1266 unsigned local_gotno, gotsym, symtabno;
1267 Elf32_Sym *symtab, *sym;
1268 unsigned g;
1269
1270 got = si->plt_got;
1271 local_gotno = si->mips_local_gotno;
1272 gotsym = si->mips_gotsym;
1273 symtabno = si->mips_symtabno;
1274 symtab = si->symtab;
1275
1276 /*
1277 * got[0] is address of lazy resolver function
1278 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001279 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001280 * (should be _rtld_bind_start)
1281 * FIXME: maybe this should be in a separate routine
1282 */
1283
1284 if ((si->flags & FLAG_LINKER) == 0) {
1285 g = 0;
1286 got[g++] = 0xdeadbeef;
1287 if (got[g] & 0x80000000) {
1288 got[g++] = 0xdeadfeed;
1289 }
1290 /*
1291 * Relocate the local GOT entries need to be relocated
1292 */
1293 for (; g < local_gotno; g++) {
1294 got[g] += si->load_bias;
1295 }
1296 }
1297
1298 /* Now for the global GOT entries */
1299 sym = symtab + gotsym;
1300 got = si->plt_got + local_gotno;
1301 for (g = gotsym; g < symtabno; g++, sym++, got++) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001302 const char* sym_name;
1303 Elf32_Sym* s;
1304 soinfo* lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001305
1306 /* This is an undefined reference... try to locate it */
1307 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001308 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001309 if (s == NULL) {
1310 /* We only allow an undefined symbol if this is a weak
1311 reference.. */
1312 s = &symtab[g];
1313 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001314 DL_ERR("cannot locate \"%s\"...", sym_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001315 return -1;
1316 }
1317 *got = 0;
1318 }
1319 else {
1320 /* FIXME: is this sufficient?
1321 * For reference see NetBSD link loader
1322 * 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
1323 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001324 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001325 }
1326 }
1327 return 0;
1328}
1329#endif
1330
David 'Digit' Turner82156792009-05-18 14:37:41 +02001331/* Please read the "Initialization and Termination functions" functions.
1332 * of the linker design note in bionic/linker/README.TXT to understand
1333 * what the following code is doing.
1334 *
1335 * The important things to remember are:
1336 *
1337 * DT_PREINIT_ARRAY must be called first for executables, and should
1338 * not appear in shared libraries.
1339 *
1340 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1341 *
1342 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1343 *
1344 * DT_FINI_ARRAY must be parsed in reverse order.
1345 */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001346void soinfo::CallArray(const char* array_name UNUSED, unsigned* array, int count, bool reverse) {
1347 if (array == NULL) {
1348 return;
1349 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001350
Elliott Hughesd23736e2012-11-01 15:16:56 -07001351 int step = 1;
1352 if (reverse) {
1353 array += (count-1);
1354 step = -1;
1355 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001356
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001357 TRACE("[ Calling %s @ %p [%d] for '%s' ]\n", array_name, array, count, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001358
1359 for (int n = count; n > 0; n--) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001360 TRACE("[ Looking at %s[%d] *%p == 0x%08x ]\n", array_name, n, array, *array);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001361 void (*func)() = (void (*)()) *array;
1362 array += step;
Elliott Hughesdb492b32013-01-03 15:44:03 -08001363 CallFunction("function", func);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001364 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001365
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001366 TRACE("[ Done calling %s for '%s' ]\n", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001367}
1368
Elliott Hughesd23736e2012-11-01 15:16:56 -07001369void soinfo::CallFunction(const char* function_name UNUSED, void (*function)()) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001370 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001371 return;
1372 }
1373
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001374 TRACE("[ Calling %s @ %p for '%s' ]\n", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001375 function();
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001376 TRACE("[ Done calling %s for '%s' ]\n", function_name, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001377
1378 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1379 // are still writable. This happens with our debug malloc (see http://b/7941716).
1380 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001381}
1382
Elliott Hughesd23736e2012-11-01 15:16:56 -07001383void soinfo::CallPreInitConstructors() {
1384 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1385}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001386
Elliott Hughesd23736e2012-11-01 15:16:56 -07001387void soinfo::CallConstructors() {
1388 if (constructors_called) {
1389 return;
1390 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001391
Elliott Hughesd23736e2012-11-01 15:16:56 -07001392 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1393 // protect against recursive constructor calls. One simple example of constructor recursion
1394 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1395 // 1. The program depends on libc, so libc's constructor is called here.
1396 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1397 // 3. dlopen() calls the constructors on the newly created
1398 // soinfo for libc_malloc_debug_leak.so.
1399 // 4. The debug .so depends on libc, so CallConstructors is
1400 // called again with the libc soinfo. If it doesn't trigger the early-
1401 // out above, the libc constructor will be called again (recursively!).
1402 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001403
Elliott Hughesd23736e2012-11-01 15:16:56 -07001404 if (!(flags & FLAG_EXE) && preinit_array) {
1405 DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array);
1406 return;
1407 }
1408
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001409 if (dynamic != NULL) {
1410 for (Elf32_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) {
1411 if (d->d_tag == DT_NEEDED) {
1412 const char* library_name = strtab + d->d_un.d_val;
1413 soinfo* lsi = find_loaded_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001414 if (lsi == NULL) {
1415 DL_ERR("\"%s\": could not initialize dependent library", name);
1416 } else {
1417 lsi->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001418 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001419 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001420 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001421 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001422
Elliott Hughesd23736e2012-11-01 15:16:56 -07001423 CallFunction("DT_INIT", init_func);
1424 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001425}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001426
Elliott Hughesd23736e2012-11-01 15:16:56 -07001427void soinfo::CallDestructors() {
1428 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
1429 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001430}
1431
1432/* Force any of the closed stdin, stdout and stderr to be associated with
1433 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001434static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001435 int dev_null, i, status;
1436 int return_value = 0;
1437
David 'Digit' Turner16084162012-06-12 16:25:37 +02001438 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001439 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001440 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001441 return -1;
1442 }
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001443 TRACE("[ Opened /dev/null file-descriptor=%d]\n", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001444
1445 /* If any of the stdio file descriptors is valid and not associated
1446 with /dev/null, dup /dev/null to it. */
1447 for (i = 0; i < 3; i++) {
1448 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001449 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001450 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001451 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001452
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001453 TRACE("[ Nullifying stdio file descriptor %d]\n", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001454 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001455
Elliott Hughes46882792012-08-03 16:49:39 -07001456 /* If file is opened, we are good. */
1457 if (status != -1) {
1458 continue;
1459 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001460
1461 /* The only error we allow is that the file descriptor does not
1462 exist, in which case we dup /dev/null to it. */
1463 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001464 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001465 return_value = -1;
1466 continue;
1467 }
1468
1469 /* Try dupping /dev/null to this stdio file descriptor and
1470 repeat if there is a signal. Note that any errors in closing
1471 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001472 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001473 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001474 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001475 return_value = -1;
1476 continue;
1477 }
1478 }
1479
1480 /* If /dev/null is not one of the stdio file descriptors, close it. */
1481 if (dev_null > 2) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001482 TRACE("[ Closing /dev/null file-descriptor=%d]\n", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001483 status = TEMP_FAILURE_RETRY(close(dev_null));
1484 if (status == -1) {
1485 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001486 return_value = -1;
1487 }
1488 }
1489
1490 return return_value;
1491}
1492
Elliott Hughes124fae92012-10-31 14:20:03 -07001493static bool soinfo_link_image(soinfo* si) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001494 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001495 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001496 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001497 int phnum = si->phnum;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001498 bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001499
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001500 /* We can't debug anything until the linker is relocated */
1501 if (!relocating_linker) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001502 INFO("[ linking %s ]\n", si->name);
1503 DEBUG("si->base = 0x%08x si->flags = 0x%08x\n", si->base, si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001504 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001505
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001506 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001507 size_t dynamic_count;
Chris Dearmancf239052013-01-11 15:32:20 -08001508 Elf32_Word dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001509 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001510 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001511 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001512 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001513 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001514 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001515 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001516 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001517 if (!relocating_linker) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001518 DEBUG("dynamic = %p\n", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001519 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001520 }
1521
1522#ifdef ANDROID_ARM_LINKER
1523 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1524 &si->ARM_exidx, &si->ARM_exidx_count);
1525#endif
1526
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001527 /* extract useful information from dynamic section */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001528 uint32_t needed_count = 0;
1529 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1530 DEBUG("d = %p, d[0](tag) = 0x%08x d[1](val) = 0x%08x\n", d, d->d_tag, d->d_un.d_val);
1531 switch(d->d_tag){
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001532 case DT_HASH:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001533 si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0];
1534 si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1];
1535 si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8);
1536 si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001537 break;
1538 case DT_STRTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001539 si->strtab = (const char *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001540 break;
1541 case DT_SYMTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001542 si->symtab = (Elf32_Sym *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001543 break;
1544 case DT_PLTREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001545 if (d->d_un.d_val != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001546 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1547 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001548 }
1549 break;
1550 case DT_JMPREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001551 si->plt_rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001552 break;
1553 case DT_PLTRELSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001554 si->plt_rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001555 break;
1556 case DT_REL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001557 si->rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001558 break;
1559 case DT_RELSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001560 si->rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001561 break;
1562 case DT_PLTGOT:
1563 /* Save this in case we decide to do lazy binding. We don't yet. */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001564 si->plt_got = (unsigned *)(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001565 break;
1566 case DT_DEBUG:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001567 // Set the DT_DEBUG entry to the address of _r_debug for GDB
Chris Dearmancf239052013-01-11 15:32:20 -08001568 // if the dynamic table is writable
Elliott Hughes99c32052013-01-14 09:56:21 -08001569 if ((dynamic_flags & PF_W) != 0) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001570 d->d_un.d_val = (int) &_r_debug;
Elliott Hughes99c32052013-01-14 09:56:21 -08001571 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001572 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001573 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001574 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1575 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001576 case DT_INIT:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001577 si->init_func = (void (*)(void))(base + d->d_un.d_ptr);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001578 DEBUG("%s constructors (init func) found at %p\n", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001579 break;
1580 case DT_FINI:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001581 si->fini_func = (void (*)(void))(base + d->d_un.d_ptr);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001582 DEBUG("%s destructors (fini func) found at %p\n", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001583 break;
1584 case DT_INIT_ARRAY:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001585 si->init_array = (unsigned *)(base + d->d_un.d_ptr);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001586 DEBUG("%s constructors (init_array) found at %p\n", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001587 break;
1588 case DT_INIT_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001589 si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001590 break;
1591 case DT_FINI_ARRAY:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001592 si->fini_array = (unsigned *)(base + d->d_un.d_ptr);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001593 DEBUG("%s destructors (fini_array) found at %p\n", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001594 break;
1595 case DT_FINI_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001596 si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001597 break;
1598 case DT_PREINIT_ARRAY:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001599 si->preinit_array = (unsigned *)(base + d->d_un.d_ptr);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001600 DEBUG("%s constructors (preinit_array) found at %p\n", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001601 break;
1602 case DT_PREINIT_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001603 si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001604 break;
1605 case DT_TEXTREL:
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001606 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001607 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001608 case DT_SYMBOLIC:
1609 si->has_DT_SYMBOLIC = true;
1610 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001611 case DT_NEEDED:
1612 ++needed_count;
1613 break;
1614#if defined DT_FLAGS
1615 // TODO: why is DT_FLAGS not defined?
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001616 case DT_FLAGS:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001617 if (d->d_un.d_val & DF_TEXTREL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001618 si->has_text_relocations = true;
1619 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001620 if (d->d_un.d_val & DF_SYMBOLIC) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001621 si->has_DT_SYMBOLIC = true;
1622 }
1623 break;
1624#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001625#if defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001626 case DT_STRSZ:
1627 case DT_SYMENT:
1628 case DT_RELENT:
1629 break;
1630 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001631 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001632 {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001633 r_debug** dp = (r_debug**) d->d_un.d_ptr;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001634 *dp = &_r_debug;
1635 }
1636 break;
1637 case DT_MIPS_RLD_VERSION:
1638 case DT_MIPS_FLAGS:
1639 case DT_MIPS_BASE_ADDRESS:
1640 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001641 break;
1642
1643 case DT_MIPS_SYMTABNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001644 si->mips_symtabno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001645 break;
1646
1647 case DT_MIPS_LOCAL_GOTNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001648 si->mips_local_gotno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001649 break;
1650
1651 case DT_MIPS_GOTSYM:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001652 si->mips_gotsym = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001653 break;
1654
1655 default:
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001656 DEBUG("Unused DT entry: type 0x%08x arg 0x%08x\n", d[-1], d[0]);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001657 break;
1658#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001659 }
1660 }
1661
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001662 DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
1663 si->base, si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001664
Elliott Hughes124fae92012-10-31 14:20:03 -07001665 // Sanity checks.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001666 if (relocating_linker && needed_count != 0) {
1667 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1668 return false;
1669 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001670 if (si->nbucket == 0) {
1671 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1672 return false;
1673 }
1674 if (si->strtab == 0) {
1675 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1676 return false;
1677 }
1678 if (si->symtab == 0) {
1679 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1680 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001681 }
1682
Matt Fischer4fd42c12009-12-31 12:09:10 -06001683 /* if this is the main executable, then load all of the preloads now */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001684 if (si->flags & FLAG_EXE) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001685 memset(gLdPreloads, 0, sizeof(gLdPreloads));
Elliott Hughesd23736e2012-11-01 15:16:56 -07001686 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1687 soinfo* lsi = find_library(gLdPreloadNames[i]);
1688 if (lsi == NULL) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001689 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001690 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Elliott Hughes124fae92012-10-31 14:20:03 -07001691 gLdPreloadNames[i], si->name, tmp_err_buf);
1692 return false;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001693 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001694 gLdPreloads[i] = lsi;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001695 }
1696 }
1697
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001698 soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*));
1699 soinfo** pneeded = needed;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001700
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001701 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1702 if (d->d_tag == DT_NEEDED) {
1703 const char* library_name = si->strtab + d->d_un.d_val;
1704 DEBUG("%s needs %s\n", si->name, library_name);
1705 soinfo* lsi = find_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001706 if (lsi == NULL) {
Dima Zavin03531952009-05-29 17:30:25 -07001707 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001708 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001709 library_name, si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001710 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001711 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001712 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001713 }
1714 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001715 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001716
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001717 if (si->has_text_relocations) {
1718 /* Unprotect the segments, i.e. make them writable, to allow
1719 * text relocations to work properly. We will later call
1720 * phdr_table_protect_segments() after all of them are applied
1721 * and all constructors are run.
1722 */
1723 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1724 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1725 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001726 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001727 }
1728 }
1729
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001730 if (si->plt_rel != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001731 DEBUG("[ relocating %s plt ]\n", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001732 if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001733 return false;
1734 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001735 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001736 if (si->rel != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001737 DEBUG("[ relocating %s ]\n", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001738 if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001739 return false;
1740 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001741 }
1742
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001743#ifdef ANDROID_MIPS_LINKER
Elliott Hughes124fae92012-10-31 14:20:03 -07001744 if (mips_relocate_got(si, needed)) {
1745 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001746 }
1747#endif
1748
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001749 si->flags |= FLAG_LINKED;
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001750 DEBUG("[ finished linking %s ]\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001751
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001752 if (si->has_text_relocations) {
1753 /* All relocations are done, we can protect our segments back to
1754 * read-only. */
1755 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1756 DL_ERR("can't protect segments for \"%s\": %s",
1757 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001758 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001759 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001760 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001761
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001762 /* We can also turn on GNU RELRO protection */
1763 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001764 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1765 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001766 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001767 }
1768
Elliott Hughes124fae92012-10-31 14:20:03 -07001769 // If this is a setuid/setgid program, close the security hole described in
1770 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
Elliott Hughes18a206c2012-10-29 17:37:13 -07001771 if (get_AT_SECURE()) {
Elliott Hughes46882792012-08-03 16:49:39 -07001772 nullify_closed_stdio();
1773 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001774 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001775 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001776}
1777
Nick Kralevich468319c2011-11-11 15:53:17 -08001778/*
1779 * This code is called after the linker has linked itself and
1780 * fixed it's own GOT. It is safe to make references to externs
1781 * and other non-local data at this point.
1782 */
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001783static unsigned __linker_init_post_relocation(KernelArgumentBlock& args, unsigned linker_base) {
1784 /* NOTE: we store the args pointer on a special location
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001785 * of the temporary TLS area in order to pass it to
1786 * the C Library's runtime initializer.
1787 *
1788 * The initializer must clear the slot and reset the TLS
1789 * to point to a different location to ensure that no other
1790 * shared library constructor can access it.
1791 */
Elliott Hughesd3920b32013-02-07 18:39:34 -08001792 __libc_init_tls(args);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001793
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001794#if TIMING
1795 struct timeval t0, t1;
1796 gettimeofday(&t0, 0);
1797#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001798
Elliott Hughes18a206c2012-10-29 17:37:13 -07001799 // Initialize environment functions, and get to the ELF aux vectors table.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001800 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001801
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001802 debuggerd_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001803
Elliott Hughes18a206c2012-10-29 17:37:13 -07001804 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001805 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1806 if (LD_DEBUG != NULL) {
1807 debug_verbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001808 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001809
Elliott Hughes18a206c2012-10-29 17:37:13 -07001810 // Normally, these are cleaned by linker_env_init, but the test
1811 // doesn't cost us anything.
1812 const char* ldpath_env = NULL;
1813 const char* ldpreload_env = NULL;
1814 if (!get_AT_SECURE()) {
1815 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1816 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001817 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001818
1819 INFO("[ android linker & debugger ]\n");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001820
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001821 soinfo* si = soinfo_alloc(args.argv[0]);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001822 if (si == NULL) {
1823 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001824 }
1825
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001826 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001827 si->flags |= FLAG_EXE;
Elliott Hughesbedfe382012-08-14 14:07:59 -07001828 link_map* map = &(si->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001829
1830 map->l_addr = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001831 map->l_name = args.argv[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001832 map->l_prev = NULL;
1833 map->l_next = NULL;
1834
1835 _r_debug.r_map = map;
1836 r_debug_tail = map;
1837
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001838 /* gdb expects the linker to be in the debug shared object list.
1839 * Without this, gdb has trouble locating the linker's ".text"
1840 * and ".plt" sections. Gdb could also potentially use this to
1841 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1842 * Don't use soinfo_alloc(), because the linker shouldn't
1843 * be on the soinfo list.
Ben Cheng06f0e742012-08-10 16:07:02 -07001844 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001845 {
1846 static soinfo linker_soinfo;
1847 strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
1848 linker_soinfo.flags = 0;
1849 linker_soinfo.base = linker_base;
1850
1851 /*
1852 * Set the dynamic field in the link map otherwise gdb will complain with
1853 * the following:
1854 * warning: .dynamic section for "/system/bin/linker" is not at the
1855 * expected address (wrong library or version mismatch?)
1856 */
1857 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
1858 Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
1859 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1860 &linker_soinfo.dynamic, NULL, NULL);
1861 insert_soinfo_into_debug_map(&linker_soinfo);
1862 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001863
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001864 // Extract information passed from the kernel.
1865 si->phdr = reinterpret_cast<Elf32_Phdr*>(args.getauxval(AT_PHDR));
1866 si->phnum = args.getauxval(AT_PHNUM);
1867 si->entry = args.getauxval(AT_ENTRY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001868
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001869 /* Compute the value of si->base. We can't rely on the fact that
1870 * the first entry is the PHDR because this will not be true
1871 * for certain executables (e.g. some in the NDK unit test suite)
1872 */
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001873 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001874 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001875 si->load_bias = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001876 for (int i = 0; i < si->phnum; ++i) {
1877 if (si->phdr[i].p_type == PT_PHDR) {
1878 si->load_bias = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_vaddr;
1879 si->base = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_offset;
1880 break;
1881 }
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001882 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001883 si->dynamic = NULL;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001884 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001885
Elliott Hughes46882792012-08-03 16:49:39 -07001886 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1887 parse_LD_LIBRARY_PATH(ldpath_env);
1888 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001889
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001890 somain = si;
1891
Elliott Hughes124fae92012-10-31 14:20:03 -07001892 if (!soinfo_link_image(si)) {
Chris Dearman20a24402012-10-31 05:39:27 -07001893 const char* msg = "CANNOT LINK EXECUTABLE\n";
Dima Zavin2e855792009-05-20 18:28:09 -07001894 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
Chris Dearman20a24402012-10-31 05:39:27 -07001895 write(2, msg, strlen(msg));
Elliott Hughes18a206c2012-10-29 17:37:13 -07001896 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001897 }
1898
Elliott Hughesd23736e2012-11-01 15:16:56 -07001899 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001900
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001901 for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
1902 gLdPreloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08001903 }
1904
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001905 /* After the link_image, the si->load_bias is initialized.
1906 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1907 * We need to update this value for so exe here. So Unwind_Backtrace
1908 * for some arch like x86 could work correctly within so exe.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08001909 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08001910 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001911 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001912
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001913#if TIMING
1914 gettimeofday(&t1,NULL);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001915 PRINT("LINKER TIME: %s: %d microseconds\n", e.argv[0], (int) (
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001916 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1917 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1918 ));
1919#endif
1920#if STATS
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001921 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", e.argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07001922 linker_stats.count[kRelocAbsolute],
1923 linker_stats.count[kRelocRelative],
1924 linker_stats.count[kRelocCopy],
1925 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001926#endif
1927#if COUNT_PAGES
1928 {
1929 unsigned n;
1930 unsigned i;
1931 unsigned count = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001932 for (n = 0; n < 4096; n++) {
1933 if (bitmask[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001934 unsigned x = bitmask[n];
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001935 for (i = 0; i < 8; i++) {
1936 if (x & 1) {
1937 count++;
1938 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001939 x >>= 1;
1940 }
1941 }
1942 }
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001943 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", e.argv[0], count, count * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001944 }
1945#endif
1946
1947#if TIMING || STATS || COUNT_PAGES
1948 fflush(stdout);
1949#endif
1950
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001951 TRACE("[ Ready to execute '%s' @ 0x%08x ]\n", si->name, si->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001952 return si->entry;
1953}
Nick Kralevich468319c2011-11-11 15:53:17 -08001954
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001955/* Compute the load-bias of an existing executable. This shall only
1956 * be used to compute the load bias of an executable or shared library
1957 * that was loaded by the kernel itself.
1958 *
1959 * Input:
1960 * elf -> address of ELF header, assumed to be at the start of the file.
1961 * Return:
1962 * load bias, i.e. add the value of any p_vaddr in the file to get
1963 * the corresponding address in memory.
1964 */
1965static Elf32_Addr
1966get_elf_exec_load_bias(const Elf32_Ehdr* elf)
1967{
1968 Elf32_Addr offset = elf->e_phoff;
1969 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
1970 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
1971 const Elf32_Phdr* phdr;
1972
1973 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
1974 if (phdr->p_type == PT_LOAD) {
1975 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
1976 }
1977 }
1978 return 0;
1979}
1980
Nick Kralevich468319c2011-11-11 15:53:17 -08001981/*
1982 * This is the entry point for the linker, called from begin.S. This
1983 * method is responsible for fixing the linker's own relocations, and
1984 * then calling __linker_init_post_relocation().
1985 *
1986 * Because this method is called before the linker has fixed it's own
1987 * relocations, any attempt to reference an extern variable, extern
1988 * function, or other GOT reference will generate a segfault.
1989 */
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001990extern "C" unsigned __linker_init(void* raw_args) {
1991 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08001992
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001993 unsigned linker_addr = args.getauxval(AT_BASE);
Nick Kralevich468319c2011-11-11 15:53:17 -08001994
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001995 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr*) linker_addr;
1996 Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08001997
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001998 soinfo linker_so;
1999 memset(&linker_so, 0, sizeof(soinfo));
Nick Kralevich468319c2011-11-11 15:53:17 -08002000
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002001 linker_so.base = linker_addr;
2002 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2003 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002004 linker_so.dynamic = NULL;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002005 linker_so.phdr = phdr;
2006 linker_so.phnum = elf_hdr->e_phnum;
2007 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07002008
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002009 if (!soinfo_link_image(&linker_so)) {
2010 // It would be nice to print an error message, but if the linker
2011 // can't link itself, there's no guarantee that we'll be able to
2012 // call write() (because it involves a GOT reference).
2013 //
2014 // This situation should never occur unless the linker itself
2015 // is corrupt.
2016 exit(EXIT_FAILURE);
2017 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002018
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002019 // We have successfully fixed our own relocations. It's safe to run
2020 // the main part of the linker now.
2021 unsigned start_address = __linker_init_post_relocation(args, linker_addr);
2022
2023 set_soinfo_pool_protection(PROT_READ);
2024
2025 // Return the address that the calling assembly stub should jump to.
2026 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002027}