blob: 81ca2f50f0b15b60b1d5d623d69b802653dc8212 [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>
Marcus Oaklande365f9d2013-10-10 15:19:31 +010034#include <stdint.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038#include <sys/atomics.h>
Elliott Hughes46882792012-08-03 16:49:39 -070039#include <sys/mman.h>
40#include <sys/stat.h>
41#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042
Elliott Hughes46882792012-08-03 16:49:39 -070043// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070044#include "private/bionic_tls.h"
45#include "private/KernelArgumentBlock.h"
46#include "private/ScopedPthreadMutexLocker.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047
48#include "linker.h"
49#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010050#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020051#include "linker_phdr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
David Bartleybc3a5c22009-06-02 18:27:28 -070053/* Assume average path length of 64 and max 8 paths */
54#define LDPATH_BUFSIZE 512
55#define LDPATH_MAX 8
56
Matt Fischer4fd42c12009-12-31 12:09:10 -060057#define LDPRELOAD_BUFSIZE 512
58#define LDPRELOAD_MAX 8
59
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
61 *
62 * Do NOT use malloc() and friends or pthread_*() code here.
63 * Don't use printf() either; it's caused mysterious memory
64 * corruption in the past.
65 * The linker runs before we bring up libc and it's easiest
66 * to make sure it does not depend on any complex libc features
67 *
68 * open issues / todo:
69 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070 * - are we doing everything we should for ARM_COPY relocations?
71 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072 * - after linking, set as much stuff as possible to READONLY
73 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070074 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075
Elliott Hughes124fae92012-10-31 14:20:03 -070076static bool soinfo_link_image(soinfo* si);
Sergey Melnikovebd506c2013-10-31 18:02:12 +040077static Elf_Addr get_elf_exec_load_bias(const Elf_Ehdr* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
Magnus Malmbornba98d922012-09-12 13:00:55 +020079// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
80// maps, each a single page in size. The pages are broken up into as many struct soinfo
81// objects as will fit, and they're all threaded together on a free list.
82#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
83struct soinfo_pool_t {
84 soinfo_pool_t* next;
85 soinfo info[SOINFO_PER_POOL];
86};
87static struct soinfo_pool_t* gSoInfoPools = NULL;
88static soinfo* gSoInfoFreeList = NULL;
89
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080090static soinfo* solist = &libdl_info;
91static soinfo* sonext = &libdl_info;
92static soinfo* somain; /* main process, always the one after libdl_info */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
Elliott Hughes124fae92012-10-31 14:20:03 -070094static const char* const gSoPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070095#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070096 "/vendor/lib64",
97 "/system/lib64",
98#else
Elliott Hughes124fae92012-10-31 14:20:03 -070099 "/vendor/lib",
100 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700101#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700102 NULL
103};
David Bartleybc3a5c22009-06-02 18:27:28 -0700104
Elliott Hughes124fae92012-10-31 14:20:03 -0700105static char gLdPathsBuffer[LDPATH_BUFSIZE];
106static const char* gLdPaths[LDPATH_MAX + 1];
107
108static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
109static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600110
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800111static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600112
Elliott Hughes650be4e2013-03-05 18:47:58 -0800113__LIBC_HIDDEN__ int gLdDebugVerbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
Elliott Hughes0d787c12013-04-04 13:46:46 -0700115__LIBC_HIDDEN__ abort_msg_t* gAbortMessage = NULL; // For debuggerd.
116
Elliott Hughesbedfe382012-08-14 14:07:59 -0700117enum RelocationKind {
118 kRelocAbsolute = 0,
119 kRelocRelative,
120 kRelocCopy,
121 kRelocSymbol,
122 kRelocMax
123};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100124
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700126struct linker_stats_t {
127 int count[kRelocMax];
128};
129
130static linker_stats_t linker_stats;
131
132static void count_relocation(RelocationKind kind) {
133 ++linker_stats.count[kind];
134}
135#else
136static void count_relocation(RelocationKind) {
137}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138#endif
139
140#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700141static unsigned bitmask[4096];
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100142#if defined(__LP64__)
143#define MARK(offset) \
144 do { \
145 if ((((offset) >> 12) >> 5) < 4096) \
146 bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
147 } while(0)
148#else
Elliott Hughesbedfe382012-08-14 14:07:59 -0700149#define MARK(offset) \
150 do { \
151 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
152 } while(0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100153#endif
Elliott Hughesbedfe382012-08-14 14:07:59 -0700154#else
155#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800156#endif
157
Elliott Hughes46882792012-08-03 16:49:39 -0700158// You shouldn't try to call memory-allocating functions in the dynamic linker.
159// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700160#define DISALLOW_ALLOCATION(return_type, name, ...) \
161 return_type name __VA_ARGS__ \
162 { \
Elliott Hughes46882792012-08-03 16:49:39 -0700163 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700164 __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
165 write(2, msg, strlen(msg)); \
166 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700167 }
Elliott Hughes46882792012-08-03 16:49:39 -0700168#define UNUSED __attribute__((unused))
169DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
170DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
171DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
172DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
Dima Zavin2e855792009-05-20 18:28:09 -0700173
Dima Zavin03531952009-05-29 17:30:25 -0700174static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700175static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700176
Elliott Hughes650be4e2013-03-05 18:47:58 -0800177char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700178 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700179}
180
Elliott Hughes650be4e2013-03-05 18:47:58 -0800181size_t linker_get_error_buffer_size() {
182 return sizeof(__linker_dl_err_buf);
183}
184
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185/*
186 * This function is an empty stub where GDB locates a breakpoint to get notified
187 * about linker activity.
188 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700189extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190
Elliott Hughes0d787c12013-04-04 13:46:46 -0700191static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity, RT_CONSISTENT, 0};
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700192static link_map_t* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193
Elliott Hughes3b297c42012-10-11 16:08:51 -0700194static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195
Elliott Hughesbedfe382012-08-14 14:07:59 -0700196static void insert_soinfo_into_debug_map(soinfo * info) {
197 // Copy the necessary fields into the debug structure.
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700198 link_map_t* map = &(info->link_map);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400199 map->l_addr = info->load_bias;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800201 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202
203 /* Stick the new library at the end of the list.
204 * gdb tends to care more about libc than it does
205 * about leaf libraries, and ordering it this way
206 * reduces the back-and-forth over the wire.
207 */
208 if (r_debug_tail) {
209 r_debug_tail->l_next = map;
210 map->l_prev = r_debug_tail;
211 map->l_next = 0;
212 } else {
213 _r_debug.r_map = map;
214 map->l_prev = 0;
215 map->l_next = 0;
216 }
217 r_debug_tail = map;
218}
219
Elliott Hughesbedfe382012-08-14 14:07:59 -0700220static void remove_soinfo_from_debug_map(soinfo* info) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700221 link_map_t* map = &(info->link_map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700222
Elliott Hughesbedfe382012-08-14 14:07:59 -0700223 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700224 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700225 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700226
Elliott Hughesbedfe382012-08-14 14:07:59 -0700227 if (map->l_prev) {
228 map->l_prev->l_next = map->l_next;
229 }
230 if (map->l_next) {
231 map->l_next->l_prev = map->l_prev;
232 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700233}
234
Elliott Hughesbedfe382012-08-14 14:07:59 -0700235static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236 if (info->flags & FLAG_EXE) {
237 // GDB already knows about the main executable
238 return;
239 }
240
Elliott Hughes3b297c42012-10-11 16:08:51 -0700241 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800242
243 _r_debug.r_state = RT_ADD;
244 rtld_db_dlactivity();
245
246 insert_soinfo_into_debug_map(info);
247
248 _r_debug.r_state = RT_CONSISTENT;
249 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700250}
251
Elliott Hughesbedfe382012-08-14 14:07:59 -0700252static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700253 if (info->flags & FLAG_EXE) {
254 // GDB already knows about the main executable
255 return;
256 }
257
Elliott Hughes3b297c42012-10-11 16:08:51 -0700258 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700259
260 _r_debug.r_state = RT_DELETE;
261 rtld_db_dlactivity();
262
263 remove_soinfo_from_debug_map(info);
264
265 _r_debug.r_state = RT_CONSISTENT;
266 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800267}
268
Elliott Hughes18a206c2012-10-29 17:37:13 -0700269void notify_gdb_of_libraries() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800270 _r_debug.r_state = RT_ADD;
271 rtld_db_dlactivity();
272 _r_debug.r_state = RT_CONSISTENT;
273 rtld_db_dlactivity();
274}
275
Magnus Malmbornba98d922012-09-12 13:00:55 +0200276static bool ensure_free_list_non_empty() {
277 if (gSoInfoFreeList != NULL) {
278 return true;
279 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800280
Magnus Malmbornba98d922012-09-12 13:00:55 +0200281 // Allocate a new pool.
282 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
283 PROT_READ|PROT_WRITE,
284 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
285 if (pool == MAP_FAILED) {
286 return false;
287 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800288
Magnus Malmbornba98d922012-09-12 13:00:55 +0200289 // Add the pool to our list of pools.
290 pool->next = gSoInfoPools;
291 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292
Magnus Malmbornba98d922012-09-12 13:00:55 +0200293 // Chain the entries in the new pool onto the free list.
294 gSoInfoFreeList = &pool->info[0];
295 soinfo* next = NULL;
296 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
297 pool->info[i].next = next;
298 next = &pool->info[i];
299 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800300
Magnus Malmbornba98d922012-09-12 13:00:55 +0200301 return true;
302}
303
Elliott Hughesd23736e2012-11-01 15:16:56 -0700304static void set_soinfo_pool_protection(int protection) {
305 for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
306 if (mprotect(p, sizeof(*p), protection) == -1) {
307 abort(); // Can't happen.
308 }
309 }
310}
311
Magnus Malmbornba98d922012-09-12 13:00:55 +0200312static soinfo* soinfo_alloc(const char* name) {
313 if (strlen(name) >= SOINFO_NAME_LEN) {
314 DL_ERR("library name \"%s\" too long", name);
315 return NULL;
316 }
317
318 if (!ensure_free_list_non_empty()) {
319 DL_ERR("out of memory when loading \"%s\"", name);
320 return NULL;
321 }
322
323 // Take the head element off the free list.
324 soinfo* si = gSoInfoFreeList;
325 gSoInfoFreeList = gSoInfoFreeList->next;
326
327 // Initialize the new element.
328 memset(si, 0, sizeof(soinfo));
329 strlcpy(si->name, name, sizeof(si->name));
330 sonext->next = si;
331 sonext = si;
332
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700333 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200334 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800335}
336
Elliott Hughes46882792012-08-03 16:49:39 -0700337static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338{
Elliott Hughes46882792012-08-03 16:49:39 -0700339 if (si == NULL) {
340 return;
341 }
342
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800343 soinfo *prev = NULL, *trav;
344
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700345 TRACE("name %s: freeing soinfo @ %p", si->name, si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800346
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800347 for (trav = solist; trav != NULL; trav = trav->next) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800348 if (trav == si)
349 break;
350 prev = trav;
351 }
352 if (trav == NULL) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800353 /* si was not in solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700354 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355 return;
356 }
357
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100358 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800359 always the static libdl_info.
360 */
361 prev->next = si->next;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800362 if (si == sonext) {
363 sonext = prev;
364 }
Magnus Malmbornba98d922012-09-12 13:00:55 +0200365 si->next = gSoInfoFreeList;
366 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800367}
368
Elliott Hughescade4c32012-12-20 14:42:14 -0800369
370static void parse_path(const char* path, const char* delimiters,
371 const char** array, char* buf, size_t buf_size, size_t max_count) {
372 if (path == NULL) {
373 return;
374 }
375
376 size_t len = strlcpy(buf, path, buf_size);
377
378 size_t i = 0;
379 char* buf_p = buf;
380 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
381 if (*array[i] != '\0') {
382 ++i;
383 }
384 }
385
386 // Forget the last path if we had to truncate; this occurs if the 2nd to
387 // last char isn't '\0' (i.e. wasn't originally a delimiter).
388 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
389 array[i - 1] = NULL;
390 } else {
391 array[i] = NULL;
392 }
393}
394
395static void parse_LD_LIBRARY_PATH(const char* path) {
396 parse_path(path, ":", gLdPaths,
397 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
398}
399
400static void parse_LD_PRELOAD(const char* path) {
401 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
402 parse_path(path, " :", gLdPreloadNames,
403 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
404}
405
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700406#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700407
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800408/* For a given PC, find the .so that it belongs to.
409 * Returns the base address of the .ARM.exidx section
410 * for that .so, and the number of 8-byte entries
411 * in that section (via *pcount).
412 *
413 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
414 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700415 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800417_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
418{
419 soinfo *si;
420 unsigned addr = (unsigned)pc;
421
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700422 for (si = solist; si != 0; si = si->next) {
Nick Kralevich468319c2011-11-11 15:53:17 -0800423 if ((addr >= si->base) && (addr < (si->base + si->size))) {
424 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900425 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800426 }
427 }
428 *pcount = 0;
429 return NULL;
430}
Elliott Hughes46882792012-08-03 16:49:39 -0700431
Christopher Ferris24053a42013-08-19 17:45:09 -0700432#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700433
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800434/* Here, we only have to provide a callback to iterate across all the
435 * loaded libraries. gcc_eh does the rest. */
436int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700437dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800438 void *data)
439{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800440 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700441 for (soinfo* si = solist; si != NULL; si = si->next) {
442 dl_phdr_info dl_info;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700443 dl_info.dlpi_addr = si->link_map.l_addr;
444 dl_info.dlpi_name = si->link_map.l_name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800445 dl_info.dlpi_phdr = si->phdr;
446 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700447 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
448 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800449 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700450 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800451 }
452 return rv;
453}
Elliott Hughes46882792012-08-03 16:49:39 -0700454
Elliott Hughesc6200592013-09-30 18:43:46 -0700455static Elf_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
456 Elf_Sym* symtab = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800457 const char* strtab = si->strtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800458
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700459 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
460 name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800461
Christopher Ferris6bec5b72013-06-03 17:27:49 -0700462 for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700463 Elf_Sym* s = symtab + n;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800464 if (strcmp(strtab + s->st_name, name)) continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800465
Doug Kwane8238072009-10-26 12:05:23 -0700466 /* only concern ourselves with global and weak symbol definitions */
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700467 switch (ELF_ST_BIND(s->st_info)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800468 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700469 case STB_WEAK:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800470 if (s->st_shndx == SHN_UNDEF) {
Robin Burchell439fa8e2012-07-05 09:21:07 +0200471 continue;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800472 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800473
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700474 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
475 name, si->name, reinterpret_cast<void*>(s->st_value),
476 static_cast<size_t>(s->st_size));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800477 return s;
478 }
479 }
480
Doug Kwan94304352009-10-23 18:11:40 -0700481 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800482}
483
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800484static unsigned elfhash(const char* _name) {
485 const unsigned char* name = (const unsigned char*) _name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800486 unsigned h = 0, g;
487
488 while(*name) {
489 h = (h << 4) + *name++;
490 g = h & 0xf0000000;
491 h ^= g;
492 h ^= g >> 24;
493 }
494 return h;
495}
496
Elliott Hughesc6200592013-09-30 18:43:46 -0700497static Elf_Sym* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
Doug Kwan94304352009-10-23 18:11:40 -0700498 unsigned elf_hash = elfhash(name);
Elliott Hughesc6200592013-09-30 18:43:46 -0700499 Elf_Sym* s = NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700500
Pavel Chupinc77c4342012-10-31 13:55:51 +0400501 if (si != NULL && somain != NULL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200502
503 /*
Pavel Chupinc77c4342012-10-31 13:55:51 +0400504 * Local scope is executable scope. Just start looking into it right away
505 * for the shortcut.
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200506 */
507
Pavel Chupinc77c4342012-10-31 13:55:51 +0400508 if (si == somain) {
509 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200510 if (s != NULL) {
Pavel Chupinc77c4342012-10-31 13:55:51 +0400511 *lsi = si;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200512 goto done;
513 }
Pavel Chupinc77c4342012-10-31 13:55:51 +0400514 } else {
515 /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200516
Pavel Chupinc77c4342012-10-31 13:55:51 +0400517 /*
518 * If this object was built with symbolic relocations disabled, the
519 * first place to look to resolve external references is the main
520 * executable.
521 */
Nick Kralevich468319c2011-11-11 15:53:17 -0800522
Pavel Chupinc77c4342012-10-31 13:55:51 +0400523 if (!si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700524 DEBUG("%s: looking up %s in executable %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700525 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400526 s = soinfo_elf_lookup(somain, elf_hash, name);
527 if (s != NULL) {
528 *lsi = somain;
529 goto done;
530 }
531 }
532
533 /* Look for symbols in the local scope (the object who is
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700534 * searching). This happens with C++ templates on x86 for some
Pavel Chupinc77c4342012-10-31 13:55:51 +0400535 * reason.
536 *
537 * Notes on weak symbols:
538 * The ELF specs are ambiguous about treatment of weak definitions in
539 * dynamic linking. Some systems return the first definition found
540 * and some the first non-weak definition. This is system dependent.
541 * Here we return the first definition found for simplicity. */
542
543 s = soinfo_elf_lookup(si, elf_hash, name);
544 if (s != NULL) {
545 *lsi = si;
546 goto done;
547 }
548
549 /*
550 * If this object was built with -Bsymbolic and symbol is not found
551 * in the local scope, try to find the symbol in the main executable.
552 */
553
554 if (si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700555 DEBUG("%s: looking up %s in executable %s after local scope",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700556 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400557 s = soinfo_elf_lookup(somain, elf_hash, name);
558 if (s != NULL) {
559 *lsi = somain;
560 goto done;
561 }
562 }
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200563 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700564 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700565
Matt Fischer4fd42c12009-12-31 12:09:10 -0600566 /* Next, look for it in the preloads list */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800567 for (int i = 0; gLdPreloads[i] != NULL; i++) {
568 s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
569 if (s != NULL) {
570 *lsi = gLdPreloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600571 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200572 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600573 }
574
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800575 for (int i = 0; needed[i] != NULL; i++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700576 DEBUG("%s: looking up %s in %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700577 si->name, name, needed[i]->name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200578 s = soinfo_elf_lookup(needed[i], elf_hash, name);
579 if (s != NULL) {
580 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200581 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200582 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700583 }
584
585done:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800586 if (s != NULL) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700587 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
588 "found in %s, base = %p, load bias = %p",
589 si->name, name, reinterpret_cast<void*>(s->st_value),
590 (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
591 reinterpret_cast<void*>((*lsi)->load_bias));
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700592 return s;
593 }
594
Doug Kwan94304352009-10-23 18:11:40 -0700595 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700596}
597
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800598/* This is used by dlsym(3). It performs symbol lookup only within the
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700599 specified soinfo object and not in any of its dependencies.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800600
601 TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
602 that it should do a breadth first search through the dependency
603 tree. This agrees with the ELF spec (aka System V Application
604 Binary Interface) where in Chapter 5 it discuss resolving "Shared
605 Object Dependencies" in breadth first search order.
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700606 */
Elliott Hughesc6200592013-09-30 18:43:46 -0700607Elf_Sym* dlsym_handle_lookup(soinfo* si, const char* name) {
David 'Digit' Turner16084162012-06-12 16:25:37 +0200608 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800609}
610
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800611/* This is used by dlsym(3) to performs a global symbol lookup. If the
612 start value is null (for RTLD_DEFAULT), the search starts at the
613 beginning of the global solist. Otherwise the search starts at the
614 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700615 */
Elliott Hughesc6200592013-09-30 18:43:46 -0700616Elf_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800617 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800618
Elliott Hughescade4c32012-12-20 14:42:14 -0800619 if (start == NULL) {
620 start = solist;
621 }
622
Elliott Hughesc6200592013-09-30 18:43:46 -0700623 Elf_Sym* s = NULL;
Elliott Hughescade4c32012-12-20 14:42:14 -0800624 for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
625 s = soinfo_elf_lookup(si, elf_hash, name);
626 if (s != NULL) {
627 *found = si;
628 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600629 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800630 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600631
Elliott Hughescade4c32012-12-20 14:42:14 -0800632 if (s != NULL) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700633 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
634 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800635 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800636
Elliott Hughescade4c32012-12-20 14:42:14 -0800637 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800638}
639
Kito Chengfa8c05d2013-03-12 14:58:06 +0800640soinfo* find_containing_library(const void* p) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700641 Elf_Addr address = reinterpret_cast<Elf_Addr>(p);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800642 for (soinfo* si = solist; si != NULL; si = si->next) {
643 if (address >= si->base && address - si->base < si->size) {
644 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600645 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800646 }
647 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600648}
649
Elliott Hughesc6200592013-09-30 18:43:46 -0700650Elf_Sym* dladdr_find_symbol(soinfo* si, const void* addr) {
651 Elf_Addr soaddr = reinterpret_cast<Elf_Addr>(addr) - si->base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600652
Kito Chengfa8c05d2013-03-12 14:58:06 +0800653 // Search the library's symbol table for any defined symbol which
654 // contains this address.
655 for (size_t i = 0; i < si->nchain; ++i) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700656 Elf_Sym* sym = &si->symtab[i];
Kito Chengfa8c05d2013-03-12 14:58:06 +0800657 if (sym->st_shndx != SHN_UNDEF &&
658 soaddr >= sym->st_value &&
659 soaddr < sym->st_value + sym->st_size) {
660 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600661 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800662 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600663
Kito Chengfa8c05d2013-03-12 14:58:06 +0800664 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600665}
666
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800667#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800668static void dump(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800669{
Elliott Hughesc6200592013-09-30 18:43:46 -0700670 Elf_Sym* s = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800671 for (unsigned n = 0; n < si->nchain; n++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700672 TRACE("%04d> %08x: %02x %04x %08x %08x %s", n, s,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800673 s->st_info, s->st_shndx, s->st_value, s->st_size,
674 si->strtab + s->st_name);
675 s++;
676 }
677}
678#endif
679
Elliott Hughes124fae92012-10-31 14:20:03 -0700680static int open_library_on_path(const char* name, const char* const paths[]) {
681 char buf[512];
682 for (size_t i = 0; paths[i] != NULL; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800683 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700684 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700685 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700686 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800687 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700688 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
689 if (fd != -1) {
690 return fd;
691 }
692 }
693 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800694}
695
Elliott Hughes124fae92012-10-31 14:20:03 -0700696static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700697 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800698
Elliott Hughes124fae92012-10-31 14:20:03 -0700699 // If the name contains a slash, we should attempt to open it directly and not search the paths.
700 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700701 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
702 if (fd != -1) {
703 return fd;
704 }
705 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700706 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800707
Elliott Hughes124fae92012-10-31 14:20:03 -0700708 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
709 int fd = open_library_on_path(name, gLdPaths);
710 if (fd == -1) {
711 fd = open_library_on_path(name, gSoPaths);
712 }
713 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800714}
715
Elliott Hughes124fae92012-10-31 14:20:03 -0700716static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700717 // Open the file.
Elliott Hughes650be4e2013-03-05 18:47:58 -0800718 int fd = open_library(name);
719 if (fd == -1) {
Elliott Hughes46882792012-08-03 16:49:39 -0700720 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800721 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700722 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800723
Elliott Hughes650be4e2013-03-05 18:47:58 -0800724 // Read the ELF header and load the segments.
725 ElfReader elf_reader(name, fd);
726 if (!elf_reader.Load()) {
Elliott Hughes46882792012-08-03 16:49:39 -0700727 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800728 }
729
Elliott Hughes650be4e2013-03-05 18:47:58 -0800730 const char* bname = strrchr(name, '/');
731 soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
732 if (si == NULL) {
Elliott Hughes46882792012-08-03 16:49:39 -0700733 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200734 }
Elliott Hughes650be4e2013-03-05 18:47:58 -0800735 si->base = elf_reader.load_start();
736 si->size = elf_reader.load_size();
737 si->load_bias = elf_reader.load_bias();
738 si->flags = 0;
739 si->entry = 0;
740 si->dynamic = NULL;
741 si->phnum = elf_reader.phdr_count();
742 si->phdr = elf_reader.loaded_phdr();
743 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800744}
745
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200746static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800747{
748 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700749 const char *bname;
750
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200751 // TODO: don't use basename only for determining libraries
752 // http://code.google.com/p/android/issues/detail?id=6670
753
754 bname = strrchr(name, '/');
755 bname = bname ? bname + 1 : name;
756
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800757 for (si = solist; si != NULL; si = si->next) {
758 if (!strcmp(bname, si->name)) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200759 return si;
760 }
761 }
762 return NULL;
763}
764
Elliott Hughesd23736e2012-11-01 15:16:56 -0700765static soinfo* find_library_internal(const char* name) {
766 if (name == NULL) {
767 return somain;
768 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200769
Elliott Hughesd23736e2012-11-01 15:16:56 -0700770 soinfo* si = find_loaded_library(name);
771 if (si != NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700772 if (si->flags & FLAG_LINKED) {
773 return si;
774 }
775 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
776 return NULL;
777 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800778
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700779 TRACE("[ '%s' has not been loaded yet. Locating...]", name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700780 si = load_library(name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800781 if (si == NULL) {
782 return NULL;
783 }
784
785 // At this point we know that whatever is loaded @ base is a valid ELF
786 // shared library whose segments are properly mapped in.
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700787 TRACE("[ init_library base=%p sz=0x%08x name='%s' ]",
788 reinterpret_cast<void*>(si->base), si->size, si->name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800789
790 if (!soinfo_link_image(si)) {
791 munmap(reinterpret_cast<void*>(si->base), si->size);
792 soinfo_free(si);
793 return NULL;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700794 }
795
796 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800797}
798
Elliott Hughesd23736e2012-11-01 15:16:56 -0700799static soinfo* find_library(const char* name) {
800 soinfo* si = find_library_internal(name);
801 if (si != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700802 si->ref_count++;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700803 }
804 return si;
805}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700806
Elliott Hughesd23736e2012-11-01 15:16:56 -0700807static int soinfo_unload(soinfo* si) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700808 if (si->ref_count == 1) {
809 TRACE("unloading '%s'", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700810 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800811
Elliott Hughesc6200592013-09-30 18:43:46 -0700812 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800813 if (d->d_tag == DT_NEEDED) {
814 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughes8147d3c2013-05-09 14:19:58 -0700815 TRACE("%s needs to unload %s", si->name, library_name);
816 soinfo_unload(find_loaded_library(library_name));
Elliott Hughesd23736e2012-11-01 15:16:56 -0700817 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800818 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700819
820 munmap(reinterpret_cast<void*>(si->base), si->size);
821 notify_gdb_of_unload(si);
822 soinfo_free(si);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700823 si->ref_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700824 } else {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700825 si->ref_count--;
Elliott Hughesc6200592013-09-30 18:43:46 -0700826 TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700827 }
828 return 0;
829}
830
Elliott Hughescade4c32012-12-20 14:42:14 -0800831void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
832 if (!get_AT_SECURE()) {
833 parse_LD_LIBRARY_PATH(ld_library_path);
834 }
835}
836
Elliott Hughese66190d2012-12-18 15:57:55 -0800837soinfo* do_dlopen(const char* name, int flags) {
838 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
839 DL_ERR("invalid flags to dlopen: %x", flags);
840 return NULL;
841 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700842 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
843 soinfo* si = find_library(name);
844 if (si != NULL) {
845 si->CallConstructors();
846 }
847 set_soinfo_pool_protection(PROT_READ);
848 return si;
849}
850
851int do_dlclose(soinfo* si) {
852 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
853 int result = soinfo_unload(si);
854 set_soinfo_pool_protection(PROT_READ);
855 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800856}
857
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700858#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700859static int soinfo_relocate_a(soinfo* si, Elf_Rela* rela, unsigned count, soinfo* needed[]) {
860 Elf_Sym* symtab = si->symtab;
861 const char* strtab = si->strtab;
862 Elf_Sym* s;
863 Elf_Rela* start = rela;
864 soinfo* lsi;
865
866 for (size_t idx = 0; idx < count; ++idx, ++rela) {
867 unsigned type = ELF_R_TYPE(rela->r_info);
868 unsigned sym = ELF_R_SYM(rela->r_info);
869 Elf_Addr reloc = static_cast<Elf_Addr>(rela->r_offset + si->load_bias);
870 Elf_Addr sym_addr = 0;
871 char* sym_name = NULL;
872
873 DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
874 if (type == 0) { // R_*_NONE
875 continue;
876 }
877 if (sym != 0) {
878 sym_name = (char *)(strtab + symtab[sym].st_name);
879 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
880 if (s == NULL) {
881 // We only allow an undefined symbol if this is a weak reference...
882 s = &symtab[sym];
883 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
884 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
885 return -1;
886 }
887
888 /* IHI0044C AAELF 4.5.1.1:
889
890 Libraries are not searched to resolve weak references.
891 It is not an error for a weak reference to remain unsatisfied.
892
893 During linking, the value of an undefined weak reference is:
894 - Zero if the relocation type is absolute
895 - The address of the place if the relocation is pc-relative
896 - The address of nominal base address if the relocation
897 type is base-relative.
898 */
899
900 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100901#if defined(__aarch64__)
902 case R_AARCH64_JUMP_SLOT:
903 case R_AARCH64_GLOB_DAT:
904 case R_AARCH64_ABS64:
905 case R_AARCH64_ABS32:
906 case R_AARCH64_ABS16:
907 case R_AARCH64_RELATIVE:
908 /*
909 * The sym_addr was initialized to be zero above, or the relocation
910 * code below does not care about value of sym_addr.
911 * No need to do anything.
912 */
913 break;
914#elif defined(__x86_64__)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700915 case R_X86_64_JUMP_SLOT:
916 case R_X86_64_GLOB_DAT:
917 case R_X86_64_32:
918 case R_X86_64_RELATIVE:
919 // No need to do anything.
920 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700921 case R_X86_64_PC32:
922 sym_addr = reloc;
923 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700924#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700925 default:
926 DL_ERR("unknown weak reloc type %d @ %p (%d)", type, rela, (int) (rela - start));
927 return -1;
928 }
929 } else {
930 // We got a definition.
931 sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias);
932 }
933 count_relocation(kRelocSymbol);
934 } else {
935 s = NULL;
936 }
937
938 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100939#if defined(__aarch64__)
940 case R_AARCH64_JUMP_SLOT:
941 count_relocation(kRelocAbsolute);
942 MARK(rela->r_offset);
943 TRACE_TYPE(RELO, "RELO JMP_SLOT %16lx <- %16lx %s\n",
944 reloc,
945 (sym_addr + rela->r_addend),
946 sym_name);
947 *reinterpret_cast<Elf_Addr*>(reloc) = (sym_addr + rela->r_addend);
948 break;
949 case R_AARCH64_GLOB_DAT:
950 count_relocation(kRelocAbsolute);
951 MARK(rela->r_offset);
952 TRACE_TYPE(RELO, "RELO GLOB_DAT %16lx <- %16lx %s\n",
953 reloc,
954 (sym_addr + rela->r_addend),
955 sym_name);
956 *reinterpret_cast<Elf_Addr*>(reloc) = (sym_addr + rela->r_addend);
957 break;
958 case R_AARCH64_ABS64:
959 count_relocation(kRelocAbsolute);
960 MARK(rela->r_offset);
961 TRACE_TYPE(RELO, "RELO ABS64 %16lx <- %16lx %s\n",
962 reloc,
963 (sym_addr + rela->r_addend),
964 sym_name);
965 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr + rela->r_addend);
966 break;
967 case R_AARCH64_ABS32:
968 count_relocation(kRelocAbsolute);
969 MARK(rela->r_offset);
970 TRACE_TYPE(RELO, "RELO ABS32 %16lx <- %16lx %s\n",
971 reloc,
972 (sym_addr + rela->r_addend),
973 sym_name);
974 if ((static_cast<Elf_Addr>(INT32_MIN) <=
975 (*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend))) &&
976 ((*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend)) <=
977 static_cast<Elf_Addr>(UINT32_MAX))) {
978 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr + rela->r_addend);
979 } else {
980 DL_ERR("0x%016lx out of range 0x%016lx to 0x%016lx",
981 (*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend)),
982 static_cast<Elf_Addr>(INT32_MIN),
983 static_cast<Elf_Addr>(UINT32_MAX));
984 return -1;
985 }
986 break;
987 case R_AARCH64_ABS16:
988 count_relocation(kRelocAbsolute);
989 MARK(rela->r_offset);
990 TRACE_TYPE(RELO, "RELO ABS16 %16lx <- %16lx %s\n",
991 reloc,
992 (sym_addr + rela->r_addend),
993 sym_name);
994 if ((static_cast<Elf_Addr>(INT16_MIN) <=
995 (*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend))) &&
996 ((*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend)) <=
997 static_cast<Elf_Addr>(UINT16_MAX))) {
998 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr + rela->r_addend);
999 } else {
1000 DL_ERR("0x%016lx out of range 0x%016lx to 0x%016lx",
1001 (*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend)),
1002 static_cast<Elf_Addr>(INT16_MIN),
1003 static_cast<Elf_Addr>(UINT16_MAX));
1004 return -1;
1005 }
1006 break;
1007 case R_AARCH64_PREL64:
1008 count_relocation(kRelocRelative);
1009 MARK(rela->r_offset);
1010 TRACE_TYPE(RELO, "RELO REL64 %16lx <- %16lx - %16lx %s\n",
1011 reloc,
1012 (sym_addr + rela->r_addend),
1013 rela->r_offset,
1014 sym_name);
1015 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
1016 break;
1017 case R_AARCH64_PREL32:
1018 count_relocation(kRelocRelative);
1019 MARK(rela->r_offset);
1020 TRACE_TYPE(RELO, "RELO REL32 %16lx <- %16lx - %16lx %s\n",
1021 reloc,
1022 (sym_addr + rela->r_addend),
1023 rela->r_offset, sym_name);
1024 if ((static_cast<Elf_Addr>(INT32_MIN) <=
1025 (*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1026 ((*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <=
1027 static_cast<Elf_Addr>(UINT32_MAX))) {
1028 *reinterpret_cast<Elf_Addr*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
1029 } else {
1030 DL_ERR("0x%016lx out of range 0x%016lx to 0x%016lx",
1031 (*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1032 static_cast<Elf_Addr>(INT32_MIN),
1033 static_cast<Elf_Addr>(UINT32_MAX));
1034 return -1;
1035 }
1036 break;
1037 case R_AARCH64_PREL16:
1038 count_relocation(kRelocRelative);
1039 MARK(rela->r_offset);
1040 TRACE_TYPE(RELO, "RELO REL16 %16lx <- %16lx - %16lx %s\n",
1041 reloc,
1042 (sym_addr + rela->r_addend),
1043 rela->r_offset, sym_name);
1044 if ((static_cast<Elf_Addr>(INT16_MIN) <=
1045 (*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1046 ((*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <=
1047 static_cast<Elf_Addr>(UINT16_MAX))) {
1048 *reinterpret_cast<Elf_Addr*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
1049 } else {
1050 DL_ERR("0x%016lx out of range 0x%016lx to 0x%016lx",
1051 (*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1052 static_cast<Elf_Addr>(INT16_MIN),
1053 static_cast<Elf_Addr>(UINT16_MAX));
1054 return -1;
1055 }
1056 break;
1057
1058 case R_AARCH64_RELATIVE:
1059 count_relocation(kRelocRelative);
1060 MARK(rela->r_offset);
1061 if (sym) {
1062 DL_ERR("odd RELATIVE form...");
1063 return -1;
1064 }
1065 TRACE_TYPE(RELO, "RELO RELATIVE %16lx <- %16lx\n",
1066 reloc,
1067 (si->base + rela->r_addend));
1068 *reinterpret_cast<Elf_Addr*>(reloc) = (si->base + rela->r_addend);
1069 break;
1070
1071 case R_AARCH64_COPY:
1072 if ((si->flags & FLAG_EXE) == 0) {
1073 /*
1074 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1075 *
1076 * Section 4.7.1.10 "Dynamic relocations"
1077 * R_AARCH64_COPY may only appear in executable objects where e_type is
1078 * set to ET_EXEC.
1079 *
1080 * FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1081 * We should explicitly disallow ET_DYN executables from having
1082 * R_AARCH64_COPY relocations.
1083 */
1084 DL_ERR("%s R_AARCH64_COPY relocations only supported for ET_EXEC", si->name);
1085 return -1;
1086 }
1087 count_relocation(kRelocCopy);
1088 MARK(rela->r_offset);
1089 TRACE_TYPE(RELO, "RELO COPY %16lx <- %ld @ %16lx %s\n",
1090 reloc,
1091 s->st_size,
1092 (sym_addr + rela->r_addend),
1093 sym_name);
1094 if (reloc == (sym_addr + rela->r_addend)) {
1095 Elf_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1096
1097 if (src == NULL) {
1098 DL_ERR("%s R_AARCH64_COPY relocation source cannot be resolved", si->name);
1099 return -1;
1100 }
1101 if (lsi->has_DT_SYMBOLIC) {
1102 DL_ERR("%s invalid R_AARCH64_COPY relocation against DT_SYMBOLIC shared "
1103 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1104 return -1;
1105 }
1106 if (s->st_size < src->st_size) {
1107 DL_ERR("%s R_AARCH64_COPY relocation size mismatch (%ld < %ld)",
1108 si->name, s->st_size, src->st_size);
1109 return -1;
1110 }
1111 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1112 } else {
1113 DL_ERR("%s R_AARCH64_COPY relocation target cannot be resolved", si->name);
1114 return -1;
1115 }
1116 break;
1117 case R_AARCH64_TLS_TPREL64:
1118 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16lx <- %16lx - %16lx\n",
1119 reloc,
1120 (sym_addr + rela->r_addend),
1121 rela->r_offset);
1122 break;
1123 case R_AARCH64_TLS_DTPREL32:
1124 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16lx <- %16lx - %16lx\n",
1125 reloc,
1126 (sym_addr + rela->r_addend),
1127 rela->r_offset);
1128 break;
1129#elif defined(__x86_64__)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001130 case R_X86_64_JUMP_SLOT:
1131 count_relocation(kRelocAbsolute);
1132 MARK(rela->r_offset);
1133 TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1134 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1135 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
1136 break;
1137 case R_X86_64_GLOB_DAT:
1138 count_relocation(kRelocAbsolute);
1139 MARK(rela->r_offset);
1140 TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1141 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1142 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
1143 break;
1144 case R_X86_64_RELATIVE:
1145 count_relocation(kRelocRelative);
1146 MARK(rela->r_offset);
1147 if (sym) {
1148 DL_ERR("odd RELATIVE form...");
1149 return -1;
1150 }
1151 TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1152 static_cast<size_t>(si->base));
1153 *reinterpret_cast<Elf_Addr*>(reloc) = si->base + rela->r_addend;
1154 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001155 case R_X86_64_32:
1156 count_relocation(kRelocRelative);
1157 MARK(rela->r_offset);
1158 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1159 static_cast<size_t>(sym_addr), sym_name);
1160 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
1161 break;
Pavel Chupinc075c182013-10-16 19:13:58 +04001162 case R_X86_64_64:
1163 count_relocation(kRelocRelative);
1164 MARK(rela->r_offset);
1165 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1166 static_cast<size_t>(sym_addr), sym_name);
1167 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
1168 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001169 case R_X86_64_PC32:
1170 count_relocation(kRelocRelative);
1171 MARK(rela->r_offset);
1172 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1173 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1174 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
1175 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend - reloc;
1176 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001177#endif
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001178
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001179 default:
1180 DL_ERR("unknown reloc type %d @ %p (%d)", type, rela, (int) (rela - start));
1181 return -1;
1182 }
1183 }
1184 return 0;
1185}
1186#else
Elliott Hughesc6200592013-09-30 18:43:46 -07001187static int soinfo_relocate(soinfo* si, Elf_Rel* rel, unsigned count,
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001188 soinfo* needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001189{
Elliott Hughesc6200592013-09-30 18:43:46 -07001190 Elf_Sym* symtab = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001191 const char* strtab = si->strtab;
Elliott Hughesc6200592013-09-30 18:43:46 -07001192 Elf_Sym* s;
1193 Elf_Rel* start = rel;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001194 soinfo* lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001195
Elliott Hughes46882792012-08-03 16:49:39 -07001196 for (size_t idx = 0; idx < count; ++idx, ++rel) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001197 unsigned type = ELF_R_TYPE(rel->r_info);
1198 // TODO: don't use unsigned for 'sym'. Use uint32_t or Elf_Addr instead.
1199 unsigned sym = ELF_R_SYM(rel->r_info);
Elliott Hughesc6200592013-09-30 18:43:46 -07001200 Elf_Addr reloc = static_cast<Elf_Addr>(rel->r_offset + si->load_bias);
1201 Elf_Addr sym_addr = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001202 char* sym_name = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001203
Elliott Hughesc6200592013-09-30 18:43:46 -07001204 DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001205 if (type == 0) { // R_*_NONE
1206 continue;
1207 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001208 if (sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001209 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001210 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001211 if (s == NULL) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001212 // We only allow an undefined symbol if this is a weak reference...
Doug Kwane8238072009-10-26 12:05:23 -07001213 s = &symtab[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001214 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -07001215 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -07001216 return -1;
1217 }
1218
1219 /* IHI0044C AAELF 4.5.1.1:
1220
1221 Libraries are not searched to resolve weak references.
1222 It is not an error for a weak reference to remain
1223 unsatisfied.
1224
1225 During linking, the value of an undefined weak reference is:
1226 - Zero if the relocation type is absolute
1227 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -07001228 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -07001229 type is base-relative.
1230 */
1231
1232 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001233#if defined(__arm__)
Doug Kwane8238072009-10-26 12:05:23 -07001234 case R_ARM_JUMP_SLOT:
1235 case R_ARM_GLOB_DAT:
1236 case R_ARM_ABS32:
1237 case R_ARM_RELATIVE: /* Don't care. */
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001238 // sym_addr was initialized to be zero above or relocation
1239 // code below does not care about value of sym_addr.
1240 // No need to do anything.
1241 break;
1242#elif defined(__i386__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001243 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001244 case R_386_GLOB_DAT:
1245 case R_386_32:
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001246 case R_386_RELATIVE: /* Don't care. */
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001247 // sym_addr was initialized to be zero above or relocation
1248 // code below does not care about value of sym_addr.
1249 // No need to do anything.
Doug Kwane8238072009-10-26 12:05:23 -07001250 break;
Doug Kwane8238072009-10-26 12:05:23 -07001251 case R_386_PC32:
1252 sym_addr = reloc;
1253 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001254#endif
Doug Kwane8238072009-10-26 12:05:23 -07001255
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001256#if defined(__arm__)
Doug Kwane8238072009-10-26 12:05:23 -07001257 case R_ARM_COPY:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001258 // Fall through. Can't really copy if weak symbol is not found at run-time.
1259#endif
Doug Kwane8238072009-10-26 12:05:23 -07001260 default:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001261 DL_ERR("unknown weak reloc type %d @ %p (%d)", type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -07001262 return -1;
1263 }
1264 } else {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001265 // We got a definition.
Elliott Hughesc6200592013-09-30 18:43:46 -07001266 sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001267 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001268 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001269 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001270 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001271 }
1272
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001273 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001274#if defined(__arm__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001275 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001276 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001277 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001278 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001279 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001280 break;
1281 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001282 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001283 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001284 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001285 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001286 break;
1287 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001288 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001289 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001290 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001291 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001292 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001293 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001294 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001295 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001296 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001297 reloc, sym_addr, rel->r_offset, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001298 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr - rel->r_offset;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001299 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001300 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001301 if ((si->flags & FLAG_EXE) == 0) {
1302 /*
1303 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1304 *
1305 * Section 4.7.1.10 "Dynamic relocations"
1306 * R_ARM_COPY may only appear in executable objects where e_type is
1307 * set to ET_EXEC.
1308 *
1309 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1310 * We should explicitly disallow ET_DYN executables from having
1311 * R_ARM_COPY relocations.
1312 */
1313 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1314 return -1;
1315 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001316 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001317 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001318 TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s", reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001319 if (reloc == sym_addr) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001320 Elf_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001321
1322 if (src == NULL) {
1323 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1324 return -1;
1325 }
1326 if (lsi->has_DT_SYMBOLIC) {
1327 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1328 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1329 return -1;
1330 }
1331 if (s->st_size < src->st_size) {
1332 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1333 si->name, s->st_size, src->st_size);
1334 return -1;
1335 }
1336 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1337 } else {
1338 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001339 return -1;
1340 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001341 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001342#elif defined(__i386__)
1343 case R_386_JMP_SLOT:
1344 count_relocation(kRelocAbsolute);
1345 MARK(rel->r_offset);
1346 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1347 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
1348 break;
1349 case R_386_GLOB_DAT:
1350 count_relocation(kRelocAbsolute);
1351 MARK(rel->r_offset);
1352 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1353 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
1354 break;
1355 case R_386_32:
1356 count_relocation(kRelocRelative);
1357 MARK(rel->r_offset);
1358 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1359 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
1360 break;
1361 case R_386_PC32:
1362 count_relocation(kRelocRelative);
1363 MARK(rel->r_offset);
1364 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1365 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1366 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr - reloc);
1367 break;
1368#elif defined(__mips__)
1369 case R_MIPS_REL32:
1370 count_relocation(kRelocAbsolute);
1371 MARK(rel->r_offset);
1372 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s",
1373 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1374 if (s) {
1375 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
1376 } else {
1377 *reinterpret_cast<Elf_Addr*>(reloc) += si->base;
1378 }
1379 break;
1380#endif
1381
1382#if defined(__arm__)
1383 case R_ARM_RELATIVE:
1384#elif defined(__i386__)
1385 case R_386_RELATIVE:
1386#endif
1387 count_relocation(kRelocRelative);
1388 MARK(rel->r_offset);
1389 if (sym) {
1390 DL_ERR("odd RELATIVE form...");
1391 return -1;
1392 }
1393 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1394 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base));
1395 *reinterpret_cast<Elf_Addr*>(reloc) += si->base;
1396 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001397
1398 default:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001399 DL_ERR("unknown reloc type %d @ %p (%d)", type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001400 return -1;
1401 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001402 }
1403 return 0;
1404}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001405#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001406
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001407#if defined(__mips__)
Brian Carlstrom87c35852013-08-20 21:05:44 -07001408static bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
1409 unsigned* got = si->plt_got;
1410 if (got == NULL) {
1411 return true;
1412 }
1413 unsigned local_gotno = si->mips_local_gotno;
1414 unsigned gotsym = si->mips_gotsym;
1415 unsigned symtabno = si->mips_symtabno;
Elliott Hughesc6200592013-09-30 18:43:46 -07001416 Elf_Sym* symtab = si->symtab;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001417
1418 /*
1419 * got[0] is address of lazy resolver function
1420 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001421 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001422 * (should be _rtld_bind_start)
1423 * FIXME: maybe this should be in a separate routine
1424 */
1425
1426 if ((si->flags & FLAG_LINKER) == 0) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001427 size_t g = 0;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001428 got[g++] = 0xdeadbeef;
1429 if (got[g] & 0x80000000) {
1430 got[g++] = 0xdeadfeed;
1431 }
1432 /*
1433 * Relocate the local GOT entries need to be relocated
1434 */
1435 for (; g < local_gotno; g++) {
1436 got[g] += si->load_bias;
1437 }
1438 }
1439
1440 /* Now for the global GOT entries */
Elliott Hughesc6200592013-09-30 18:43:46 -07001441 Elf_Sym* sym = symtab + gotsym;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001442 got = si->plt_got + local_gotno;
Brian Carlstrom87c35852013-08-20 21:05:44 -07001443 for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001444 const char* sym_name;
Elliott Hughesc6200592013-09-30 18:43:46 -07001445 Elf_Sym* s;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001446 soinfo* lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001447
1448 /* This is an undefined reference... try to locate it */
1449 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001450 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001451 if (s == NULL) {
1452 /* We only allow an undefined symbol if this is a weak
1453 reference.. */
1454 s = &symtab[g];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001455 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001456 DL_ERR("cannot locate \"%s\"...", sym_name);
Brian Carlstrom87c35852013-08-20 21:05:44 -07001457 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001458 }
1459 *got = 0;
1460 }
1461 else {
1462 /* FIXME: is this sufficient?
1463 * For reference see NetBSD link loader
1464 * 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
1465 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001466 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001467 }
1468 }
Brian Carlstrom87c35852013-08-20 21:05:44 -07001469 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001470}
1471#endif
1472
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001473void soinfo::CallArray(const char* array_name UNUSED, linker_function_t* functions, size_t count, bool reverse) {
1474 if (functions == NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001475 return;
1476 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001477
Elliott Hughesc6200592013-09-30 18:43:46 -07001478 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001479
1480 int begin = reverse ? (count - 1) : 0;
1481 int end = reverse ? -1 : count;
1482 int step = reverse ? -1 : 1;
1483
1484 for (int i = begin; i != end; i += step) {
1485 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1486 CallFunction("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001487 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001488
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001489 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001490}
1491
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001492void soinfo::CallFunction(const char* function_name UNUSED, linker_function_t function) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001493 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001494 return;
1495 }
1496
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001497 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001498 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001499 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001500
1501 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1502 // are still writable. This happens with our debug malloc (see http://b/7941716).
1503 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001504}
1505
Elliott Hughesd23736e2012-11-01 15:16:56 -07001506void soinfo::CallPreInitConstructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001507 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1508 // but ignored in a shared library.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001509 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1510}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001511
Elliott Hughesd23736e2012-11-01 15:16:56 -07001512void soinfo::CallConstructors() {
1513 if (constructors_called) {
1514 return;
1515 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001516
Elliott Hughesd23736e2012-11-01 15:16:56 -07001517 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1518 // protect against recursive constructor calls. One simple example of constructor recursion
1519 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1520 // 1. The program depends on libc, so libc's constructor is called here.
1521 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1522 // 3. dlopen() calls the constructors on the newly created
1523 // soinfo for libc_malloc_debug_leak.so.
1524 // 4. The debug .so depends on libc, so CallConstructors is
1525 // called again with the libc soinfo. If it doesn't trigger the early-
1526 // out above, the libc constructor will be called again (recursively!).
1527 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001528
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001529 if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
1530 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001531 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001532 name, preinit_array_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001533 }
1534
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001535 if (dynamic != NULL) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001536 for (Elf_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001537 if (d->d_tag == DT_NEEDED) {
1538 const char* library_name = strtab + d->d_un.d_val;
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001539 TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
1540 find_loaded_library(library_name)->CallConstructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001541 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001542 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001543 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001544
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001545 TRACE("\"%s\": calling constructors", name);
1546
1547 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001548 CallFunction("DT_INIT", init_func);
1549 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001550}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001551
Elliott Hughesd23736e2012-11-01 15:16:56 -07001552void soinfo::CallDestructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001553 TRACE("\"%s\": calling destructors", name);
1554
1555 // DT_FINI_ARRAY must be parsed in reverse order.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001556 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001557
1558 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001559 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001560}
1561
1562/* Force any of the closed stdin, stdout and stderr to be associated with
1563 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001564static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001565 int dev_null, i, status;
1566 int return_value = 0;
1567
David 'Digit' Turner16084162012-06-12 16:25:37 +02001568 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001569 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001570 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001571 return -1;
1572 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001573 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001574
1575 /* If any of the stdio file descriptors is valid and not associated
1576 with /dev/null, dup /dev/null to it. */
1577 for (i = 0; i < 3; i++) {
1578 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001579 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001580 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001581 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001582
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001583 TRACE("[ Nullifying stdio file descriptor %d]", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001584 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001585
Elliott Hughes46882792012-08-03 16:49:39 -07001586 /* If file is opened, we are good. */
1587 if (status != -1) {
1588 continue;
1589 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001590
1591 /* The only error we allow is that the file descriptor does not
1592 exist, in which case we dup /dev/null to it. */
1593 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001594 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001595 return_value = -1;
1596 continue;
1597 }
1598
1599 /* Try dupping /dev/null to this stdio file descriptor and
1600 repeat if there is a signal. Note that any errors in closing
1601 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001602 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001603 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001604 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001605 return_value = -1;
1606 continue;
1607 }
1608 }
1609
1610 /* If /dev/null is not one of the stdio file descriptors, close it. */
1611 if (dev_null > 2) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001612 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001613 status = TEMP_FAILURE_RETRY(close(dev_null));
1614 if (status == -1) {
1615 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001616 return_value = -1;
1617 }
1618 }
1619
1620 return return_value;
1621}
1622
Elliott Hughes124fae92012-10-31 14:20:03 -07001623static bool soinfo_link_image(soinfo* si) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001624 /* "base" might wrap around UINT32_MAX. */
Elliott Hughesc6200592013-09-30 18:43:46 -07001625 Elf_Addr base = si->load_bias;
1626 const Elf_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001627 int phnum = si->phnum;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001628 bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001629
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001630 /* We can't debug anything until the linker is relocated */
1631 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001632 INFO("[ linking %s ]", si->name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001633 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001634 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001635
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001636 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001637 size_t dynamic_count;
Elliott Hughesc6200592013-09-30 18:43:46 -07001638 Elf_Word dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001639 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001640 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001641 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001642 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001643 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001644 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001645 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001646 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001647 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001648 DEBUG("dynamic = %p", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001649 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001650 }
1651
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001652#if defined(__arm__)
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001653 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1654 &si->ARM_exidx, &si->ARM_exidx_count);
1655#endif
1656
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001657 // Extract useful information from dynamic section.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001658 uint32_t needed_count = 0;
Elliott Hughesc6200592013-09-30 18:43:46 -07001659 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001660 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1661 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1662 switch (d->d_tag) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001663 case DT_HASH:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001664 si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0];
1665 si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1];
1666 si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8);
1667 si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001668 break;
1669 case DT_STRTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001670 si->strtab = (const char *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001671 break;
1672 case DT_SYMTAB:
Elliott Hughesc6200592013-09-30 18:43:46 -07001673 si->symtab = (Elf_Sym *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001674 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001675#if !defined(__LP64__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001676 case DT_PLTREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001677 if (d->d_un.d_val != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001678 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1679 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001680 }
1681 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001682#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001683 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001684#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001685 si->plt_rela = (Elf_Rela*) (base + d->d_un.d_ptr);
1686#else
Elliott Hughesc6200592013-09-30 18:43:46 -07001687 si->plt_rel = (Elf_Rel*) (base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001688#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001689 break;
1690 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001691#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001692 si->plt_rela_count = d->d_un.d_val / sizeof(Elf_Rela);
1693#else
Elliott Hughesc6200592013-09-30 18:43:46 -07001694 si->plt_rel_count = d->d_un.d_val / sizeof(Elf_Rel);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001695#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001696 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001697#if !defined(__LP64__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001698 case DT_PLTGOT:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001699 // Used by 32-bit MIPS.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001700 si->plt_got = (unsigned *)(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001701 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001702#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001703 case DT_DEBUG:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001704 // Set the DT_DEBUG entry to the address of _r_debug for GDB
Chris Dearmancf239052013-01-11 15:32:20 -08001705 // if the dynamic table is writable
Elliott Hughes99c32052013-01-14 09:56:21 -08001706 if ((dynamic_flags & PF_W) != 0) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001707 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
Elliott Hughes99c32052013-01-14 09:56:21 -08001708 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001709 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001710#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001711 case DT_RELA:
1712 si->rela = (Elf_Rela*) (base + d->d_un.d_ptr);
1713 break;
1714 case DT_RELASZ:
1715 si->rela_count = d->d_un.d_val / sizeof(Elf_Rela);
1716 break;
1717 case DT_REL:
1718 DL_ERR("unsupported DT_REL in \"%s\"", si->name);
1719 return false;
1720 case DT_RELSZ:
1721 DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name);
1722 return false;
1723#else
1724 case DT_REL:
1725 si->rel = (Elf_Rel*) (base + d->d_un.d_ptr);
1726 break;
1727 case DT_RELSZ:
1728 si->rel_count = d->d_un.d_val / sizeof(Elf_Rel);
1729 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001730 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001731 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1732 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001733#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001734 case DT_INIT:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001735 si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001736 DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001737 break;
1738 case DT_FINI:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001739 si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001740 DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001741 break;
1742 case DT_INIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001743 si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001744 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001745 break;
1746 case DT_INIT_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001747 si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001748 break;
1749 case DT_FINI_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001750 si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001751 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001752 break;
1753 case DT_FINI_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001754 si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001755 break;
1756 case DT_PREINIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001757 si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001758 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001759 break;
1760 case DT_PREINIT_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001761 si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001762 break;
1763 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07001764#if defined(__LP64__)
1765 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1766 return false;
1767#else
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001768 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001769 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07001770#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001771 case DT_SYMBOLIC:
1772 si->has_DT_SYMBOLIC = true;
1773 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001774 case DT_NEEDED:
1775 ++needed_count;
1776 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001777 case DT_FLAGS:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001778 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001779#if defined(__LP64__)
1780 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1781 return false;
1782#else
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001783 si->has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07001784#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001785 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001786 if (d->d_un.d_val & DF_SYMBOLIC) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001787 si->has_DT_SYMBOLIC = true;
1788 }
1789 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001790#if defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001791 case DT_STRSZ:
1792 case DT_SYMENT:
1793 case DT_RELENT:
1794 break;
1795 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001796 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001797 {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001798 r_debug** dp = (r_debug**) d->d_un.d_ptr;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001799 *dp = &_r_debug;
1800 }
1801 break;
1802 case DT_MIPS_RLD_VERSION:
1803 case DT_MIPS_FLAGS:
1804 case DT_MIPS_BASE_ADDRESS:
1805 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001806 break;
1807
1808 case DT_MIPS_SYMTABNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001809 si->mips_symtabno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001810 break;
1811
1812 case DT_MIPS_LOCAL_GOTNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001813 si->mips_local_gotno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001814 break;
1815
1816 case DT_MIPS_GOTSYM:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001817 si->mips_gotsym = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001818 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001819#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001820
1821 default:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001822 DEBUG("Unused DT entry: type %p arg %p",
1823 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001824 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001825 }
1826 }
1827
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001828 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
1829 reinterpret_cast<void*>(si->base), si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001830
Elliott Hughes124fae92012-10-31 14:20:03 -07001831 // Sanity checks.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001832 if (relocating_linker && needed_count != 0) {
1833 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1834 return false;
1835 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001836 if (si->nbucket == 0) {
1837 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1838 return false;
1839 }
1840 if (si->strtab == 0) {
1841 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1842 return false;
1843 }
1844 if (si->symtab == 0) {
1845 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1846 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001847 }
1848
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001849 // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001850 if (si->flags & FLAG_EXE) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001851 memset(gLdPreloads, 0, sizeof(gLdPreloads));
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001852 size_t preload_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001853 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1854 soinfo* lsi = find_library(gLdPreloadNames[i]);
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001855 if (lsi != NULL) {
1856 gLdPreloads[preload_count++] = lsi;
1857 } else {
1858 // As with glibc, failure to load an LD_PRELOAD library is just a warning.
1859 DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
1860 gLdPreloadNames[i], si->name, linker_get_error_buffer());
Matt Fischer4fd42c12009-12-31 12:09:10 -06001861 }
Matt Fischer4fd42c12009-12-31 12:09:10 -06001862 }
1863 }
1864
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001865 soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*));
1866 soinfo** pneeded = needed;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001867
Elliott Hughesc6200592013-09-30 18:43:46 -07001868 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001869 if (d->d_tag == DT_NEEDED) {
1870 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001871 DEBUG("%s needs %s", si->name, library_name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001872 soinfo* lsi = find_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001873 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001874 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001875 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001876 library_name, si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001877 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001878 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001879 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001880 }
1881 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001882 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001883
Elliott Hughese4d792a2013-10-28 14:19:05 -07001884#if !defined(__LP64__)
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001885 if (si->has_text_relocations) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001886 // Make segments writable to allow text relocations to work properly. We will later call
1887 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
Nick Kralevich3d4470c2013-10-22 12:06:36 -07001888 DL_WARN("%s has text relocations. This is wasting memory and prevents "
1889 "security hardening. Please fix.", si->name);
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001890 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1891 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1892 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001893 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001894 }
1895 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07001896#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001897
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001898#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001899 if (si->plt_rela != NULL) {
1900 DEBUG("[ relocating %s plt ]\n", si->name );
1901 if (soinfo_relocate_a(si, si->plt_rela, si->plt_rela_count, needed)) {
1902 return false;
1903 }
1904 }
1905 if (si->rela != NULL) {
1906 DEBUG("[ relocating %s ]\n", si->name );
1907 if (soinfo_relocate_a(si, si->rela, si->rela_count, needed)) {
1908 return false;
1909 }
1910 }
1911#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001912 if (si->plt_rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001913 DEBUG("[ relocating %s plt ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001914 if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001915 return false;
1916 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001917 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001918 if (si->rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001919 DEBUG("[ relocating %s ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001920 if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001921 return false;
1922 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001923 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001924#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001925
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001926#if defined(__mips__)
Brian Carlstrom87c35852013-08-20 21:05:44 -07001927 if (!mips_relocate_got(si, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001928 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001929 }
1930#endif
1931
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001932 si->flags |= FLAG_LINKED;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001933 DEBUG("[ finished linking %s ]", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001934
Elliott Hughese4d792a2013-10-28 14:19:05 -07001935#if !defined(__LP64__)
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001936 if (si->has_text_relocations) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001937 // All relocations are done, we can protect our segments back to read-only.
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001938 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1939 DL_ERR("can't protect segments for \"%s\": %s",
1940 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001941 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001942 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001943 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07001944#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001945
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001946 /* We can also turn on GNU RELRO protection */
1947 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001948 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1949 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001950 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001951 }
1952
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001953 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001954 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001955}
1956
Nick Kralevich468319c2011-11-11 15:53:17 -08001957/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001958 * This function add vdso to internal dso list.
1959 * It helps to stack unwinding through signal handlers.
1960 * Also, it makes bionic more like glibc.
1961 */
1962static void add_vdso(KernelArgumentBlock& args UNUSED) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001963#if defined(AT_SYSINFO_EHDR)
Elliott Hughesc6200592013-09-30 18:43:46 -07001964 Elf_Ehdr* ehdr_vdso = reinterpret_cast<Elf_Ehdr*>(args.getauxval(AT_SYSINFO_EHDR));
Pavel Chupin5407eed2013-12-09 18:08:48 +04001965 if (ehdr_vdso == NULL) {
1966 return;
1967 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001968
1969 soinfo* si = soinfo_alloc("[vdso]");
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001970
Elliott Hughesc6200592013-09-30 18:43:46 -07001971 si->phdr = reinterpret_cast<Elf_Phdr*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001972 si->phnum = ehdr_vdso->e_phnum;
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001973 si->base = reinterpret_cast<Elf_Addr>(ehdr_vdso);
1974 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1975 si->flags = 0;
1976 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
1977
1978 soinfo_link_image(si);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001979#endif
1980}
1981
1982/*
Nick Kralevich468319c2011-11-11 15:53:17 -08001983 * This code is called after the linker has linked itself and
1984 * fixed it's own GOT. It is safe to make references to externs
1985 * and other non-local data at this point.
1986 */
Elliott Hughesc6200592013-09-30 18:43:46 -07001987static Elf_Addr __linker_init_post_relocation(KernelArgumentBlock& args, Elf_Addr linker_base) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001988 /* NOTE: we store the args pointer on a special location
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001989 * of the temporary TLS area in order to pass it to
1990 * the C Library's runtime initializer.
1991 *
1992 * The initializer must clear the slot and reset the TLS
1993 * to point to a different location to ensure that no other
1994 * shared library constructor can access it.
1995 */
Elliott Hughesd3920b32013-02-07 18:39:34 -08001996 __libc_init_tls(args);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001997
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001998#if TIMING
1999 struct timeval t0, t1;
2000 gettimeofday(&t0, 0);
2001#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002002
Elliott Hughes18a206c2012-10-29 17:37:13 -07002003 // Initialize environment functions, and get to the ELF aux vectors table.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002004 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002005
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002006 // If this is a setuid/setgid program, close the security hole described in
2007 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2008 if (get_AT_SECURE()) {
2009 nullify_closed_stdio();
2010 }
2011
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002012 debuggerd_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002013
Elliott Hughes18a206c2012-10-29 17:37:13 -07002014 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07002015 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2016 if (LD_DEBUG != NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08002017 gLdDebugVerbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07002018 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002019
Elliott Hughes18a206c2012-10-29 17:37:13 -07002020 // Normally, these are cleaned by linker_env_init, but the test
2021 // doesn't cost us anything.
2022 const char* ldpath_env = NULL;
2023 const char* ldpreload_env = NULL;
2024 if (!get_AT_SECURE()) {
2025 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2026 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002027 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002028
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002029 INFO("[ android linker & debugger ]");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002030
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002031 soinfo* si = soinfo_alloc(args.argv[0]);
Elliott Hughes18a206c2012-10-29 17:37:13 -07002032 if (si == NULL) {
2033 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002034 }
2035
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07002036 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002037 si->flags |= FLAG_EXE;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002038 link_map_t* map = &(si->link_map);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002039
2040 map->l_addr = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002041 map->l_name = args.argv[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002042 map->l_prev = NULL;
2043 map->l_next = NULL;
2044
2045 _r_debug.r_map = map;
2046 r_debug_tail = map;
2047
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002048 /* gdb expects the linker to be in the debug shared object list.
2049 * Without this, gdb has trouble locating the linker's ".text"
2050 * and ".plt" sections. Gdb could also potentially use this to
2051 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2052 * Don't use soinfo_alloc(), because the linker shouldn't
2053 * be on the soinfo list.
Ben Cheng06f0e742012-08-10 16:07:02 -07002054 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002055 {
2056 static soinfo linker_soinfo;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002057#if defined(__LP64__)
Pavel Chupin1a57f9f2013-02-06 19:21:46 +04002058 strlcpy(linker_soinfo.name, "/system/bin/linker64", sizeof(linker_soinfo.name));
2059#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002060 strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
Pavel Chupin1a57f9f2013-02-06 19:21:46 +04002061#endif
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002062 linker_soinfo.flags = 0;
2063 linker_soinfo.base = linker_base;
2064
2065 /*
2066 * Set the dynamic field in the link map otherwise gdb will complain with
2067 * the following:
2068 * warning: .dynamic section for "/system/bin/linker" is not at the
2069 * expected address (wrong library or version mismatch?)
2070 */
Elliott Hughesc6200592013-09-30 18:43:46 -07002071 Elf_Ehdr *elf_hdr = (Elf_Ehdr *) linker_base;
2072 Elf_Phdr *phdr = (Elf_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002073 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
2074 &linker_soinfo.dynamic, NULL, NULL);
2075 insert_soinfo_into_debug_map(&linker_soinfo);
2076 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002077
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002078 // Extract information passed from the kernel.
Elliott Hughesc6200592013-09-30 18:43:46 -07002079 si->phdr = reinterpret_cast<Elf_Phdr*>(args.getauxval(AT_PHDR));
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002080 si->phnum = args.getauxval(AT_PHNUM);
2081 si->entry = args.getauxval(AT_ENTRY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002082
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002083 /* Compute the value of si->base. We can't rely on the fact that
2084 * the first entry is the PHDR because this will not be true
2085 * for certain executables (e.g. some in the NDK unit test suite)
2086 */
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002087 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002088 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002089 si->load_bias = 0;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002090 for (size_t i = 0; i < si->phnum; ++i) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002091 if (si->phdr[i].p_type == PT_PHDR) {
Elliott Hughesc6200592013-09-30 18:43:46 -07002092 si->load_bias = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_vaddr;
2093 si->base = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_offset;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002094 break;
2095 }
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002096 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002097 si->dynamic = NULL;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002098 si->ref_count = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002099
Elliott Hughes46882792012-08-03 16:49:39 -07002100 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2101 parse_LD_LIBRARY_PATH(ldpath_env);
2102 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06002103
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02002104 somain = si;
2105
Elliott Hughes124fae92012-10-31 14:20:03 -07002106 if (!soinfo_link_image(si)) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08002107 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
Elliott Hughes18a206c2012-10-29 17:37:13 -07002108 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002109 }
2110
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002111 add_vdso(args);
2112
Elliott Hughesd23736e2012-11-01 15:16:56 -07002113 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04002114
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002115 for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
2116 gLdPreloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08002117 }
2118
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002119 /* After the link_image, the si->load_bias is initialized.
2120 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2121 * We need to update this value for so exe here. So Unwind_Backtrace
2122 * for some arch like x86 could work correctly within so exe.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08002123 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08002124 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002125 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002126
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002127#if TIMING
2128 gettimeofday(&t1,NULL);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002129 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002130 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2131 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
2132 ));
2133#endif
2134#if STATS
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002135 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07002136 linker_stats.count[kRelocAbsolute],
2137 linker_stats.count[kRelocRelative],
2138 linker_stats.count[kRelocCopy],
2139 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002140#endif
2141#if COUNT_PAGES
2142 {
2143 unsigned n;
2144 unsigned i;
2145 unsigned count = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002146 for (n = 0; n < 4096; n++) {
2147 if (bitmask[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002148 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002149#if defined(__LP64__)
2150 for (i = 0; i < 32; i++) {
2151#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002152 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002153#endif
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002154 if (x & 1) {
2155 count++;
2156 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002157 x >>= 1;
2158 }
2159 }
2160 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002161 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002162 }
2163#endif
2164
2165#if TIMING || STATS || COUNT_PAGES
2166 fflush(stdout);
2167#endif
2168
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002169 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002170 return si->entry;
2171}
Nick Kralevich468319c2011-11-11 15:53:17 -08002172
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002173/* Compute the load-bias of an existing executable. This shall only
2174 * be used to compute the load bias of an executable or shared library
2175 * that was loaded by the kernel itself.
2176 *
2177 * Input:
2178 * elf -> address of ELF header, assumed to be at the start of the file.
2179 * Return:
2180 * load bias, i.e. add the value of any p_vaddr in the file to get
2181 * the corresponding address in memory.
2182 */
Elliott Hughesc6200592013-09-30 18:43:46 -07002183static Elf_Addr get_elf_exec_load_bias(const Elf_Ehdr* elf) {
2184 Elf_Addr offset = elf->e_phoff;
2185 const Elf_Phdr* phdr_table = (const Elf_Phdr*)((char*)elf + offset);
2186 const Elf_Phdr* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002187
Elliott Hughesc6200592013-09-30 18:43:46 -07002188 for (const Elf_Phdr* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002189 if (phdr->p_type == PT_LOAD) {
Elliott Hughesc6200592013-09-30 18:43:46 -07002190 return reinterpret_cast<Elf_Addr>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002191 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002192 }
2193 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002194}
2195
Nick Kralevich468319c2011-11-11 15:53:17 -08002196/*
2197 * This is the entry point for the linker, called from begin.S. This
2198 * method is responsible for fixing the linker's own relocations, and
2199 * then calling __linker_init_post_relocation().
2200 *
2201 * Because this method is called before the linker has fixed it's own
2202 * relocations, any attempt to reference an extern variable, extern
2203 * function, or other GOT reference will generate a segfault.
2204 */
Elliott Hughesc6200592013-09-30 18:43:46 -07002205extern "C" Elf_Addr __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002206 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002207
Elliott Hughesc6200592013-09-30 18:43:46 -07002208 Elf_Addr linker_addr = args.getauxval(AT_BASE);
Elliott Hughesc6200592013-09-30 18:43:46 -07002209 Elf_Ehdr* elf_hdr = reinterpret_cast<Elf_Ehdr*>(linker_addr);
2210 Elf_Phdr* phdr = (Elf_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002211
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002212 soinfo linker_so;
2213 memset(&linker_so, 0, sizeof(soinfo));
Nick Kralevich468319c2011-11-11 15:53:17 -08002214
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002215 linker_so.base = linker_addr;
2216 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2217 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002218 linker_so.dynamic = NULL;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002219 linker_so.phdr = phdr;
2220 linker_so.phnum = elf_hdr->e_phnum;
2221 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07002222
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002223 if (!soinfo_link_image(&linker_so)) {
2224 // It would be nice to print an error message, but if the linker
2225 // can't link itself, there's no guarantee that we'll be able to
2226 // call write() (because it involves a GOT reference).
2227 //
2228 // This situation should never occur unless the linker itself
2229 // is corrupt.
2230 exit(EXIT_FAILURE);
2231 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002232
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002233 // We have successfully fixed our own relocations. It's safe to run
2234 // the main part of the linker now.
Elliott Hughes0d787c12013-04-04 13:46:46 -07002235 args.abort_message_ptr = &gAbortMessage;
Elliott Hughesc6200592013-09-30 18:43:46 -07002236 Elf_Addr start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002237
2238 set_soinfo_pool_protection(PROT_READ);
2239
2240 // Return the address that the calling assembly stub should jump to.
2241 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002242}