blob: d36b1f6b14945b331a52b4c6808241c9b2fc4a50 [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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090static soinfo *solist = &libdl_info;
91static soinfo *sonext = &libdl_info;
Iliyan Malchev4a9afcb2009-09-29 11:43:20 -070092static 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
106static soinfo *preloads[LDPRELOAD_MAX + 1];
107
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
335 for(trav = solist; trav != NULL; trav = trav->next){
336 if (trav == si)
337 break;
338 prev = trav;
339 }
340 if (trav == NULL) {
341 /* si was not ni 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;
350 if (si == sonext) sonext = prev;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200351 si->next = gSoInfoFreeList;
352 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800353}
354
Elliott Hughescade4c32012-12-20 14:42:14 -0800355
356static void parse_path(const char* path, const char* delimiters,
357 const char** array, char* buf, size_t buf_size, size_t max_count) {
358 if (path == NULL) {
359 return;
360 }
361
362 size_t len = strlcpy(buf, path, buf_size);
363
364 size_t i = 0;
365 char* buf_p = buf;
366 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
367 if (*array[i] != '\0') {
368 ++i;
369 }
370 }
371
372 // Forget the last path if we had to truncate; this occurs if the 2nd to
373 // last char isn't '\0' (i.e. wasn't originally a delimiter).
374 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
375 array[i - 1] = NULL;
376 } else {
377 array[i] = NULL;
378 }
379}
380
381static void parse_LD_LIBRARY_PATH(const char* path) {
382 parse_path(path, ":", gLdPaths,
383 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
384}
385
386static void parse_LD_PRELOAD(const char* path) {
387 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
388 parse_path(path, " :", gLdPreloadNames,
389 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
390}
391
Elliott Hughes46882792012-08-03 16:49:39 -0700392#ifdef ANDROID_ARM_LINKER
393
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800394/* For a given PC, find the .so that it belongs to.
395 * Returns the base address of the .ARM.exidx section
396 * for that .so, and the number of 8-byte entries
397 * in that section (via *pcount).
398 *
399 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
400 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700401 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800403_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
404{
405 soinfo *si;
406 unsigned addr = (unsigned)pc;
407
Nick Kralevich468319c2011-11-11 15:53:17 -0800408 for (si = solist; si != 0; si = si->next){
409 if ((addr >= si->base) && (addr < (si->base + si->size))) {
410 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900411 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800412 }
413 }
414 *pcount = 0;
415 return NULL;
416}
Elliott Hughes46882792012-08-03 16:49:39 -0700417
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700418#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
Elliott Hughes46882792012-08-03 16:49:39 -0700419
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800420/* Here, we only have to provide a callback to iterate across all the
421 * loaded libraries. gcc_eh does the rest. */
422int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700423dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800424 void *data)
425{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800426 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700427 for (soinfo* si = solist; si != NULL; si = si->next) {
428 dl_phdr_info dl_info;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800429 dl_info.dlpi_addr = si->linkmap.l_addr;
430 dl_info.dlpi_name = si->linkmap.l_name;
431 dl_info.dlpi_phdr = si->phdr;
432 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700433 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
434 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800435 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700436 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800437 }
438 return rv;
439}
Elliott Hughes46882792012-08-03 16:49:39 -0700440
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800441#endif
442
David 'Digit' Turner16084162012-06-12 16:25:37 +0200443static Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800444{
445 Elf32_Sym *s;
446 Elf32_Sym *symtab = si->symtab;
447 const char *strtab = si->strtab;
448 unsigned n;
449
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700450 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800451 name, si->name, si->base, hash, hash % si->nbucket);
452 n = hash % si->nbucket;
453
454 for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
455 s = symtab + n;
456 if(strcmp(strtab + s->st_name, name)) continue;
457
Doug Kwane8238072009-10-26 12:05:23 -0700458 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800459 switch(ELF32_ST_BIND(s->st_info)){
460 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700461 case STB_WEAK:
Robin Burchell439fa8e2012-07-05 09:21:07 +0200462 if(s->st_shndx == SHN_UNDEF)
463 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800464
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700465 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800466 name, si->name, s->st_value, s->st_size);
467 return s;
468 }
469 }
470
Doug Kwan94304352009-10-23 18:11:40 -0700471 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800472}
473
474static unsigned elfhash(const char *_name)
475{
476 const unsigned char *name = (const unsigned char *) _name;
477 unsigned h = 0, g;
478
479 while(*name) {
480 h = (h << 4) + *name++;
481 g = h & 0xf0000000;
482 h ^= g;
483 h ^= g >> 24;
484 }
485 return h;
486}
487
488static Elf32_Sym *
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200489soinfo_do_lookup(soinfo *si, const char *name, soinfo **lsi,
490 soinfo *needed[])
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700491{
Doug Kwan94304352009-10-23 18:11:40 -0700492 unsigned elf_hash = elfhash(name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700493 Elf32_Sym *s = NULL;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600494 int i;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700495
Pavel Chupinc77c4342012-10-31 13:55:51 +0400496 if (si != NULL && somain != NULL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200497
498 /*
Pavel Chupinc77c4342012-10-31 13:55:51 +0400499 * Local scope is executable scope. Just start looking into it right away
500 * for the shortcut.
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200501 */
502
Pavel Chupinc77c4342012-10-31 13:55:51 +0400503 if (si == somain) {
504 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200505 if (s != NULL) {
Pavel Chupinc77c4342012-10-31 13:55:51 +0400506 *lsi = si;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200507 goto done;
508 }
Pavel Chupinc77c4342012-10-31 13:55:51 +0400509 } else {
510 /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200511
Pavel Chupinc77c4342012-10-31 13:55:51 +0400512 /*
513 * If this object was built with symbolic relocations disabled, the
514 * first place to look to resolve external references is the main
515 * executable.
516 */
Nick Kralevich468319c2011-11-11 15:53:17 -0800517
Pavel Chupinc77c4342012-10-31 13:55:51 +0400518 if (!si->has_DT_SYMBOLIC) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700519 DEBUG("%s: looking up %s in executable %s\n",
520 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400521 s = soinfo_elf_lookup(somain, elf_hash, name);
522 if (s != NULL) {
523 *lsi = somain;
524 goto done;
525 }
526 }
527
528 /* Look for symbols in the local scope (the object who is
529 * searching). This happens with C++ templates on i386 for some
530 * reason.
531 *
532 * Notes on weak symbols:
533 * The ELF specs are ambiguous about treatment of weak definitions in
534 * dynamic linking. Some systems return the first definition found
535 * and some the first non-weak definition. This is system dependent.
536 * Here we return the first definition found for simplicity. */
537
538 s = soinfo_elf_lookup(si, elf_hash, name);
539 if (s != NULL) {
540 *lsi = si;
541 goto done;
542 }
543
544 /*
545 * If this object was built with -Bsymbolic and symbol is not found
546 * in the local scope, try to find the symbol in the main executable.
547 */
548
549 if (si->has_DT_SYMBOLIC) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700550 DEBUG("%s: looking up %s in executable %s after local scope\n",
551 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400552 s = soinfo_elf_lookup(somain, elf_hash, name);
553 if (s != NULL) {
554 *lsi = somain;
555 goto done;
556 }
557 }
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200558 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700559 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700560
Matt Fischer4fd42c12009-12-31 12:09:10 -0600561 /* Next, look for it in the preloads list */
562 for(i = 0; preloads[i] != NULL; i++) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200563 s = soinfo_elf_lookup(preloads[i], elf_hash, name);
564 if(s != NULL) {
565 *lsi = preloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600566 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200567 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600568 }
569
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200570 for(i = 0; needed[i] != NULL; i++) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700571 DEBUG("%s: looking up %s in %s\n",
572 si->name, name, needed[i]->name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200573 s = soinfo_elf_lookup(needed[i], elf_hash, name);
574 if (s != NULL) {
575 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200576 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200577 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700578 }
579
580done:
581 if(s != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700582 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = 0x%08x, "
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200583 "found in %s, base = 0x%08x, load bias = 0x%08x\n",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700584 si->name, name, s->st_value,
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200585 (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700586 return s;
587 }
588
Doug Kwan94304352009-10-23 18:11:40 -0700589 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700590}
591
592/* This is used by dl_sym(). It performs symbol lookup only within the
593 specified soinfo object and not in any of its dependencies.
594 */
David 'Digit' Turner16084162012-06-12 16:25:37 +0200595Elf32_Sym *soinfo_lookup(soinfo *si, const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800596{
David 'Digit' Turner16084162012-06-12 16:25:37 +0200597 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598}
599
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700600/* This is used by dl_sym(). It performs a global symbol lookup.
601 */
Elliott Hughescade4c32012-12-20 14:42:14 -0800602Elf32_Sym* lookup(const char* name, soinfo** found, soinfo* start) {
603 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800604
Elliott Hughescade4c32012-12-20 14:42:14 -0800605 if (start == NULL) {
606 start = solist;
607 }
608
609 Elf32_Sym* s = NULL;
610 for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
611 s = soinfo_elf_lookup(si, elf_hash, name);
612 if (s != NULL) {
613 *found = si;
614 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600615 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800616 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600617
Elliott Hughescade4c32012-12-20 14:42:14 -0800618 if (s != NULL) {
619 TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, found->base = 0x%08x\n",
620 name, s->st_value, (*found)->base);
621 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800622
Elliott Hughescade4c32012-12-20 14:42:14 -0800623 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800624}
625
Mathias Agopianbda5da02011-09-27 22:30:19 -0700626soinfo *find_containing_library(const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600627{
628 soinfo *si;
629
630 for(si = solist; si != NULL; si = si->next)
631 {
632 if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
633 return si;
634 }
635 }
636
637 return NULL;
638}
639
David 'Digit' Turner16084162012-06-12 16:25:37 +0200640Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600641{
642 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 */
647 for(i=0; i<si->nchain; i++) {
648 Elf32_Sym *sym = &si->symtab[i];
649
650 if(sym->st_shndx != SHN_UNDEF &&
651 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
661static void dump(soinfo *si)
662{
663 Elf32_Sym *s = si->symtab;
664 unsigned n;
665
666 for(n = 0; n < si->nchain; n++) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700667 TRACE("%04d> %08x: %02x %04x %08x %08x %s\n", n, s,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800668 s->st_info, s->st_shndx, s->st_value, s->st_size,
669 si->strtab + s->st_name);
670 s++;
671 }
672}
673#endif
674
Elliott Hughes124fae92012-10-31 14:20:03 -0700675static int open_library_on_path(const char* name, const char* const paths[]) {
676 char buf[512];
677 for (size_t i = 0; paths[i] != NULL; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800678 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700679 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughes9c94fc92012-11-05 09:11:43 -0800680 PRINT("Warning: ignoring very long library path: %s/%s\n", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700681 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800682 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700683 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
684 if (fd != -1) {
685 return fd;
686 }
687 }
688 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800689}
690
Elliott Hughes124fae92012-10-31 14:20:03 -0700691static int open_library(const char* name) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700692 TRACE("[ opening %s ]\n", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800693
Elliott Hughes124fae92012-10-31 14:20:03 -0700694 // If the name contains a slash, we should attempt to open it directly and not search the paths.
695 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700696 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
697 if (fd != -1) {
698 return fd;
699 }
700 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700701 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800702
Elliott Hughes124fae92012-10-31 14:20:03 -0700703 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
704 int fd = open_library_on_path(name, gLdPaths);
705 if (fd == -1) {
706 fd = open_library_on_path(name, gSoPaths);
707 }
708 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800709}
710
Elliott Hughes46882792012-08-03 16:49:39 -0700711// Returns 'true' if the library is prelinked or on failure so we error out
712// either way. We no longer support prelinking.
713static bool is_prelinked(int fd, const char* name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800714{
Elliott Hughes46882792012-08-03 16:49:39 -0700715 struct prelink_info_t {
716 long mmap_addr;
717 char tag[4]; // "PRE ".
718 };
719
Elliott Hughesbedfe382012-08-14 14:07:59 -0700720 off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800721 if (sz < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700722 DL_ERR("lseek failed: %s", strerror(errno));
723 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800724 }
725
Elliott Hughesbedfe382012-08-14 14:07:59 -0700726 prelink_info_t info;
Elliott Hughes8dfc0732012-07-27 15:30:51 -0700727 int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
728 if (rc != sizeof(info)) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700729 DL_ERR("could not read prelink_info_t structure for \"%s\": %s", name, strerror(errno));
Elliott Hughes46882792012-08-03 16:49:39 -0700730 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800731 }
732
Elliott Hughes46882792012-08-03 16:49:39 -0700733 if (memcmp(info.tag, "PRE ", 4) == 0) {
734 DL_ERR("prelinked libraries no longer supported: %s", name);
735 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800736 }
Elliott Hughes46882792012-08-03 16:49:39 -0700737 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800738}
739
David 'Digit' Turner16084162012-06-12 16:25:37 +0200740/* verify_elf_header
741 * Verifies the content of an ELF header.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800742 *
743 * Args:
744 *
745 * Returns:
746 * 0 on success
747 * -1 if no valid ELF object is found @ base.
748 */
749static int
David 'Digit' Turner16084162012-06-12 16:25:37 +0200750verify_elf_header(const Elf32_Ehdr* hdr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800751{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800752 if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
753 if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
754 if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
755 if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700756 if (hdr->e_type != ET_DYN) return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800757
758 /* TODO: Should we verify anything else in the header? */
Zhenghua Wang897815a2011-10-19 00:29:14 +0800759#ifdef ANDROID_ARM_LINKER
760 if (hdr->e_machine != EM_ARM) return -1;
761#elif defined(ANDROID_X86_LINKER)
762 if (hdr->e_machine != EM_386) return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700763#elif defined(ANDROID_MIPS_LINKER)
764 if (hdr->e_machine != EM_MIPS) return -1;
Zhenghua Wang897815a2011-10-19 00:29:14 +0800765#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800766 return 0;
767}
768
Elliott Hughes46882792012-08-03 16:49:39 -0700769struct scoped_fd {
770 ~scoped_fd() {
771 if (fd != -1) {
772 close(fd);
773 }
774 }
775 int fd;
776};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800777
Elliott Hughes46882792012-08-03 16:49:39 -0700778struct soinfo_ptr {
779 soinfo_ptr(const char* name) {
780 const char* bname = strrchr(name, '/');
781 ptr = soinfo_alloc(bname ? bname + 1 : name);
782 }
783 ~soinfo_ptr() {
784 soinfo_free(ptr);
785 }
786 soinfo* release() {
787 soinfo* result = ptr;
788 ptr = NULL;
789 return result;
790 }
791 soinfo* ptr;
792};
793
794// TODO: rewrite linker_phdr.h to use a class, then lose this.
795struct phdr_ptr {
796 phdr_ptr() : phdr_mmap(NULL) {}
797 ~phdr_ptr() {
798 if (phdr_mmap != NULL) {
799 phdr_table_unload(phdr_mmap, phdr_size);
800 }
801 }
802 void* phdr_mmap;
803 Elf32_Addr phdr_size;
804};
805
Elliott Hughes124fae92012-10-31 14:20:03 -0700806static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700807 // Open the file.
808 scoped_fd fd;
809 fd.fd = open_library(name);
810 if (fd.fd == -1) {
811 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800812 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700813 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800814
Elliott Hughes46882792012-08-03 16:49:39 -0700815 // Read the ELF header.
816 Elf32_Ehdr header[1];
817 int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200818 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700819 DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
820 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200821 }
822 if (ret != (int)sizeof(header)) {
Elliott Hughes46882792012-08-03 16:49:39 -0700823 DL_ERR("too small to be an ELF executable: %s", name);
824 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200825 }
826 if (verify_elf_header(header) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700827 DL_ERR("not a valid ELF executable: %s", name);
828 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800829 }
830
Elliott Hughes46882792012-08-03 16:49:39 -0700831 // Read the program header table.
832 const Elf32_Phdr* phdr_table;
833 phdr_ptr phdr_holder;
834 ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
835 &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200836 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700837 DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
838 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200839 }
Elliott Hughes46882792012-08-03 16:49:39 -0700840 size_t phdr_count = header->e_phnum;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200841
Elliott Hughes46882792012-08-03 16:49:39 -0700842 // Get the load extents.
843 Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700844 TRACE("[ '%s' wants sz=0x%08x ]\n", name, ext_sz);
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200845 if (ext_sz == 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700846 DL_ERR("no loadable segments in file: %s", name);
847 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800848 }
849
Elliott Hughes46882792012-08-03 16:49:39 -0700850 // We no longer support pre-linked libraries.
851 if (is_prelinked(fd.fd, name)) {
852 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200853 }
David 'Digit' Turner16084162012-06-12 16:25:37 +0200854
Elliott Hughes46882792012-08-03 16:49:39 -0700855 // Reserve address space for all loadable segments.
856 void* load_start = NULL;
857 Elf32_Addr load_size = 0;
858 Elf32_Addr load_bias = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200859 ret = phdr_table_reserve_memory(phdr_table,
860 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200861 &load_start,
862 &load_size,
863 &load_bias);
864 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700865 DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
866 ext_sz, name, strerror(errno));
867 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200868 }
869
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700870 TRACE("[ allocated memory for %s @ %p (0x%08x) ]\n", name, load_start, load_size);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200871
872 /* Map all the segments in our address space with default protections */
873 ret = phdr_table_load_segments(phdr_table,
874 phdr_count,
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200875 load_bias,
Elliott Hughes46882792012-08-03 16:49:39 -0700876 fd.fd);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200877 if (ret < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -0700878 DL_ERR("can't map loadable segments for \"%s\": %s",
879 name, strerror(errno));
880 return NULL;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200881 }
882
Elliott Hughes46882792012-08-03 16:49:39 -0700883 soinfo_ptr si(name);
884 if (si.ptr == NULL) {
885 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800886 }
887
Elliott Hughes46882792012-08-03 16:49:39 -0700888 si.ptr->base = (Elf32_Addr) load_start;
889 si.ptr->size = load_size;
890 si.ptr->load_bias = load_bias;
891 si.ptr->flags = 0;
892 si.ptr->entry = 0;
893 si.ptr->dynamic = (unsigned *)-1;
894 si.ptr->phnum = phdr_count;
895 si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
896 if (si.ptr->phdr == NULL) {
897 DL_ERR("can't find loaded PHDR for \"%s\"", name);
898 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200899 }
Elliott Hughes46882792012-08-03 16:49:39 -0700900
901 return si.release();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800902}
903
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200904static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800905{
906 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700907 const char *bname;
908
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200909 // TODO: don't use basename only for determining libraries
910 // http://code.google.com/p/android/issues/detail?id=6670
911
912 bname = strrchr(name, '/');
913 bname = bname ? bname + 1 : name;
914
915 for(si = solist; si != NULL; si = si->next){
916 if(!strcmp(bname, si->name)) {
917 return si;
918 }
919 }
920 return NULL;
921}
922
Elliott Hughesd23736e2012-11-01 15:16:56 -0700923static soinfo* find_library_internal(const char* name) {
924 if (name == NULL) {
925 return somain;
926 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200927
Elliott Hughesd23736e2012-11-01 15:16:56 -0700928 soinfo* si = find_loaded_library(name);
929 if (si != NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700930 if (si->flags & FLAG_LINKED) {
931 return si;
932 }
933 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
934 return NULL;
935 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800936
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700937 TRACE("[ '%s' has not been loaded yet. Locating...]\n", name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700938 si = load_library(name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800939 if (si == NULL) {
940 return NULL;
941 }
942
943 // At this point we know that whatever is loaded @ base is a valid ELF
944 // shared library whose segments are properly mapped in.
945 TRACE("[ init_library base=0x%08x sz=0x%08x name='%s') ]\n",
946 si->base, si->size, si->name);
947
948 if (!soinfo_link_image(si)) {
949 munmap(reinterpret_cast<void*>(si->base), si->size);
950 soinfo_free(si);
951 return NULL;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700952 }
953
954 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800955}
956
Elliott Hughesd23736e2012-11-01 15:16:56 -0700957static soinfo* find_library(const char* name) {
958 soinfo* si = find_library_internal(name);
959 if (si != NULL) {
960 si->refcount++;
961 }
962 return si;
963}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700964
Elliott Hughesd23736e2012-11-01 15:16:56 -0700965static int soinfo_unload(soinfo* si) {
966 if (si->refcount == 1) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700967 TRACE("unloading '%s'\n", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700968 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800969
Elliott Hughesd23736e2012-11-01 15:16:56 -0700970 for (unsigned* d = si->dynamic; *d; d += 2) {
971 if (d[0] == DT_NEEDED) {
972 soinfo* lsi = find_loaded_library(si->strtab + d[1]);
973 if (lsi != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700974 TRACE("%s needs to unload %s\n", si->name, lsi->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700975 soinfo_unload(lsi);
976 } else {
977 // TODO: should we return -1 in this case?
978 DL_ERR("\"%s\": could not unload dependent library", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800979 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700980 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800981 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700982
983 munmap(reinterpret_cast<void*>(si->base), si->size);
984 notify_gdb_of_unload(si);
985 soinfo_free(si);
986 si->refcount = 0;
987 } else {
988 si->refcount--;
Elliott Hughes9c94fc92012-11-05 09:11:43 -0800989 TRACE("not unloading '%s', decrementing refcount to %d\n", si->name, si->refcount);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700990 }
991 return 0;
992}
993
Elliott Hughescade4c32012-12-20 14:42:14 -0800994void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
995 if (!get_AT_SECURE()) {
996 parse_LD_LIBRARY_PATH(ld_library_path);
997 }
998}
999
Elliott Hughese66190d2012-12-18 15:57:55 -08001000soinfo* do_dlopen(const char* name, int flags) {
1001 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
1002 DL_ERR("invalid flags to dlopen: %x", flags);
1003 return NULL;
1004 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001005 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
1006 soinfo* si = find_library(name);
1007 if (si != NULL) {
1008 si->CallConstructors();
1009 }
1010 set_soinfo_pool_protection(PROT_READ);
1011 return si;
1012}
1013
1014int do_dlclose(soinfo* si) {
1015 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
1016 int result = soinfo_unload(si);
1017 set_soinfo_pool_protection(PROT_READ);
1018 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001019}
1020
1021/* TODO: don't use unsigned for addrs below. It works, but is not
1022 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
1023 * long.
1024 */
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001025static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count,
1026 soinfo *needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001027{
1028 Elf32_Sym *symtab = si->symtab;
1029 const char *strtab = si->strtab;
1030 Elf32_Sym *s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001031 Elf32_Rel *start = rel;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001032 soinfo *lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001033
Elliott Hughes46882792012-08-03 16:49:39 -07001034 for (size_t idx = 0; idx < count; ++idx, ++rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001035 unsigned type = ELF32_R_TYPE(rel->r_info);
1036 unsigned sym = ELF32_R_SYM(rel->r_info);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001037 unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001038 unsigned sym_addr = 0;
1039 char *sym_name = NULL;
1040
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001041 DEBUG("Processing '%s' relocation at index %d\n", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001042 if (type == 0) { // R_*_NONE
1043 continue;
1044 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001045 if(sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001046 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001047 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Doug Kwane8238072009-10-26 12:05:23 -07001048 if(s == NULL) {
1049 /* We only allow an undefined symbol if this is a weak
1050 reference.. */
1051 s = &symtab[sym];
1052 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -07001053 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -07001054 return -1;
1055 }
1056
1057 /* IHI0044C AAELF 4.5.1.1:
1058
1059 Libraries are not searched to resolve weak references.
1060 It is not an error for a weak reference to remain
1061 unsatisfied.
1062
1063 During linking, the value of an undefined weak reference is:
1064 - Zero if the relocation type is absolute
1065 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -07001066 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -07001067 type is base-relative.
1068 */
1069
1070 switch (type) {
1071#if defined(ANDROID_ARM_LINKER)
1072 case R_ARM_JUMP_SLOT:
1073 case R_ARM_GLOB_DAT:
1074 case R_ARM_ABS32:
1075 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -07001076#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001077 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001078 case R_386_GLOB_DAT:
1079 case R_386_32:
1080 case R_386_RELATIVE: /* Dont' care. */
1081#endif /* ANDROID_*_LINKER */
1082 /* sym_addr was initialized to be zero above or relocation
1083 code below does not care about value of sym_addr.
1084 No need to do anything. */
1085 break;
1086
1087#if defined(ANDROID_X86_LINKER)
1088 case R_386_PC32:
1089 sym_addr = reloc;
1090 break;
1091#endif /* ANDROID_X86_LINKER */
1092
1093#if defined(ANDROID_ARM_LINKER)
1094 case R_ARM_COPY:
1095 /* Fall through. Can't really copy if weak symbol is
1096 not found in run-time. */
1097#endif /* ANDROID_ARM_LINKER */
1098 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001099 DL_ERR("unknown weak reloc type %d @ %p (%d)",
1100 type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -07001101 return -1;
1102 }
1103 } else {
1104 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001105#if 0
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001106 if((base == 0) && (si->base != 0)){
1107 /* linking from libraries to main image is bad */
Elliott Hughes46882792012-08-03 16:49:39 -07001108 DL_ERR("cannot locate \"%s\"...",
1109 strtab + symtab[sym].st_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001110 return -1;
1111 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001112#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001113 sym_addr = (unsigned)(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001114 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001115 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001116 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001117 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001118 }
1119
1120/* TODO: This is ugly. Split up the relocations by arch into
1121 * different files.
1122 */
1123 switch(type){
1124#if defined(ANDROID_ARM_LINKER)
1125 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001126 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001127 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001128 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 -08001129 *((unsigned*)reloc) = sym_addr;
1130 break;
1131 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001132 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001133 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001134 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 -08001135 *((unsigned*)reloc) = sym_addr;
1136 break;
1137 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001138 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001139 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001140 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s\n", reloc, sym_addr, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001141 *((unsigned*)reloc) += sym_addr;
1142 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001143 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001144 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001145 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001146 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s\n",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001147 reloc, sym_addr, rel->r_offset, sym_name);
1148 *((unsigned*)reloc) += sym_addr - rel->r_offset;
1149 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001150#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001151 case R_386_JMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001152 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001153 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001154 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 -08001155 *((unsigned*)reloc) = sym_addr;
1156 break;
1157 case R_386_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001158 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001159 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001160 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 -08001161 *((unsigned*)reloc) = sym_addr;
1162 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001163#elif defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001164 case R_MIPS_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001165 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001166 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001167 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s\n",
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001168 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1169 if (s) {
1170 *((unsigned*)reloc) += sym_addr;
1171 } else {
1172 *((unsigned*)reloc) += si->base;
1173 }
1174 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001175#endif /* ANDROID_*_LINKER */
1176
1177#if defined(ANDROID_ARM_LINKER)
1178 case R_ARM_RELATIVE:
1179#elif defined(ANDROID_X86_LINKER)
1180 case R_386_RELATIVE:
1181#endif /* ANDROID_*_LINKER */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001182 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001183 MARK(rel->r_offset);
Elliott Hughes46882792012-08-03 16:49:39 -07001184 if (sym) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001185 DL_ERR("odd RELATIVE form...");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001186 return -1;
1187 }
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001188 TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x\n", reloc, si->base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001189 *((unsigned*)reloc) += si->base;
1190 break;
1191
1192#if defined(ANDROID_X86_LINKER)
1193 case R_386_32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001194 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001195 MARK(rel->r_offset);
1196
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001197 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 -08001198 *((unsigned *)reloc) += (unsigned)sym_addr;
1199 break;
1200
1201 case R_386_PC32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001202 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001203 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001204 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s\n",
1205 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001206 *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
1207 break;
1208#endif /* ANDROID_X86_LINKER */
1209
1210#ifdef ANDROID_ARM_LINKER
1211 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001212 if ((si->flags & FLAG_EXE) == 0) {
1213 /*
1214 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1215 *
1216 * Section 4.7.1.10 "Dynamic relocations"
1217 * R_ARM_COPY may only appear in executable objects where e_type is
1218 * set to ET_EXEC.
1219 *
1220 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1221 * We should explicitly disallow ET_DYN executables from having
1222 * R_ARM_COPY relocations.
1223 */
1224 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1225 return -1;
1226 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001227 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001228 MARK(rel->r_offset);
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001229 TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s\n", reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001230 if (reloc == sym_addr) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001231 Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1232
1233 if (src == NULL) {
1234 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1235 return -1;
1236 }
1237 if (lsi->has_DT_SYMBOLIC) {
1238 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1239 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1240 return -1;
1241 }
1242 if (s->st_size < src->st_size) {
1243 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1244 si->name, s->st_size, src->st_size);
1245 return -1;
1246 }
1247 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1248 } else {
1249 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001250 return -1;
1251 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001252 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001253#endif /* ANDROID_ARM_LINKER */
1254
1255 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001256 DL_ERR("unknown reloc type %d @ %p (%d)",
1257 type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001258 return -1;
1259 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001260 }
1261 return 0;
1262}
1263
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001264#ifdef ANDROID_MIPS_LINKER
Elliott Hughesbedfe382012-08-14 14:07:59 -07001265static int mips_relocate_got(soinfo* si, soinfo* needed[]) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001266 unsigned *got;
1267 unsigned local_gotno, gotsym, symtabno;
1268 Elf32_Sym *symtab, *sym;
1269 unsigned g;
1270
1271 got = si->plt_got;
1272 local_gotno = si->mips_local_gotno;
1273 gotsym = si->mips_gotsym;
1274 symtabno = si->mips_symtabno;
1275 symtab = si->symtab;
1276
1277 /*
1278 * got[0] is address of lazy resolver function
1279 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001280 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001281 * (should be _rtld_bind_start)
1282 * FIXME: maybe this should be in a separate routine
1283 */
1284
1285 if ((si->flags & FLAG_LINKER) == 0) {
1286 g = 0;
1287 got[g++] = 0xdeadbeef;
1288 if (got[g] & 0x80000000) {
1289 got[g++] = 0xdeadfeed;
1290 }
1291 /*
1292 * Relocate the local GOT entries need to be relocated
1293 */
1294 for (; g < local_gotno; g++) {
1295 got[g] += si->load_bias;
1296 }
1297 }
1298
1299 /* Now for the global GOT entries */
1300 sym = symtab + gotsym;
1301 got = si->plt_got + local_gotno;
1302 for (g = gotsym; g < symtabno; g++, sym++, got++) {
1303 const char *sym_name;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001304 Elf32_Sym *s;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001305 soinfo *lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001306
1307 /* This is an undefined reference... try to locate it */
1308 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001309 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001310 if (s == NULL) {
1311 /* We only allow an undefined symbol if this is a weak
1312 reference.. */
1313 s = &symtab[g];
1314 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001315 DL_ERR("cannot locate \"%s\"...", sym_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001316 return -1;
1317 }
1318 *got = 0;
1319 }
1320 else {
1321 /* FIXME: is this sufficient?
1322 * For reference see NetBSD link loader
1323 * 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
1324 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001325 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001326 }
1327 }
1328 return 0;
1329}
1330#endif
1331
David 'Digit' Turner82156792009-05-18 14:37:41 +02001332/* Please read the "Initialization and Termination functions" functions.
1333 * of the linker design note in bionic/linker/README.TXT to understand
1334 * what the following code is doing.
1335 *
1336 * The important things to remember are:
1337 *
1338 * DT_PREINIT_ARRAY must be called first for executables, and should
1339 * not appear in shared libraries.
1340 *
1341 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1342 *
1343 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1344 *
1345 * DT_FINI_ARRAY must be parsed in reverse order.
1346 */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001347void soinfo::CallArray(const char* array_name UNUSED, unsigned* array, int count, bool reverse) {
1348 if (array == NULL) {
1349 return;
1350 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001351
Elliott Hughesd23736e2012-11-01 15:16:56 -07001352 int step = 1;
1353 if (reverse) {
1354 array += (count-1);
1355 step = -1;
1356 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001357
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001358 TRACE("[ Calling %s @ %p [%d] for '%s' ]\n", array_name, array, count, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001359
1360 for (int n = count; n > 0; n--) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001361 TRACE("[ Looking at %s[%d] *%p == 0x%08x ]\n", array_name, n, array, *array);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001362 void (*func)() = (void (*)()) *array;
1363 array += step;
Elliott Hughesdb492b32013-01-03 15:44:03 -08001364 CallFunction("function", func);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001365 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001366
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001367 TRACE("[ Done calling %s for '%s' ]\n", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001368}
1369
Elliott Hughesd23736e2012-11-01 15:16:56 -07001370void soinfo::CallFunction(const char* function_name UNUSED, void (*function)()) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001371 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001372 return;
1373 }
1374
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001375 TRACE("[ Calling %s @ %p for '%s' ]\n", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001376 function();
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001377 TRACE("[ Done calling %s for '%s' ]\n", function_name, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001378
1379 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1380 // are still writable. This happens with our debug malloc (see http://b/7941716).
1381 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001382}
1383
Elliott Hughesd23736e2012-11-01 15:16:56 -07001384void soinfo::CallPreInitConstructors() {
1385 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1386}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001387
Elliott Hughesd23736e2012-11-01 15:16:56 -07001388void soinfo::CallConstructors() {
1389 if (constructors_called) {
1390 return;
1391 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001392
Elliott Hughesd23736e2012-11-01 15:16:56 -07001393 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1394 // protect against recursive constructor calls. One simple example of constructor recursion
1395 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1396 // 1. The program depends on libc, so libc's constructor is called here.
1397 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1398 // 3. dlopen() calls the constructors on the newly created
1399 // soinfo for libc_malloc_debug_leak.so.
1400 // 4. The debug .so depends on libc, so CallConstructors is
1401 // called again with the libc soinfo. If it doesn't trigger the early-
1402 // out above, the libc constructor will be called again (recursively!).
1403 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001404
Elliott Hughesd23736e2012-11-01 15:16:56 -07001405 if (!(flags & FLAG_EXE) && preinit_array) {
1406 DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array);
1407 return;
1408 }
1409
1410 if (dynamic) {
1411 for (unsigned* d = dynamic; *d; d += 2) {
1412 if (d[0] == DT_NEEDED) {
1413 soinfo* lsi = find_loaded_library(strtab + d[1]);
1414 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;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001498 int relocating_linker = (si->flags & FLAG_LINKER) != 0;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001499 soinfo **needed, **pneeded;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001500
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001501 /* We can't debug anything until the linker is relocated */
1502 if (!relocating_linker) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001503 INFO("[ linking %s ]\n", si->name);
1504 DEBUG("si->base = 0x%08x si->flags = 0x%08x\n", si->base, si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001505 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001506
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001507 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001508 size_t dynamic_count;
Chris Dearmancf239052013-01-11 15:32:20 -08001509 Elf32_Word dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001510 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001511 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001512 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001513 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001514 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001515 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001516 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001517 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001518 if (!relocating_linker) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001519 DEBUG("dynamic = %p\n", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001520 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001521 }
1522
1523#ifdef ANDROID_ARM_LINKER
1524 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1525 &si->ARM_exidx, &si->ARM_exidx_count);
1526#endif
1527
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001528 /* extract useful information from dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001529 for (unsigned* d = si->dynamic; *d; ++d) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001530 DEBUG("d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", d, d[0], d[1]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001531 switch(*d++){
1532 case DT_HASH:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001533 si->nbucket = ((unsigned *) (base + *d))[0];
1534 si->nchain = ((unsigned *) (base + *d))[1];
1535 si->bucket = (unsigned *) (base + *d + 8);
1536 si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001537 break;
1538 case DT_STRTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001539 si->strtab = (const char *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001540 break;
1541 case DT_SYMTAB:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001542 si->symtab = (Elf32_Sym *) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001543 break;
1544 case DT_PLTREL:
1545 if(*d != 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:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001551 si->plt_rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001552 break;
1553 case DT_PLTRELSZ:
1554 si->plt_rel_count = *d / 8;
1555 break;
1556 case DT_REL:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001557 si->rel = (Elf32_Rel*) (base + *d);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001558 break;
1559 case DT_RELSZ:
1560 si->rel_count = *d / 8;
1561 break;
1562 case DT_PLTGOT:
1563 /* Save this in case we decide to do lazy binding. We don't yet. */
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001564 si->plt_got = (unsigned *)(base + *d);
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) {
Chris Dearmancf239052013-01-11 15:32:20 -08001570 *d = (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:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001577 si->init_func = (void (*)(void))(base + *d);
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:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001581 si->fini_func = (void (*)(void))(base + *d);
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:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001585 si->init_array = (unsigned *)(base + *d);
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:
1589 si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1590 break;
1591 case DT_FINI_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001592 si->fini_array = (unsigned *)(base + *d);
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:
1596 si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1597 break;
1598 case DT_PREINIT_ARRAY:
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001599 si->preinit_array = (unsigned *)(base + *d);
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:
1603 si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
1604 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;
1611#if defined(DT_FLAGS)
1612 case DT_FLAGS:
1613 if (*d & DF_TEXTREL) {
1614 si->has_text_relocations = true;
1615 }
1616 if (*d & DF_SYMBOLIC) {
1617 si->has_DT_SYMBOLIC = true;
1618 }
1619 break;
1620#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001621#if defined(ANDROID_MIPS_LINKER)
1622 case DT_NEEDED:
1623 case DT_STRSZ:
1624 case DT_SYMENT:
1625 case DT_RELENT:
1626 break;
1627 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001628 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001629 {
Elliott Hughesbedfe382012-08-14 14:07:59 -07001630 r_debug** dp = (r_debug**) *d;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001631 *dp = &_r_debug;
1632 }
1633 break;
1634 case DT_MIPS_RLD_VERSION:
1635 case DT_MIPS_FLAGS:
1636 case DT_MIPS_BASE_ADDRESS:
1637 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001638 break;
1639
1640 case DT_MIPS_SYMTABNO:
1641 si->mips_symtabno = *d;
1642 break;
1643
1644 case DT_MIPS_LOCAL_GOTNO:
1645 si->mips_local_gotno = *d;
1646 break;
1647
1648 case DT_MIPS_GOTSYM:
1649 si->mips_gotsym = *d;
1650 break;
1651
1652 default:
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001653 DEBUG("Unused DT entry: type 0x%08x arg 0x%08x\n", d[-1], d[0]);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001654 break;
1655#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001656 }
1657 }
1658
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001659 DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
1660 si->base, si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001661
Elliott Hughes124fae92012-10-31 14:20:03 -07001662 // Sanity checks.
1663 if (si->nbucket == 0) {
1664 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1665 return false;
1666 }
1667 if (si->strtab == 0) {
1668 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1669 return false;
1670 }
1671 if (si->symtab == 0) {
1672 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1673 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001674 }
1675
Matt Fischer4fd42c12009-12-31 12:09:10 -06001676 /* if this is the main executable, then load all of the preloads now */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001677 if (si->flags & FLAG_EXE) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001678 memset(preloads, 0, sizeof(preloads));
Elliott Hughesd23736e2012-11-01 15:16:56 -07001679 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1680 soinfo* lsi = find_library(gLdPreloadNames[i]);
1681 if (lsi == NULL) {
Matt Fischer4fd42c12009-12-31 12:09:10 -06001682 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001683 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Elliott Hughes124fae92012-10-31 14:20:03 -07001684 gLdPreloadNames[i], si->name, tmp_err_buf);
1685 return false;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001686 }
Matt Fischer4fd42c12009-12-31 12:09:10 -06001687 preloads[i] = lsi;
1688 }
1689 }
1690
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001691 /* dynamic_count is an upper bound for the number of needed libs */
1692 pneeded = needed = (soinfo**) alloca((1 + dynamic_count) * sizeof(soinfo*));
1693
Elliott Hughes124fae92012-10-31 14:20:03 -07001694 for (unsigned* d = si->dynamic; *d; d += 2) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001695 if (d[0] == DT_NEEDED) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001696 DEBUG("%s needs %s\n", si->name, si->strtab + d[1]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001697 soinfo* lsi = find_library(si->strtab + d[1]);
1698 if (lsi == NULL) {
Dima Zavin03531952009-05-29 17:30:25 -07001699 strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001700 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1701 si->strtab + d[1], si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001702 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001703 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001704 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001705 }
1706 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001707 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001708
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001709 if (si->has_text_relocations) {
1710 /* Unprotect the segments, i.e. make them writable, to allow
1711 * text relocations to work properly. We will later call
1712 * phdr_table_protect_segments() after all of them are applied
1713 * and all constructors are run.
1714 */
1715 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1716 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1717 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001718 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001719 }
1720 }
1721
Elliott Hughes124fae92012-10-31 14:20:03 -07001722 if (si->plt_rel) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001723 DEBUG("[ relocating %s plt ]\n", si->name );
Elliott Hughes124fae92012-10-31 14:20:03 -07001724 if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1725 return false;
1726 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001727 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001728 if (si->rel) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001729 DEBUG("[ relocating %s ]\n", si->name );
Elliott Hughes124fae92012-10-31 14:20:03 -07001730 if(soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1731 return false;
1732 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001733 }
1734
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001735#ifdef ANDROID_MIPS_LINKER
Elliott Hughes124fae92012-10-31 14:20:03 -07001736 if (mips_relocate_got(si, needed)) {
1737 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001738 }
1739#endif
1740
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001741 si->flags |= FLAG_LINKED;
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001742 DEBUG("[ finished linking %s ]\n", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001743
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001744 if (si->has_text_relocations) {
1745 /* All relocations are done, we can protect our segments back to
1746 * read-only. */
1747 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1748 DL_ERR("can't protect segments for \"%s\": %s",
1749 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001750 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001751 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001752 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001753
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001754 /* We can also turn on GNU RELRO protection */
1755 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001756 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1757 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001758 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001759 }
1760
Elliott Hughes124fae92012-10-31 14:20:03 -07001761 // If this is a setuid/setgid program, close the security hole described in
1762 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
Elliott Hughes18a206c2012-10-29 17:37:13 -07001763 if (get_AT_SECURE()) {
Elliott Hughes46882792012-08-03 16:49:39 -07001764 nullify_closed_stdio();
1765 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001766 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001767 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001768}
1769
Nick Kralevich468319c2011-11-11 15:53:17 -08001770/*
1771 * This code is called after the linker has linked itself and
1772 * fixed it's own GOT. It is safe to make references to externs
1773 * and other non-local data at this point.
1774 */
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001775static unsigned __linker_init_post_relocation(KernelArgumentBlock& args, unsigned linker_base) {
1776 /* NOTE: we store the args pointer on a special location
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001777 * of the temporary TLS area in order to pass it to
1778 * the C Library's runtime initializer.
1779 *
1780 * The initializer must clear the slot and reset the TLS
1781 * to point to a different location to ensure that no other
1782 * shared library constructor can access it.
1783 */
Elliott Hughesd3920b32013-02-07 18:39:34 -08001784 __libc_init_tls(args);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001785
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001786#if TIMING
1787 struct timeval t0, t1;
1788 gettimeofday(&t0, 0);
1789#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001790
Elliott Hughes18a206c2012-10-29 17:37:13 -07001791 // Initialize environment functions, and get to the ELF aux vectors table.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001792 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001793
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001794 debugger_init();
1795
Elliott Hughes18a206c2012-10-29 17:37:13 -07001796 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001797 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1798 if (LD_DEBUG != NULL) {
1799 debug_verbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001800 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001801
Elliott Hughes18a206c2012-10-29 17:37:13 -07001802 // Normally, these are cleaned by linker_env_init, but the test
1803 // doesn't cost us anything.
1804 const char* ldpath_env = NULL;
1805 const char* ldpreload_env = NULL;
1806 if (!get_AT_SECURE()) {
1807 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1808 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001809 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001810
1811 INFO("[ android linker & debugger ]\n");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001812
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001813 soinfo* si = soinfo_alloc(args.argv[0]);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001814 if (si == NULL) {
1815 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001816 }
1817
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001818 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001819 si->flags |= FLAG_EXE;
Elliott Hughesbedfe382012-08-14 14:07:59 -07001820 link_map* map = &(si->linkmap);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001821
1822 map->l_addr = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001823 map->l_name = args.argv[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001824 map->l_prev = NULL;
1825 map->l_next = NULL;
1826
1827 _r_debug.r_map = map;
1828 r_debug_tail = map;
1829
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001830 /* gdb expects the linker to be in the debug shared object list.
1831 * Without this, gdb has trouble locating the linker's ".text"
1832 * and ".plt" sections. Gdb could also potentially use this to
1833 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1834 * Don't use soinfo_alloc(), because the linker shouldn't
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001835 * be on the soinfo list.
1836 */
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001837 static soinfo linker_soinfo;
1838 strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001839 linker_soinfo.flags = 0;
Ryan V. Bissellbb5c30a2012-07-16 02:16:18 -05001840 linker_soinfo.base = linker_base;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001841
Ben Cheng06f0e742012-08-10 16:07:02 -07001842 /*
1843 * Set the dynamic field in the link map otherwise gdb will complain with
1844 * the following:
1845 * warning: .dynamic section for "/system/bin/linker" is not at the
1846 * expected address (wrong library or version mismatch?)
1847 */
1848 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001849 Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001850 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Chris Dearmancf239052013-01-11 15:32:20 -08001851 &linker_soinfo.dynamic, NULL, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001852 insert_soinfo_into_debug_map(&linker_soinfo);
1853
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001854 // Extract information passed from the kernel.
1855 si->phdr = reinterpret_cast<Elf32_Phdr*>(args.getauxval(AT_PHDR));
1856 si->phnum = args.getauxval(AT_PHNUM);
1857 si->entry = args.getauxval(AT_ENTRY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001858
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001859 /* Compute the value of si->base. We can't rely on the fact that
1860 * the first entry is the PHDR because this will not be true
1861 * for certain executables (e.g. some in the NDK unit test suite)
1862 */
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001863 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001864 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001865 si->load_bias = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001866 for (int i = 0; i < si->phnum; ++i) {
1867 if (si->phdr[i].p_type == PT_PHDR) {
1868 si->load_bias = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_vaddr;
1869 si->base = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_offset;
1870 break;
1871 }
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001872 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001873 si->dynamic = (unsigned *)-1;
David 'Digit' Turner67748092010-07-21 16:18:21 -07001874 si->refcount = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001875
Elliott Hughes46882792012-08-03 16:49:39 -07001876 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1877 parse_LD_LIBRARY_PATH(ldpath_env);
1878 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001879
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001880 somain = si;
1881
Elliott Hughes124fae92012-10-31 14:20:03 -07001882 if (!soinfo_link_image(si)) {
Chris Dearman20a24402012-10-31 05:39:27 -07001883 const char* msg = "CANNOT LINK EXECUTABLE\n";
Dima Zavin2e855792009-05-20 18:28:09 -07001884 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
Chris Dearman20a24402012-10-31 05:39:27 -07001885 write(2, msg, strlen(msg));
Elliott Hughes18a206c2012-10-29 17:37:13 -07001886 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001887 }
1888
Elliott Hughesd23736e2012-11-01 15:16:56 -07001889 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001890
Elliott Hughes18a206c2012-10-29 17:37:13 -07001891 for (size_t i = 0; preloads[i] != NULL; ++i) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001892 preloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08001893 }
1894
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08001895 /*After the link_image, the si->load_bias is initialized.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08001896 *For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1897 *We need to update this value for so exe here. So Unwind_Backtrace
1898 *for some arch like x86 could work correctly within so exe.
1899 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08001900 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001901 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001902
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001903#if TIMING
1904 gettimeofday(&t1,NULL);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001905 PRINT("LINKER TIME: %s: %d microseconds\n", e.argv[0], (int) (
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001906 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1907 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1908 ));
1909#endif
1910#if STATS
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001911 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", e.argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07001912 linker_stats.count[kRelocAbsolute],
1913 linker_stats.count[kRelocRelative],
1914 linker_stats.count[kRelocCopy],
1915 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001916#endif
1917#if COUNT_PAGES
1918 {
1919 unsigned n;
1920 unsigned i;
1921 unsigned count = 0;
1922 for(n = 0; n < 4096; n++){
1923 if(bitmask[n]){
1924 unsigned x = bitmask[n];
1925 for(i = 0; i < 8; i++){
1926 if(x & 1) count++;
1927 x >>= 1;
1928 }
1929 }
1930 }
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001931 PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", e.argv[0], count, count * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001932 }
1933#endif
1934
1935#if TIMING || STATS || COUNT_PAGES
1936 fflush(stdout);
1937#endif
1938
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001939 TRACE("[ Ready to execute '%s' @ 0x%08x ]\n", si->name, si->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001940 return si->entry;
1941}
Nick Kralevich468319c2011-11-11 15:53:17 -08001942
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001943/* Compute the load-bias of an existing executable. This shall only
1944 * be used to compute the load bias of an executable or shared library
1945 * that was loaded by the kernel itself.
1946 *
1947 * Input:
1948 * elf -> address of ELF header, assumed to be at the start of the file.
1949 * Return:
1950 * load bias, i.e. add the value of any p_vaddr in the file to get
1951 * the corresponding address in memory.
1952 */
1953static Elf32_Addr
1954get_elf_exec_load_bias(const Elf32_Ehdr* elf)
1955{
1956 Elf32_Addr offset = elf->e_phoff;
1957 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
1958 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
1959 const Elf32_Phdr* phdr;
1960
1961 for (phdr = phdr_table; phdr < phdr_end; phdr++) {
1962 if (phdr->p_type == PT_LOAD) {
1963 return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
1964 }
1965 }
1966 return 0;
1967}
1968
Nick Kralevich468319c2011-11-11 15:53:17 -08001969/*
1970 * This is the entry point for the linker, called from begin.S. This
1971 * method is responsible for fixing the linker's own relocations, and
1972 * then calling __linker_init_post_relocation().
1973 *
1974 * Because this method is called before the linker has fixed it's own
1975 * relocations, any attempt to reference an extern variable, extern
1976 * function, or other GOT reference will generate a segfault.
1977 */
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001978extern "C" unsigned __linker_init(void* raw_args) {
1979 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08001980
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001981 unsigned linker_addr = args.getauxval(AT_BASE);
Nick Kralevich468319c2011-11-11 15:53:17 -08001982
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001983 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr*) linker_addr;
1984 Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08001985
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001986 soinfo linker_so;
1987 memset(&linker_so, 0, sizeof(soinfo));
Nick Kralevich468319c2011-11-11 15:53:17 -08001988
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001989 linker_so.base = linker_addr;
1990 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
1991 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
1992 linker_so.dynamic = (unsigned*) -1;
1993 linker_so.phdr = phdr;
1994 linker_so.phnum = elf_hdr->e_phnum;
1995 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07001996
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001997 if (!soinfo_link_image(&linker_so)) {
1998 // It would be nice to print an error message, but if the linker
1999 // can't link itself, there's no guarantee that we'll be able to
2000 // call write() (because it involves a GOT reference).
2001 //
2002 // This situation should never occur unless the linker itself
2003 // is corrupt.
2004 exit(EXIT_FAILURE);
2005 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002006
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002007 // We have successfully fixed our own relocations. It's safe to run
2008 // the main part of the linker now.
2009 unsigned start_address = __linker_init_post_relocation(args, linker_addr);
2010
2011 set_soinfo_pool_protection(PROT_READ);
2012
2013 // Return the address that the calling assembly stub should jump to.
2014 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002015}