blob: e0fec0f080bf0fa1d7557c6a7d38a4175b2758f4 [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>
Elliott Hughes0266ae52014-02-10 17:46:57 -080032#include <inttypes.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>
Elliott Hughes46882792012-08-03 16:49:39 -070037#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080038#include <sys/param.h>
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -080039#include <sys/personality.h>
Elliott Hughes46882792012-08-03 16:49:39 -070040#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070042#include <new>
43
Elliott Hughes46882792012-08-03 16:49:39 -070044// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070045#include "private/bionic_tls.h"
46#include "private/KernelArgumentBlock.h"
47#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070048#include "private/ScopedFd.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070049#include "private/ScopeGuard.h"
50#include "private/UniquePtr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051
52#include "linker.h"
53#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010054#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020055#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080056#include "linker_relocs.h"
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070057#include "linker_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080058
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 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070 * - after linking, set as much stuff as possible to READONLY
71 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070072 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073
Dmitriy Ivanov489e4982014-05-19 15:19:52 -070074#if defined(__LP64__)
75#define SEARCH_NAME(x) x
76#else
77// Nvidia drivers are relying on the bug:
78// http://code.google.com/p/android/issues/detail?id=6670
79// so we continue to use base-name lookup for lp32
80static const char* get_base_name(const char* name) {
81 const char* bname = strrchr(name, '/');
82 return bname ? bname + 1 : name;
83}
84#define SEARCH_NAME(x) get_base_name(x)
85#endif
86
Elliott Hughes0266ae52014-02-10 17:46:57 -080087static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088
Elliott Hughes1728b232014-05-14 10:02:03 -070089static LinkerAllocator<soinfo> g_soinfo_allocator;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070090static LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020091
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070092static soinfo* solist;
93static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -070094static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
Elliott Hughes1728b232014-05-14 10:02:03 -070096static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070097#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070098 "/vendor/lib64",
99 "/system/lib64",
100#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700101 "/vendor/lib",
102 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700103#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700104 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700105};
David Bartleybc3a5c22009-06-02 18:27:28 -0700106
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800107#define LDPATH_BUFSIZE (LDPATH_MAX*64)
108#define LDPATH_MAX 8
109
110#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
111#define LDPRELOAD_MAX 8
112
Elliott Hughes1728b232014-05-14 10:02:03 -0700113static char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
114static const char* g_ld_library_paths[LDPATH_MAX + 1];
Elliott Hughes124fae92012-10-31 14:20:03 -0700115
Elliott Hughes1728b232014-05-14 10:02:03 -0700116static char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
117static const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600118
Elliott Hughes1728b232014-05-14 10:02:03 -0700119static soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600120
Elliott Hughes1728b232014-05-14 10:02:03 -0700121__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700123__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700124
Elliott Hughesbedfe382012-08-14 14:07:59 -0700125enum RelocationKind {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700126 kRelocAbsolute = 0,
127 kRelocRelative,
128 kRelocCopy,
129 kRelocSymbol,
130 kRelocMax
Elliott Hughesbedfe382012-08-14 14:07:59 -0700131};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100132
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700134struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700135 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700136};
137
138static linker_stats_t linker_stats;
139
140static void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700141 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700142}
143#else
144static void count_relocation(RelocationKind) {
145}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146#endif
147
148#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700149static unsigned bitmask[4096];
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100150#if defined(__LP64__)
151#define MARK(offset) \
152 do { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700153 if ((((offset) >> 12) >> 5) < 4096) \
154 bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800155 } while (0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100156#else
Elliott Hughesbedfe382012-08-14 14:07:59 -0700157#define MARK(offset) \
158 do { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700159 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800160 } while (0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100161#endif
Elliott Hughesbedfe382012-08-14 14:07:59 -0700162#else
163#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164#endif
165
Elliott Hughes46882792012-08-03 16:49:39 -0700166// You shouldn't try to call memory-allocating functions in the dynamic linker.
167// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700168#define DISALLOW_ALLOCATION(return_type, name, ...) \
169 return_type name __VA_ARGS__ \
170 { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700171 __libc_fatal("ERROR: " #name " called from the dynamic linker!\n"); \
Dima Zavin2e855792009-05-20 18:28:09 -0700172 }
Kito Cheng812fd422014-03-25 22:53:56 +0800173DISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
174DISALLOW_ALLOCATION(void, free, (void* u __unused));
175DISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
176DISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
Dima Zavin2e855792009-05-20 18:28:09 -0700177
178static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700179
Elliott Hughes650be4e2013-03-05 18:47:58 -0800180char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700181 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700182}
183
Elliott Hughes650be4e2013-03-05 18:47:58 -0800184size_t linker_get_error_buffer_size() {
185 return sizeof(__linker_dl_err_buf);
186}
187
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700188// This function is an empty stub where GDB locates a breakpoint to get notified
189// about linker activity.
Elliott Hughes5419b942012-10-16 15:54:46 -0700190extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191
Elliott Hughes1728b232014-05-14 10:02:03 -0700192static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700193static r_debug _r_debug = {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800194static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800196static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700197 // Copy the necessary fields into the debug structure.
198 link_map* map = &(info->link_map_head);
199 map->l_addr = info->load_bias;
Dmitriy Ivanovc9d16582014-10-24 14:46:12 -0700200 map->l_name = info->name;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700201 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700203 // Stick the new library at the end of the list.
204 // gdb tends to care more about libc than it does
205 // about leaf libraries, and ordering it this way
206 // reduces the back-and-forth over the wire.
207 if (r_debug_tail) {
208 r_debug_tail->l_next = map;
209 map->l_prev = r_debug_tail;
210 map->l_next = 0;
211 } else {
212 _r_debug.r_map = map;
213 map->l_prev = 0;
214 map->l_next = 0;
215 }
216 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217}
218
Elliott Hughesbedfe382012-08-14 14:07:59 -0700219static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700220 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700221
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700222 if (r_debug_tail == map) {
223 r_debug_tail = map->l_prev;
224 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700225
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700226 if (map->l_prev) {
227 map->l_prev->l_next = map->l_next;
228 }
229 if (map->l_next) {
230 map->l_next->l_prev = map->l_prev;
231 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700232}
233
Elliott Hughesbedfe382012-08-14 14:07:59 -0700234static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800235 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700236 // GDB already knows about the main executable
237 return;
238 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700240 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700242 _r_debug.r_state = r_debug::RT_ADD;
243 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700245 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700247 _r_debug.r_state = r_debug::RT_CONSISTENT;
248 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700249}
250
Elliott Hughesbedfe382012-08-14 14:07:59 -0700251static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800252 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700253 // GDB already knows about the main executable
254 return;
255 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700256
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700257 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700258
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700259 _r_debug.r_state = r_debug::RT_DELETE;
260 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700261
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700262 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700263
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700264 _r_debug.r_state = r_debug::RT_CONSISTENT;
265 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800266}
267
Elliott Hughes18a206c2012-10-29 17:37:13 -0700268void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800269 _r_debug.r_state = r_debug::RT_ADD;
270 rtld_db_dlactivity();
271 _r_debug.r_state = r_debug::RT_CONSISTENT;
272 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273}
274
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700275LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
276 return g_soinfo_links_allocator.alloc();
277}
278
279void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
280 g_soinfo_links_allocator.free(entry);
281}
282
283static void protect_data(int protection) {
284 g_soinfo_allocator.protect_all(protection);
285 g_soinfo_links_allocator.protect_all(protection);
286}
287
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700288static soinfo* soinfo_alloc(const char* name, struct stat* file_stat, off64_t file_offset, uint32_t rtld_flags) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200289 if (strlen(name) >= SOINFO_NAME_LEN) {
290 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700291 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200292 }
293
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700294 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700295
Magnus Malmbornba98d922012-09-12 13:00:55 +0200296 sonext->next = si;
297 sonext = si;
298
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700299 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200300 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800301}
302
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800303static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700304 if (si == nullptr) {
305 return;
306 }
307
308 if (si->base != 0 && si->size != 0) {
309 munmap(reinterpret_cast<void*>(si->base), si->size);
310 }
311
312 soinfo *prev = nullptr, *trav;
313
314 TRACE("name %s: freeing soinfo @ %p", si->name, si);
315
316 for (trav = solist; trav != nullptr; trav = trav->next) {
317 if (trav == si) {
318 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700319 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700320 prev = trav;
321 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800322
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700323 if (trav == nullptr) {
324 // si was not in solist
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -0800325 DL_ERR("name \"%s\"@%p is not in solist!", si->name, si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700326 return;
327 }
Elliott Hughes46882792012-08-03 16:49:39 -0700328
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700329 // clear links to/from si
330 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700331
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700332 // prev will never be null, because the first entry in solist is
333 // always the static libdl_info.
334 prev->next = si->next;
335 if (si == sonext) {
336 sonext = prev;
337 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700339 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800340}
341
Elliott Hughescade4c32012-12-20 14:42:14 -0800342static void parse_path(const char* path, const char* delimiters,
343 const char** array, char* buf, size_t buf_size, size_t max_count) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700344 if (path == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800345 return;
346 }
347
348 size_t len = strlcpy(buf, path, buf_size);
349
350 size_t i = 0;
351 char* buf_p = buf;
352 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
353 if (*array[i] != '\0') {
354 ++i;
355 }
356 }
357
358 // Forget the last path if we had to truncate; this occurs if the 2nd to
359 // last char isn't '\0' (i.e. wasn't originally a delimiter).
360 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700361 array[i - 1] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800362 } else {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700363 array[i] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800364 }
365}
366
367static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700368 parse_path(path, ":", g_ld_library_paths,
369 g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800370}
371
372static void parse_LD_PRELOAD(const char* path) {
373 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes1728b232014-05-14 10:02:03 -0700374 parse_path(path, " :", g_ld_preload_names,
375 g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800376}
377
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700378#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700379
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700380// For a given PC, find the .so that it belongs to.
381// Returns the base address of the .ARM.exidx section
382// for that .so, and the number of 8-byte entries
383// in that section (via *pcount).
384//
385// Intended to be called by libc's __gnu_Unwind_Find_exidx().
386//
387// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800388_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700389 unsigned addr = (unsigned)pc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800390
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700391 for (soinfo* si = solist; si != 0; si = si->next) {
392 if ((addr >= si->base) && (addr < (si->base + si->size))) {
393 *pcount = si->ARM_exidx_count;
394 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800395 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700396 }
397 *pcount = 0;
398 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800399}
Elliott Hughes46882792012-08-03 16:49:39 -0700400
Christopher Ferris24053a42013-08-19 17:45:09 -0700401#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700402
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700403// Here, we only have to provide a callback to iterate across all the
404// loaded libraries. gcc_eh does the rest.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800405int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700406 int rv = 0;
407 for (soinfo* si = solist; si != nullptr; si = si->next) {
408 dl_phdr_info dl_info;
409 dl_info.dlpi_addr = si->link_map_head.l_addr;
410 dl_info.dlpi_name = si->link_map_head.l_name;
411 dl_info.dlpi_phdr = si->phdr;
412 dl_info.dlpi_phnum = si->phnum;
413 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
414 if (rv != 0) {
415 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700417 }
418 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800419}
Elliott Hughes46882792012-08-03 16:49:39 -0700420
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800421ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name) {
422 return is_gnu_hash() ? gnu_lookup(symbol_name) : elf_lookup(symbol_name);
423}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800424
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800425static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
426 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
427 ELF_ST_BIND(s->st_info) == STB_WEAK) {
428 return s->st_shndx != SHN_UNDEF;
429 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
430 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
431 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->name);
432 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800434 return false;
435}
436
437ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name) {
438 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800439 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800440
441 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800442 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
443 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800444
445 // test against bloom filter
446 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
447 return nullptr;
448 }
449
450 // bloom test says "probably yes"...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800451 uint32_t n = bucket_[hash % nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800452
453 if (n == 0) {
454 return nullptr;
455 }
456
457 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800458 ElfW(Sym)* s = symtab_ + n;
459 if (((chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800460 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
461 is_symbol_global_and_defined(this, s)) {
462 return s;
463 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800464 } while ((chain_[n++] & 1) == 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800465
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800466 return nullptr;
467}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800468
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800469ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name) {
470 uint32_t hash = symbol_name.elf_hash();
471
472 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800473 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800474
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800475 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
476 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800477 if (strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && is_symbol_global_and_defined(this, s)) {
478 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
479 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
480 static_cast<size_t>(s->st_size));
481 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800482 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800483 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800484
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700485 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800486 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700487
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700488 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800489}
490
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700491soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700492 memset(this, 0, sizeof(*this));
493
494 strlcpy(this->name, name, sizeof(this->name));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800495 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800496 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700497
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700498 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800499 this->st_dev_ = file_stat->st_dev;
500 this->st_ino_ = file_stat->st_ino;
501 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700502 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700503
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800504 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700505}
506
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800507
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800508uint32_t SymbolName::elf_hash() {
509 if (!has_elf_hash_) {
510 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
511 uint32_t h = 0, g;
512
513 while (*name) {
514 h = (h << 4) + *name++;
515 g = h & 0xf0000000;
516 h ^= g;
517 h ^= g >> 24;
518 }
519
520 elf_hash_ = h;
521 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700522 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800523
524 return elf_hash_;
525}
526
527uint32_t SymbolName::gnu_hash() {
528 if (!has_gnu_hash_) {
529 uint32_t h = 5381;
530 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
531 while (*name != 0) {
532 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
533 }
534
535 gnu_hash_ = h;
536 has_gnu_hash_ = true;
537 }
538
539 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800540}
541
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700542static ElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
543 const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800544 SymbolName symbol_name(name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700545 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700546
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700547 /* "This element's presence in a shared object library alters the dynamic linker's
548 * symbol resolution algorithm for references within the library. Instead of starting
549 * a symbol search with the executable file, the dynamic linker starts from the shared
550 * object itself. If the shared object fails to supply the referenced symbol, the
551 * dynamic linker then searches the executable file and other shared objects as usual."
552 *
553 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
554 *
555 * Note that this is unlikely since static linker avoids generating
556 * relocations for -Bsymbolic linked dynamic executables.
557 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700558 if (si_from->has_DT_SYMBOLIC) {
559 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->name, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800560 s = si_from->find_symbol_by_name(symbol_name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700561 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700562 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700563 }
564 }
565
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700566 // 1. Look for it in global_group
567 if (s == nullptr) {
568 global_group.visit([&](soinfo* global_si) {
569 DEBUG("%s: looking up %s in %s (from global group)", si_from->name, name, global_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800570 s = global_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700571 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700572 *si_found_in = global_si;
573 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700574 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700575
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700576 return true;
577 });
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700578 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700579
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700580 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700581 if (s == nullptr) {
582 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700583 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700584 // we already did this - skip
585 return true;
586 }
587
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700588 DEBUG("%s: looking up %s in %s (from local group)", si_from->name, name, local_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800589 s = local_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700590 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700591 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700592 return false;
593 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700594
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700595 return true;
596 });
597 }
598
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700599 if (s != nullptr) {
600 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
601 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700602 si_from->name, name, reinterpret_cast<void*>(s->st_value),
603 (*si_found_in)->name, reinterpret_cast<void*>((*si_found_in)->base),
604 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700605 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700606
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700607 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700608}
609
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700610// Each size has it's own allocator.
611template<size_t size>
612class SizeBasedAllocator {
613 public:
614 static void* alloc() {
615 return allocator_.alloc();
616 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700617
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700618 static void free(void* ptr) {
619 allocator_.free(ptr);
620 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700621
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700622 private:
623 static LinkerBlockAllocator allocator_;
624};
625
626template<size_t size>
627LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
628
629template<typename T>
630class TypeBasedAllocator {
631 public:
632 static T* alloc() {
633 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
634 }
635
636 static void free(T* ptr) {
637 SizeBasedAllocator<sizeof(T)>::free(ptr);
638 }
639};
640
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700641class LoadTask {
642 public:
643 struct deleter_t {
644 void operator()(LoadTask* t) {
645 TypeBasedAllocator<LoadTask>::free(t);
646 }
647 };
648
649 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
650
651 static deleter_t deleter;
652
653 static LoadTask* create(const char* name, soinfo* needed_by) {
654 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
655 return new (ptr) LoadTask(name, needed_by);
656 }
657
658 const char* get_name() const {
659 return name_;
660 }
661
662 soinfo* get_needed_by() const {
663 return needed_by_;
664 }
665 private:
666 LoadTask(const char* name, soinfo* needed_by)
667 : name_(name), needed_by_(needed_by) {}
668
669 const char* name_;
670 soinfo* needed_by_;
671
672 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
673};
674
Ningsheng Jiane93be992014-09-16 15:22:10 +0800675LoadTask::deleter_t LoadTask::deleter;
676
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700677template <typename T>
678using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
679
680typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700681typedef linked_list_t<const char> StringLinkedList;
682typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700683
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700684
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700685// This function walks down the tree of soinfo dependencies
686// in breadth-first order and
687// * calls action(soinfo* si) for each node, and
688// * terminates walk if action returns false.
689//
690// walk_dependencies_tree returns false if walk was terminated
691// by the action and true otherwise.
692template<typename F>
693static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700694 SoinfoLinkedList visit_list;
695 SoinfoLinkedList visited;
696
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700697 for (size_t i = 0; i < root_soinfos_size; ++i) {
698 visit_list.push_back(root_soinfos[i]);
699 }
700
701 soinfo* si;
702 while ((si = visit_list.pop_front()) != nullptr) {
703 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700704 continue;
705 }
706
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700707 if (!action(si)) {
708 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700709 }
710
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700711 visited.push_back(si);
712
713 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700714 visit_list.push_back(child);
715 });
716 }
717
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700718 return true;
719}
720
721
722// This is used by dlsym(3). It performs symbol lookup only within the
723// specified soinfo object and its dependencies in breadth first order.
724ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
725 ElfW(Sym)* result = nullptr;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800726 SymbolName symbol_name(name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700727
728
729 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800730 result = current_soinfo->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700731 if (result != nullptr) {
732 *found = current_soinfo;
733 return false;
734 }
735
736 return true;
737 });
738
739 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800740}
741
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800742/* This is used by dlsym(3) to performs a global symbol lookup. If the
743 start value is null (for RTLD_DEFAULT), the search starts at the
744 beginning of the global solist. Otherwise the search starts at the
745 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700746 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700747ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800748 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800749
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700750 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800751 start = solist;
752 }
753
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700754 ElfW(Sym)* s = nullptr;
755 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700756 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
757 continue;
758 }
759
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800760 s = si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700761 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800762 *found = si;
763 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600764 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800765 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600766
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700767 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700768 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
769 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800770 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800771
Elliott Hughescade4c32012-12-20 14:42:14 -0800772 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800773}
774
Kito Chengfa8c05d2013-03-12 14:58:06 +0800775soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800776 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700777 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800778 if (address >= si->base && address - si->base < si->size) {
779 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600780 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800781 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700782 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600783}
784
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800785ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
786 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
787}
788
789static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
790 return sym->st_shndx != SHN_UNDEF &&
791 soaddr >= sym->st_value &&
792 soaddr < sym->st_value + sym->st_size;
793}
794
795ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
796 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - base;
797
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800798 for (size_t i = 0; i < nbucket_; ++i) {
799 uint32_t n = bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800800
801 if (n == 0) {
802 continue;
803 }
804
805 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800806 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800807 if (symbol_matches_soaddr(sym, soaddr)) {
808 return sym;
809 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800810 } while ((chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800811 }
812
813 return nullptr;
814}
815
816ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
817 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600818
Kito Chengfa8c05d2013-03-12 14:58:06 +0800819 // Search the library's symbol table for any defined symbol which
820 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800821 for (size_t i = 0; i < nchain_; ++i) {
822 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800823 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800824 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600825 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800826 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600827
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700828 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600829}
830
Elliott Hughes124fae92012-10-31 14:20:03 -0700831static int open_library_on_path(const char* name, const char* const paths[]) {
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +0000832 char buf[512];
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700833 for (size_t i = 0; paths[i] != nullptr; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800834 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700835 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700836 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700837 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800838 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700839 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
840 if (fd != -1) {
841 return fd;
842 }
843 }
844 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800845}
846
Elliott Hughes124fae92012-10-31 14:20:03 -0700847static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700848 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800849
Elliott Hughes124fae92012-10-31 14:20:03 -0700850 // If the name contains a slash, we should attempt to open it directly and not search the paths.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700851 if (strchr(name, '/') != nullptr) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700852 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
853 if (fd != -1) {
854 return fd;
855 }
856 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700857#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700858 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700859#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700860 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800861
Elliott Hughes124fae92012-10-31 14:20:03 -0700862 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Elliott Hughes1728b232014-05-14 10:02:03 -0700863 int fd = open_library_on_path(name, g_ld_library_paths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700864 if (fd == -1) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700865 fd = open_library_on_path(name, kDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700866 }
867 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800868}
869
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700870template<typename F>
871static void for_each_dt_needed(const soinfo* si, F action) {
872 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
873 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700874 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700875 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700876 }
877}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800878
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700879static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700880 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700881 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700882 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700883
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700884 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
885 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700886 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
887 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700888 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700889 } else {
890 // Open the file.
891 fd = open_library(name);
892 if (fd == -1) {
893 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700894 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700895 }
896
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700897 file_guard.reset(fd);
898 }
899
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700900 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700901 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700902 return nullptr;
903 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800904 if (file_offset < 0) {
905 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
906 return nullptr;
907 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700908
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700909 struct stat file_stat;
910 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700911 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700912 return nullptr;
913 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800914 if (file_offset >= file_stat.st_size) {
915 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64, name, file_offset, file_stat.st_size);
916 return nullptr;
917 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700918
919 // Check for symlink and other situations where
920 // file can have different names.
921 for (soinfo* si = solist; si != nullptr; si = si->next) {
922 if (si->get_st_dev() != 0 &&
923 si->get_st_ino() != 0 &&
924 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700925 si->get_st_ino() == file_stat.st_ino &&
926 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700927 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
928 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700929 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700930 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700931
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700932 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700933 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700934 return nullptr;
935 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700936
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700937 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700938 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700939 if (!elf_reader.Load(extinfo)) {
940 return nullptr;
941 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800942
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700943 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700944 if (si == nullptr) {
945 return nullptr;
946 }
947 si->base = elf_reader.load_start();
948 si->size = elf_reader.load_size();
949 si->load_bias = elf_reader.load_bias();
950 si->phnum = elf_reader.phdr_count();
951 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700952
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800953 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700954 soinfo_free(si);
955 return nullptr;
956 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700957
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700958 for_each_dt_needed(si, [&] (const char* name) {
959 load_tasks.push_back(LoadTask::create(name, si));
960 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700961
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700962 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800963}
964
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700965static soinfo *find_loaded_library_by_name(const char* name) {
966 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700967 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700968 if (!strcmp(search_name, si->name)) {
969 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200970 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700971 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700972 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200973}
974
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700975static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200976
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700977 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700978
979 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700980 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700981 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700982 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700983 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700984 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800985
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700986 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800987}
988
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700989static void soinfo_unload(soinfo* si);
990
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700991// TODO: this is slightly unusual way to construct
992// the global group for relocation. Not every RTLD_GLOBAL
993// library is included in this group for backwards-compatibility
994// reasons.
995//
996// This group consists of the main executable, LD_PRELOADs
997// and libraries with the DF_1_GLOBAL flag set.
998static soinfo::soinfo_list_t make_global_group() {
999 soinfo::soinfo_list_t global_group;
1000 for (soinfo* si = somain; si != nullptr; si = si->next) {
1001 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1002 global_group.push_back(si);
1003 }
1004 }
1005
1006 return global_group;
1007}
1008
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001009static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
1010 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001011 // Step 0: prepare.
1012 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001013 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001014 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001015 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001016 }
1017
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001018 // Construct global_group.
1019 soinfo::soinfo_list_t global_group = make_global_group();
1020
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001021 // If soinfos array is null allocate one on stack.
1022 // The array is needed in case of failure; for example
1023 // when library_names[] = {libone.so, libtwo.so} and libone.so
1024 // is loaded correctly but libtwo.so failed for some reason.
1025 // In this case libone.so should be unloaded on return.
1026 // See also implementation of failure_guard below.
1027
1028 if (soinfos == nullptr) {
1029 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1030 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1031 memset(soinfos, 0, soinfos_size);
1032 }
1033
1034 // list of libraries to link - see step 2.
1035 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001036
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001037 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001038 // Housekeeping
1039 load_tasks.for_each([] (LoadTask* t) {
1040 LoadTask::deleter(t);
1041 });
1042
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001043 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001044 soinfo_unload(soinfos[i]);
1045 }
1046 });
1047
1048 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
1049 for (LoadTask::unique_ptr task(load_tasks.pop_front()); task.get() != nullptr; task.reset(load_tasks.pop_front())) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001050 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001051 if (si == nullptr) {
1052 return false;
1053 }
1054
1055 soinfo* needed_by = task->get_needed_by();
1056
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001057 if (needed_by != nullptr) {
1058 needed_by->add_child(si);
1059 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001060
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001061 if (si->is_linked()) {
1062 si->increment_ref_count();
1063 }
1064
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001065 // When ld_preloads is not null, the first
1066 // ld_preloads_count libs are in fact ld_preloads.
1067 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001068 // Add LD_PRELOADed libraries to the global group for future runs.
1069 // There is no need to explicitly add them to the global group
1070 // for this run because they are going to appear in the local
1071 // group in the correct order.
1072 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001073 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001074 }
1075
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001076 if (soinfos_count < library_names_count) {
1077 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001078 }
1079 }
1080
1081 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001082 soinfo::soinfo_list_t local_group;
1083 walk_dependencies_tree(
1084 start_with == nullptr ? soinfos : &start_with,
1085 start_with == nullptr ? soinfos_count : 1,
1086 [&] (soinfo* si) {
1087 local_group.push_back(si);
1088 return true;
1089 });
1090
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001091 // We need to increment ref_count in case
1092 // the root of the local group was not linked.
1093 bool was_local_group_root_linked = local_group.front()->is_linked();
1094
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001095 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001096 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001097 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001098 return false;
1099 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001100 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001101 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001102
1103 return true;
1104 });
1105
1106 if (linked) {
1107 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001108 }
1109
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001110 if (!was_local_group_root_linked) {
1111 local_group.front()->increment_ref_count();
1112 }
1113
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001114 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001115}
1116
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001117static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001118 soinfo* si;
1119
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001120 if (name == nullptr) {
1121 si = somain;
1122 } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001123 return nullptr;
1124 }
1125
Elliott Hughesd23736e2012-11-01 15:16:56 -07001126 return si;
1127}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001128
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001129static void soinfo_unload(soinfo* root) {
1130 // Note that the library can be loaded but not linked;
1131 // in which case there is no root but we still need
1132 // to walk the tree and unload soinfos involved.
1133 //
1134 // This happens on unsuccessful dlopen, when one of
1135 // the DT_NEEDED libraries could not be linked/found.
1136 if (root->is_linked()) {
1137 root = root->get_local_group_root();
1138 }
1139
1140 if (!root->can_unload()) {
1141 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->name);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001142 return;
1143 }
1144
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001145 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001146
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001147 if (ref_count == 0) {
1148 soinfo::soinfo_list_t local_unload_list;
1149 soinfo::soinfo_list_t external_unload_list;
1150 soinfo::soinfo_list_t depth_first_list;
1151 depth_first_list.push_back(root);
1152 soinfo* si = nullptr;
1153
1154 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001155 if (local_unload_list.contains(si)) {
1156 continue;
1157 }
1158
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001159 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001160
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001161 if (si->has_min_version(0)) {
1162 soinfo* child = nullptr;
1163 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001164 TRACE("%s@%p needs to unload %s@%p", si->name, si, child->name, child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001165 if (local_unload_list.contains(child)) {
1166 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001167 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001168 external_unload_list.push_back(child);
1169 } else {
1170 depth_first_list.push_front(child);
1171 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001172 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001173 } else {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001174#ifdef __LP64__
1175 __libc_fatal("soinfo for \"%s\"@%p has no version", si->name, si);
1176#else
1177 PRINT("warning: soinfo for \"%s\"@%p has no version", si->name, si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001178 for_each_dt_needed(si, [&] (const char* library_name) {
1179 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1180 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1181 if (needed != nullptr) {
1182 // Not found: for example if symlink was deleted between dlopen and dlclose
1183 // Since we cannot really handle errors at this point - print and continue.
1184 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
1185 return;
1186 } else if (local_unload_list.contains(needed)) {
1187 // already visited
1188 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001189 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001190 // external group
1191 external_unload_list.push_back(needed);
1192 } else {
1193 // local group
1194 depth_first_list.push_front(needed);
1195 }
1196 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001197#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001198 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001199 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001200
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001201 local_unload_list.for_each([](soinfo* si) {
1202 si->call_destructors();
1203 });
1204
1205 while ((si = local_unload_list.pop_front()) != nullptr) {
1206 notify_gdb_of_unload(si);
1207 soinfo_free(si);
1208 }
1209
1210 while ((si = external_unload_list.pop_front()) != nullptr) {
1211 soinfo_unload(si);
1212 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001213 } else {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001214 TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->name, ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001215 }
1216}
1217
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001218void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001219 // Use basic string manipulation calls to avoid snprintf.
1220 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1221 // When debug malloc is enabled, this call returns 0. This in turn causes
1222 // snprintf to do nothing, which causes libraries to fail to load.
1223 // See b/17302493 for further details.
1224 // Once the above bug is fixed, this code can be modified to use
1225 // snprintf again.
1226 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1227 if (buffer_size < required_len) {
1228 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1229 buffer_size, required_len);
1230 }
1231 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1232 *end = ':';
1233 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001234}
1235
Elliott Hughescade4c32012-12-20 14:42:14 -08001236void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
1237 if (!get_AT_SECURE()) {
1238 parse_LD_LIBRARY_PATH(ld_library_path);
1239 }
1240}
1241
Elliott Hughes1a586292014-06-03 16:23:08 -07001242soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001243 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001244 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001245 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001246 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001247 if (extinfo != nullptr) {
1248 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1249 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1250 return nullptr;
1251 }
1252 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001253 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1254 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001255 return nullptr;
1256 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001257 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001258 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001259 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001260 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001261 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001262 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001263 protect_data(PROT_READ);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001264 return si;
1265}
1266
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001267void do_dlclose(soinfo* si) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001268 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001269 soinfo_unload(si);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001270 protect_data(PROT_READ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001271}
1272
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001273static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1274 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1275 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1276 ElfW(Addr) ifunc_addr = ifunc_resolver();
1277 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p", ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001278
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001279 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001280}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001281
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001282#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001283int soinfo::relocate(ElfW(Rela)* rela, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001284 for (size_t idx = 0; idx < count; ++idx, ++rela) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001285 unsigned type = ELFW(R_TYPE)(rela->r_info);
1286 unsigned sym = ELFW(R_SYM)(rela->r_info);
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001287 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001288 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001289 const char* sym_name = nullptr;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001290
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001291 DEBUG("Processing '%s' relocation at index %zd", name, idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001292 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001293 continue;
1294 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001295
1296 ElfW(Sym)* s = nullptr;
1297 soinfo* lsi = nullptr;
1298
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001299 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001300 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001301 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001302 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001303 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001304 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001305 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001306 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001307 return -1;
1308 }
1309
1310 /* IHI0044C AAELF 4.5.1.1:
1311
1312 Libraries are not searched to resolve weak references.
1313 It is not an error for a weak reference to remain unsatisfied.
1314
1315 During linking, the value of an undefined weak reference is:
1316 - Zero if the relocation type is absolute
1317 - The address of the place if the relocation is pc-relative
1318 - The address of nominal base address if the relocation
1319 type is base-relative.
1320 */
1321
1322 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001323#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001324 case R_AARCH64_JUMP_SLOT:
1325 case R_AARCH64_GLOB_DAT:
1326 case R_AARCH64_ABS64:
1327 case R_AARCH64_ABS32:
1328 case R_AARCH64_ABS16:
1329 case R_AARCH64_RELATIVE:
1330 case R_AARCH64_IRELATIVE:
1331 /*
1332 * The sym_addr was initialized to be zero above, or the relocation
1333 * code below does not care about value of sym_addr.
1334 * No need to do anything.
1335 */
1336 break;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001337#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001338 case R_X86_64_JUMP_SLOT:
1339 case R_X86_64_GLOB_DAT:
1340 case R_X86_64_32:
1341 case R_X86_64_64:
1342 case R_X86_64_RELATIVE:
1343 case R_X86_64_IRELATIVE:
1344 // No need to do anything.
1345 break;
1346 case R_X86_64_PC32:
1347 sym_addr = reloc;
1348 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001349#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001350 default:
1351 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
1352 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001353 }
1354 } else {
1355 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001356 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001357 }
1358 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001359 }
1360
1361 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001362 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001363 count_relocation(kRelocAbsolute);
1364 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001365 TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
1366 reloc, (sym_addr + rela->r_addend), sym_name);
1367 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001368 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001369 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001370 count_relocation(kRelocAbsolute);
1371 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001372 TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
1373 reloc, (sym_addr + rela->r_addend), sym_name);
1374 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001375 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001376 case R_GENERIC_RELATIVE:
1377 count_relocation(kRelocRelative);
1378 MARK(rela->r_offset);
1379 if (sym) {
1380 DL_ERR("error: encountered _RELATIVE relocation with a symbol");
1381 return -1;
1382 }
1383 TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
1384 reloc, (base + rela->r_addend));
1385 *reinterpret_cast<ElfW(Addr)*>(reloc) = (base + rela->r_addend);
1386 break;
1387
1388 case R_GENERIC_IRELATIVE:
1389 count_relocation(kRelocRelative);
1390 MARK(rela->r_offset);
1391 TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
1392 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
1393 break;
1394
1395#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001396 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001397 count_relocation(kRelocAbsolute);
1398 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001399 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
1400 reloc, (sym_addr + rela->r_addend), sym_name);
1401 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001402 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001403 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001404 count_relocation(kRelocAbsolute);
1405 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001406 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
1407 reloc, (sym_addr + rela->r_addend), sym_name);
1408 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1409 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001410 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001411 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001412 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1413 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1414 static_cast<ElfW(Addr)>(INT32_MIN),
1415 static_cast<ElfW(Addr)>(UINT32_MAX));
1416 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001417 }
1418 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001419 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001420 count_relocation(kRelocAbsolute);
1421 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001422 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
1423 reloc, (sym_addr + rela->r_addend), sym_name);
1424 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1425 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001426 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001427 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001428 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1429 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1430 static_cast<ElfW(Addr)>(INT16_MIN),
1431 static_cast<ElfW(Addr)>(UINT16_MAX));
1432 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001433 }
1434 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001435 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001436 count_relocation(kRelocRelative);
1437 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001438 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
1439 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1440 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001441 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001442 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001443 count_relocation(kRelocRelative);
1444 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001445 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
1446 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1447 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1448 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001449 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001450 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001451 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1452 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1453 static_cast<ElfW(Addr)>(INT32_MIN),
1454 static_cast<ElfW(Addr)>(UINT32_MAX));
1455 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001456 }
1457 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001458 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001459 count_relocation(kRelocRelative);
1460 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001461 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
1462 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1463 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1464 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001465 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001466 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001467 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1468 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1469 static_cast<ElfW(Addr)>(INT16_MIN),
1470 static_cast<ElfW(Addr)>(UINT16_MAX));
1471 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001472 }
1473 break;
1474
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001475 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001476 /*
1477 * ET_EXEC is not supported so this should not happen.
1478 *
1479 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1480 *
1481 * Section 4.7.1.10 "Dynamic relocations"
1482 * R_AARCH64_COPY may only appear in executable objects where e_type is
1483 * set to ET_EXEC.
1484 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001485 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Nick Kralevich76e289c2014-07-03 12:04:31 -07001486 return -1;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001487 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001488 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
1489 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001490 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001491 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001492 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
1493 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001494 break;
1495#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001496 case R_X86_64_32:
1497 count_relocation(kRelocRelative);
1498 MARK(rela->r_offset);
1499 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1500 static_cast<size_t>(sym_addr), sym_name);
1501 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1502 break;
1503 case R_X86_64_64:
1504 count_relocation(kRelocRelative);
1505 MARK(rela->r_offset);
1506 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1507 static_cast<size_t>(sym_addr), sym_name);
1508 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1509 break;
1510 case R_X86_64_PC32:
1511 count_relocation(kRelocRelative);
1512 MARK(rela->r_offset);
1513 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1514 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1515 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
1516 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
1517 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001518#endif
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001519
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001520 default:
1521 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
1522 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001523 }
1524 }
1525 return 0;
1526}
Chris Dearman99186652014-02-06 20:36:51 -08001527
1528#else // REL, not RELA.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001529int soinfo::relocate(ElfW(Rel)* rel, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001530 for (size_t idx = 0; idx < count; ++idx, ++rel) {
1531 unsigned type = ELFW(R_TYPE)(rel->r_info);
1532 // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
1533 unsigned sym = ELFW(R_SYM)(rel->r_info);
1534 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
1535 ElfW(Addr) sym_addr = 0;
1536 const char* sym_name = nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001537
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001538 DEBUG("Processing '%s' relocation at index %zd", name, idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001539 if (type == R_GENERIC_NONE) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001540 continue;
1541 }
1542
1543 ElfW(Sym)* s = nullptr;
1544 soinfo* lsi = nullptr;
1545
1546 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001547 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001548 s = soinfo_do_lookup(this, sym_name, &lsi, global_group, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001549 if (s == nullptr) {
1550 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001551 s = &symtab_[sym];
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001552 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1553 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
1554 return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001555 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001556
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001557 /* IHI0044C AAELF 4.5.1.1:
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001558
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001559 Libraries are not searched to resolve weak references.
1560 It is not an error for a weak reference to remain
1561 unsatisfied.
Doug Kwane8238072009-10-26 12:05:23 -07001562
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001563 During linking, the value of an undefined weak reference is:
1564 - Zero if the relocation type is absolute
1565 - The address of the place if the relocation is pc-relative
1566 - The address of nominal base address if the relocation
1567 type is base-relative.
1568 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001569
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001570 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001571#if !defined(__mips__)
1572 case R_GENERIC_JUMP_SLOT:
1573 case R_GENERIC_GLOB_DAT:
1574 case R_GENERIC_RELATIVE:
1575 case R_GENERIC_IRELATIVE:
1576#endif
1577
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001578#if defined(__arm__)
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001579 case R_ARM_ABS32: /* Don't care. */
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001580 // sym_addr was initialized to be zero above or relocation
1581 // code below does not care about value of sym_addr.
1582 // No need to do anything.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001583 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001584#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001585 case R_386_32:
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001586 // sym_addr was initialized to be zero above or relocation
1587 // code below does not care about value of sym_addr.
1588 // No need to do anything.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001589 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001590 case R_386_PC32:
1591 sym_addr = reloc;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001592 break;
1593#endif
1594
1595#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001596 case R_ARM_COPY:
1597 // Fall through. Can't really copy if weak symbol is not found at run-time.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001598#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001599 default:
1600 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001601 return -1;
1602 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001603 } else {
1604 // We got a definition.
1605 sym_addr = lsi->resolve_symbol_address(s);
1606 }
1607 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001608 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001609
1610 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001611#if !defined(__mips__)
1612 case R_GENERIC_JUMP_SLOT:
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001613 count_relocation(kRelocAbsolute);
1614 MARK(rel->r_offset);
1615 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1616 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1617 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001618
1619 case R_GENERIC_GLOB_DAT:
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001620 count_relocation(kRelocAbsolute);
1621 MARK(rel->r_offset);
1622 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1623 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1624 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001625
1626 case R_GENERIC_RELATIVE:
1627 count_relocation(kRelocRelative);
1628 MARK(rel->r_offset);
1629 if (sym) {
1630 DL_ERR("odd RELATIVE form...");
1631 return -1;
1632 }
1633 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1634 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1635 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1636 break;
1637
1638 case R_GENERIC_IRELATIVE:
1639 count_relocation(kRelocRelative);
1640 MARK(rel->r_offset);
1641 TRACE_TYPE(RELO, "RELO IRELATIVE %p <- %p", reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1642 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + *reinterpret_cast<ElfW(Addr)*>(reloc));
1643 break;
1644#endif
1645
1646#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001647 case R_ARM_ABS32:
1648 count_relocation(kRelocAbsolute);
1649 MARK(rel->r_offset);
1650 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1651 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1652 break;
1653 case R_ARM_REL32:
1654 count_relocation(kRelocRelative);
1655 MARK(rel->r_offset);
1656 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1657 reloc, sym_addr, rel->r_offset, sym_name);
1658 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1659 break;
1660 case R_ARM_COPY:
1661 /*
1662 * ET_EXEC is not supported so this should not happen.
1663 *
1664 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1665 *
1666 * Section 4.7.1.10 "Dynamic relocations"
1667 * R_ARM_COPY may only appear in executable objects where e_type is
1668 * set to ET_EXEC.
1669 */
1670 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
1671 return -1;
1672#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001673 case R_386_32:
1674 count_relocation(kRelocRelative);
1675 MARK(rel->r_offset);
1676 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1677 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1678 break;
1679 case R_386_PC32:
1680 count_relocation(kRelocRelative);
1681 MARK(rel->r_offset);
1682 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1683 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1684 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1685 break;
1686#elif defined(__mips__)
1687 case R_MIPS_REL32:
1688#if defined(__LP64__)
1689 // MIPS Elf64_Rel entries contain compound relocations
1690 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
1691 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
1692 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
1693 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
1694 type, (unsigned)ELF64_R_TYPE2(rel->r_info),
1695 (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
1696 return -1;
1697 }
1698#endif
1699 count_relocation(kRelocAbsolute);
1700 MARK(rel->r_offset);
1701 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
1702 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
1703 if (s) {
1704 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1705 } else {
1706 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1707 }
1708 break;
1709#endif
1710
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001711 default:
1712 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
1713 return -1;
1714 }
1715 }
1716 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001717}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001718#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001719
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001720#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001721bool soinfo::mips_relocate_got(const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001722 ElfW(Addr)** got = plt_got_;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001723 if (got == nullptr) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001724 return true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001725 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001726
1727 // got[0] is the address of the lazy resolver function.
1728 // got[1] may be used for a GNU extension.
1729 // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
1730 // FIXME: maybe this should be in a separate routine?
Dmitriy Ivanov20463e32014-12-02 13:27:40 -08001731 if ((flags_ & FLAG_LINKER) == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001732 size_t g = 0;
1733 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
1734 if (reinterpret_cast<intptr_t>(got[g]) < 0) {
1735 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
1736 }
1737 // Relocate the local GOT entries.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001738 for (; g < mips_local_gotno_; g++) {
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001739 got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + load_bias);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001740 }
1741 }
1742
1743 // Now for the global GOT entries...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001744 ElfW(Sym)* sym = symtab_ + mips_gotsym_;
1745 got = plt_got_ + mips_local_gotno_;
1746 for (size_t g = mips_gotsym_; g < mips_symtabno_; g++, sym++, got++) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001747 // This is an undefined reference... try to locate it.
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001748 const char* sym_name = get_string(sym->st_name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001749 soinfo* lsi = nullptr;
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001750 ElfW(Sym)* s = soinfo_do_lookup(this, sym_name, &lsi, global_group, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001751 if (s == nullptr) {
1752 // We only allow an undefined symbol if this is a weak reference.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001753 s = &symtab_[g];
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001754 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1755 DL_ERR("cannot locate \"%s\"...", sym_name);
1756 return false;
1757 }
1758 *got = 0;
1759 } else {
1760 // FIXME: is this sufficient?
1761 // For reference see NetBSD link loader
1762 // 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
1763 *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s));
1764 }
1765 }
1766 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001767}
1768#endif
1769
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001770void soinfo::call_array(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001771 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001772 return;
1773 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001774
Elliott Hughesc6200592013-09-30 18:43:46 -07001775 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001776
1777 int begin = reverse ? (count - 1) : 0;
1778 int end = reverse ? -1 : count;
1779 int step = reverse ? -1 : 1;
1780
1781 for (int i = begin; i != end; i += step) {
1782 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001783 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001784 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001785
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001786 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001787}
1788
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001789void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001790 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001791 return;
1792 }
1793
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001794 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001795 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001796 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001797
1798 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1799 // are still writable. This happens with our debug malloc (see http://b/7941716).
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001800 protect_data(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001801}
1802
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001803void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001804 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1805 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001806 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001807}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001808
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001809void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001810 if (constructors_called) {
1811 return;
1812 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001813
Elliott Hughesd23736e2012-11-01 15:16:56 -07001814 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1815 // protect against recursive constructor calls. One simple example of constructor recursion
1816 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1817 // 1. The program depends on libc, so libc's constructor is called here.
1818 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1819 // 3. dlopen() calls the constructors on the newly created
1820 // soinfo for libc_malloc_debug_leak.so.
1821 // 4. The debug .so depends on libc, so CallConstructors is
1822 // called again with the libc soinfo. If it doesn't trigger the early-
1823 // out above, the libc constructor will be called again (recursively!).
1824 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001825
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001826 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001827 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001828 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001829 name, preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001830 }
1831
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001832 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001833 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001834 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001835
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001836 TRACE("\"%s\": calling constructors", name);
1837
1838 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001839 call_function("DT_INIT", init_func_);
1840 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001841}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001842
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001843void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001844 if (!constructors_called) {
1845 return;
1846 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001847 TRACE("\"%s\": calling destructors", name);
1848
1849 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001850 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001851
1852 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001853 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001854
1855 // This is needed on second call to dlopen
1856 // after library has been unloaded with RTLD_NODELETE
1857 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001858}
1859
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001860void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001861 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001862 child->parents_.push_back(this);
1863 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001864 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001865}
1866
1867void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001868 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001869 return;
1870 }
1871
1872 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001873 children_.for_each([&] (soinfo* child) {
1874 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001875 return parent == this;
1876 });
1877 });
1878
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001879 parents_.for_each([&] (soinfo* parent) {
1880 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001881 return child == this;
1882 });
1883 });
1884
1885 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001886 parents_.clear();
1887 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001888}
1889
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001890dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001891 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001892 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001893 }
1894
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001895 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001896};
1897
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001898ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001899 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001900 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001901 }
1902
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001903 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001904}
1905
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001906off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001907 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001908 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001909 }
1910
1911 return 0;
1912}
1913
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001914uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001915 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001916 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001917 }
1918
1919 return 0;
1920}
1921
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001922uint32_t soinfo::get_dt_flags_1() const {
1923 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001924 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001925 }
1926
1927 return 0;
1928}
1929void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1930 if (has_min_version(1)) {
1931 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001932 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001933 }
1934
1935 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001936 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001937 }
1938
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001939 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001940 }
1941}
1942
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001943// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001944// 'this->flags' does not have FLAG_NEW_SOINFO set.
1945static soinfo::soinfo_list_t g_empty_list;
1946
1947soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001948 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001949 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001950 }
1951
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001952 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001953}
1954
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001955soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001956 if (has_min_version(0)) {
1957 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001958 }
1959
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001960 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001961}
1962
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001963ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1964 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1965 return call_ifunc_resolver(s->st_value + load_bias);
1966 }
1967
1968 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1969}
1970
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001971const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001972 if (has_min_version(1) && (index >= strtab_size_)) {
1973 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001974 }
1975
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001976 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001977}
1978
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001979bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001980 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001981}
1982
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001983bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001984 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001985}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001986
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001987bool soinfo::is_linked() const {
1988 return (flags_ & FLAG_LINKED) != 0;
1989}
1990
1991bool soinfo::is_main_executable() const {
1992 return (flags_ & FLAG_EXE) != 0;
1993}
1994
1995void soinfo::set_linked() {
1996 flags_ |= FLAG_LINKED;
1997}
1998
1999void soinfo::set_linker_flag() {
2000 flags_ |= FLAG_LINKER;
2001}
2002
2003void soinfo::set_main_executable() {
2004 flags_ |= FLAG_EXE;
2005}
2006
2007void soinfo::increment_ref_count() {
2008 local_group_root_->ref_count_++;
2009}
2010
2011size_t soinfo::decrement_ref_count() {
2012 return --local_group_root_->ref_count_;
2013}
2014
2015soinfo* soinfo::get_local_group_root() const {
2016 return local_group_root_;
2017}
2018
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002019/* Force any of the closed stdin, stdout and stderr to be associated with
2020 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07002021static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002022 int dev_null, i, status;
2023 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002024
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002025 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
2026 if (dev_null < 0) {
2027 DL_ERR("cannot open /dev/null: %s", strerror(errno));
2028 return -1;
2029 }
2030 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002031
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002032 /* If any of the stdio file descriptors is valid and not associated
2033 with /dev/null, dup /dev/null to it. */
2034 for (i = 0; i < 3; i++) {
2035 /* If it is /dev/null already, we are done. */
2036 if (i == dev_null) {
2037 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002038 }
2039
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002040 TRACE("[ Nullifying stdio file descriptor %d]", i);
2041 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
2042
2043 /* If file is opened, we are good. */
2044 if (status != -1) {
2045 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002046 }
2047
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002048 /* The only error we allow is that the file descriptor does not
2049 exist, in which case we dup /dev/null to it. */
2050 if (errno != EBADF) {
2051 DL_ERR("fcntl failed: %s", strerror(errno));
2052 return_value = -1;
2053 continue;
2054 }
2055
2056 /* Try dupping /dev/null to this stdio file descriptor and
2057 repeat if there is a signal. Note that any errors in closing
2058 the stdio descriptor are lost. */
2059 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
2060 if (status < 0) {
2061 DL_ERR("dup2 failed: %s", strerror(errno));
2062 return_value = -1;
2063 continue;
2064 }
2065 }
2066
2067 /* If /dev/null is not one of the stdio file descriptors, close it. */
2068 if (dev_null > 2) {
2069 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
2070 status = TEMP_FAILURE_RETRY(close(dev_null));
2071 if (status == -1) {
2072 DL_ERR("close failed: %s", strerror(errno));
2073 return_value = -1;
2074 }
2075 }
2076
2077 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002078}
2079
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002080bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002081 /* Extract dynamic section */
2082 ElfW(Word) dynamic_flags = 0;
2083 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002084
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002085 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002086 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002087 if (!relocating_linker) {
2088 INFO("[ linking %s ]", name);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002089 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002090 }
2091
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002092 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002093 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002094 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002095 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002096 return false;
2097 } else {
2098 if (!relocating_linker) {
2099 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002100 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002101 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002102
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002103#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002104 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2105 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002106#endif
2107
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002108 // Extract useful information from dynamic section.
2109 uint32_t needed_count = 0;
2110 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2111 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2112 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2113 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002114 case DT_SONAME:
2115 // TODO: glibc dynamic linker uses this name for
2116 // initial library lookup; consider doing the same here.
2117 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002118
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002119 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002120 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002121 // in case of --hash-style=both, we prefer gnu
2122 break;
2123 }
2124
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002125 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2126 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2127 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2128 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002129 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002130
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002131 case DT_GNU_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002132 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002133 // in case of --hash-style=both, we prefer gnu
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002134 nchain_ = 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002135 }
2136
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002137 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002138 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002139 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2140 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002141
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002142 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
2143 bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002144 // amend chain for symndx = header[1]
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002145 chain_ = bucket_ + nbucket_ - reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002146
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002147 if (!powerof2(gnu_maskwords_)) {
2148 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two", gnu_maskwords_, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002149 return false;
2150 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002151 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002152
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002153 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002154 break;
2155
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002156 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002157 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002158 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002159
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002160 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002161 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002162 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002163
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002164 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002165 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002166 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002167
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002168 case DT_SYMENT:
2169 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002170 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"", static_cast<size_t>(d->d_un.d_val), name);
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002171 return false;
2172 }
2173 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002174
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002175 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002176#if defined(USE_RELA)
2177 if (d->d_un.d_val != DT_RELA) {
2178 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002179 return false;
2180 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002181#else
2182 if (d->d_un.d_val != DT_REL) {
2183 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
2184 return false;
2185 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002186#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002187 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002188
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002189 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002190#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002191 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002192#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002193 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002194#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002195 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002196
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002197 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002198#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002199 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002200#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002201 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002202#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002203 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002204
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002205 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002206#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002207 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002208 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002209#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002210 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2211 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002212
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002213 case DT_DEBUG:
2214 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2215 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002216// FIXME: not working currently for N64
2217// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002218// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002219// read-only, but the DYNAMIC header claims it is writable.
2220#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002221 if ((dynamic_flags & PF_W) != 0) {
2222 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2223 }
2224 break;
Chris Dearman99186652014-02-06 20:36:51 -08002225#endif
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002226#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002227 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002228 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002229 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002230
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002231 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002232 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002233 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002234
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002235 case DT_RELAENT:
2236 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002237 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002238 return false;
2239 }
2240 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002241
2242 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002243 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002244 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002245
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002246 case DT_REL:
2247 DL_ERR("unsupported DT_REL in \"%s\"", name);
2248 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002249
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002250 case DT_RELSZ:
2251 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2252 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002253#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002254 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002255 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002256 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002257
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002258 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002259 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002260 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002261
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002262 case DT_RELENT:
2263 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002264 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002265 return false;
2266 }
2267 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002268
2269 // "Indicates that all RELATIVE relocations have been concatenated together,
2270 // and specifies the RELATIVE relocation count."
2271 //
2272 // TODO: Spec also mentions that this can be used to optimize relocation process;
2273 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002274 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002275 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002276 case DT_RELA:
2277 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2278 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002279#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002280 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002281 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2282 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002283 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002284
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002285 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002286 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2287 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002288 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002289
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002290 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002291 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2292 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002293 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002294
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002295 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002296 init_array_count_ = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002297 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002298
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002299 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002300 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2301 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002302 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002303
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002304 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002305 fini_array_count_ = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002306 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002307
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002308 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002309 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2310 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002311 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002312
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002313 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002314 preinit_array_count_ = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002315 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002316
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002317 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002318#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002319 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2320 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002321#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002322 has_text_relocations = true;
2323 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002324#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002325
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002326 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002327 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002328 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002329
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002330 case DT_NEEDED:
2331 ++needed_count;
2332 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002333
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002334 case DT_FLAGS:
2335 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002336#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002337 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2338 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002339#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002340 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002341#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002342 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002343 if (d->d_un.d_val & DF_SYMBOLIC) {
2344 has_DT_SYMBOLIC = true;
2345 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002346 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002347
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002348 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002349 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002350
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002351 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002352 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2353 }
2354 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002355#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002356 case DT_MIPS_RLD_MAP:
2357 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2358 {
2359 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2360 *dp = &_r_debug;
2361 }
2362 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002363 case DT_MIPS_RLD_MAP2:
2364 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2365 {
2366 r_debug** dp = reinterpret_cast<r_debug**>(reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
2367 *dp = &_r_debug;
2368 }
2369 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002370
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002371 case DT_MIPS_RLD_VERSION:
2372 case DT_MIPS_FLAGS:
2373 case DT_MIPS_BASE_ADDRESS:
2374 case DT_MIPS_UNREFEXTNO:
2375 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002376
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002377 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002378 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002379 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002380
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002381 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002382 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002383 break;
2384
2385 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002386 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002387 break;
2388#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002389 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2390 case DT_BIND_NOW:
2391 break;
2392
2393 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002394 case DT_VERSYM:
2395 case DT_VERDEF:
2396 case DT_VERDEFNUM:
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002397 case DT_VERNEED:
2398 case DT_VERNEEDNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002399 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002400
2401 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002402 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002403 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002404 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2405 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002406 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002407 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002408 }
2409
2410 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002411 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002412
2413 // Sanity checks.
2414 if (relocating_linker && needed_count != 0) {
2415 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2416 return false;
2417 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002418 if (nbucket_ == 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002419 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" (new hash type from the future?)", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002420 return false;
2421 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002422 if (strtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002423 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2424 return false;
2425 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002426 if (symtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002427 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2428 return false;
2429 }
2430 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002431}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002432
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002433bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group, const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002434
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002435 local_group_root_ = local_group.front();
2436 if (local_group_root_ == nullptr) {
2437 local_group_root_ = this;
2438 }
2439
Elliott Hughese4d792a2013-10-28 14:19:05 -07002440#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002441 if (has_text_relocations) {
2442 // Make segments writable to allow text relocations to work properly. We will later call
2443 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2444 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2445 "security hardening. Please fix.", name);
2446 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2447 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2448 name, strerror(errno));
2449 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002450 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002451 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002452#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002453
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002454#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002455 if (rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002456 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002457 if (relocate(rela_, rela_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002458 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002459 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002460 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002461 if (plt_rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002462 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002463 if (relocate(plt_rela_, plt_rela_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002464 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002465 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002466 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002467#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002468 if (rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002469 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002470 if (relocate(rel_, rel_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002471 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002472 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002473 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002474 if (plt_rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002475 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002476 if (relocate(plt_rel_, plt_rel_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002477 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002478 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002479 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002480#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002481
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002482#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08002483 if (!mips_relocate_got(global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002484 return false;
2485 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002486#endif
2487
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002488 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002489
Elliott Hughese4d792a2013-10-28 14:19:05 -07002490#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002491 if (has_text_relocations) {
2492 // All relocations are done, we can protect our segments back to read-only.
2493 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2494 DL_ERR("can't protect segments for \"%s\": %s",
2495 name, strerror(errno));
2496 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002497 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002498 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002499#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002500
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002501 /* We can also turn on GNU RELRO protection */
2502 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2503 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2504 name, strerror(errno));
2505 return false;
2506 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002507
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002508 /* Handle serializing/sharing the RELRO segment */
2509 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2510 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2511 extinfo->relro_fd) < 0) {
2512 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2513 name, strerror(errno));
2514 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002515 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002516 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2517 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2518 extinfo->relro_fd) < 0) {
2519 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2520 name, strerror(errno));
2521 return false;
2522 }
2523 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002524
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002525 notify_gdb_of_load(this);
2526 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002527}
2528
Nick Kralevich468319c2011-11-11 15:53:17 -08002529/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002530 * This function add vdso to internal dso list.
2531 * It helps to stack unwinding through signal handlers.
2532 * Also, it makes bionic more like glibc.
2533 */
Kito Cheng812fd422014-03-25 22:53:56 +08002534static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002535#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002536 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002537 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002538 return;
2539 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002540
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002541 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002542
Elliott Hughes0266ae52014-02-10 17:46:57 -08002543 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2544 si->phnum = ehdr_vdso->e_phnum;
2545 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2546 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002547 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002548
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002549 si->prelink_image();
2550 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002551#endif
2552}
2553
2554/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002555 * This is linker soinfo for GDB. See details below.
2556 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002557#if defined(__LP64__)
2558#define LINKER_PATH "/system/bin/linker64"
2559#else
2560#define LINKER_PATH "/system/bin/linker"
2561#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002562static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002563
2564/* gdb expects the linker to be in the debug shared object list.
2565 * Without this, gdb has trouble locating the linker's ".text"
2566 * and ".plt" sections. Gdb could also potentially use this to
2567 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2568 * Don't use soinfo_alloc(), because the linker shouldn't
2569 * be on the soinfo list.
2570 */
2571static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002572 linker_soinfo_for_gdb.base = linker_base;
2573
2574 /*
2575 * Set the dynamic field in the link map otherwise gdb will complain with
2576 * the following:
2577 * warning: .dynamic section for "/system/bin/linker" is not at the
2578 * expected address (wrong library or version mismatch?)
2579 */
2580 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2581 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2582 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002583 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002584 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2585}
2586
2587/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002588 * This code is called after the linker has linked itself and
2589 * fixed it's own GOT. It is safe to make references to externs
2590 * and other non-local data at this point.
2591 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002592static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002593#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002594 struct timeval t0, t1;
2595 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002596#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002597
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002598 // Initialize environment functions, and get to the ELF aux vectors table.
2599 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002600
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002601 // If this is a setuid/setgid program, close the security hole described in
2602 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2603 if (get_AT_SECURE()) {
2604 nullify_closed_stdio();
2605 }
2606
2607 debuggerd_init();
2608
2609 // Get a few environment variables.
2610 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2611 if (LD_DEBUG != nullptr) {
2612 g_ld_debug_verbosity = atoi(LD_DEBUG);
2613 }
2614
2615 // Normally, these are cleaned by linker_env_init, but the test
2616 // doesn't cost us anything.
2617 const char* ldpath_env = nullptr;
2618 const char* ldpreload_env = nullptr;
2619 if (!get_AT_SECURE()) {
2620 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2621 ldpreload_env = linker_env_get("LD_PRELOAD");
2622 }
2623
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -08002624#if !defined(__LP64__)
2625 if (personality(PER_LINUX32) == -1) {
2626 __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
2627 }
2628#endif
2629
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002630 INFO("[ android linker & debugger ]");
2631
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002632 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002633 if (si == nullptr) {
2634 exit(EXIT_FAILURE);
2635 }
2636
2637 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002638 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002639 link_map* map = &(si->link_map_head);
2640
2641 map->l_addr = 0;
2642 map->l_name = args.argv[0];
2643 map->l_prev = nullptr;
2644 map->l_next = nullptr;
2645
2646 _r_debug.r_map = map;
2647 r_debug_tail = map;
2648
2649 init_linker_info_for_gdb(linker_base);
2650
2651 // Extract information passed from the kernel.
2652 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2653 si->phnum = args.getauxval(AT_PHNUM);
2654 si->entry = args.getauxval(AT_ENTRY);
2655
2656 /* Compute the value of si->base. We can't rely on the fact that
2657 * the first entry is the PHDR because this will not be true
2658 * for certain executables (e.g. some in the NDK unit test suite)
2659 */
2660 si->base = 0;
2661 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2662 si->load_bias = 0;
2663 for (size_t i = 0; i < si->phnum; ++i) {
2664 if (si->phdr[i].p_type == PT_PHDR) {
2665 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2666 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2667 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002668 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002669 }
2670 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002671
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002672 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2673 if (elf_hdr->e_type != ET_DYN) {
2674 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2675 exit(EXIT_FAILURE);
2676 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002677
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002678 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2679 parse_LD_LIBRARY_PATH(ldpath_env);
2680 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002681
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002682 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002683
Dmitriy Ivanov67181252015-01-07 15:48:25 -08002684 if (!si->prelink_image()) {
2685 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2686 exit(EXIT_FAILURE);
2687 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002688
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002689 // add somain to global group
2690 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2691
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002692 // Load ld_preloads and dependencies.
2693 StringLinkedList needed_library_name_list;
2694 size_t needed_libraries_count = 0;
2695 size_t ld_preloads_count = 0;
2696 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2697 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2698 ++needed_libraries_count;
2699 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002700
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002701 for_each_dt_needed(si, [&](const char* name) {
2702 needed_library_name_list.push_back(name);
2703 ++needed_libraries_count;
2704 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002705
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002706 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002707
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002708 memset(needed_library_names, 0, sizeof(needed_library_names));
2709 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002710
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002711 if (needed_libraries_count > 0 && !find_libraries(si, needed_library_names, needed_libraries_count, nullptr, g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002712 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2713 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002714 } else if (needed_libraries_count == 0) {
2715 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
2716 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2717 exit(EXIT_FAILURE);
2718 }
2719 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002720 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002721
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002722 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002723
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002724 si->call_pre_init_constructors();
Matt Fischer4fd42c12009-12-31 12:09:10 -06002725
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002726 /* After the prelink_image, the si->load_bias is initialized.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002727 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2728 * We need to update this value for so exe here. So Unwind_Backtrace
2729 * for some arch like x86 could work correctly within so exe.
2730 */
2731 map->l_addr = si->load_bias;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002732 si->call_constructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002733
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002734#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002735 gettimeofday(&t1, nullptr);
2736 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2737 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2738 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002739#endif
2740#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002741 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2742 linker_stats.count[kRelocAbsolute],
2743 linker_stats.count[kRelocRelative],
2744 linker_stats.count[kRelocCopy],
2745 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002746#endif
2747#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002748 {
2749 unsigned n;
2750 unsigned i;
2751 unsigned count = 0;
2752 for (n = 0; n < 4096; n++) {
2753 if (bitmask[n]) {
2754 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002755#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002756 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002757#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002758 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002759#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002760 if (x & 1) {
2761 count++;
2762 }
2763 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002764 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002765 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002766 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002767 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2768 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002769#endif
2770
2771#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002772 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002773#endif
2774
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002775 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2776 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002777}
Nick Kralevich468319c2011-11-11 15:53:17 -08002778
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002779/* Compute the load-bias of an existing executable. This shall only
2780 * be used to compute the load bias of an executable or shared library
2781 * that was loaded by the kernel itself.
2782 *
2783 * Input:
2784 * elf -> address of ELF header, assumed to be at the start of the file.
2785 * Return:
2786 * load bias, i.e. add the value of any p_vaddr in the file to get
2787 * the corresponding address in memory.
2788 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002789static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2790 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002791 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002792 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002793
Elliott Hughes0266ae52014-02-10 17:46:57 -08002794 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002795 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002796 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002797 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002798 }
2799 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002800}
2801
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002802extern "C" void _start();
2803
Nick Kralevich468319c2011-11-11 15:53:17 -08002804/*
2805 * This is the entry point for the linker, called from begin.S. This
2806 * method is responsible for fixing the linker's own relocations, and
2807 * then calling __linker_init_post_relocation().
2808 *
2809 * Because this method is called before the linker has fixed it's own
2810 * relocations, any attempt to reference an extern variable, extern
2811 * function, or other GOT reference will generate a segfault.
2812 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002813extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002814 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002815
Elliott Hughes0266ae52014-02-10 17:46:57 -08002816 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002817 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002818 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002819 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002820
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002821 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002822
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002823 // If the linker is not acting as PT_INTERP entry_point is equal to
2824 // _start. Which means that the linker is running as an executable and
2825 // already linked by PT_INTERP.
2826 //
2827 // This happens when user tries to run 'adb shell /system/bin/linker'
2828 // see also https://code.google.com/p/android/issues/detail?id=63174
2829 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2830 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2831 }
2832
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002833 linker_so.base = linker_addr;
2834 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2835 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002836 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002837 linker_so.phdr = phdr;
2838 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002839 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07002840
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002841 // This might not be obvious... The reasons why we pass g_empty_list
2842 // in place of local_group here are (1) we do not really need it, because
2843 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2844 // itself without having to look into local_group and (2) allocators
2845 // are not yet initialized, and therefore we cannot use linked_list.push_*
2846 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002847 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002848 // It would be nice to print an error message, but if the linker
2849 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002850 // call write() (because it involves a GOT reference). We may as
2851 // well try though...
2852 const char* msg = "CANNOT LINK EXECUTABLE: ";
2853 write(2, msg, strlen(msg));
2854 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2855 write(2, "\n", 1);
2856 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002857 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002858
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002859 __libc_init_tls(args);
2860
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002861 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002862 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002863
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002864 // Initialize static variables. Note that in order to
2865 // get correct libdl_info we need to call constructors
2866 // before get_libdl_info().
2867 solist = get_libdl_info();
2868 sonext = get_libdl_info();
2869
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002870 // We have successfully fixed our own relocations. It's safe to run
2871 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002872 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002873 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002874
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002875 protect_data(PROT_READ);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002876
2877 // Return the address that the calling assembly stub should jump to.
2878 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002879}