blob: 3afd314058b5087d637e33f040ee49b42ee2e1e9 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Doug Kwan94304352009-10-23 18:11:40 -07002 * Copyright (C) 2008, 2009 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughes46882792012-08-03 16:49:39 -070029#include <dlfcn.h>
30#include <errno.h>
31#include <fcntl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <linux/auxvec.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037#include <sys/atomics.h>
Elliott Hughes46882792012-08-03 16:49:39 -070038#include <sys/mman.h>
39#include <sys/stat.h>
40#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Elliott Hughes46882792012-08-03 16:49:39 -070042// Private C library headers.
43#include <private/bionic_tls.h>
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080044#include <private/KernelArgumentBlock.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070045#include <private/ScopedPthreadMutexLocker.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046
47#include "linker.h"
48#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010049#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020050#include "linker_phdr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051
David Bartleybc3a5c22009-06-02 18:27:28 -070052/* Assume average path length of 64 and max 8 paths */
53#define LDPATH_BUFSIZE 512
54#define LDPATH_MAX 8
55
Matt Fischer4fd42c12009-12-31 12:09:10 -060056#define LDPRELOAD_BUFSIZE 512
57#define LDPRELOAD_MAX 8
58
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
60 *
61 * Do NOT use malloc() and friends or pthread_*() code here.
62 * Don't use printf() either; it's caused mysterious memory
63 * corruption in the past.
64 * The linker runs before we bring up libc and it's easiest
65 * to make sure it does not depend on any complex libc features
66 *
67 * open issues / todo:
68 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069 * - are we doing everything we should for ARM_COPY relocations?
70 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071 * - after linking, set as much stuff as possible to READONLY
72 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070073 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074
Elliott Hughes124fae92012-10-31 14:20:03 -070075static bool soinfo_link_image(soinfo* si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076
Magnus Malmbornba98d922012-09-12 13:00:55 +020077// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
78// maps, each a single page in size. The pages are broken up into as many struct soinfo
79// objects as will fit, and they're all threaded together on a free list.
80#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
81struct soinfo_pool_t {
82 soinfo_pool_t* next;
83 soinfo info[SOINFO_PER_POOL];
84};
85static struct soinfo_pool_t* gSoInfoPools = NULL;
86static soinfo* gSoInfoFreeList = NULL;
87
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080088static soinfo* solist = &libdl_info;
89static soinfo* sonext = &libdl_info;
90static soinfo* somain; /* main process, always the one after libdl_info */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091
Elliott Hughes124fae92012-10-31 14:20:03 -070092static const char* const gSoPaths[] = {
93 "/vendor/lib",
94 "/system/lib",
95 NULL
96};
David Bartleybc3a5c22009-06-02 18:27:28 -070097
Elliott Hughes124fae92012-10-31 14:20:03 -070098static char gLdPathsBuffer[LDPATH_BUFSIZE];
99static const char* gLdPaths[LDPATH_MAX + 1];
100
101static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
102static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600103
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800104static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600105
Elliott Hughes650be4e2013-03-05 18:47:58 -0800106__LIBC_HIDDEN__ int gLdDebugVerbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107
Elliott Hughesbedfe382012-08-14 14:07:59 -0700108enum RelocationKind {
109 kRelocAbsolute = 0,
110 kRelocRelative,
111 kRelocCopy,
112 kRelocSymbol,
113 kRelocMax
114};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100115
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700117struct linker_stats_t {
118 int count[kRelocMax];
119};
120
121static linker_stats_t linker_stats;
122
123static void count_relocation(RelocationKind kind) {
124 ++linker_stats.count[kind];
125}
126#else
127static void count_relocation(RelocationKind) {
128}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129#endif
130
131#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700132static unsigned bitmask[4096];
133#define MARK(offset) \
134 do { \
135 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
136 } while(0)
137#else
138#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139#endif
140
Elliott Hughes46882792012-08-03 16:49:39 -0700141// You shouldn't try to call memory-allocating functions in the dynamic linker.
142// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700143#define DISALLOW_ALLOCATION(return_type, name, ...) \
144 return_type name __VA_ARGS__ \
145 { \
Elliott Hughes46882792012-08-03 16:49:39 -0700146 const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700147 __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
148 write(2, msg, strlen(msg)); \
149 abort(); \
Dima Zavin2e855792009-05-20 18:28:09 -0700150 }
Elliott Hughes46882792012-08-03 16:49:39 -0700151#define UNUSED __attribute__((unused))
152DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
153DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
154DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
155DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
Dima Zavin2e855792009-05-20 18:28:09 -0700156
Dima Zavin03531952009-05-29 17:30:25 -0700157static char tmp_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700158static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700159
Elliott Hughes650be4e2013-03-05 18:47:58 -0800160char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700161 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700162}
163
Elliott Hughes650be4e2013-03-05 18:47:58 -0800164size_t linker_get_error_buffer_size() {
165 return sizeof(__linker_dl_err_buf);
166}
167
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168/*
169 * This function is an empty stub where GDB locates a breakpoint to get notified
170 * about linker activity.
171 */
Elliott Hughes5419b942012-10-16 15:54:46 -0700172extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800173
Elliott Hughesbedfe382012-08-14 14:07:59 -0700174static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175 RT_CONSISTENT, 0};
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700176static link_map_t* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800177
Elliott Hughes3b297c42012-10-11 16:08:51 -0700178static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179
Elliott Hughesbedfe382012-08-14 14:07:59 -0700180static void insert_soinfo_into_debug_map(soinfo * info) {
181 // Copy the necessary fields into the debug structure.
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700182 link_map_t* map = &(info->link_map);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183 map->l_addr = info->base;
184 map->l_name = (char*) info->name;
Thinker K.F Li5cf640c2009-07-03 19:40:32 +0800185 map->l_ld = (uintptr_t)info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186
187 /* Stick the new library at the end of the list.
188 * gdb tends to care more about libc than it does
189 * about leaf libraries, and ordering it this way
190 * reduces the back-and-forth over the wire.
191 */
192 if (r_debug_tail) {
193 r_debug_tail->l_next = map;
194 map->l_prev = r_debug_tail;
195 map->l_next = 0;
196 } else {
197 _r_debug.r_map = map;
198 map->l_prev = 0;
199 map->l_next = 0;
200 }
201 r_debug_tail = map;
202}
203
Elliott Hughesbedfe382012-08-14 14:07:59 -0700204static void remove_soinfo_from_debug_map(soinfo* info) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700205 link_map_t* map = &(info->link_map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700206
Elliott Hughesbedfe382012-08-14 14:07:59 -0700207 if (r_debug_tail == map) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700208 r_debug_tail = map->l_prev;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700209 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700210
Elliott Hughesbedfe382012-08-14 14:07:59 -0700211 if (map->l_prev) {
212 map->l_prev->l_next = map->l_next;
213 }
214 if (map->l_next) {
215 map->l_next->l_prev = map->l_prev;
216 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700217}
218
Elliott Hughesbedfe382012-08-14 14:07:59 -0700219static void notify_gdb_of_load(soinfo* info) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220 if (info->flags & FLAG_EXE) {
221 // GDB already knows about the main executable
222 return;
223 }
224
Elliott Hughes3b297c42012-10-11 16:08:51 -0700225 ScopedPthreadMutexLocker locker(&gDebugMutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226
227 _r_debug.r_state = RT_ADD;
228 rtld_db_dlactivity();
229
230 insert_soinfo_into_debug_map(info);
231
232 _r_debug.r_state = RT_CONSISTENT;
233 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700234}
235
Elliott Hughesbedfe382012-08-14 14:07:59 -0700236static void notify_gdb_of_unload(soinfo* info) {
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700237 if (info->flags & FLAG_EXE) {
238 // GDB already knows about the main executable
239 return;
240 }
241
Elliott Hughes3b297c42012-10-11 16:08:51 -0700242 ScopedPthreadMutexLocker locker(&gDebugMutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700243
244 _r_debug.r_state = RT_DELETE;
245 rtld_db_dlactivity();
246
247 remove_soinfo_from_debug_map(info);
248
249 _r_debug.r_state = RT_CONSISTENT;
250 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800251}
252
Elliott Hughes18a206c2012-10-29 17:37:13 -0700253void notify_gdb_of_libraries() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800254 _r_debug.r_state = RT_ADD;
255 rtld_db_dlactivity();
256 _r_debug.r_state = RT_CONSISTENT;
257 rtld_db_dlactivity();
258}
259
Magnus Malmbornba98d922012-09-12 13:00:55 +0200260static bool ensure_free_list_non_empty() {
261 if (gSoInfoFreeList != NULL) {
262 return true;
263 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264
Magnus Malmbornba98d922012-09-12 13:00:55 +0200265 // Allocate a new pool.
266 soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
267 PROT_READ|PROT_WRITE,
268 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
269 if (pool == MAP_FAILED) {
270 return false;
271 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800272
Magnus Malmbornba98d922012-09-12 13:00:55 +0200273 // Add the pool to our list of pools.
274 pool->next = gSoInfoPools;
275 gSoInfoPools = pool;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800276
Magnus Malmbornba98d922012-09-12 13:00:55 +0200277 // Chain the entries in the new pool onto the free list.
278 gSoInfoFreeList = &pool->info[0];
279 soinfo* next = NULL;
280 for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
281 pool->info[i].next = next;
282 next = &pool->info[i];
283 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800284
Magnus Malmbornba98d922012-09-12 13:00:55 +0200285 return true;
286}
287
Elliott Hughesd23736e2012-11-01 15:16:56 -0700288static void set_soinfo_pool_protection(int protection) {
289 for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
290 if (mprotect(p, sizeof(*p), protection) == -1) {
291 abort(); // Can't happen.
292 }
293 }
294}
295
Magnus Malmbornba98d922012-09-12 13:00:55 +0200296static soinfo* soinfo_alloc(const char* name) {
297 if (strlen(name) >= SOINFO_NAME_LEN) {
298 DL_ERR("library name \"%s\" too long", name);
299 return NULL;
300 }
301
302 if (!ensure_free_list_non_empty()) {
303 DL_ERR("out of memory when loading \"%s\"", name);
304 return NULL;
305 }
306
307 // Take the head element off the free list.
308 soinfo* si = gSoInfoFreeList;
309 gSoInfoFreeList = gSoInfoFreeList->next;
310
311 // Initialize the new element.
312 memset(si, 0, sizeof(soinfo));
313 strlcpy(si->name, name, sizeof(si->name));
314 sonext->next = si;
315 sonext = si;
316
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700317 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200318 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800319}
320
Elliott Hughes46882792012-08-03 16:49:39 -0700321static void soinfo_free(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800322{
Elliott Hughes46882792012-08-03 16:49:39 -0700323 if (si == NULL) {
324 return;
325 }
326
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800327 soinfo *prev = NULL, *trav;
328
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700329 TRACE("name %s: freeing soinfo @ %p", si->name, si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800330
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800331 for (trav = solist; trav != NULL; trav = trav->next) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800332 if (trav == si)
333 break;
334 prev = trav;
335 }
336 if (trav == NULL) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800337 /* si was not in solist */
Elliott Hughes46882792012-08-03 16:49:39 -0700338 DL_ERR("name \"%s\" is not in solist!", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339 return;
340 }
341
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100342 /* prev will never be NULL, because the first entry in solist is
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800343 always the static libdl_info.
344 */
345 prev->next = si->next;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800346 if (si == sonext) {
347 sonext = prev;
348 }
Magnus Malmbornba98d922012-09-12 13:00:55 +0200349 si->next = gSoInfoFreeList;
350 gSoInfoFreeList = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800351}
352
Elliott Hughescade4c32012-12-20 14:42:14 -0800353
354static void parse_path(const char* path, const char* delimiters,
355 const char** array, char* buf, size_t buf_size, size_t max_count) {
356 if (path == NULL) {
357 return;
358 }
359
360 size_t len = strlcpy(buf, path, buf_size);
361
362 size_t i = 0;
363 char* buf_p = buf;
364 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
365 if (*array[i] != '\0') {
366 ++i;
367 }
368 }
369
370 // Forget the last path if we had to truncate; this occurs if the 2nd to
371 // last char isn't '\0' (i.e. wasn't originally a delimiter).
372 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
373 array[i - 1] = NULL;
374 } else {
375 array[i] = NULL;
376 }
377}
378
379static void parse_LD_LIBRARY_PATH(const char* path) {
380 parse_path(path, ":", gLdPaths,
381 gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
382}
383
384static void parse_LD_PRELOAD(const char* path) {
385 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
386 parse_path(path, " :", gLdPreloadNames,
387 gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
388}
389
Elliott Hughes46882792012-08-03 16:49:39 -0700390#ifdef ANDROID_ARM_LINKER
391
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800392/* For a given PC, find the .so that it belongs to.
393 * Returns the base address of the .ARM.exidx section
394 * for that .so, and the number of 8-byte entries
395 * in that section (via *pcount).
396 *
397 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
398 *
Elliott Hughes3b297c42012-10-11 16:08:51 -0700399 * This function is exposed via dlfcn.cpp and libdl.so.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800400 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800401_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
402{
403 soinfo *si;
404 unsigned addr = (unsigned)pc;
405
Nick Kralevich468319c2011-11-11 15:53:17 -0800406 for (si = solist; si != 0; si = si->next){
407 if ((addr >= si->base) && (addr < (si->base + si->size))) {
408 *pcount = si->ARM_exidx_count;
Ji-Hwan Leef186a182012-05-31 20:20:36 +0900409 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800410 }
411 }
412 *pcount = 0;
413 return NULL;
414}
Elliott Hughes46882792012-08-03 16:49:39 -0700415
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700416#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
Elliott Hughes46882792012-08-03 16:49:39 -0700417
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800418/* Here, we only have to provide a callback to iterate across all the
419 * loaded libraries. gcc_eh does the rest. */
420int
Elliott Hughesbedfe382012-08-14 14:07:59 -0700421dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800422 void *data)
423{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800424 int rv = 0;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700425 for (soinfo* si = solist; si != NULL; si = si->next) {
426 dl_phdr_info dl_info;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700427 dl_info.dlpi_addr = si->link_map.l_addr;
428 dl_info.dlpi_name = si->link_map.l_name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800429 dl_info.dlpi_phdr = si->phdr;
430 dl_info.dlpi_phnum = si->phnum;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700431 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
432 if (rv != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433 break;
Elliott Hughesbedfe382012-08-14 14:07:59 -0700434 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800435 }
436 return rv;
437}
Elliott Hughes46882792012-08-03 16:49:39 -0700438
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800439#endif
440
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800441static Elf32_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
442 Elf32_Sym* s;
443 Elf32_Sym* symtab = si->symtab;
444 const char* strtab = si->strtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800445 unsigned n;
446
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700447 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800448 name, si->name, si->base, hash, hash % si->nbucket);
449 n = hash % si->nbucket;
450
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800451 for (n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800452 s = symtab + n;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800453 if (strcmp(strtab + s->st_name, name)) continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800454
Doug Kwane8238072009-10-26 12:05:23 -0700455 /* only concern ourselves with global and weak symbol definitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800456 switch(ELF32_ST_BIND(s->st_info)){
457 case STB_GLOBAL:
Doug Kwane8238072009-10-26 12:05:23 -0700458 case STB_WEAK:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800459 if (s->st_shndx == SHN_UNDEF) {
Robin Burchell439fa8e2012-07-05 09:21:07 +0200460 continue;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800461 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800462
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700463 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800464 name, si->name, s->st_value, s->st_size);
465 return s;
466 }
467 }
468
Doug Kwan94304352009-10-23 18:11:40 -0700469 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800470}
471
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800472static unsigned elfhash(const char* _name) {
473 const unsigned char* name = (const unsigned char*) _name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800474 unsigned h = 0, g;
475
476 while(*name) {
477 h = (h << 4) + *name++;
478 g = h & 0xf0000000;
479 h ^= g;
480 h ^= g >> 24;
481 }
482 return h;
483}
484
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800485static Elf32_Sym* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
Doug Kwan94304352009-10-23 18:11:40 -0700486 unsigned elf_hash = elfhash(name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800487 Elf32_Sym* s = NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700488
Pavel Chupinc77c4342012-10-31 13:55:51 +0400489 if (si != NULL && somain != NULL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200490
491 /*
Pavel Chupinc77c4342012-10-31 13:55:51 +0400492 * Local scope is executable scope. Just start looking into it right away
493 * for the shortcut.
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200494 */
495
Pavel Chupinc77c4342012-10-31 13:55:51 +0400496 if (si == somain) {
497 s = soinfo_elf_lookup(si, elf_hash, name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200498 if (s != NULL) {
Pavel Chupinc77c4342012-10-31 13:55:51 +0400499 *lsi = si;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200500 goto done;
501 }
Pavel Chupinc77c4342012-10-31 13:55:51 +0400502 } else {
503 /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200504
Pavel Chupinc77c4342012-10-31 13:55:51 +0400505 /*
506 * If this object was built with symbolic relocations disabled, the
507 * first place to look to resolve external references is the main
508 * executable.
509 */
Nick Kralevich468319c2011-11-11 15:53:17 -0800510
Pavel Chupinc77c4342012-10-31 13:55:51 +0400511 if (!si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700512 DEBUG("%s: looking up %s in executable %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700513 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400514 s = soinfo_elf_lookup(somain, elf_hash, name);
515 if (s != NULL) {
516 *lsi = somain;
517 goto done;
518 }
519 }
520
521 /* Look for symbols in the local scope (the object who is
522 * searching). This happens with C++ templates on i386 for some
523 * reason.
524 *
525 * Notes on weak symbols:
526 * The ELF specs are ambiguous about treatment of weak definitions in
527 * dynamic linking. Some systems return the first definition found
528 * and some the first non-weak definition. This is system dependent.
529 * Here we return the first definition found for simplicity. */
530
531 s = soinfo_elf_lookup(si, elf_hash, name);
532 if (s != NULL) {
533 *lsi = si;
534 goto done;
535 }
536
537 /*
538 * If this object was built with -Bsymbolic and symbol is not found
539 * in the local scope, try to find the symbol in the main executable.
540 */
541
542 if (si->has_DT_SYMBOLIC) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700543 DEBUG("%s: looking up %s in executable %s after local scope",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700544 si->name, name, somain->name);
Pavel Chupinc77c4342012-10-31 13:55:51 +0400545 s = soinfo_elf_lookup(somain, elf_hash, name);
546 if (s != NULL) {
547 *lsi = somain;
548 goto done;
549 }
550 }
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200551 }
Nick Kralevichd39c3ab2012-08-24 13:25:51 -0700552 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700553
Matt Fischer4fd42c12009-12-31 12:09:10 -0600554 /* Next, look for it in the preloads list */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800555 for (int i = 0; gLdPreloads[i] != NULL; i++) {
556 s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
557 if (s != NULL) {
558 *lsi = gLdPreloads[i];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600559 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200560 }
Matt Fischer4fd42c12009-12-31 12:09:10 -0600561 }
562
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800563 for (int i = 0; needed[i] != NULL; i++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700564 DEBUG("%s: looking up %s in %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700565 si->name, name, needed[i]->name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200566 s = soinfo_elf_lookup(needed[i], elf_hash, name);
567 if (s != NULL) {
568 *lsi = needed[i];
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200569 goto done;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200570 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700571 }
572
573done:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800574 if (s != NULL) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700575 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = 0x%08x, "
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700576 "found in %s, base = 0x%08x, load bias = 0x%08x",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -0700577 si->name, name, s->st_value,
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200578 (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700579 return s;
580 }
581
Doug Kwan94304352009-10-23 18:11:40 -0700582 return NULL;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700583}
584
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800585/* This is used by dlsym(3). It performs symbol lookup only within the
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700586 specified soinfo object and not in any of its dependencies.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800587
588 TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
589 that it should do a breadth first search through the dependency
590 tree. This agrees with the ELF spec (aka System V Application
591 Binary Interface) where in Chapter 5 it discuss resolving "Shared
592 Object Dependencies" in breadth first search order.
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700593 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800594Elf32_Sym* dlsym_handle_lookup(soinfo* si, const char* name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800595{
David 'Digit' Turner16084162012-06-12 16:25:37 +0200596 return soinfo_elf_lookup(si, elfhash(name), name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800597}
598
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800599/* This is used by dlsym(3) to performs a global symbol lookup. If the
600 start value is null (for RTLD_DEFAULT), the search starts at the
601 beginning of the global solist. Otherwise the search starts at the
602 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700603 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800604Elf32_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800605 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800606
Elliott Hughescade4c32012-12-20 14:42:14 -0800607 if (start == NULL) {
608 start = solist;
609 }
610
611 Elf32_Sym* s = NULL;
612 for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
613 s = soinfo_elf_lookup(si, elf_hash, name);
614 if (s != NULL) {
615 *found = si;
616 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600617 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800618 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600619
Elliott Hughescade4c32012-12-20 14:42:14 -0800620 if (s != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700621 TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, found->base = 0x%08x",
Elliott Hughescade4c32012-12-20 14:42:14 -0800622 name, s->st_value, (*found)->base);
623 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800624
Elliott Hughescade4c32012-12-20 14:42:14 -0800625 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800626}
627
Kito Chengfa8c05d2013-03-12 14:58:06 +0800628soinfo* find_containing_library(const void* p) {
629 Elf32_Addr address = reinterpret_cast<Elf32_Addr>(p);
630 for (soinfo* si = solist; si != NULL; si = si->next) {
631 if (address >= si->base && address - si->base < si->size) {
632 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600633 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800634 }
635 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600636}
637
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800638Elf32_Sym* dladdr_find_symbol(soinfo* si, const void* addr) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800639 Elf32_Addr soaddr = reinterpret_cast<Elf32_Addr>(addr) - si->base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600640
Kito Chengfa8c05d2013-03-12 14:58:06 +0800641 // Search the library's symbol table for any defined symbol which
642 // contains this address.
643 for (size_t i = 0; i < si->nchain; ++i) {
644 Elf32_Sym* sym = &si->symtab[i];
645 if (sym->st_shndx != SHN_UNDEF &&
646 soaddr >= sym->st_value &&
647 soaddr < sym->st_value + sym->st_size) {
648 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600649 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800650 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600651
Kito Chengfa8c05d2013-03-12 14:58:06 +0800652 return NULL;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600653}
654
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800655#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800656static void dump(soinfo* si)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657{
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800658 Elf32_Sym* s = si->symtab;
659 for (unsigned n = 0; n < si->nchain; n++) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700660 TRACE("%04d> %08x: %02x %04x %08x %08x %s", n, s,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800661 s->st_info, s->st_shndx, s->st_value, s->st_size,
662 si->strtab + s->st_name);
663 s++;
664 }
665}
666#endif
667
Elliott Hughes124fae92012-10-31 14:20:03 -0700668static int open_library_on_path(const char* name, const char* const paths[]) {
669 char buf[512];
670 for (size_t i = 0; paths[i] != NULL; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800671 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700672 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700673 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700674 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800675 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700676 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
677 if (fd != -1) {
678 return fd;
679 }
680 }
681 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800682}
683
Elliott Hughes124fae92012-10-31 14:20:03 -0700684static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700685 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800686
Elliott Hughes124fae92012-10-31 14:20:03 -0700687 // If the name contains a slash, we should attempt to open it directly and not search the paths.
688 if (strchr(name, '/') != NULL) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700689 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
690 if (fd != -1) {
691 return fd;
692 }
693 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Elliott Hughes124fae92012-10-31 14:20:03 -0700694 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800695
Elliott Hughes124fae92012-10-31 14:20:03 -0700696 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
697 int fd = open_library_on_path(name, gLdPaths);
698 if (fd == -1) {
699 fd = open_library_on_path(name, gSoPaths);
700 }
701 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800702}
703
Elliott Hughes124fae92012-10-31 14:20:03 -0700704static soinfo* load_library(const char* name) {
Elliott Hughes46882792012-08-03 16:49:39 -0700705 // Open the file.
Elliott Hughes650be4e2013-03-05 18:47:58 -0800706 int fd = open_library(name);
707 if (fd == -1) {
Elliott Hughes46882792012-08-03 16:49:39 -0700708 DL_ERR("library \"%s\" not found", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800709 return NULL;
Dima Zavin2e855792009-05-20 18:28:09 -0700710 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800711
Elliott Hughes650be4e2013-03-05 18:47:58 -0800712 // Read the ELF header and load the segments.
713 ElfReader elf_reader(name, fd);
714 if (!elf_reader.Load()) {
Elliott Hughes46882792012-08-03 16:49:39 -0700715 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800716 }
717
Elliott Hughes650be4e2013-03-05 18:47:58 -0800718 const char* bname = strrchr(name, '/');
719 soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
720 if (si == NULL) {
Elliott Hughes46882792012-08-03 16:49:39 -0700721 return NULL;
David 'Digit' Turner23363ed2012-06-18 18:13:49 +0200722 }
Elliott Hughes650be4e2013-03-05 18:47:58 -0800723 si->base = elf_reader.load_start();
724 si->size = elf_reader.load_size();
725 si->load_bias = elf_reader.load_bias();
726 si->flags = 0;
727 si->entry = 0;
728 si->dynamic = NULL;
729 si->phnum = elf_reader.phdr_count();
730 si->phdr = elf_reader.loaded_phdr();
731 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800732}
733
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200734static soinfo *find_loaded_library(const char *name)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800735{
736 soinfo *si;
David 'Digit' Turner67748092010-07-21 16:18:21 -0700737 const char *bname;
738
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200739 // TODO: don't use basename only for determining libraries
740 // http://code.google.com/p/android/issues/detail?id=6670
741
742 bname = strrchr(name, '/');
743 bname = bname ? bname + 1 : name;
744
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800745 for (si = solist; si != NULL; si = si->next) {
746 if (!strcmp(bname, si->name)) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200747 return si;
748 }
749 }
750 return NULL;
751}
752
Elliott Hughesd23736e2012-11-01 15:16:56 -0700753static soinfo* find_library_internal(const char* name) {
754 if (name == NULL) {
755 return somain;
756 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200757
Elliott Hughesd23736e2012-11-01 15:16:56 -0700758 soinfo* si = find_loaded_library(name);
759 if (si != NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -0700760 if (si->flags & FLAG_LINKED) {
761 return si;
762 }
763 DL_ERR("OOPS: recursive link to \"%s\"", si->name);
764 return NULL;
765 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800766
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700767 TRACE("[ '%s' has not been loaded yet. Locating...]", name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700768 si = load_library(name);
Elliott Hughescade4c32012-12-20 14:42:14 -0800769 if (si == NULL) {
770 return NULL;
771 }
772
773 // At this point we know that whatever is loaded @ base is a valid ELF
774 // shared library whose segments are properly mapped in.
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700775 TRACE("[ init_library base=0x%08x sz=0x%08x name='%s' ]",
Elliott Hughescade4c32012-12-20 14:42:14 -0800776 si->base, si->size, si->name);
777
778 if (!soinfo_link_image(si)) {
779 munmap(reinterpret_cast<void*>(si->base), si->size);
780 soinfo_free(si);
781 return NULL;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700782 }
783
784 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800785}
786
Elliott Hughesd23736e2012-11-01 15:16:56 -0700787static soinfo* find_library(const char* name) {
788 soinfo* si = find_library_internal(name);
789 if (si != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700790 si->ref_count++;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700791 }
792 return si;
793}
Elliott Hughesbedfe382012-08-14 14:07:59 -0700794
Elliott Hughesd23736e2012-11-01 15:16:56 -0700795static int soinfo_unload(soinfo* si) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700796 if (si->ref_count == 1) {
797 TRACE("unloading '%s'", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700798 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800799
Brian Carlstrom2d4b9b72013-03-06 15:32:16 -0800800 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800801 if (d->d_tag == DT_NEEDED) {
802 const char* library_name = si->strtab + d->d_un.d_val;
803 soinfo* lsi = find_loaded_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700804 if (lsi != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700805 TRACE("%s needs to unload %s", si->name, lsi->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700806 soinfo_unload(lsi);
807 } else {
808 // TODO: should we return -1 in this case?
809 DL_ERR("\"%s\": could not unload dependent library", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800810 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700811 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800812 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700813
814 munmap(reinterpret_cast<void*>(si->base), si->size);
815 notify_gdb_of_unload(si);
816 soinfo_free(si);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700817 si->ref_count = 0;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700818 } else {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700819 si->ref_count--;
820 TRACE("not unloading '%s', decrementing ref_count to %d", si->name, si->ref_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700821 }
822 return 0;
823}
824
Elliott Hughescade4c32012-12-20 14:42:14 -0800825void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
826 if (!get_AT_SECURE()) {
827 parse_LD_LIBRARY_PATH(ld_library_path);
828 }
829}
830
Elliott Hughese66190d2012-12-18 15:57:55 -0800831soinfo* do_dlopen(const char* name, int flags) {
832 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
833 DL_ERR("invalid flags to dlopen: %x", flags);
834 return NULL;
835 }
Elliott Hughesd23736e2012-11-01 15:16:56 -0700836 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
837 soinfo* si = find_library(name);
838 if (si != NULL) {
839 si->CallConstructors();
840 }
841 set_soinfo_pool_protection(PROT_READ);
842 return si;
843}
844
845int do_dlclose(soinfo* si) {
846 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
847 int result = soinfo_unload(si);
848 set_soinfo_pool_protection(PROT_READ);
849 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800850}
851
852/* TODO: don't use unsigned for addrs below. It works, but is not
853 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
854 * long.
855 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800856static int soinfo_relocate(soinfo* si, Elf32_Rel* rel, unsigned count,
857 soinfo* needed[])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800858{
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800859 Elf32_Sym* symtab = si->symtab;
860 const char* strtab = si->strtab;
861 Elf32_Sym* s;
862 Elf32_Rel* start = rel;
863 soinfo* lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800864
Elliott Hughes46882792012-08-03 16:49:39 -0700865 for (size_t idx = 0; idx < count; ++idx, ++rel) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800866 unsigned type = ELF32_R_TYPE(rel->r_info);
867 unsigned sym = ELF32_R_SYM(rel->r_info);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800868 Elf32_Addr reloc = static_cast<Elf32_Addr>(rel->r_offset + si->load_bias);
869 Elf32_Addr sym_addr = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800870 char* sym_name = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800871
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700872 DEBUG("Processing '%s' relocation at index %d", si->name, idx);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700873 if (type == 0) { // R_*_NONE
874 continue;
875 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800876 if (sym != 0) {
Dima Zavind1b40d82009-05-12 10:59:09 -0700877 sym_name = (char *)(strtab + symtab[sym].st_name);
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200878 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800879 if (s == NULL) {
Doug Kwane8238072009-10-26 12:05:23 -0700880 /* We only allow an undefined symbol if this is a weak
881 reference.. */
882 s = &symtab[sym];
883 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughese9b6fc62012-08-29 13:10:54 -0700884 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
Doug Kwane8238072009-10-26 12:05:23 -0700885 return -1;
886 }
887
888 /* IHI0044C AAELF 4.5.1.1:
889
890 Libraries are not searched to resolve weak references.
891 It is not an error for a weak reference to remain
892 unsatisfied.
893
894 During linking, the value of an undefined weak reference is:
895 - Zero if the relocation type is absolute
896 - The address of the place if the relocation is pc-relative
Elliott Hughesbedfe382012-08-14 14:07:59 -0700897 - The address of nominal base address if the relocation
Doug Kwane8238072009-10-26 12:05:23 -0700898 type is base-relative.
899 */
900
901 switch (type) {
902#if defined(ANDROID_ARM_LINKER)
903 case R_ARM_JUMP_SLOT:
904 case R_ARM_GLOB_DAT:
905 case R_ARM_ABS32:
906 case R_ARM_RELATIVE: /* Don't care. */
Doug Kwane8238072009-10-26 12:05:23 -0700907#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700908 case R_386_JMP_SLOT:
Doug Kwane8238072009-10-26 12:05:23 -0700909 case R_386_GLOB_DAT:
910 case R_386_32:
911 case R_386_RELATIVE: /* Dont' care. */
912#endif /* ANDROID_*_LINKER */
913 /* sym_addr was initialized to be zero above or relocation
914 code below does not care about value of sym_addr.
915 No need to do anything. */
916 break;
917
918#if defined(ANDROID_X86_LINKER)
919 case R_386_PC32:
920 sym_addr = reloc;
921 break;
922#endif /* ANDROID_X86_LINKER */
923
924#if defined(ANDROID_ARM_LINKER)
925 case R_ARM_COPY:
926 /* Fall through. Can't really copy if weak symbol is
927 not found in run-time. */
928#endif /* ANDROID_ARM_LINKER */
929 default:
Elliott Hughes46882792012-08-03 16:49:39 -0700930 DL_ERR("unknown weak reloc type %d @ %p (%d)",
931 type, rel, (int) (rel - start));
Doug Kwane8238072009-10-26 12:05:23 -0700932 return -1;
933 }
934 } else {
935 /* We got a definition. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800936#if 0
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800937 if ((base == 0) && (si->base != 0)) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700938 /* linking from libraries to main image is bad */
Elliott Hughes46882792012-08-03 16:49:39 -0700939 DL_ERR("cannot locate \"%s\"...",
940 strtab + symtab[sym].st_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700941 return -1;
942 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800943#endif
Kito Chengfa8c05d2013-03-12 14:58:06 +0800944 sym_addr = static_cast<Elf32_Addr>(s->st_value + lsi->load_bias);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700945 }
Elliott Hughesbedfe382012-08-14 14:07:59 -0700946 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800947 } else {
Doug Kwane8238072009-10-26 12:05:23 -0700948 s = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800949 }
950
951/* TODO: This is ugly. Split up the relocations by arch into
952 * different files.
953 */
954 switch(type){
955#if defined(ANDROID_ARM_LINKER)
956 case R_ARM_JUMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700957 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800958 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700959 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800960 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800961 break;
962 case R_ARM_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700963 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800964 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700965 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800966 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800967 break;
968 case R_ARM_ABS32:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700969 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800970 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700971 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800972 *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800973 break;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -0800974 case R_ARM_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700975 count_relocation(kRelocRelative);
David 'Digit' Turner34ea5112009-11-17 14:56:26 -0800976 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700977 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
David 'Digit' Turner34ea5112009-11-17 14:56:26 -0800978 reloc, sym_addr, rel->r_offset, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800979 *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr - rel->r_offset;
David 'Digit' Turner34ea5112009-11-17 14:56:26 -0800980 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800981#elif defined(ANDROID_X86_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700982 case R_386_JMP_SLOT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700983 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800984 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700985 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800986 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800987 break;
988 case R_386_GLOB_DAT:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700989 count_relocation(kRelocAbsolute);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800990 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700991 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +0800992 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800993 break;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700994#elif defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700995 case R_MIPS_REL32:
Elliott Hughesbedfe382012-08-14 14:07:59 -0700996 count_relocation(kRelocAbsolute);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700997 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700998 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s",
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700999 reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1000 if (s) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001001 *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001002 } else {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001003 *reinterpret_cast<Elf32_Addr*>(reloc) += si->base;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001004 }
1005 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001006#endif /* ANDROID_*_LINKER */
1007
1008#if defined(ANDROID_ARM_LINKER)
1009 case R_ARM_RELATIVE:
1010#elif defined(ANDROID_X86_LINKER)
1011 case R_386_RELATIVE:
1012#endif /* ANDROID_*_LINKER */
Elliott Hughesbedfe382012-08-14 14:07:59 -07001013 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001014 MARK(rel->r_offset);
Elliott Hughes46882792012-08-03 16:49:39 -07001015 if (sym) {
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001016 DL_ERR("odd RELATIVE form...");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001017 return -1;
1018 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001019 TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x", reloc, si->base);
Kito Chengfa8c05d2013-03-12 14:58:06 +08001020 *reinterpret_cast<Elf32_Addr*>(reloc) += si->base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001021 break;
1022
1023#if defined(ANDROID_X86_LINKER)
1024 case R_386_32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001025 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001026 MARK(rel->r_offset);
1027
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001028 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +08001029 *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001030 break;
1031
1032 case R_386_PC32:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001033 count_relocation(kRelocRelative);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001034 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001035 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001036 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
Kito Chengfa8c05d2013-03-12 14:58:06 +08001037 *reinterpret_cast<Elf32_Addr*>(reloc) += (sym_addr - reloc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001038 break;
1039#endif /* ANDROID_X86_LINKER */
1040
1041#ifdef ANDROID_ARM_LINKER
1042 case R_ARM_COPY:
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001043 if ((si->flags & FLAG_EXE) == 0) {
1044 /*
1045 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1046 *
1047 * Section 4.7.1.10 "Dynamic relocations"
1048 * R_ARM_COPY may only appear in executable objects where e_type is
1049 * set to ET_EXEC.
1050 *
1051 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1052 * We should explicitly disallow ET_DYN executables from having
1053 * R_ARM_COPY relocations.
1054 */
1055 DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1056 return -1;
1057 }
Elliott Hughesbedfe382012-08-14 14:07:59 -07001058 count_relocation(kRelocCopy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001059 MARK(rel->r_offset);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001060 TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s", reloc, s->st_size, sym_addr, sym_name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001061 if (reloc == sym_addr) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001062 Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1063
1064 if (src == NULL) {
1065 DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1066 return -1;
1067 }
1068 if (lsi->has_DT_SYMBOLIC) {
1069 DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1070 "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1071 return -1;
1072 }
1073 if (s->st_size < src->st_size) {
1074 DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1075 si->name, s->st_size, src->st_size);
1076 return -1;
1077 }
1078 memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1079 } else {
1080 DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001081 return -1;
1082 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001083 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001084#endif /* ANDROID_ARM_LINKER */
1085
1086 default:
Elliott Hughes46882792012-08-03 16:49:39 -07001087 DL_ERR("unknown reloc type %d @ %p (%d)",
1088 type, rel, (int) (rel - start));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001089 return -1;
1090 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001091 }
1092 return 0;
1093}
1094
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001095#ifdef ANDROID_MIPS_LINKER
Elliott Hughesbedfe382012-08-14 14:07:59 -07001096static int mips_relocate_got(soinfo* si, soinfo* needed[]) {
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001097 unsigned *got;
1098 unsigned local_gotno, gotsym, symtabno;
1099 Elf32_Sym *symtab, *sym;
1100 unsigned g;
1101
1102 got = si->plt_got;
1103 local_gotno = si->mips_local_gotno;
1104 gotsym = si->mips_gotsym;
1105 symtabno = si->mips_symtabno;
1106 symtab = si->symtab;
1107
1108 /*
1109 * got[0] is address of lazy resolver function
1110 * got[1] may be used for a GNU extension
Elliott Hughesbedfe382012-08-14 14:07:59 -07001111 * set it to a recognizable address in case someone calls it
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001112 * (should be _rtld_bind_start)
1113 * FIXME: maybe this should be in a separate routine
1114 */
1115
1116 if ((si->flags & FLAG_LINKER) == 0) {
1117 g = 0;
1118 got[g++] = 0xdeadbeef;
1119 if (got[g] & 0x80000000) {
1120 got[g++] = 0xdeadfeed;
1121 }
1122 /*
1123 * Relocate the local GOT entries need to be relocated
1124 */
1125 for (; g < local_gotno; g++) {
1126 got[g] += si->load_bias;
1127 }
1128 }
1129
1130 /* Now for the global GOT entries */
1131 sym = symtab + gotsym;
1132 got = si->plt_got + local_gotno;
1133 for (g = gotsym; g < symtabno; g++, sym++, got++) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001134 const char* sym_name;
1135 Elf32_Sym* s;
1136 soinfo* lsi;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001137
1138 /* This is an undefined reference... try to locate it */
1139 sym_name = si->strtab + sym->st_name;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001140 s = soinfo_do_lookup(si, sym_name, &lsi, needed);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001141 if (s == NULL) {
1142 /* We only allow an undefined symbol if this is a weak
1143 reference.. */
1144 s = &symtab[g];
1145 if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
Elliott Hughes46882792012-08-03 16:49:39 -07001146 DL_ERR("cannot locate \"%s\"...", sym_name);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001147 return -1;
1148 }
1149 *got = 0;
1150 }
1151 else {
1152 /* FIXME: is this sufficient?
1153 * For reference see NetBSD link loader
1154 * 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
1155 */
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001156 *got = lsi->load_bias + s->st_value;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001157 }
1158 }
1159 return 0;
1160}
1161#endif
1162
David 'Digit' Turner82156792009-05-18 14:37:41 +02001163/* Please read the "Initialization and Termination functions" functions.
1164 * of the linker design note in bionic/linker/README.TXT to understand
1165 * what the following code is doing.
1166 *
1167 * The important things to remember are:
1168 *
1169 * DT_PREINIT_ARRAY must be called first for executables, and should
1170 * not appear in shared libraries.
1171 *
1172 * DT_INIT should be called before DT_INIT_ARRAY if both are present
1173 *
1174 * DT_FINI should be called after DT_FINI_ARRAY if both are present
1175 *
1176 * DT_FINI_ARRAY must be parsed in reverse order.
1177 */
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001178void soinfo::CallArray(const char* array_name UNUSED, linker_function_t* functions, size_t count, bool reverse) {
1179 if (functions == NULL) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001180 return;
1181 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001182
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001183 TRACE("[ Calling %s (size %d) @ %p for '%s' ]", array_name, count, functions, name);
1184
1185 int begin = reverse ? (count - 1) : 0;
1186 int end = reverse ? -1 : count;
1187 int step = reverse ? -1 : 1;
1188
1189 for (int i = begin; i != end; i += step) {
1190 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1191 CallFunction("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001192 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001193
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001194 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001195}
1196
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001197void soinfo::CallFunction(const char* function_name UNUSED, linker_function_t function) {
Elliott Hughesdb492b32013-01-03 15:44:03 -08001198 if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001199 return;
1200 }
1201
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001202 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001203 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001204 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001205
1206 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1207 // are still writable. This happens with our debug malloc (see http://b/7941716).
1208 set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001209}
1210
Elliott Hughesd23736e2012-11-01 15:16:56 -07001211void soinfo::CallPreInitConstructors() {
1212 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1213}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001214
Elliott Hughesd23736e2012-11-01 15:16:56 -07001215void soinfo::CallConstructors() {
1216 if (constructors_called) {
1217 return;
1218 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001219
Elliott Hughesd23736e2012-11-01 15:16:56 -07001220 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1221 // protect against recursive constructor calls. One simple example of constructor recursion
1222 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1223 // 1. The program depends on libc, so libc's constructor is called here.
1224 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1225 // 3. dlopen() calls the constructors on the newly created
1226 // soinfo for libc_malloc_debug_leak.so.
1227 // 4. The debug .so depends on libc, so CallConstructors is
1228 // called again with the libc soinfo. If it doesn't trigger the early-
1229 // out above, the libc constructor will be called again (recursively!).
1230 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001231
Elliott Hughesd23736e2012-11-01 15:16:56 -07001232 if (!(flags & FLAG_EXE) && preinit_array) {
1233 DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array);
1234 return;
1235 }
1236
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001237 if (dynamic != NULL) {
1238 for (Elf32_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) {
1239 if (d->d_tag == DT_NEEDED) {
1240 const char* library_name = strtab + d->d_un.d_val;
1241 soinfo* lsi = find_loaded_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001242 if (lsi == NULL) {
1243 DL_ERR("\"%s\": could not initialize dependent library", name);
1244 } else {
1245 lsi->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001246 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001247 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001248 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001249 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001250
Elliott Hughesd23736e2012-11-01 15:16:56 -07001251 CallFunction("DT_INIT", init_func);
1252 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001253}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001254
Elliott Hughesd23736e2012-11-01 15:16:56 -07001255void soinfo::CallDestructors() {
1256 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
1257 CallFunction("DT_FINI", fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001258}
1259
1260/* Force any of the closed stdin, stdout and stderr to be associated with
1261 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001262static int nullify_closed_stdio() {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001263 int dev_null, i, status;
1264 int return_value = 0;
1265
David 'Digit' Turner16084162012-06-12 16:25:37 +02001266 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001267 if (dev_null < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001268 DL_ERR("cannot open /dev/null: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001269 return -1;
1270 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001271 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001272
1273 /* If any of the stdio file descriptors is valid and not associated
1274 with /dev/null, dup /dev/null to it. */
1275 for (i = 0; i < 3; i++) {
1276 /* If it is /dev/null already, we are done. */
Elliott Hughes46882792012-08-03 16:49:39 -07001277 if (i == dev_null) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001278 continue;
Elliott Hughes46882792012-08-03 16:49:39 -07001279 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001280
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001281 TRACE("[ Nullifying stdio file descriptor %d]", i);
Elliott Hughes46882792012-08-03 16:49:39 -07001282 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001283
Elliott Hughes46882792012-08-03 16:49:39 -07001284 /* If file is opened, we are good. */
1285 if (status != -1) {
1286 continue;
1287 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001288
1289 /* The only error we allow is that the file descriptor does not
1290 exist, in which case we dup /dev/null to it. */
1291 if (errno != EBADF) {
Elliott Hughes46882792012-08-03 16:49:39 -07001292 DL_ERR("fcntl failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001293 return_value = -1;
1294 continue;
1295 }
1296
1297 /* Try dupping /dev/null to this stdio file descriptor and
1298 repeat if there is a signal. Note that any errors in closing
1299 the stdio descriptor are lost. */
Elliott Hughes46882792012-08-03 16:49:39 -07001300 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001301 if (status < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001302 DL_ERR("dup2 failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001303 return_value = -1;
1304 continue;
1305 }
1306 }
1307
1308 /* If /dev/null is not one of the stdio file descriptors, close it. */
1309 if (dev_null > 2) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001310 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
Elliott Hughes46882792012-08-03 16:49:39 -07001311 status = TEMP_FAILURE_RETRY(close(dev_null));
1312 if (status == -1) {
1313 DL_ERR("close failed: %s", strerror(errno));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001314 return_value = -1;
1315 }
1316 }
1317
1318 return return_value;
1319}
1320
Elliott Hughes124fae92012-10-31 14:20:03 -07001321static bool soinfo_link_image(soinfo* si) {
Ji-Hwan Leef186a182012-05-31 20:20:36 +09001322 /* "base" might wrap around UINT32_MAX. */
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001323 Elf32_Addr base = si->load_bias;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001324 const Elf32_Phdr *phdr = si->phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001325 int phnum = si->phnum;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001326 bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001327
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001328 /* We can't debug anything until the linker is relocated */
1329 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001330 INFO("[ linking %s ]", si->name);
1331 DEBUG("si->base = 0x%08x si->flags = 0x%08x", si->base, si->flags);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001332 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001333
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001334 /* Extract dynamic section */
Elliott Hughes124fae92012-10-31 14:20:03 -07001335 size_t dynamic_count;
Chris Dearmancf239052013-01-11 15:32:20 -08001336 Elf32_Word dynamic_flags;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001337 phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -08001338 &dynamic_count, &dynamic_flags);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001339 if (si->dynamic == NULL) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001340 if (!relocating_linker) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001341 DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001342 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001343 return false;
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001344 } else {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001345 if (!relocating_linker) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001346 DEBUG("dynamic = %p", si->dynamic);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001347 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001348 }
1349
1350#ifdef ANDROID_ARM_LINKER
1351 (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1352 &si->ARM_exidx, &si->ARM_exidx_count);
1353#endif
1354
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001355 /* extract useful information from dynamic section */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001356 uint32_t needed_count = 0;
1357 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001358 DEBUG("d = %p, d[0](tag) = 0x%08x d[1](val) = 0x%08x", d, d->d_tag, d->d_un.d_val);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001359 switch(d->d_tag){
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001360 case DT_HASH:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001361 si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0];
1362 si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1];
1363 si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8);
1364 si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001365 break;
1366 case DT_STRTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001367 si->strtab = (const char *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001368 break;
1369 case DT_SYMTAB:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001370 si->symtab = (Elf32_Sym *) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001371 break;
1372 case DT_PLTREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001373 if (d->d_un.d_val != DT_REL) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001374 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1375 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001376 }
1377 break;
1378 case DT_JMPREL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001379 si->plt_rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001380 break;
1381 case DT_PLTRELSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001382 si->plt_rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001383 break;
1384 case DT_REL:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001385 si->rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001386 break;
1387 case DT_RELSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001388 si->rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001389 break;
1390 case DT_PLTGOT:
1391 /* Save this in case we decide to do lazy binding. We don't yet. */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001392 si->plt_got = (unsigned *)(base + d->d_un.d_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001393 break;
1394 case DT_DEBUG:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001395 // Set the DT_DEBUG entry to the address of _r_debug for GDB
Chris Dearmancf239052013-01-11 15:32:20 -08001396 // if the dynamic table is writable
Elliott Hughes99c32052013-01-14 09:56:21 -08001397 if ((dynamic_flags & PF_W) != 0) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001398 d->d_un.d_val = (int) &_r_debug;
Elliott Hughes99c32052013-01-14 09:56:21 -08001399 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001400 break;
Shin-ichiro KAWASAKIad13c572009-11-06 10:36:37 +09001401 case DT_RELA:
Elliott Hughes124fae92012-10-31 14:20:03 -07001402 DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1403 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001404 case DT_INIT:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001405 si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
1406 DEBUG("%s constructors (init func) found at %p", si->name, si->init_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001407 break;
1408 case DT_FINI:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001409 si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
1410 DEBUG("%s destructors (fini func) found at %p", si->name, si->fini_func);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001411 break;
1412 case DT_INIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001413 si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1414 DEBUG("%s constructors (init_array) found at %p", si->name, si->init_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001415 break;
1416 case DT_INIT_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001417 si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001418 break;
1419 case DT_FINI_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001420 si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1421 DEBUG("%s destructors (fini_array) found at %p", si->name, si->fini_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001422 break;
1423 case DT_FINI_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001424 si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001425 break;
1426 case DT_PREINIT_ARRAY:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001427 si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1428 DEBUG("%s constructors (preinit_array) found at %p", si->name, si->preinit_array);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001429 break;
1430 case DT_PREINIT_ARRAYSZ:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001431 si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001432 break;
1433 case DT_TEXTREL:
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001434 si->has_text_relocations = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001435 break;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001436 case DT_SYMBOLIC:
1437 si->has_DT_SYMBOLIC = true;
1438 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001439 case DT_NEEDED:
1440 ++needed_count;
1441 break;
1442#if defined DT_FLAGS
1443 // TODO: why is DT_FLAGS not defined?
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001444 case DT_FLAGS:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001445 if (d->d_un.d_val & DF_TEXTREL) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001446 si->has_text_relocations = true;
1447 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001448 if (d->d_un.d_val & DF_SYMBOLIC) {
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001449 si->has_DT_SYMBOLIC = true;
1450 }
1451 break;
1452#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001453#if defined(ANDROID_MIPS_LINKER)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001454 case DT_STRSZ:
1455 case DT_SYMENT:
1456 case DT_RELENT:
1457 break;
1458 case DT_MIPS_RLD_MAP:
Elliott Hughesbedfe382012-08-14 14:07:59 -07001459 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001460 {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001461 r_debug** dp = (r_debug**) d->d_un.d_ptr;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001462 *dp = &_r_debug;
1463 }
1464 break;
1465 case DT_MIPS_RLD_VERSION:
1466 case DT_MIPS_FLAGS:
1467 case DT_MIPS_BASE_ADDRESS:
1468 case DT_MIPS_UNREFEXTNO:
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001469 break;
1470
1471 case DT_MIPS_SYMTABNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001472 si->mips_symtabno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001473 break;
1474
1475 case DT_MIPS_LOCAL_GOTNO:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001476 si->mips_local_gotno = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001477 break;
1478
1479 case DT_MIPS_GOTSYM:
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001480 si->mips_gotsym = d->d_un.d_val;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001481 break;
1482
1483 default:
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001484 DEBUG("Unused DT entry: type 0x%08x arg 0x%08x", d->d_tag, d->d_un.d_val);
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001485 break;
1486#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001487 }
1488 }
1489
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001490 DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p",
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001491 si->base, si->strtab, si->symtab);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001492
Elliott Hughes124fae92012-10-31 14:20:03 -07001493 // Sanity checks.
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001494 if (relocating_linker && needed_count != 0) {
1495 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1496 return false;
1497 }
Elliott Hughes124fae92012-10-31 14:20:03 -07001498 if (si->nbucket == 0) {
1499 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1500 return false;
1501 }
1502 if (si->strtab == 0) {
1503 DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1504 return false;
1505 }
1506 if (si->symtab == 0) {
1507 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1508 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001509 }
1510
Matt Fischer4fd42c12009-12-31 12:09:10 -06001511 /* if this is the main executable, then load all of the preloads now */
Elliott Hughesd23736e2012-11-01 15:16:56 -07001512 if (si->flags & FLAG_EXE) {
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001513 memset(gLdPreloads, 0, sizeof(gLdPreloads));
Elliott Hughesd23736e2012-11-01 15:16:56 -07001514 for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1515 soinfo* lsi = find_library(gLdPreloadNames[i]);
1516 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001517 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001518 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Elliott Hughes124fae92012-10-31 14:20:03 -07001519 gLdPreloadNames[i], si->name, tmp_err_buf);
1520 return false;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001521 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001522 gLdPreloads[i] = lsi;
Matt Fischer4fd42c12009-12-31 12:09:10 -06001523 }
1524 }
1525
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001526 soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*));
1527 soinfo** pneeded = needed;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001528
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001529 for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1530 if (d->d_tag == DT_NEEDED) {
1531 const char* library_name = si->strtab + d->d_un.d_val;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001532 DEBUG("%s needs %s", si->name, library_name);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001533 soinfo* lsi = find_library(library_name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001534 if (lsi == NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001535 strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
Elliott Hughes46882792012-08-03 16:49:39 -07001536 DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001537 library_name, si->name, tmp_err_buf);
Elliott Hughes124fae92012-10-31 14:20:03 -07001538 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001539 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001540 *pneeded++ = lsi;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001541 }
1542 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001543 *pneeded = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001544
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001545 if (si->has_text_relocations) {
1546 /* Unprotect the segments, i.e. make them writable, to allow
1547 * text relocations to work properly. We will later call
1548 * phdr_table_protect_segments() after all of them are applied
1549 * and all constructors are run.
1550 */
1551 if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1552 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1553 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001554 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001555 }
1556 }
1557
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001558 if (si->plt_rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001559 DEBUG("[ relocating %s plt ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001560 if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001561 return false;
1562 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001563 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001564 if (si->rel != NULL) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001565 DEBUG("[ relocating %s ]", si->name );
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001566 if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
Elliott Hughes124fae92012-10-31 14:20:03 -07001567 return false;
1568 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001569 }
1570
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001571#ifdef ANDROID_MIPS_LINKER
Elliott Hughes124fae92012-10-31 14:20:03 -07001572 if (mips_relocate_got(si, needed)) {
1573 return false;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001574 }
1575#endif
1576
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001577 si->flags |= FLAG_LINKED;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001578 DEBUG("[ finished linking %s ]", si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001579
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001580 if (si->has_text_relocations) {
1581 /* All relocations are done, we can protect our segments back to
1582 * read-only. */
1583 if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1584 DL_ERR("can't protect segments for \"%s\": %s",
1585 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001586 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07001587 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001588 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001589
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001590 /* We can also turn on GNU RELRO protection */
1591 if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
Elliott Hughes46882792012-08-03 16:49:39 -07001592 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1593 si->name, strerror(errno));
Elliott Hughes124fae92012-10-31 14:20:03 -07001594 return false;
Nick Kralevich9ec0f032012-02-28 10:40:00 -08001595 }
1596
Elliott Hughes124fae92012-10-31 14:20:03 -07001597 // If this is a setuid/setgid program, close the security hole described in
1598 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
Elliott Hughes18a206c2012-10-29 17:37:13 -07001599 if (get_AT_SECURE()) {
Elliott Hughes46882792012-08-03 16:49:39 -07001600 nullify_closed_stdio();
1601 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001602 notify_gdb_of_load(si);
Elliott Hughes124fae92012-10-31 14:20:03 -07001603 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001604}
1605
Nick Kralevich468319c2011-11-11 15:53:17 -08001606/*
1607 * This code is called after the linker has linked itself and
1608 * fixed it's own GOT. It is safe to make references to externs
1609 * and other non-local data at this point.
1610 */
Kito Chengfa8c05d2013-03-12 14:58:06 +08001611static Elf32_Addr __linker_init_post_relocation(KernelArgumentBlock& args, Elf32_Addr linker_base) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001612 /* NOTE: we store the args pointer on a special location
David 'Digit' Turneref0bd182009-07-17 17:55:01 +02001613 * of the temporary TLS area in order to pass it to
1614 * the C Library's runtime initializer.
1615 *
1616 * The initializer must clear the slot and reset the TLS
1617 * to point to a different location to ensure that no other
1618 * shared library constructor can access it.
1619 */
Elliott Hughesd3920b32013-02-07 18:39:34 -08001620 __libc_init_tls(args);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001621
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04001622#if TIMING
1623 struct timeval t0, t1;
1624 gettimeofday(&t0, 0);
1625#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001626
Elliott Hughes18a206c2012-10-29 17:37:13 -07001627 // Initialize environment functions, and get to the ELF aux vectors table.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001628 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001629
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001630 debuggerd_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001631
Elliott Hughes18a206c2012-10-29 17:37:13 -07001632 // Get a few environment variables.
Elliott Hughes61a9ccb2012-11-02 12:37:13 -07001633 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1634 if (LD_DEBUG != NULL) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001635 gLdDebugVerbosity = atoi(LD_DEBUG);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001636 }
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01001637
Elliott Hughes18a206c2012-10-29 17:37:13 -07001638 // Normally, these are cleaned by linker_env_init, but the test
1639 // doesn't cost us anything.
1640 const char* ldpath_env = NULL;
1641 const char* ldpreload_env = NULL;
1642 if (!get_AT_SECURE()) {
1643 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1644 ldpreload_env = linker_env_get("LD_PRELOAD");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001645 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001646
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001647 INFO("[ android linker & debugger ]");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001648
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001649 soinfo* si = soinfo_alloc(args.argv[0]);
Elliott Hughes18a206c2012-10-29 17:37:13 -07001650 if (si == NULL) {
1651 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652 }
1653
Nick Kralevichd39c3ab2012-08-24 13:25:51 -07001654 /* bootstrap the link map, the main exe always needs to be first */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001655 si->flags |= FLAG_EXE;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001656 link_map_t* map = &(si->link_map);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001657
1658 map->l_addr = 0;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001659 map->l_name = args.argv[0];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001660 map->l_prev = NULL;
1661 map->l_next = NULL;
1662
1663 _r_debug.r_map = map;
1664 r_debug_tail = map;
1665
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001666 /* gdb expects the linker to be in the debug shared object list.
1667 * Without this, gdb has trouble locating the linker's ".text"
1668 * and ".plt" sections. Gdb could also potentially use this to
1669 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1670 * Don't use soinfo_alloc(), because the linker shouldn't
1671 * be on the soinfo list.
Ben Cheng06f0e742012-08-10 16:07:02 -07001672 */
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001673 {
1674 static soinfo linker_soinfo;
1675 strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
1676 linker_soinfo.flags = 0;
1677 linker_soinfo.base = linker_base;
1678
1679 /*
1680 * Set the dynamic field in the link map otherwise gdb will complain with
1681 * the following:
1682 * warning: .dynamic section for "/system/bin/linker" is not at the
1683 * expected address (wrong library or version mismatch?)
1684 */
1685 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
1686 Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
1687 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1688 &linker_soinfo.dynamic, NULL, NULL);
1689 insert_soinfo_into_debug_map(&linker_soinfo);
1690 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001691
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001692 // Extract information passed from the kernel.
1693 si->phdr = reinterpret_cast<Elf32_Phdr*>(args.getauxval(AT_PHDR));
1694 si->phnum = args.getauxval(AT_PHNUM);
1695 si->entry = args.getauxval(AT_ENTRY);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001696
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001697 /* Compute the value of si->base. We can't rely on the fact that
1698 * the first entry is the PHDR because this will not be true
1699 * for certain executables (e.g. some in the NDK unit test suite)
1700 */
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001701 si->base = 0;
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001702 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001703 si->load_bias = 0;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001704 for (size_t i = 0; i < si->phnum; ++i) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001705 if (si->phdr[i].p_type == PT_PHDR) {
1706 si->load_bias = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_vaddr;
1707 si->base = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_offset;
1708 break;
1709 }
David 'Digit' Turner8180b082011-11-15 17:17:28 +01001710 }
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001711 si->dynamic = NULL;
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001712 si->ref_count = 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001713
Elliott Hughes46882792012-08-03 16:49:39 -07001714 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1715 parse_LD_LIBRARY_PATH(ldpath_env);
1716 parse_LD_PRELOAD(ldpreload_env);
Matt Fischer4fd42c12009-12-31 12:09:10 -06001717
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +02001718 somain = si;
1719
Elliott Hughes124fae92012-10-31 14:20:03 -07001720 if (!soinfo_link_image(si)) {
Elliott Hughes650be4e2013-03-05 18:47:58 -08001721 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
Elliott Hughes18a206c2012-10-29 17:37:13 -07001722 exit(EXIT_FAILURE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001723 }
1724
Elliott Hughesd23736e2012-11-01 15:16:56 -07001725 si->CallPreInitConstructors();
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001726
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001727 for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
1728 gLdPreloads[i]->CallConstructors();
Kito Cheng326e85e2012-07-15 00:49:27 +08001729 }
1730
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001731 /* After the link_image, the si->load_bias is initialized.
1732 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1733 * We need to update this value for so exe here. So Unwind_Backtrace
1734 * for some arch like x86 could work correctly within so exe.
Xiaokang Qin9c3449e2012-09-13 18:07:24 +08001735 */
Chao-Ying Fuc5db9692012-11-15 02:00:17 -08001736 map->l_addr = si->load_bias;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001737 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001738
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001739#if TIMING
1740 gettimeofday(&t1,NULL);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001741 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001742 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1743 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1744 ));
1745#endif
1746#if STATS
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001747 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
Elliott Hughesbedfe382012-08-14 14:07:59 -07001748 linker_stats.count[kRelocAbsolute],
1749 linker_stats.count[kRelocRelative],
1750 linker_stats.count[kRelocCopy],
1751 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001752#endif
1753#if COUNT_PAGES
1754 {
1755 unsigned n;
1756 unsigned i;
1757 unsigned count = 0;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001758 for (n = 0; n < 4096; n++) {
1759 if (bitmask[n]) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001760 unsigned x = bitmask[n];
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001761 for (i = 0; i < 8; i++) {
1762 if (x & 1) {
1763 count++;
1764 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001765 x >>= 1;
1766 }
1767 }
1768 }
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001769 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001770 }
1771#endif
1772
1773#if TIMING || STATS || COUNT_PAGES
1774 fflush(stdout);
1775#endif
1776
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001777 TRACE("[ Ready to execute '%s' @ 0x%08x ]", si->name, si->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001778 return si->entry;
1779}
Nick Kralevich468319c2011-11-11 15:53:17 -08001780
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001781/* Compute the load-bias of an existing executable. This shall only
1782 * be used to compute the load bias of an executable or shared library
1783 * that was loaded by the kernel itself.
1784 *
1785 * Input:
1786 * elf -> address of ELF header, assumed to be at the start of the file.
1787 * Return:
1788 * load bias, i.e. add the value of any p_vaddr in the file to get
1789 * the corresponding address in memory.
1790 */
Kito Chengfa8c05d2013-03-12 14:58:06 +08001791static Elf32_Addr get_elf_exec_load_bias(const Elf32_Ehdr* elf) {
1792 Elf32_Addr offset = elf->e_phoff;
1793 const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
1794 const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001795
Kito Chengfa8c05d2013-03-12 14:58:06 +08001796 for (const Elf32_Phdr* phdr = phdr_table; phdr < phdr_end; phdr++) {
1797 if (phdr->p_type == PT_LOAD) {
1798 return reinterpret_cast<Elf32_Addr>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001799 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001800 }
1801 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02001802}
1803
Nick Kralevich468319c2011-11-11 15:53:17 -08001804/*
1805 * This is the entry point for the linker, called from begin.S. This
1806 * method is responsible for fixing the linker's own relocations, and
1807 * then calling __linker_init_post_relocation().
1808 *
1809 * Because this method is called before the linker has fixed it's own
1810 * relocations, any attempt to reference an extern variable, extern
1811 * function, or other GOT reference will generate a segfault.
1812 */
Kito Chengfa8c05d2013-03-12 14:58:06 +08001813extern "C" Elf32_Addr __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001814 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08001815
Kito Chengfa8c05d2013-03-12 14:58:06 +08001816 Elf32_Addr linker_addr = args.getauxval(AT_BASE);
Nick Kralevich468319c2011-11-11 15:53:17 -08001817
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001818 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr*) linker_addr;
1819 Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08001820
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001821 soinfo linker_so;
1822 memset(&linker_so, 0, sizeof(soinfo));
Nick Kralevich468319c2011-11-11 15:53:17 -08001823
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001824 linker_so.base = linker_addr;
1825 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
1826 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001827 linker_so.dynamic = NULL;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001828 linker_so.phdr = phdr;
1829 linker_so.phnum = elf_hdr->e_phnum;
1830 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07001831
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001832 if (!soinfo_link_image(&linker_so)) {
1833 // It would be nice to print an error message, but if the linker
1834 // can't link itself, there's no guarantee that we'll be able to
1835 // call write() (because it involves a GOT reference).
1836 //
1837 // This situation should never occur unless the linker itself
1838 // is corrupt.
1839 exit(EXIT_FAILURE);
1840 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001841
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001842 // We have successfully fixed our own relocations. It's safe to run
1843 // the main part of the linker now.
Kito Chengfa8c05d2013-03-12 14:58:06 +08001844 Elf32_Addr start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001845
1846 set_soinfo_pool_protection(PROT_READ);
1847
1848 // Return the address that the calling assembly stub should jump to.
1849 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08001850}