blob: 2058050a65cccf472e469dbcf423cc99567901b5 [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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
54 *
55 * Do NOT use malloc() and friends or pthread_*() code here.
56 * Don't use printf() either; it's caused mysterious memory
57 * corruption in the past.
58 * The linker runs before we bring up libc and it's easiest
59 * to make sure it does not depend on any complex libc features
60 *
61 * open issues / todo:
62 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063 * - are we doing everything we should for ARM_COPY relocations?
64 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065 * - after linking, set as much stuff as possible to READONLY
66 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070067 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
Elliott Hughes124fae92012-10-31 14:20:03 -070069static bool soinfo_link_image(soinfo* si);
Sergey Melnikovebd506c2013-10-31 18:02:12 +040070static Elf_Addr get_elf_exec_load_bias(const Elf_Ehdr* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071
Magnus Malmbornba98d922012-09-12 13:00:55 +020072// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
73// maps, each a single page in size. The pages are broken up into as many struct soinfo
74// objects as will fit, and they're all threaded together on a free list.
75#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
76struct soinfo_pool_t {
77 soinfo_pool_t* next;
78 soinfo info[SOINFO_PER_POOL];
79};
80static struct soinfo_pool_t* gSoInfoPools = NULL;
81static soinfo* gSoInfoFreeList = NULL;
82
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080083static soinfo* solist = &libdl_info;
84static soinfo* sonext = &libdl_info;
85static soinfo* somain; /* main process, always the one after libdl_info */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
Elliott Hughesa4aafd12014-01-13 16:37:47 -080087static const char* const gDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070088#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070089 "/vendor/lib64",
90 "/system/lib64",
91#else
Elliott Hughes124fae92012-10-31 14:20:03 -070092 "/vendor/lib",
93 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -070094#endif
Elliott Hughes124fae92012-10-31 14:20:03 -070095 NULL
96};
David Bartleybc3a5c22009-06-02 18:27:28 -070097
Elliott Hughesa4aafd12014-01-13 16:37:47 -080098#define LDPATH_BUFSIZE (LDPATH_MAX*64)
99#define LDPATH_MAX 8
100
101#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
102#define LDPRELOAD_MAX 8
103
Elliott Hughes124fae92012-10-31 14:20:03 -0700104static char gLdPathsBuffer[LDPATH_BUFSIZE];
105static const char* gLdPaths[LDPATH_MAX + 1];
106
107static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
108static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600109
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800110static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600111
Elliott Hughes650be4e2013-03-05 18:47:58 -0800112__LIBC_HIDDEN__ int gLdDebugVerbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113
Elliott Hughes0d787c12013-04-04 13:46:46 -0700114__LIBC_HIDDEN__ abort_msg_t* gAbortMessage = NULL; // For debuggerd.
115
Elliott Hughesbedfe382012-08-14 14:07:59 -0700116enum RelocationKind {
117 kRelocAbsolute = 0,
118 kRelocRelative,
119 kRelocCopy,
120 kRelocSymbol,
121 kRelocMax
122};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100123
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700125struct linker_stats_t {
126 int count[kRelocMax];
127};
128
129static linker_stats_t linker_stats;
130
131static void count_relocation(RelocationKind kind) {
132 ++linker_stats.count[kind];
133}
134#else
135static void count_relocation(RelocationKind) {
136}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800137#endif
138
139#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700140static unsigned bitmask[4096];
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100141#if defined(__LP64__)
142#define MARK(offset) \
143 do { \
144 if ((((offset) >> 12) >> 5) < 4096) \
145 bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
146 } while(0)
147#else
Elliott Hughesbedfe382012-08-14 14:07:59 -0700148#define MARK(offset) \
149 do { \
150 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
151 } while(0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100152#endif
Elliott Hughesbedfe382012-08-14 14:07:59 -0700153#else
154#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155#endif
156
Elliott Hughes46882792012-08-03 16:49:39 -0700157// You shouldn't try to call memory-allocating functions in the dynamic linker.
158// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700159#define DISALLOW_ALLOCATION(return_type, name, ...) \
160 return_type name __VA_ARGS__ \
161 { \
Elliott Hughes46882792012-08-03 16:49:39 -0700162 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700163 __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
164 write(2, msg, strlen(msg)); \
165 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700166 }
Elliott Hughes46882792012-08-03 16:49:39 -0700167#define UNUSED __attribute__((unused))
168DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
169DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
170DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
171DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
Dima Zavin2e855792009-05-20 18:28:09 -0700172
Dima Zavin03531952009-05-29 17:30:25 -0700173static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700174static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700175
Elliott Hughes650be4e2013-03-05 18:47:58 -0800176char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700177 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700178}
179
Elliott Hughes650be4e2013-03-05 18:47:58 -0800180size_t linker_get_error_buffer_size() {
181 return sizeof(__linker_dl_err_buf);
182}
183
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184/*
185 * This function is an empty stub where GDB locates a breakpoint to get notified
186 * about linker activity.
187 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700188extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800190static r_debug _r_debug = {1, NULL, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
191static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192
Elliott Hughes3b297c42012-10-11 16:08:51 -0700193static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800194
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800195static void insert_soinfo_into_debug_map(soinfo* info) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700196 // Copy the necessary fields into the debug structure.
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800197 link_map* map = &(info->link_map_head);
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400198 map->l_addr = info->load_bias;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199 map->l_name = (char*) info->name;
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800200 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201
202 /* Stick the new library at the end of the list.
203 * gdb tends to care more about libc than it does
204 * about leaf libraries, and ordering it this way
205 * reduces the back-and-forth over the wire.
206 */
207 if (r_debug_tail) {
208 r_debug_tail->l_next = map;
209 map->l_prev = r_debug_tail;
210 map->l_next = 0;
211 } else {
212 _r_debug.r_map = map;
213 map->l_prev = 0;
214 map->l_next = 0;
215 }
216 r_debug_tail = map;
217}
218
Elliott Hughesbedfe382012-08-14 14:07:59 -0700219static void remove_soinfo_from_debug_map(soinfo* info) {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800220 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700221
Elliott Hughesbedfe382012-08-14 14:07:59 -0700222 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700223 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700224 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700225
Elliott Hughesbedfe382012-08-14 14:07:59 -0700226 if (map->l_prev) {
227 map->l_prev->l_next = map->l_next;
228 }
229 if (map->l_next) {
230 map->l_next->l_prev = map->l_prev;
231 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700232}
233
Elliott Hughesbedfe382012-08-14 14:07:59 -0700234static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800235 if (info->flags & FLAG_EXE) {
236 // GDB already knows about the main executable
237 return;
238 }
239
Elliott Hughes3b297c42012-10-11 16:08:51 -0700240 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800242 _r_debug.r_state = r_debug::RT_ADD;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243 rtld_db_dlactivity();
244
245 insert_soinfo_into_debug_map(info);
246
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800247 _r_debug.r_state = r_debug::RT_CONSISTENT;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700249}
250
Elliott Hughesbedfe382012-08-14 14:07:59 -0700251static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700252 if (info->flags & FLAG_EXE) {
253 // GDB already knows about the main executable
254 return;
255 }
256
Elliott Hughes3b297c42012-10-11 16:08:51 -0700257 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700258
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800259 _r_debug.r_state = r_debug::RT_DELETE;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700260 rtld_db_dlactivity();
261
262 remove_soinfo_from_debug_map(info);
263
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800264 _r_debug.r_state = r_debug::RT_CONSISTENT;
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700265 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800266}
267
Elliott Hughes18a206c2012-10-29 17:37:13 -0700268void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800269 _r_debug.r_state = r_debug::RT_ADD;
270 rtld_db_dlactivity();
271 _r_debug.r_state = r_debug::RT_CONSISTENT;
272 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273}
274
Magnus Malmbornba98d922012-09-12 13:00:55 +0200275static bool ensure_free_list_non_empty() {
276 if (gSoInfoFreeList != NULL) {
277 return true;
278 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800279
Magnus Malmbornba98d922012-09-12 13:00:55 +0200280 // Allocate a new pool.
281 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
282 PROT_READ|PROT_WRITE,
283 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
284 if (pool == MAP_FAILED) {
285 return false;
286 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800287
Magnus Malmbornba98d922012-09-12 13:00:55 +0200288 // Add the pool to our list of pools.
289 pool->next = gSoInfoPools;
290 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291
Magnus Malmbornba98d922012-09-12 13:00:55 +0200292 // Chain the entries in the new pool onto the free list.
293 gSoInfoFreeList = &pool->info[0];
294 soinfo* next = NULL;
295 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
296 pool->info[i].next = next;
297 next = &pool->info[i];
298 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800299
Magnus Malmbornba98d922012-09-12 13:00:55 +0200300 return true;
301}
302
Elliott Hughesd23736e2012-11-01 15:16:56 -0700303static void set_soinfo_pool_protection(int protection) {
304 for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
305 if (mprotect(p, sizeof(*p), protection) == -1) {
306 abort(); // Can't happen.
307 }
308 }
309}
310
Magnus Malmbornba98d922012-09-12 13:00:55 +0200311static soinfo* soinfo_alloc(const char* name) {
312 if (strlen(name) >= SOINFO_NAME_LEN) {
313 DL_ERR("library name \"%s\" too long", name);
314 return NULL;
315 }
316
317 if (!ensure_free_list_non_empty()) {
318 DL_ERR("out of memory when loading \"%s\"", name);
319 return NULL;
320 }
321
322 // Take the head element off the free list.
323 soinfo* si = gSoInfoFreeList;
324 gSoInfoFreeList = gSoInfoFreeList->next;
325
326 // Initialize the new element.
327 memset(si, 0, sizeof(soinfo));
328 strlcpy(si->name, name, sizeof(si->name));
329 sonext->next = si;
330 sonext = si;
331
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700332 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200333 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334}
335
Elliott Hughes46882792012-08-03 16:49:39 -0700336static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800337{
Elliott Hughes46882792012-08-03 16:49:39 -0700338 if (si == NULL) {
339 return;
340 }
341
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800342 soinfo *prev = NULL, *trav;
343
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700344 TRACE("name %s: freeing soinfo @ %p", si->name, si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800345
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800346 for (trav = solist; trav != NULL; trav = trav->next) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800347 if (trav == si)
348 break;
349 prev = trav;
350 }
351 if (trav == NULL) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800352 /* si was not in solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700353 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800354 return;
355 }
356
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100357 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800358 always the static libdl_info.
359 */
360 prev->next = si->next;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800361 if (si == sonext) {
362 sonext = prev;
363 }
Magnus Malmbornba98d922012-09-12 13:00:55 +0200364 si->next = gSoInfoFreeList;
365 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800366}
367
Elliott Hughescade4c32012-12-20 14:42:14 -0800368
369static void parse_path(const char* path, const char* delimiters,
370 const char** array, char* buf, size_t buf_size, size_t max_count) {
371 if (path == NULL) {
372 return;
373 }
374
375 size_t len = strlcpy(buf, path, buf_size);
376
377 size_t i = 0;
378 char* buf_p = buf;
379 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
380 if (*array[i] != '\0') {
381 ++i;
382 }
383 }
384
385 // Forget the last path if we had to truncate; this occurs if the 2nd to
386 // last char isn't '\0' (i.e. wasn't originally a delimiter).
387 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
388 array[i - 1] = NULL;
389 } else {
390 array[i] = NULL;
391 }
392}
393
394static void parse_LD_LIBRARY_PATH(const char* path) {
395 parse_path(path, ":", gLdPaths,
396 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
397}
398
399static void parse_LD_PRELOAD(const char* path) {
400 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
401 parse_path(path, " :", gLdPreloadNames,
402 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
403}
404
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700405#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700406
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800407/* For a given PC, find the .so that it belongs to.
408 * Returns the base address of the .ARM.exidx section
409 * for that .so, and the number of 8-byte entries
410 * in that section (via *pcount).
411 *
412 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
413 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700414 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800415 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
417{
418 soinfo *si;
419 unsigned addr = (unsigned)pc;
420
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700421 for (si = solist; si != 0; si = si->next) {
Nick Kralevich468319c2011-11-11 15:53:17 -0800422 if ((addr >= si->base) && (addr < (si->base + si->size))) {
423 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900424 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800425 }
426 }
427 *pcount = 0;
428 return NULL;
429}
Elliott Hughes46882792012-08-03 16:49:39 -0700430
Christopher Ferris24053a42013-08-19 17:45:09 -0700431#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700432
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433/* Here, we only have to provide a callback to iterate across all the
434 * loaded libraries. gcc_eh does the rest. */
435int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700436dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800437 void *data)
438{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800439 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700440 for (soinfo* si = solist; si != NULL; si = si->next) {
441 dl_phdr_info dl_info;
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800442 dl_info.dlpi_addr = si->link_map_head.l_addr;
443 dl_info.dlpi_name = si->link_map_head.l_name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800444 dl_info.dlpi_phdr = si->phdr;
445 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700446 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
447 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800448 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700449 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800450 }
451 return rv;
452}
Elliott Hughes46882792012-08-03 16:49:39 -0700453
Elliott Hughesc6200592013-09-30 18:43:46 -0700454static Elf_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
455 Elf_Sym* symtab = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800456 const char* strtab = si->strtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800457
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700458 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
459 name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800460
Christopher Ferris6bec5b72013-06-03 17:27:49 -0700461 for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700462 Elf_Sym* s = symtab + n;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800463 if (strcmp(strtab + s->st_name, name)) continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800464
Doug Kwane8238072009-10-26 12:05:23 -0700465 /* only concern ourselves with global and weak symbol definitions */
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700466 switch (ELF_ST_BIND(s->st_info)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800467 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700468 case STB_WEAK:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800469 if (s->st_shndx == SHN_UNDEF) {
Robin Burchell439fa8e2012-07-05 09:21:07 +0200470 continue;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800471 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800472
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700473 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
474 name, si->name, reinterpret_cast<void*>(s->st_value),
475 static_cast<size_t>(s->st_size));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800476 return s;
477 }
478 }
479
Doug Kwan94304352009-10-23 18:11:40 -0700480 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800481}
482
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800483static unsigned elfhash(const char* _name) {
484 const unsigned char* name = (const unsigned char*) _name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800485 unsigned h = 0, g;
486
487 while(*name) {
488 h = (h << 4) + *name++;
489 g = h & 0xf0000000;
490 h ^= g;
491 h ^= g >> 24;
492 }
493 return h;
494}
495
Elliott Hughesc6200592013-09-30 18:43:46 -0700496static Elf_Sym* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
Doug Kwan94304352009-10-23 18:11:40 -0700497 unsigned elf_hash = elfhash(name);
Elliott Hughesc6200592013-09-30 18:43:46 -0700498 Elf_Sym* s = NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700499
Pavel Chupinc77c4342012-10-31 13:55:51 +0400500 if (si != NULL && somain != NULL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200501
502 /*
Pavel Chupinc77c4342012-10-31 13:55:51 +0400503 * Local scope is executable scope. Just start looking into it right away
504 * for the shortcut.
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200505 */
506
Pavel Chupinc77c4342012-10-31 13:55:51 +0400507 if (si == somain) {
508 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200509 if (s != NULL) {
Pavel Chupinc77c4342012-10-31 13:55:51 +0400510 *lsi = si;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200511 goto done;
512 }
Pavel Chupinc77c4342012-10-31 13:55:51 +0400513 } else {
514 /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200515
Pavel Chupinc77c4342012-10-31 13:55:51 +0400516 /*
517 * If this object was built with symbolic relocations disabled, the
518 * first place to look to resolve external references is the main
519 * executable.
520 */
Nick Kralevich468319c2011-11-11 15:53:17 -0800521
Pavel Chupinc77c4342012-10-31 13:55:51 +0400522 if (!si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700523 DEBUG("%s: looking up %s in executable %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700524 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400525 s = soinfo_elf_lookup(somain, elf_hash, name);
526 if (s != NULL) {
527 *lsi = somain;
528 goto done;
529 }
530 }
531
532 /* Look for symbols in the local scope (the object who is
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700533 * searching). This happens with C++ templates on x86 for some
Pavel Chupinc77c4342012-10-31 13:55:51 +0400534 * reason.
535 *
536 * Notes on weak symbols:
537 * The ELF specs are ambiguous about treatment of weak definitions in
538 * dynamic linking. Some systems return the first definition found
539 * and some the first non-weak definition. This is system dependent.
540 * Here we return the first definition found for simplicity. */
541
542 s = soinfo_elf_lookup(si, elf_hash, name);
543 if (s != NULL) {
544 *lsi = si;
545 goto done;
546 }
547
548 /*
549 * If this object was built with -Bsymbolic and symbol is not found
550 * in the local scope, try to find the symbol in the main executable.
551 */
552
553 if (si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700554 DEBUG("%s: looking up %s in executable %s after local scope",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700555 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400556 s = soinfo_elf_lookup(somain, elf_hash, name);
557 if (s != NULL) {
558 *lsi = somain;
559 goto done;
560 }
561 }
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200562 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700563 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700564
Matt Fischer4fd42c12009-12-31 12:09:10 -0600565 /* Next, look for it in the preloads list */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800566 for (int i = 0; gLdPreloads[i] != NULL; i++) {
567 s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
568 if (s != NULL) {
569 *lsi = gLdPreloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600570 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200571 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600572 }
573
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800574 for (int i = 0; needed[i] != NULL; i++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700575 DEBUG("%s: looking up %s in %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700576 si->name, name, needed[i]->name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200577 s = soinfo_elf_lookup(needed[i], elf_hash, name);
578 if (s != NULL) {
579 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200580 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200581 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700582 }
583
584done:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800585 if (s != NULL) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700586 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
587 "found in %s, base = %p, load bias = %p",
588 si->name, name, reinterpret_cast<void*>(s->st_value),
589 (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
590 reinterpret_cast<void*>((*lsi)->load_bias));
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700591 return s;
592 }
593
Doug Kwan94304352009-10-23 18:11:40 -0700594 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700595}
596
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800597/* This is used by dlsym(3). It performs symbol lookup only within the
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700598 specified soinfo object and not in any of its dependencies.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800599
600 TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
601 that it should do a breadth first search through the dependency
602 tree. This agrees with the ELF spec (aka System V Application
603 Binary Interface) where in Chapter 5 it discuss resolving "Shared
604 Object Dependencies" in breadth first search order.
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700605 */
Elliott Hughesc6200592013-09-30 18:43:46 -0700606Elf_Sym* dlsym_handle_lookup(soinfo* si, const char* name) {
David 'Digit' Turner16084162012-06-12 16:25:37 +0200607 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800608}
609
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800610/* This is used by dlsym(3) to performs a global symbol lookup. If the
611 start value is null (for RTLD_DEFAULT), the search starts at the
612 beginning of the global solist. Otherwise the search starts at the
613 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700614 */
Elliott Hughesc6200592013-09-30 18:43:46 -0700615Elf_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800616 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800617
Elliott Hughescade4c32012-12-20 14:42:14 -0800618 if (start == NULL) {
619 start = solist;
620 }
621
Elliott Hughesc6200592013-09-30 18:43:46 -0700622 Elf_Sym* s = NULL;
Elliott Hughescade4c32012-12-20 14:42:14 -0800623 for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
624 s = soinfo_elf_lookup(si, elf_hash, name);
625 if (s != NULL) {
626 *found = si;
627 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600628 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800629 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600630
Elliott Hughescade4c32012-12-20 14:42:14 -0800631 if (s != NULL) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700632 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
633 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800634 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800635
Elliott Hughescade4c32012-12-20 14:42:14 -0800636 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800637}
638
Kito Chengfa8c05d2013-03-12 14:58:06 +0800639soinfo* find_containing_library(const void* p) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700640 Elf_Addr address = reinterpret_cast<Elf_Addr>(p);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800641 for (soinfo* si = solist; si != NULL; si = si->next) {
642 if (address >= si->base && address - si->base < si->size) {
643 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600644 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800645 }
646 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600647}
648
Elliott Hughesc6200592013-09-30 18:43:46 -0700649Elf_Sym* dladdr_find_symbol(soinfo* si, const void* addr) {
650 Elf_Addr soaddr = reinterpret_cast<Elf_Addr>(addr) - si->base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600651
Kito Chengfa8c05d2013-03-12 14:58:06 +0800652 // Search the library's symbol table for any defined symbol which
653 // contains this address.
654 for (size_t i = 0; i < si->nchain; ++i) {
Elliott Hughesc6200592013-09-30 18:43:46 -0700655 Elf_Sym* sym = &si->symtab[i];
Kito Chengfa8c05d2013-03-12 14:58:06 +0800656 if (sym->st_shndx != SHN_UNDEF &&
657 soaddr >= sym->st_value &&
658 soaddr < sym->st_value + sym->st_size) {
659 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600660 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800661 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600662
Kito Chengfa8c05d2013-03-12 14:58:06 +0800663 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600664}
665
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800666#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800667static void dump(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800668{
Elliott Hughesc6200592013-09-30 18:43:46 -0700669 Elf_Sym* s = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800670 for (unsigned n = 0; n < si->nchain; n++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700671 TRACE("%04d> %08x: %02x %04x %08x %08x %s", n, s,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800672 s->st_info, s->st_shndx, s->st_value, s->st_size,
673 si->strtab + s->st_name);
674 s++;
675 }
676}
677#endif
678
Elliott Hughes124fae92012-10-31 14:20:03 -0700679static int open_library_on_path(const char* name, const char* const paths[]) {
680 char buf[512];
681 for (size_t i = 0; paths[i] != NULL; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800682 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700683 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700684 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700685 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800686 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700687 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
688 if (fd != -1) {
689 return fd;
690 }
691 }
692 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800693}
694
Elliott Hughes124fae92012-10-31 14:20:03 -0700695static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700696 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800697
Elliott Hughes124fae92012-10-31 14:20:03 -0700698 // If the name contains a slash, we should attempt to open it directly and not search the paths.
699 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700700 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
701 if (fd != -1) {
702 return fd;
703 }
704 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700705 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800706
Elliott Hughes124fae92012-10-31 14:20:03 -0700707 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
708 int fd = open_library_on_path(name, gLdPaths);
709 if (fd == -1) {
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800710 fd = open_library_on_path(name, gDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700711 }
712 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800713}
714
Elliott Hughes124fae92012-10-31 14:20:03 -0700715static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700716 // Open the file.
Elliott Hughes650be4e2013-03-05 18:47:58 -0800717 int fd = open_library(name);
718 if (fd == -1) {
Elliott Hughes46882792012-08-03 16:49:39 -0700719 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800720 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700721 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800722
Elliott Hughes650be4e2013-03-05 18:47:58 -0800723 // Read the ELF header and load the segments.
724 ElfReader elf_reader(name, fd);
725 if (!elf_reader.Load()) {
Elliott Hughes46882792012-08-03 16:49:39 -0700726 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800727 }
728
Elliott Hughes650be4e2013-03-05 18:47:58 -0800729 const char* bname = strrchr(name, '/');
730 soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
731 if (si == NULL) {
Elliott Hughes46882792012-08-03 16:49:39 -0700732 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200733 }
Elliott Hughes650be4e2013-03-05 18:47:58 -0800734 si->base = elf_reader.load_start();
735 si->size = elf_reader.load_size();
736 si->load_bias = elf_reader.load_bias();
737 si->flags = 0;
738 si->entry = 0;
739 si->dynamic = NULL;
740 si->phnum = elf_reader.phdr_count();
741 si->phdr = elf_reader.loaded_phdr();
742 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800743}
744
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200745static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800746{
747 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700748 const char *bname;
749
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200750 // TODO: don't use basename only for determining libraries
751 // http://code.google.com/p/android/issues/detail?id=6670
752
753 bname = strrchr(name, '/');
754 bname = bname ? bname + 1 : name;
755
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800756 for (si = solist; si != NULL; si = si->next) {
757 if (!strcmp(bname, si->name)) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200758 return si;
759 }
760 }
761 return NULL;
762}
763
Elliott Hughesd23736e2012-11-01 15:16:56 -0700764static soinfo* find_library_internal(const char* name) {
765 if (name == NULL) {
766 return somain;
767 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200768
Elliott Hughesd23736e2012-11-01 15:16:56 -0700769 soinfo* si = find_loaded_library(name);
770 if (si != NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700771 if (si->flags & FLAG_LINKED) {
772 return si;
773 }
774 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
775 return NULL;
776 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800777
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700778 TRACE("[ '%s' has not been loaded yet. Locating...]", name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700779 si = load_library(name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800780 if (si == NULL) {
781 return NULL;
782 }
783
784 // At this point we know that whatever is loaded @ base is a valid ELF
785 // shared library whose segments are properly mapped in.
Weiwu Chen5ceb8892013-12-03 19:47:34 +0800786 TRACE("[ find_library_internal base=%p size=%zu name='%s' ]",
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700787 reinterpret_cast<void*>(si->base), si->size, si->name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800788
789 if (!soinfo_link_image(si)) {
790 munmap(reinterpret_cast<void*>(si->base), si->size);
791 soinfo_free(si);
792 return NULL;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700793 }
794
795 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800796}
797
Elliott Hughesd23736e2012-11-01 15:16:56 -0700798static soinfo* find_library(const char* name) {
799 soinfo* si = find_library_internal(name);
800 if (si != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700801 si->ref_count++;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700802 }
803 return si;
804}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700805
Elliott Hughesd23736e2012-11-01 15:16:56 -0700806static int soinfo_unload(soinfo* si) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700807 if (si->ref_count == 1) {
808 TRACE("unloading '%s'", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700809 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800810
Elliott Hughesc6200592013-09-30 18:43:46 -0700811 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800812 if (d->d_tag == DT_NEEDED) {
813 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughes8147d3c2013-05-09 14:19:58 -0700814 TRACE("%s needs to unload %s", si->name, library_name);
815 soinfo_unload(find_loaded_library(library_name));
Elliott Hughesd23736e2012-11-01 15:16:56 -0700816 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800817 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700818
819 munmap(reinterpret_cast<void*>(si->base), si->size);
820 notify_gdb_of_unload(si);
821 soinfo_free(si);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700822 si->ref_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700823 } else {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700824 si->ref_count--;
Elliott Hughesc6200592013-09-30 18:43:46 -0700825 TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700826 }
827 return 0;
828}
829
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800830void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
831 snprintf(buffer, buffer_size, "%s:%s", gDefaultLdPaths[0], gDefaultLdPaths[1]);
832}
833
Elliott Hughescade4c32012-12-20 14:42:14 -0800834void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
835 if (!get_AT_SECURE()) {
836 parse_LD_LIBRARY_PATH(ld_library_path);
837 }
838}
839
Elliott Hughese66190d2012-12-18 15:57:55 -0800840soinfo* do_dlopen(const char* name, int flags) {
841 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
842 DL_ERR("invalid flags to dlopen: %x", flags);
843 return NULL;
844 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700845 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
846 soinfo* si = find_library(name);
847 if (si != NULL) {
848 si->CallConstructors();
849 }
850 set_soinfo_pool_protection(PROT_READ);
851 return si;
852}
853
854int do_dlclose(soinfo* si) {
855 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
856 int result = soinfo_unload(si);
857 set_soinfo_pool_protection(PROT_READ);
858 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800859}
860
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700861#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700862static int soinfo_relocate_a(soinfo* si, Elf_Rela* rela, unsigned count, soinfo* needed[]) {
863 Elf_Sym* symtab = si->symtab;
864 const char* strtab = si->strtab;
865 Elf_Sym* s;
866 Elf_Rela* start = rela;
867 soinfo* lsi;
868
869 for (size_t idx = 0; idx < count; ++idx, ++rela) {
870 unsigned type = ELF_R_TYPE(rela->r_info);
871 unsigned sym = ELF_R_SYM(rela->r_info);
872 Elf_Addr reloc = static_cast<Elf_Addr>(rela->r_offset + si->load_bias);
873 Elf_Addr sym_addr = 0;
874 char* sym_name = NULL;
875
876 DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
877 if (type == 0) { // R_*_NONE
878 continue;
879 }
880 if (sym != 0) {
881 sym_name = (char *)(strtab + symtab[sym].st_name);
882 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
883 if (s == NULL) {
884 // We only allow an undefined symbol if this is a weak reference...
885 s = &symtab[sym];
886 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
887 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
888 return -1;
889 }
890
891 /* IHI0044C AAELF 4.5.1.1:
892
893 Libraries are not searched to resolve weak references.
894 It is not an error for a weak reference to remain unsatisfied.
895
896 During linking, the value of an undefined weak reference is:
897 - Zero if the relocation type is absolute
898 - The address of the place if the relocation is pc-relative
899 - The address of nominal base address if the relocation
900 type is base-relative.
901 */
902
903 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100904#if defined(__aarch64__)
905 case R_AARCH64_JUMP_SLOT:
906 case R_AARCH64_GLOB_DAT:
907 case R_AARCH64_ABS64:
908 case R_AARCH64_ABS32:
909 case R_AARCH64_ABS16:
910 case R_AARCH64_RELATIVE:
911 /*
912 * The sym_addr was initialized to be zero above, or the relocation
913 * code below does not care about value of sym_addr.
914 * No need to do anything.
915 */
916 break;
917#elif defined(__x86_64__)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700918 case R_X86_64_JUMP_SLOT:
919 case R_X86_64_GLOB_DAT:
920 case R_X86_64_32:
921 case R_X86_64_RELATIVE:
922 // No need to do anything.
923 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700924 case R_X86_64_PC32:
925 sym_addr = reloc;
926 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700927#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700928 default:
929 DL_ERR("unknown weak reloc type %d @ %p (%d)", type, rela, (int) (rela - start));
930 return -1;
931 }
932 } else {
933 // We got a definition.
934 sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias);
935 }
936 count_relocation(kRelocSymbol);
937 } else {
938 s = NULL;
939 }
940
941 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100942#if defined(__aarch64__)
943 case R_AARCH64_JUMP_SLOT:
944 count_relocation(kRelocAbsolute);
945 MARK(rela->r_offset);
946 TRACE_TYPE(RELO, "RELO JMP_SLOT %16lx <- %16lx %s\n",
947 reloc,
948 (sym_addr + rela->r_addend),
949 sym_name);
950 *reinterpret_cast<Elf_Addr*>(reloc) = (sym_addr + rela->r_addend);
951 break;
952 case R_AARCH64_GLOB_DAT:
953 count_relocation(kRelocAbsolute);
954 MARK(rela->r_offset);
955 TRACE_TYPE(RELO, "RELO GLOB_DAT %16lx <- %16lx %s\n",
956 reloc,
957 (sym_addr + rela->r_addend),
958 sym_name);
959 *reinterpret_cast<Elf_Addr*>(reloc) = (sym_addr + rela->r_addend);
960 break;
961 case R_AARCH64_ABS64:
962 count_relocation(kRelocAbsolute);
963 MARK(rela->r_offset);
964 TRACE_TYPE(RELO, "RELO ABS64 %16lx <- %16lx %s\n",
965 reloc,
966 (sym_addr + rela->r_addend),
967 sym_name);
968 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr + rela->r_addend);
969 break;
970 case R_AARCH64_ABS32:
971 count_relocation(kRelocAbsolute);
972 MARK(rela->r_offset);
973 TRACE_TYPE(RELO, "RELO ABS32 %16lx <- %16lx %s\n",
974 reloc,
975 (sym_addr + rela->r_addend),
976 sym_name);
977 if ((static_cast<Elf_Addr>(INT32_MIN) <=
978 (*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend))) &&
979 ((*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend)) <=
980 static_cast<Elf_Addr>(UINT32_MAX))) {
981 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr + rela->r_addend);
982 } else {
983 DL_ERR("0x%016lx out of range 0x%016lx to 0x%016lx",
984 (*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend)),
985 static_cast<Elf_Addr>(INT32_MIN),
986 static_cast<Elf_Addr>(UINT32_MAX));
987 return -1;
988 }
989 break;
990 case R_AARCH64_ABS16:
991 count_relocation(kRelocAbsolute);
992 MARK(rela->r_offset);
993 TRACE_TYPE(RELO, "RELO ABS16 %16lx <- %16lx %s\n",
994 reloc,
995 (sym_addr + rela->r_addend),
996 sym_name);
997 if ((static_cast<Elf_Addr>(INT16_MIN) <=
998 (*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend))) &&
999 ((*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend)) <=
1000 static_cast<Elf_Addr>(UINT16_MAX))) {
1001 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr + rela->r_addend);
1002 } else {
1003 DL_ERR("0x%016lx out of range 0x%016lx to 0x%016lx",
1004 (*reinterpret_cast<Elf_Addr*>(reloc) + (sym_addr + rela->r_addend)),
1005 static_cast<Elf_Addr>(INT16_MIN),
1006 static_cast<Elf_Addr>(UINT16_MAX));
1007 return -1;
1008 }
1009 break;
1010 case R_AARCH64_PREL64:
1011 count_relocation(kRelocRelative);
1012 MARK(rela->r_offset);
1013 TRACE_TYPE(RELO, "RELO REL64 %16lx <- %16lx - %16lx %s\n",
1014 reloc,
1015 (sym_addr + rela->r_addend),
1016 rela->r_offset,
1017 sym_name);
1018 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
1019 break;
1020 case R_AARCH64_PREL32:
1021 count_relocation(kRelocRelative);
1022 MARK(rela->r_offset);
1023 TRACE_TYPE(RELO, "RELO REL32 %16lx <- %16lx - %16lx %s\n",
1024 reloc,
1025 (sym_addr + rela->r_addend),
1026 rela->r_offset, sym_name);
1027 if ((static_cast<Elf_Addr>(INT32_MIN) <=
1028 (*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1029 ((*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <=
1030 static_cast<Elf_Addr>(UINT32_MAX))) {
1031 *reinterpret_cast<Elf_Addr*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
1032 } else {
1033 DL_ERR("0x%016lx out of range 0x%016lx to 0x%016lx",
1034 (*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1035 static_cast<Elf_Addr>(INT32_MIN),
1036 static_cast<Elf_Addr>(UINT32_MAX));
1037 return -1;
1038 }
1039 break;
1040 case R_AARCH64_PREL16:
1041 count_relocation(kRelocRelative);
1042 MARK(rela->r_offset);
1043 TRACE_TYPE(RELO, "RELO REL16 %16lx <- %16lx - %16lx %s\n",
1044 reloc,
1045 (sym_addr + rela->r_addend),
1046 rela->r_offset, sym_name);
1047 if ((static_cast<Elf_Addr>(INT16_MIN) <=
1048 (*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1049 ((*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <=
1050 static_cast<Elf_Addr>(UINT16_MAX))) {
1051 *reinterpret_cast<Elf_Addr*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
1052 } else {
1053 DL_ERR("0x%016lx out of range 0x%016lx to 0x%016lx",
1054 (*reinterpret_cast<Elf_Addr*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1055 static_cast<Elf_Addr>(INT16_MIN),
1056 static_cast<Elf_Addr>(UINT16_MAX));
1057 return -1;
1058 }
1059 break;
1060
1061 case R_AARCH64_RELATIVE:
1062 count_relocation(kRelocRelative);
1063 MARK(rela->r_offset);
1064 if (sym) {
1065 DL_ERR("odd RELATIVE form...");
1066 return -1;
1067 }
1068 TRACE_TYPE(RELO, "RELO RELATIVE %16lx <- %16lx\n",
1069 reloc,
1070 (si->base + rela->r_addend));
1071 *reinterpret_cast<Elf_Addr*>(reloc) = (si->base + rela->r_addend);
1072 break;
1073
1074 case R_AARCH64_COPY:
1075 if ((si->flags & FLAG_EXE) == 0) {
1076 /*
1077 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1078 *
1079 * Section 4.7.1.10 "Dynamic relocations"
1080 * R_AARCH64_COPY may only appear in executable objects where e_type is
1081 * set to ET_EXEC.
1082 *
1083 * FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1084 * We should explicitly disallow ET_DYN executables from having
1085 * R_AARCH64_COPY relocations.
1086 */
1087 DL_ERR("%s R_AARCH64_COPY relocations only supported for ET_EXEC", si->name);
1088 return -1;
1089 }
1090 count_relocation(kRelocCopy);
1091 MARK(rela->r_offset);
1092 TRACE_TYPE(RELO, "RELO COPY %16lx <- %ld @ %16lx %s\n",
1093 reloc,
1094 s->st_size,
1095 (sym_addr + rela->r_addend),
1096 sym_name);
1097 if (reloc == (sym_addr + rela->r_addend)) {
1098 Elf_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1099
1100 if (src == NULL) {
1101 DL_ERR("%s R_AARCH64_COPY relocation source cannot be resolved", si->name);
1102 return -1;
1103 }
1104 if (lsi->has_DT_SYMBOLIC) {
1105 DL_ERR("%s invalid R_AARCH64_COPY relocation against DT_SYMBOLIC shared "
1106 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1107 return -1;
1108 }
1109 if (s->st_size < src->st_size) {
1110 DL_ERR("%s R_AARCH64_COPY relocation size mismatch (%ld < %ld)",
1111 si->name, s->st_size, src->st_size);
1112 return -1;
1113 }
1114 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1115 } else {
1116 DL_ERR("%s R_AARCH64_COPY relocation target cannot be resolved", si->name);
1117 return -1;
1118 }
1119 break;
1120 case R_AARCH64_TLS_TPREL64:
1121 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16lx <- %16lx - %16lx\n",
1122 reloc,
1123 (sym_addr + rela->r_addend),
1124 rela->r_offset);
1125 break;
1126 case R_AARCH64_TLS_DTPREL32:
1127 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16lx <- %16lx - %16lx\n",
1128 reloc,
1129 (sym_addr + rela->r_addend),
1130 rela->r_offset);
1131 break;
1132#elif defined(__x86_64__)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001133 case R_X86_64_JUMP_SLOT:
1134 count_relocation(kRelocAbsolute);
1135 MARK(rela->r_offset);
1136 TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1137 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1138 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
1139 break;
1140 case R_X86_64_GLOB_DAT:
1141 count_relocation(kRelocAbsolute);
1142 MARK(rela->r_offset);
1143 TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1144 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1145 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
1146 break;
1147 case R_X86_64_RELATIVE:
1148 count_relocation(kRelocRelative);
1149 MARK(rela->r_offset);
1150 if (sym) {
1151 DL_ERR("odd RELATIVE form...");
1152 return -1;
1153 }
1154 TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1155 static_cast<size_t>(si->base));
1156 *reinterpret_cast<Elf_Addr*>(reloc) = si->base + rela->r_addend;
1157 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001158 case R_X86_64_32:
1159 count_relocation(kRelocRelative);
1160 MARK(rela->r_offset);
1161 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1162 static_cast<size_t>(sym_addr), sym_name);
1163 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
1164 break;
Pavel Chupinc075c182013-10-16 19:13:58 +04001165 case R_X86_64_64:
1166 count_relocation(kRelocRelative);
1167 MARK(rela->r_offset);
1168 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1169 static_cast<size_t>(sym_addr), sym_name);
1170 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
1171 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001172 case R_X86_64_PC32:
1173 count_relocation(kRelocRelative);
1174 MARK(rela->r_offset);
1175 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1176 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1177 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
1178 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend - reloc;
1179 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001180#endif
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001181
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001182 default:
1183 DL_ERR("unknown reloc type %d @ %p (%d)", type, rela, (int) (rela - start));
1184 return -1;
1185 }
1186 }
1187 return 0;
1188}
1189#else
Elliott Hughesc6200592013-09-30 18:43:46 -07001190static int soinfo_relocate(soinfo* si, Elf_Rel* rel, unsigned count,
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001191 soinfo* needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001192{
Elliott Hughesc6200592013-09-30 18:43:46 -07001193 Elf_Sym* symtab = si->symtab;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001194 const char* strtab = si->strtab;
Elliott Hughesc6200592013-09-30 18:43:46 -07001195 Elf_Sym* s;
1196 Elf_Rel* start = rel;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001197 soinfo* lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001198
Elliott Hughes46882792012-08-03 16:49:39 -07001199 for (size_t idx = 0; idx < count; ++idx, ++rel) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001200 unsigned type = ELF_R_TYPE(rel->r_info);
1201 // TODO: don't use unsigned for 'sym'. Use uint32_t or Elf_Addr instead.
1202 unsigned sym = ELF_R_SYM(rel->r_info);
Elliott Hughesc6200592013-09-30 18:43:46 -07001203 Elf_Addr reloc = static_cast<Elf_Addr>(rel->r_offset + si->load_bias);
1204 Elf_Addr sym_addr = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001205 char* sym_name = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001206
Elliott Hughesc6200592013-09-30 18:43:46 -07001207 DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001208 if (type == 0) { // R_*_NONE
1209 continue;
1210 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001211 if (sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -07001212 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001213 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001214 if (s == NULL) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001215 // We only allow an undefined symbol if this is a weak reference...
Doug Kwane8238072009-10-26 12:05:23 -07001216 s = &symtab[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001217 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -07001218 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -07001219 return -1;
1220 }
1221
1222 /* IHI0044C AAELF 4.5.1.1:
1223
1224 Libraries are not searched to resolve weak references.
1225 It is not an error for a weak reference to remain
1226 unsatisfied.
1227
1228 During linking, the value of an undefined weak reference is:
1229 - Zero if the relocation type is absolute
1230 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -07001231 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -07001232 type is base-relative.
1233 */
1234
1235 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001236#if defined(__arm__)
Doug Kwane8238072009-10-26 12:05:23 -07001237 case R_ARM_JUMP_SLOT:
1238 case R_ARM_GLOB_DAT:
1239 case R_ARM_ABS32:
1240 case R_ARM_RELATIVE: /* Don't care. */
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001241 // sym_addr was initialized to be zero above or relocation
1242 // code below does not care about value of sym_addr.
1243 // No need to do anything.
1244 break;
1245#elif defined(__i386__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001246 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -07001247 case R_386_GLOB_DAT:
1248 case R_386_32:
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001249 case R_386_RELATIVE: /* Don't care. */
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001250 // sym_addr was initialized to be zero above or relocation
1251 // code below does not care about value of sym_addr.
1252 // No need to do anything.
Doug Kwane8238072009-10-26 12:05:23 -07001253 break;
Doug Kwane8238072009-10-26 12:05:23 -07001254 case R_386_PC32:
1255 sym_addr = reloc;
1256 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001257#endif
Doug Kwane8238072009-10-26 12:05:23 -07001258
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001259#if defined(__arm__)
Doug Kwane8238072009-10-26 12:05:23 -07001260 case R_ARM_COPY:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001261 // Fall through. Can't really copy if weak symbol is not found at run-time.
1262#endif
Doug Kwane8238072009-10-26 12:05:23 -07001263 default:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001264 DL_ERR("unknown weak reloc type %d @ %p (%d)", type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -07001265 return -1;
1266 }
1267 } else {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001268 // We got a definition.
Elliott Hughesc6200592013-09-30 18:43:46 -07001269 sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001270 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001271 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001272 } else {
Doug Kwane8238072009-10-26 12:05:23 -07001273 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001274 }
1275
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001276 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001277#if defined(__arm__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001278 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001279 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001280 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001281 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001282 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001283 break;
1284 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001285 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001286 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001287 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001288 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001289 break;
1290 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001291 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001292 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001293 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001294 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001295 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001296 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001297 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001298 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001299 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001300 reloc, sym_addr, rel->r_offset, sym_name);
Elliott Hughesc6200592013-09-30 18:43:46 -07001301 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr - rel->r_offset;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -08001302 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001303 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001304 if ((si->flags & FLAG_EXE) == 0) {
1305 /*
1306 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1307 *
1308 * Section 4.7.1.10 "Dynamic relocations"
1309 * R_ARM_COPY may only appear in executable objects where e_type is
1310 * set to ET_EXEC.
1311 *
1312 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1313 * We should explicitly disallow ET_DYN executables from having
1314 * R_ARM_COPY relocations.
1315 */
1316 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1317 return -1;
1318 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001319 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001320 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001321 TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s", reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001322 if (reloc == sym_addr) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001323 Elf_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001324
1325 if (src == NULL) {
1326 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1327 return -1;
1328 }
1329 if (lsi->has_DT_SYMBOLIC) {
1330 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1331 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1332 return -1;
1333 }
1334 if (s->st_size < src->st_size) {
1335 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1336 si->name, s->st_size, src->st_size);
1337 return -1;
1338 }
1339 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1340 } else {
1341 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001342 return -1;
1343 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001344 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001345#elif defined(__i386__)
1346 case R_386_JMP_SLOT:
1347 count_relocation(kRelocAbsolute);
1348 MARK(rel->r_offset);
1349 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1350 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
1351 break;
1352 case R_386_GLOB_DAT:
1353 count_relocation(kRelocAbsolute);
1354 MARK(rel->r_offset);
1355 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1356 *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
1357 break;
1358 case R_386_32:
1359 count_relocation(kRelocRelative);
1360 MARK(rel->r_offset);
1361 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1362 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
1363 break;
1364 case R_386_PC32:
1365 count_relocation(kRelocRelative);
1366 MARK(rel->r_offset);
1367 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1368 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1369 *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr - reloc);
1370 break;
1371#elif defined(__mips__)
1372 case R_MIPS_REL32:
1373 count_relocation(kRelocAbsolute);
1374 MARK(rel->r_offset);
1375 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s",
1376 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1377 if (s) {
1378 *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
1379 } else {
1380 *reinterpret_cast<Elf_Addr*>(reloc) += si->base;
1381 }
1382 break;
1383#endif
1384
1385#if defined(__arm__)
1386 case R_ARM_RELATIVE:
1387#elif defined(__i386__)
1388 case R_386_RELATIVE:
1389#endif
1390 count_relocation(kRelocRelative);
1391 MARK(rel->r_offset);
1392 if (sym) {
1393 DL_ERR("odd RELATIVE form...");
1394 return -1;
1395 }
1396 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1397 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base));
1398 *reinterpret_cast<Elf_Addr*>(reloc) += si->base;
1399 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001400
1401 default:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001402 DL_ERR("unknown reloc type %d @ %p (%d)", type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001403 return -1;
1404 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001405 }
1406 return 0;
1407}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001408#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001409
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001410#if defined(__mips__)
Brian Carlstrom87c35852013-08-20 21:05:44 -07001411static bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
1412 unsigned* got = si->plt_got;
1413 if (got == NULL) {
1414 return true;
1415 }
1416 unsigned local_gotno = si->mips_local_gotno;
1417 unsigned gotsym = si->mips_gotsym;
1418 unsigned symtabno = si->mips_symtabno;
Elliott Hughesc6200592013-09-30 18:43:46 -07001419 Elf_Sym* symtab = si->symtab;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001420
1421 /*
1422 * got[0] is address of lazy resolver function
1423 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001424 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001425 * (should be _rtld_bind_start)
1426 * FIXME: maybe this should be in a separate routine
1427 */
1428
1429 if ((si->flags & FLAG_LINKER) == 0) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001430 size_t g = 0;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001431 got[g++] = 0xdeadbeef;
1432 if (got[g] & 0x80000000) {
1433 got[g++] = 0xdeadfeed;
1434 }
1435 /*
1436 * Relocate the local GOT entries need to be relocated
1437 */
1438 for (; g < local_gotno; g++) {
1439 got[g] += si->load_bias;
1440 }
1441 }
1442
1443 /* Now for the global GOT entries */
Elliott Hughesc6200592013-09-30 18:43:46 -07001444 Elf_Sym* sym = symtab + gotsym;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001445 got = si->plt_got + local_gotno;
Brian Carlstrom87c35852013-08-20 21:05:44 -07001446 for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001447 const char* sym_name;
Elliott Hughesc6200592013-09-30 18:43:46 -07001448 Elf_Sym* s;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001449 soinfo* lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001450
1451 /* This is an undefined reference... try to locate it */
1452 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001453 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001454 if (s == NULL) {
1455 /* We only allow an undefined symbol if this is a weak
1456 reference.. */
1457 s = &symtab[g];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001458 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001459 DL_ERR("cannot locate \"%s\"...", sym_name);
Brian Carlstrom87c35852013-08-20 21:05:44 -07001460 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001461 }
1462 *got = 0;
1463 }
1464 else {
1465 /* FIXME: is this sufficient?
1466 * For reference see NetBSD link loader
1467 * 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
1468 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001469 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001470 }
1471 }
Brian Carlstrom87c35852013-08-20 21:05:44 -07001472 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001473}
1474#endif
1475
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001476void soinfo::CallArray(const char* array_name UNUSED, linker_function_t* functions, size_t count, bool reverse) {
1477 if (functions == NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001478 return;
1479 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001480
Elliott Hughesc6200592013-09-30 18:43:46 -07001481 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001482
1483 int begin = reverse ? (count - 1) : 0;
1484 int end = reverse ? -1 : count;
1485 int step = reverse ? -1 : 1;
1486
1487 for (int i = begin; i != end; i += step) {
1488 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1489 CallFunction("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001490 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001491
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001492 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001493}
1494
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001495void soinfo::CallFunction(const char* function_name UNUSED, linker_function_t function) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001496 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001497 return;
1498 }
1499
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001500 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001501 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001502 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001503
1504 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1505 // are still writable. This happens with our debug malloc (see http://b/7941716).
1506 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001507}
1508
Elliott Hughesd23736e2012-11-01 15:16:56 -07001509void soinfo::CallPreInitConstructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001510 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1511 // but ignored in a shared library.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001512 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1513}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001514
Elliott Hughesd23736e2012-11-01 15:16:56 -07001515void soinfo::CallConstructors() {
1516 if (constructors_called) {
1517 return;
1518 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001519
Elliott Hughesd23736e2012-11-01 15:16:56 -07001520 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1521 // protect against recursive constructor calls. One simple example of constructor recursion
1522 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1523 // 1. The program depends on libc, so libc's constructor is called here.
1524 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1525 // 3. dlopen() calls the constructors on the newly created
1526 // soinfo for libc_malloc_debug_leak.so.
1527 // 4. The debug .so depends on libc, so CallConstructors is
1528 // called again with the libc soinfo. If it doesn't trigger the early-
1529 // out above, the libc constructor will be called again (recursively!).
1530 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001531
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001532 if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
1533 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001534 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001535 name, preinit_array_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001536 }
1537
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001538 if (dynamic != NULL) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001539 for (Elf_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001540 if (d->d_tag == DT_NEEDED) {
1541 const char* library_name = strtab + d->d_un.d_val;
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001542 TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
1543 find_loaded_library(library_name)->CallConstructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001544 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001545 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001546 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001547
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001548 TRACE("\"%s\": calling constructors", name);
1549
1550 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001551 CallFunction("DT_INIT", init_func);
1552 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001553}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001554
Elliott Hughesd23736e2012-11-01 15:16:56 -07001555void soinfo::CallDestructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001556 TRACE("\"%s\": calling destructors", name);
1557
1558 // DT_FINI_ARRAY must be parsed in reverse order.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001559 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001560
1561 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001562 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001563}
1564
1565/* Force any of the closed stdin, stdout and stderr to be associated with
1566 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001567static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001568 int dev_null, i, status;
1569 int return_value = 0;
1570
David 'Digit' Turner16084162012-06-12 16:25:37 +02001571 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001572 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001573 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001574 return -1;
1575 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001576 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001577
1578 /* If any of the stdio file descriptors is valid and not associated
1579 with /dev/null, dup /dev/null to it. */
1580 for (i = 0; i < 3; i++) {
1581 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001582 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001583 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001584 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001585
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001586 TRACE("[ Nullifying stdio file descriptor %d]", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001587 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001588
Elliott Hughes46882792012-08-03 16:49:39 -07001589 /* If file is opened, we are good. */
1590 if (status != -1) {
1591 continue;
1592 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001593
1594 /* The only error we allow is that the file descriptor does not
1595 exist, in which case we dup /dev/null to it. */
1596 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001597 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001598 return_value = -1;
1599 continue;
1600 }
1601
1602 /* Try dupping /dev/null to this stdio file descriptor and
1603 repeat if there is a signal. Note that any errors in closing
1604 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001605 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001606 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001607 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001608 return_value = -1;
1609 continue;
1610 }
1611 }
1612
1613 /* If /dev/null is not one of the stdio file descriptors, close it. */
1614 if (dev_null > 2) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001615 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001616 status = TEMP_FAILURE_RETRY(close(dev_null));
1617 if (status == -1) {
1618 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001619 return_value = -1;
1620 }
1621 }
1622
1623 return return_value;
1624}
1625
Elliott Hughes124fae92012-10-31 14:20:03 -07001626static bool soinfo_link_image(soinfo* si) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001627 /* "base" might wrap around UINT32_MAX. */
Elliott Hughesc6200592013-09-30 18:43:46 -07001628 Elf_Addr base = si->load_bias;
1629 const Elf_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001630 int phnum = si->phnum;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001631 bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001632
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001633 /* We can't debug anything until the linker is relocated */
1634 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001635 INFO("[ linking %s ]", si->name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001636 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001637 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001638
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001639 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001640 size_t dynamic_count;
Elliott Hughesc6200592013-09-30 18:43:46 -07001641 Elf_Word dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001642 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001643 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001644 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001645 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001646 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001647 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001648 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001649 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001650 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001651 DEBUG("dynamic = %p", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001652 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001653 }
1654
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001655#if defined(__arm__)
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001656 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1657 &si->ARM_exidx, &si->ARM_exidx_count);
1658#endif
1659
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001660 // Extract useful information from dynamic section.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001661 uint32_t needed_count = 0;
Elliott Hughesc6200592013-09-30 18:43:46 -07001662 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001663 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1664 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1665 switch (d->d_tag) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001666 case DT_HASH:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001667 si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0];
1668 si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1];
1669 si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8);
1670 si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001671 break;
1672 case DT_STRTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001673 si->strtab = (const char *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001674 break;
1675 case DT_SYMTAB:
Elliott Hughesc6200592013-09-30 18:43:46 -07001676 si->symtab = (Elf_Sym *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001677 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001678#if !defined(__LP64__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001679 case DT_PLTREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001680 if (d->d_un.d_val != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001681 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1682 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001683 }
1684 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001685#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001686 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001687#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001688 si->plt_rela = (Elf_Rela*) (base + d->d_un.d_ptr);
1689#else
Elliott Hughesc6200592013-09-30 18:43:46 -07001690 si->plt_rel = (Elf_Rel*) (base + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001691#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001692 break;
1693 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001694#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001695 si->plt_rela_count = d->d_un.d_val / sizeof(Elf_Rela);
1696#else
Elliott Hughesc6200592013-09-30 18:43:46 -07001697 si->plt_rel_count = d->d_un.d_val / sizeof(Elf_Rel);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001698#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001699 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001700#if !defined(__LP64__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001701 case DT_PLTGOT:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001702 // Used by 32-bit MIPS.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001703 si->plt_got = (unsigned *)(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001704 break;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001705#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001706 case DT_DEBUG:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001707 // Set the DT_DEBUG entry to the address of _r_debug for GDB
Chris Dearmancf239052013-01-11 15:32:20 -08001708 // if the dynamic table is writable
Elliott Hughes99c32052013-01-14 09:56:21 -08001709 if ((dynamic_flags & PF_W) != 0) {
Elliott Hughesc6200592013-09-30 18:43:46 -07001710 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
Elliott Hughes99c32052013-01-14 09:56:21 -08001711 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001712 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001713#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001714 case DT_RELA:
1715 si->rela = (Elf_Rela*) (base + d->d_un.d_ptr);
1716 break;
1717 case DT_RELASZ:
1718 si->rela_count = d->d_un.d_val / sizeof(Elf_Rela);
1719 break;
1720 case DT_REL:
1721 DL_ERR("unsupported DT_REL in \"%s\"", si->name);
1722 return false;
1723 case DT_RELSZ:
1724 DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name);
1725 return false;
1726#else
1727 case DT_REL:
1728 si->rel = (Elf_Rel*) (base + d->d_un.d_ptr);
1729 break;
1730 case DT_RELSZ:
1731 si->rel_count = d->d_un.d_val / sizeof(Elf_Rel);
1732 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001733 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001734 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1735 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001736#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001737 case DT_INIT:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001738 si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001739 DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001740 break;
1741 case DT_FINI:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001742 si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001743 DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001744 break;
1745 case DT_INIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001746 si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001747 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001748 break;
1749 case DT_INIT_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001750 si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001751 break;
1752 case DT_FINI_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001753 si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001754 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001755 break;
1756 case DT_FINI_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001757 si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001758 break;
1759 case DT_PREINIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001760 si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001761 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001762 break;
1763 case DT_PREINIT_ARRAYSZ:
Elliott Hughesc6200592013-09-30 18:43:46 -07001764 si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001765 break;
1766 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07001767#if defined(__LP64__)
1768 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1769 return false;
1770#else
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001771 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001772 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07001773#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001774 case DT_SYMBOLIC:
1775 si->has_DT_SYMBOLIC = true;
1776 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001777 case DT_NEEDED:
1778 ++needed_count;
1779 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001780 case DT_FLAGS:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001781 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001782#if defined(__LP64__)
1783 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1784 return false;
1785#else
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001786 si->has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07001787#endif
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001788 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001789 if (d->d_un.d_val & DF_SYMBOLIC) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001790 si->has_DT_SYMBOLIC = true;
1791 }
1792 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001793#if defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001794 case DT_STRSZ:
1795 case DT_SYMENT:
1796 case DT_RELENT:
1797 break;
1798 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001799 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001800 {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001801 r_debug** dp = (r_debug**) d->d_un.d_ptr;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001802 *dp = &_r_debug;
1803 }
1804 break;
1805 case DT_MIPS_RLD_VERSION:
1806 case DT_MIPS_FLAGS:
1807 case DT_MIPS_BASE_ADDRESS:
1808 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001809 break;
1810
1811 case DT_MIPS_SYMTABNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001812 si->mips_symtabno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001813 break;
1814
1815 case DT_MIPS_LOCAL_GOTNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001816 si->mips_local_gotno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001817 break;
1818
1819 case DT_MIPS_GOTSYM:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001820 si->mips_gotsym = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001821 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001822#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001823
1824 default:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001825 DEBUG("Unused DT entry: type %p arg %p",
1826 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001827 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001828 }
1829 }
1830
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001831 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
1832 reinterpret_cast<void*>(si->base), si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001833
Elliott Hughes124fae92012-10-31 14:20:03 -07001834 // Sanity checks.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001835 if (relocating_linker && needed_count != 0) {
1836 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1837 return false;
1838 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001839 if (si->nbucket == 0) {
1840 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1841 return false;
1842 }
1843 if (si->strtab == 0) {
1844 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1845 return false;
1846 }
1847 if (si->symtab == 0) {
1848 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1849 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001850 }
1851
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001852 // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001853 if (si->flags & FLAG_EXE) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001854 memset(gLdPreloads, 0, sizeof(gLdPreloads));
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001855 size_t preload_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001856 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1857 soinfo* lsi = find_library(gLdPreloadNames[i]);
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -07001858 if (lsi != NULL) {
1859 gLdPreloads[preload_count++] = lsi;
1860 } else {
1861 // As with glibc, failure to load an LD_PRELOAD library is just a warning.
1862 DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
1863 gLdPreloadNames[i], si->name, linker_get_error_buffer());
Matt Fischer4fd42c12009-12-31 12:09:10 -06001864 }
Matt Fischer4fd42c12009-12-31 12:09:10 -06001865 }
1866 }
1867
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001868 soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*));
1869 soinfo** pneeded = needed;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001870
Elliott Hughesc6200592013-09-30 18:43:46 -07001871 for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001872 if (d->d_tag == DT_NEEDED) {
1873 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001874 DEBUG("%s needs %s", si->name, library_name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001875 soinfo* lsi = find_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001876 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001877 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001878 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001879 library_name, si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001880 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001881 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001882 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001883 }
1884 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001885 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001886
Elliott Hughese4d792a2013-10-28 14:19:05 -07001887#if !defined(__LP64__)
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001888 if (si->has_text_relocations) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001889 // Make segments writable to allow text relocations to work properly. We will later call
1890 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
Nick Kralevich3d4470c2013-10-22 12:06:36 -07001891 DL_WARN("%s has text relocations. This is wasting memory and prevents "
1892 "security hardening. Please fix.", si->name);
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001893 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1894 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1895 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001896 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001897 }
1898 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07001899#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001900
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001901#if defined(USE_RELA)
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001902 if (si->plt_rela != NULL) {
1903 DEBUG("[ relocating %s plt ]\n", si->name );
1904 if (soinfo_relocate_a(si, si->plt_rela, si->plt_rela_count, needed)) {
1905 return false;
1906 }
1907 }
1908 if (si->rela != NULL) {
1909 DEBUG("[ relocating %s ]\n", si->name );
1910 if (soinfo_relocate_a(si, si->rela, si->rela_count, needed)) {
1911 return false;
1912 }
1913 }
1914#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001915 if (si->plt_rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001916 DEBUG("[ relocating %s plt ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001917 if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001918 return false;
1919 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001920 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001921 if (si->rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001922 DEBUG("[ relocating %s ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001923 if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001924 return false;
1925 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001926 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001927#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001928
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001929#if defined(__mips__)
Brian Carlstrom87c35852013-08-20 21:05:44 -07001930 if (!mips_relocate_got(si, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001931 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001932 }
1933#endif
1934
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001935 si->flags |= FLAG_LINKED;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001936 DEBUG("[ finished linking %s ]", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001937
Elliott Hughese4d792a2013-10-28 14:19:05 -07001938#if !defined(__LP64__)
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001939 if (si->has_text_relocations) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07001940 // All relocations are done, we can protect our segments back to read-only.
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001941 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1942 DL_ERR("can't protect segments for \"%s\": %s",
1943 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001944 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001945 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001946 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07001947#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001948
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001949 /* We can also turn on GNU RELRO protection */
1950 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001951 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1952 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001953 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001954 }
1955
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001956 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001957 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001958}
1959
Nick Kralevich468319c2011-11-11 15:53:17 -08001960/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001961 * This function add vdso to internal dso list.
1962 * It helps to stack unwinding through signal handlers.
1963 * Also, it makes bionic more like glibc.
1964 */
1965static void add_vdso(KernelArgumentBlock& args UNUSED) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001966#if defined(AT_SYSINFO_EHDR)
Elliott Hughesc6200592013-09-30 18:43:46 -07001967 Elf_Ehdr* ehdr_vdso = reinterpret_cast<Elf_Ehdr*>(args.getauxval(AT_SYSINFO_EHDR));
Pavel Chupin5407eed2013-12-09 18:08:48 +04001968 if (ehdr_vdso == NULL) {
1969 return;
1970 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001971
1972 soinfo* si = soinfo_alloc("[vdso]");
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001973
Elliott Hughesc6200592013-09-30 18:43:46 -07001974 si->phdr = reinterpret_cast<Elf_Phdr*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001975 si->phnum = ehdr_vdso->e_phnum;
Sergey Melnikovebd506c2013-10-31 18:02:12 +04001976 si->base = reinterpret_cast<Elf_Addr>(ehdr_vdso);
1977 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1978 si->flags = 0;
1979 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
1980
1981 soinfo_link_image(si);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001982#endif
1983}
1984
1985/*
Nick Kralevich468319c2011-11-11 15:53:17 -08001986 * This code is called after the linker has linked itself and
1987 * fixed it's own GOT. It is safe to make references to externs
1988 * and other non-local data at this point.
1989 */
Elliott Hughesc6200592013-09-30 18:43:46 -07001990static Elf_Addr __linker_init_post_relocation(KernelArgumentBlock& args, Elf_Addr linker_base) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001991 /* NOTE: we store the args pointer on a special location
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001992 * of the temporary TLS area in order to pass it to
1993 * the C Library's runtime initializer.
1994 *
1995 * The initializer must clear the slot and reset the TLS
1996 * to point to a different location to ensure that no other
1997 * shared library constructor can access it.
1998 */
Elliott Hughesd3920b32013-02-07 18:39:34 -08001999 __libc_init_tls(args);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002000
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002001#if TIMING
2002 struct timeval t0, t1;
2003 gettimeofday(&t0, 0);
2004#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002005
Elliott Hughes18a206c2012-10-29 17:37:13 -07002006 // Initialize environment functions, and get to the ELF aux vectors table.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002007 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002008
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002009 // If this is a setuid/setgid program, close the security hole described in
2010 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2011 if (get_AT_SECURE()) {
2012 nullify_closed_stdio();
2013 }
2014
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002015 debuggerd_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002016
Elliott Hughes18a206c2012-10-29 17:37:13 -07002017 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07002018 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2019 if (LD_DEBUG != NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08002020 gLdDebugVerbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07002021 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002022
Elliott Hughes18a206c2012-10-29 17:37:13 -07002023 // Normally, these are cleaned by linker_env_init, but the test
2024 // doesn't cost us anything.
2025 const char* ldpath_env = NULL;
2026 const char* ldpreload_env = NULL;
2027 if (!get_AT_SECURE()) {
2028 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2029 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002030 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002031
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002032 INFO("[ android linker & debugger ]");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002033
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002034 soinfo* si = soinfo_alloc(args.argv[0]);
Elliott Hughes18a206c2012-10-29 17:37:13 -07002035 if (si == NULL) {
2036 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002037 }
2038
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07002039 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002040 si->flags |= FLAG_EXE;
Elliott Hughes3a9c5d62014-02-10 13:31:13 -08002041 link_map* map = &(si->link_map_head);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002042
2043 map->l_addr = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002044 map->l_name = args.argv[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002045 map->l_prev = NULL;
2046 map->l_next = NULL;
2047
2048 _r_debug.r_map = map;
2049 r_debug_tail = map;
2050
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002051 /* gdb expects the linker to be in the debug shared object list.
2052 * Without this, gdb has trouble locating the linker's ".text"
2053 * and ".plt" sections. Gdb could also potentially use this to
2054 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2055 * Don't use soinfo_alloc(), because the linker shouldn't
2056 * be on the soinfo list.
Ben Cheng06f0e742012-08-10 16:07:02 -07002057 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002058 {
2059 static soinfo linker_soinfo;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002060#if defined(__LP64__)
Pavel Chupin1a57f9f2013-02-06 19:21:46 +04002061 strlcpy(linker_soinfo.name, "/system/bin/linker64", sizeof(linker_soinfo.name));
2062#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002063 strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
Pavel Chupin1a57f9f2013-02-06 19:21:46 +04002064#endif
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002065 linker_soinfo.flags = 0;
2066 linker_soinfo.base = linker_base;
2067
2068 /*
2069 * Set the dynamic field in the link map otherwise gdb will complain with
2070 * the following:
2071 * warning: .dynamic section for "/system/bin/linker" is not at the
2072 * expected address (wrong library or version mismatch?)
2073 */
Elliott Hughesc6200592013-09-30 18:43:46 -07002074 Elf_Ehdr *elf_hdr = (Elf_Ehdr *) linker_base;
2075 Elf_Phdr *phdr = (Elf_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002076 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
2077 &linker_soinfo.dynamic, NULL, NULL);
2078 insert_soinfo_into_debug_map(&linker_soinfo);
2079 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002080
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002081 // Extract information passed from the kernel.
Elliott Hughesc6200592013-09-30 18:43:46 -07002082 si->phdr = reinterpret_cast<Elf_Phdr*>(args.getauxval(AT_PHDR));
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002083 si->phnum = args.getauxval(AT_PHNUM);
2084 si->entry = args.getauxval(AT_ENTRY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002085
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002086 /* Compute the value of si->base. We can't rely on the fact that
2087 * the first entry is the PHDR because this will not be true
2088 * for certain executables (e.g. some in the NDK unit test suite)
2089 */
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002090 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002091 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002092 si->load_bias = 0;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002093 for (size_t i = 0; i < si->phnum; ++i) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002094 if (si->phdr[i].p_type == PT_PHDR) {
Elliott Hughesc6200592013-09-30 18:43:46 -07002095 si->load_bias = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_vaddr;
2096 si->base = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_offset;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002097 break;
2098 }
David 'Digit' Turner8180b082011-11-15 17:17:28 +01002099 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002100 si->dynamic = NULL;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002101 si->ref_count = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002102
Elliott Hughes46882792012-08-03 16:49:39 -07002103 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2104 parse_LD_LIBRARY_PATH(ldpath_env);
2105 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06002106
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02002107 somain = si;
2108
Elliott Hughes124fae92012-10-31 14:20:03 -07002109 if (!soinfo_link_image(si)) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08002110 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
Elliott Hughes18a206c2012-10-29 17:37:13 -07002111 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002112 }
2113
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002114 add_vdso(args);
2115
Elliott Hughesd23736e2012-11-01 15:16:56 -07002116 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04002117
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002118 for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
2119 gLdPreloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08002120 }
2121
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002122 /* After the link_image, the si->load_bias is initialized.
2123 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2124 * We need to update this value for so exe here. So Unwind_Backtrace
2125 * for some arch like x86 could work correctly within so exe.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08002126 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08002127 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002128 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002129
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002130#if TIMING
2131 gettimeofday(&t1,NULL);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002132 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002133 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2134 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
2135 ));
2136#endif
2137#if STATS
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002138 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07002139 linker_stats.count[kRelocAbsolute],
2140 linker_stats.count[kRelocRelative],
2141 linker_stats.count[kRelocCopy],
2142 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002143#endif
2144#if COUNT_PAGES
2145 {
2146 unsigned n;
2147 unsigned i;
2148 unsigned count = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002149 for (n = 0; n < 4096; n++) {
2150 if (bitmask[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002151 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002152#if defined(__LP64__)
2153 for (i = 0; i < 32; i++) {
2154#else
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002155 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002156#endif
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002157 if (x & 1) {
2158 count++;
2159 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002160 x >>= 1;
2161 }
2162 }
2163 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002164 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002165 }
2166#endif
2167
2168#if TIMING || STATS || COUNT_PAGES
2169 fflush(stdout);
2170#endif
2171
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002172 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002173 return si->entry;
2174}
Nick Kralevich468319c2011-11-11 15:53:17 -08002175
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002176/* Compute the load-bias of an existing executable. This shall only
2177 * be used to compute the load bias of an executable or shared library
2178 * that was loaded by the kernel itself.
2179 *
2180 * Input:
2181 * elf -> address of ELF header, assumed to be at the start of the file.
2182 * Return:
2183 * load bias, i.e. add the value of any p_vaddr in the file to get
2184 * the corresponding address in memory.
2185 */
Elliott Hughesc6200592013-09-30 18:43:46 -07002186static Elf_Addr get_elf_exec_load_bias(const Elf_Ehdr* elf) {
2187 Elf_Addr offset = elf->e_phoff;
2188 const Elf_Phdr* phdr_table = (const Elf_Phdr*)((char*)elf + offset);
2189 const Elf_Phdr* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002190
Elliott Hughesc6200592013-09-30 18:43:46 -07002191 for (const Elf_Phdr* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002192 if (phdr->p_type == PT_LOAD) {
Elliott Hughesc6200592013-09-30 18:43:46 -07002193 return reinterpret_cast<Elf_Addr>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002194 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002195 }
2196 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002197}
2198
Nick Kralevich468319c2011-11-11 15:53:17 -08002199/*
2200 * This is the entry point for the linker, called from begin.S. This
2201 * method is responsible for fixing the linker's own relocations, and
2202 * then calling __linker_init_post_relocation().
2203 *
2204 * Because this method is called before the linker has fixed it's own
2205 * relocations, any attempt to reference an extern variable, extern
2206 * function, or other GOT reference will generate a segfault.
2207 */
Elliott Hughesc6200592013-09-30 18:43:46 -07002208extern "C" Elf_Addr __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002209 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002210
Elliott Hughesc6200592013-09-30 18:43:46 -07002211 Elf_Addr linker_addr = args.getauxval(AT_BASE);
Elliott Hughesc6200592013-09-30 18:43:46 -07002212 Elf_Ehdr* elf_hdr = reinterpret_cast<Elf_Ehdr*>(linker_addr);
2213 Elf_Phdr* phdr = (Elf_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002214
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002215 soinfo linker_so;
2216 memset(&linker_so, 0, sizeof(soinfo));
Nick Kralevich468319c2011-11-11 15:53:17 -08002217
Elliott Hughesb93702a2013-12-21 16:07:45 -08002218 strcpy(linker_so.name, "[dynamic linker]");
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002219 linker_so.base = linker_addr;
2220 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2221 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002222 linker_so.dynamic = NULL;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002223 linker_so.phdr = phdr;
2224 linker_so.phnum = elf_hdr->e_phnum;
2225 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07002226
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002227 if (!soinfo_link_image(&linker_so)) {
2228 // It would be nice to print an error message, but if the linker
2229 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002230 // call write() (because it involves a GOT reference). We may as
2231 // well try though...
2232 const char* msg = "CANNOT LINK EXECUTABLE: ";
2233 write(2, msg, strlen(msg));
2234 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2235 write(2, "\n", 1);
2236 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002237 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002238
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002239 // We have successfully fixed our own relocations. It's safe to run
2240 // the main part of the linker now.
Elliott Hughes0d787c12013-04-04 13:46:46 -07002241 args.abort_message_ptr = &gAbortMessage;
Elliott Hughesc6200592013-09-30 18:43:46 -07002242 Elf_Addr start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002243
2244 set_soinfo_pool_protection(PROT_READ);
2245
2246 // Return the address that the calling assembly stub should jump to.
2247 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002248}