blob: 87fce95c29af52588618d33a5064fcb3660e74a3 [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"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080053#include "linker_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010055#include "linker_environ.h"
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080056#include "linker_leb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020057#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080058#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080059#include "linker_reloc_iterators.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
62 *
63 * Do NOT use malloc() and friends or pthread_*() code here.
64 * Don't use printf() either; it's caused mysterious memory
65 * corruption in the past.
66 * The linker runs before we bring up libc and it's easiest
67 * to make sure it does not depend on any complex libc features
68 *
69 * open issues / todo:
70 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072 * - after linking, set as much stuff as possible to READONLY
73 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070074 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080076// Override macros to use C++ style casts
77#undef ELF_ST_TYPE
78#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
79
Dmitriy Ivanov489e4982014-05-19 15:19:52 -070080#if defined(__LP64__)
81#define SEARCH_NAME(x) x
82#else
83// Nvidia drivers are relying on the bug:
84// http://code.google.com/p/android/issues/detail?id=6670
85// so we continue to use base-name lookup for lp32
86static const char* get_base_name(const char* name) {
87 const char* bname = strrchr(name, '/');
88 return bname ? bname + 1 : name;
89}
90#define SEARCH_NAME(x) get_base_name(x)
91#endif
92
Elliott Hughes0266ae52014-02-10 17:46:57 -080093static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094
Elliott Hughes1728b232014-05-14 10:02:03 -070095static LinkerAllocator<soinfo> g_soinfo_allocator;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070096static LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020097
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070098static soinfo* solist;
99static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700100static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101
Elliott Hughes1728b232014-05-14 10:02:03 -0700102static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700103#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700104 "/vendor/lib64",
105 "/system/lib64",
106#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700107 "/vendor/lib",
108 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700109#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700110 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700111};
David Bartleybc3a5c22009-06-02 18:27:28 -0700112
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800113#define LDPATH_BUFSIZE (LDPATH_MAX*64)
114#define LDPATH_MAX 8
115
116#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
117#define LDPRELOAD_MAX 8
118
Elliott Hughes1728b232014-05-14 10:02:03 -0700119static char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
120static const char* g_ld_library_paths[LDPATH_MAX + 1];
Elliott Hughes124fae92012-10-31 14:20:03 -0700121
Elliott Hughes1728b232014-05-14 10:02:03 -0700122static char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
123static const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600124
Elliott Hughes1728b232014-05-14 10:02:03 -0700125static soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600126
Elliott Hughes1728b232014-05-14 10:02:03 -0700127__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700129__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700130
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700132struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700133 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700134};
135
136static linker_stats_t linker_stats;
137
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800138void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700139 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700140}
141#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800142void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700143}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144#endif
145
146#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800147uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148#endif
149
Elliott Hughes46882792012-08-03 16:49:39 -0700150// You shouldn't try to call memory-allocating functions in the dynamic linker.
151// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700152#define DISALLOW_ALLOCATION(return_type, name, ...) \
153 return_type name __VA_ARGS__ \
154 { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700155 __libc_fatal("ERROR: " #name " called from the dynamic linker!\n"); \
Dima Zavin2e855792009-05-20 18:28:09 -0700156 }
Kito Cheng812fd422014-03-25 22:53:56 +0800157DISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
158DISALLOW_ALLOCATION(void, free, (void* u __unused));
159DISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
160DISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
Dima Zavin2e855792009-05-20 18:28:09 -0700161
162static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700163
Elliott Hughes650be4e2013-03-05 18:47:58 -0800164char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700165 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700166}
167
Elliott Hughes650be4e2013-03-05 18:47:58 -0800168size_t linker_get_error_buffer_size() {
169 return sizeof(__linker_dl_err_buf);
170}
171
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700172// This function is an empty stub where GDB locates a breakpoint to get notified
173// about linker activity.
Elliott Hughes5419b942012-10-16 15:54:46 -0700174extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
Elliott Hughes1728b232014-05-14 10:02:03 -0700176static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700177static 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 -0800178static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800180static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700181 // Copy the necessary fields into the debug structure.
182 link_map* map = &(info->link_map_head);
183 map->l_addr = info->load_bias;
Dmitriy Ivanovc9d16582014-10-24 14:46:12 -0700184 map->l_name = info->name;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700185 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700187 // Stick the new library at the end of the list.
188 // gdb tends to care more about libc than it does
189 // about leaf libraries, and ordering it this way
190 // reduces the back-and-forth over the wire.
191 if (r_debug_tail) {
192 r_debug_tail->l_next = map;
193 map->l_prev = r_debug_tail;
194 map->l_next = 0;
195 } else {
196 _r_debug.r_map = map;
197 map->l_prev = 0;
198 map->l_next = 0;
199 }
200 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201}
202
Elliott Hughesbedfe382012-08-14 14:07:59 -0700203static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700204 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700205
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700206 if (r_debug_tail == map) {
207 r_debug_tail = map->l_prev;
208 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700209
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700210 if (map->l_prev) {
211 map->l_prev->l_next = map->l_next;
212 }
213 if (map->l_next) {
214 map->l_next->l_prev = map->l_prev;
215 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700216}
217
Elliott Hughesbedfe382012-08-14 14:07:59 -0700218static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800219 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700220 // GDB already knows about the main executable
221 return;
222 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700224 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700226 _r_debug.r_state = r_debug::RT_ADD;
227 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700229 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700231 _r_debug.r_state = r_debug::RT_CONSISTENT;
232 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700233}
234
Elliott Hughesbedfe382012-08-14 14:07:59 -0700235static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800236 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700237 // GDB already knows about the main executable
238 return;
239 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700240
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700241 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700242
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700243 _r_debug.r_state = r_debug::RT_DELETE;
244 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700245
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700246 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700247
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700248 _r_debug.r_state = r_debug::RT_CONSISTENT;
249 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800250}
251
Elliott Hughes18a206c2012-10-29 17:37:13 -0700252void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800253 _r_debug.r_state = r_debug::RT_ADD;
254 rtld_db_dlactivity();
255 _r_debug.r_state = r_debug::RT_CONSISTENT;
256 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800257}
258
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700259LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
260 return g_soinfo_links_allocator.alloc();
261}
262
263void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
264 g_soinfo_links_allocator.free(entry);
265}
266
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700267static 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 +0200268 if (strlen(name) >= SOINFO_NAME_LEN) {
269 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700270 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200271 }
272
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700273 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700274
Magnus Malmbornba98d922012-09-12 13:00:55 +0200275 sonext->next = si;
276 sonext = si;
277
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700278 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200279 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800280}
281
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800282static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700283 if (si == nullptr) {
284 return;
285 }
286
287 if (si->base != 0 && si->size != 0) {
288 munmap(reinterpret_cast<void*>(si->base), si->size);
289 }
290
291 soinfo *prev = nullptr, *trav;
292
293 TRACE("name %s: freeing soinfo @ %p", si->name, si);
294
295 for (trav = solist; trav != nullptr; trav = trav->next) {
296 if (trav == si) {
297 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700298 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700299 prev = trav;
300 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800301
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700302 if (trav == nullptr) {
303 // si was not in solist
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -0800304 DL_ERR("name \"%s\"@%p is not in solist!", si->name, si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700305 return;
306 }
Elliott Hughes46882792012-08-03 16:49:39 -0700307
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700308 // clear links to/from si
309 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700310
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700311 // prev will never be null, because the first entry in solist is
312 // always the static libdl_info.
313 prev->next = si->next;
314 if (si == sonext) {
315 sonext = prev;
316 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800317
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700318 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800319}
320
Elliott Hughescade4c32012-12-20 14:42:14 -0800321static void parse_path(const char* path, const char* delimiters,
322 const char** array, char* buf, size_t buf_size, size_t max_count) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700323 if (path == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800324 return;
325 }
326
327 size_t len = strlcpy(buf, path, buf_size);
328
329 size_t i = 0;
330 char* buf_p = buf;
331 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
332 if (*array[i] != '\0') {
333 ++i;
334 }
335 }
336
337 // Forget the last path if we had to truncate; this occurs if the 2nd to
338 // last char isn't '\0' (i.e. wasn't originally a delimiter).
339 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700340 array[i - 1] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800341 } else {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700342 array[i] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800343 }
344}
345
346static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700347 parse_path(path, ":", g_ld_library_paths,
348 g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800349}
350
351static void parse_LD_PRELOAD(const char* path) {
352 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes1728b232014-05-14 10:02:03 -0700353 parse_path(path, " :", g_ld_preload_names,
354 g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800355}
356
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700357#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700358
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700359// For a given PC, find the .so that it belongs to.
360// Returns the base address of the .ARM.exidx section
361// for that .so, and the number of 8-byte entries
362// in that section (via *pcount).
363//
364// Intended to be called by libc's __gnu_Unwind_Find_exidx().
365//
366// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800367_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800368 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800369
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700370 for (soinfo* si = solist; si != 0; si = si->next) {
371 if ((addr >= si->base) && (addr < (si->base + si->size))) {
372 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800373 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800374 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700375 }
376 *pcount = 0;
377 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800378}
Elliott Hughes46882792012-08-03 16:49:39 -0700379
Christopher Ferris24053a42013-08-19 17:45:09 -0700380#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700381
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700382// Here, we only have to provide a callback to iterate across all the
383// loaded libraries. gcc_eh does the rest.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800384int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700385 int rv = 0;
386 for (soinfo* si = solist; si != nullptr; si = si->next) {
387 dl_phdr_info dl_info;
388 dl_info.dlpi_addr = si->link_map_head.l_addr;
389 dl_info.dlpi_name = si->link_map_head.l_name;
390 dl_info.dlpi_phdr = si->phdr;
391 dl_info.dlpi_phnum = si->phnum;
392 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
393 if (rv != 0) {
394 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800395 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700396 }
397 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398}
Elliott Hughes46882792012-08-03 16:49:39 -0700399
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800400ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name) {
401 return is_gnu_hash() ? gnu_lookup(symbol_name) : elf_lookup(symbol_name);
402}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800403
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800404static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
405 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
406 ELF_ST_BIND(s->st_info) == STB_WEAK) {
407 return s->st_shndx != SHN_UNDEF;
408 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
409 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
410 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->name);
411 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800412
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800413 return false;
414}
415
416ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name) {
417 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800418 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800419
420 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800421 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
422 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800423
424 // test against bloom filter
425 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
426 return nullptr;
427 }
428
429 // bloom test says "probably yes"...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800430 uint32_t n = bucket_[hash % nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800431
432 if (n == 0) {
433 return nullptr;
434 }
435
436 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800437 ElfW(Sym)* s = symtab_ + n;
438 if (((chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800439 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
440 is_symbol_global_and_defined(this, s)) {
441 return s;
442 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800443 } while ((chain_[n++] & 1) == 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800444
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800445 return nullptr;
446}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800447
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800448ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name) {
449 uint32_t hash = symbol_name.elf_hash();
450
451 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800452 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800453
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800454 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
455 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800456 if (strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && is_symbol_global_and_defined(this, s)) {
457 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
458 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
459 static_cast<size_t>(s->st_size));
460 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800461 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800462 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800463
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700464 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800465 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700466
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700467 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800468}
469
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700470soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700471 memset(this, 0, sizeof(*this));
472
473 strlcpy(this->name, name, sizeof(this->name));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800474 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800475 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700476
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700477 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800478 this->st_dev_ = file_stat->st_dev;
479 this->st_ino_ = file_stat->st_ino;
480 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700481 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700482
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800483 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700484}
485
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800486
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800487uint32_t SymbolName::elf_hash() {
488 if (!has_elf_hash_) {
489 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
490 uint32_t h = 0, g;
491
492 while (*name) {
493 h = (h << 4) + *name++;
494 g = h & 0xf0000000;
495 h ^= g;
496 h ^= g >> 24;
497 }
498
499 elf_hash_ = h;
500 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700501 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800502
503 return elf_hash_;
504}
505
506uint32_t SymbolName::gnu_hash() {
507 if (!has_gnu_hash_) {
508 uint32_t h = 5381;
509 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
510 while (*name != 0) {
511 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
512 }
513
514 gnu_hash_ = h;
515 has_gnu_hash_ = true;
516 }
517
518 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800519}
520
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800521ElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700522 const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800523 SymbolName symbol_name(name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700524 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700525
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700526 /* "This element's presence in a shared object library alters the dynamic linker's
527 * symbol resolution algorithm for references within the library. Instead of starting
528 * a symbol search with the executable file, the dynamic linker starts from the shared
529 * object itself. If the shared object fails to supply the referenced symbol, the
530 * dynamic linker then searches the executable file and other shared objects as usual."
531 *
532 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
533 *
534 * Note that this is unlikely since static linker avoids generating
535 * relocations for -Bsymbolic linked dynamic executables.
536 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700537 if (si_from->has_DT_SYMBOLIC) {
538 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->name, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800539 s = si_from->find_symbol_by_name(symbol_name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700540 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700541 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700542 }
543 }
544
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700545 // 1. Look for it in global_group
546 if (s == nullptr) {
547 global_group.visit([&](soinfo* global_si) {
548 DEBUG("%s: looking up %s in %s (from global group)", si_from->name, name, global_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800549 s = global_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700550 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700551 *si_found_in = global_si;
552 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700553 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700554
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700555 return true;
556 });
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700557 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700558
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700559 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700560 if (s == nullptr) {
561 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700562 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700563 // we already did this - skip
564 return true;
565 }
566
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700567 DEBUG("%s: looking up %s in %s (from local group)", si_from->name, name, local_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800568 s = local_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700569 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700570 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700571 return false;
572 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700573
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700574 return true;
575 });
576 }
577
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700578 if (s != nullptr) {
579 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
580 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700581 si_from->name, name, reinterpret_cast<void*>(s->st_value),
582 (*si_found_in)->name, reinterpret_cast<void*>((*si_found_in)->base),
583 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700584 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700585
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700586 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700587}
588
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800589class ProtectedDataGuard {
590 public:
591 ProtectedDataGuard() {
592 if (ref_count_++ == 0) {
593 protect_data(PROT_READ | PROT_WRITE);
594 }
595 }
596
597 ~ProtectedDataGuard() {
598 if (ref_count_ == 0) { // overflow
599 __libc_fatal("Too many nested calls to dlopen()");
600 }
601
602 if (--ref_count_ == 0) {
603 protect_data(PROT_READ);
604 }
605 }
606 private:
607 void protect_data(int protection) {
608 g_soinfo_allocator.protect_all(protection);
609 g_soinfo_links_allocator.protect_all(protection);
610 }
611
612 static size_t ref_count_;
613};
614
615size_t ProtectedDataGuard::ref_count_ = 0;
616
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700617// Each size has it's own allocator.
618template<size_t size>
619class SizeBasedAllocator {
620 public:
621 static void* alloc() {
622 return allocator_.alloc();
623 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700624
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700625 static void free(void* ptr) {
626 allocator_.free(ptr);
627 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700628
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700629 private:
630 static LinkerBlockAllocator allocator_;
631};
632
633template<size_t size>
634LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
635
636template<typename T>
637class TypeBasedAllocator {
638 public:
639 static T* alloc() {
640 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
641 }
642
643 static void free(T* ptr) {
644 SizeBasedAllocator<sizeof(T)>::free(ptr);
645 }
646};
647
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700648class LoadTask {
649 public:
650 struct deleter_t {
651 void operator()(LoadTask* t) {
652 TypeBasedAllocator<LoadTask>::free(t);
653 }
654 };
655
656 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
657
658 static deleter_t deleter;
659
660 static LoadTask* create(const char* name, soinfo* needed_by) {
661 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
662 return new (ptr) LoadTask(name, needed_by);
663 }
664
665 const char* get_name() const {
666 return name_;
667 }
668
669 soinfo* get_needed_by() const {
670 return needed_by_;
671 }
672 private:
673 LoadTask(const char* name, soinfo* needed_by)
674 : name_(name), needed_by_(needed_by) {}
675
676 const char* name_;
677 soinfo* needed_by_;
678
679 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
680};
681
Ningsheng Jiane93be992014-09-16 15:22:10 +0800682LoadTask::deleter_t LoadTask::deleter;
683
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700684template <typename T>
685using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
686
687typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700688typedef linked_list_t<const char> StringLinkedList;
689typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700690
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700691
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700692// This function walks down the tree of soinfo dependencies
693// in breadth-first order and
694// * calls action(soinfo* si) for each node, and
695// * terminates walk if action returns false.
696//
697// walk_dependencies_tree returns false if walk was terminated
698// by the action and true otherwise.
699template<typename F>
700static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700701 SoinfoLinkedList visit_list;
702 SoinfoLinkedList visited;
703
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700704 for (size_t i = 0; i < root_soinfos_size; ++i) {
705 visit_list.push_back(root_soinfos[i]);
706 }
707
708 soinfo* si;
709 while ((si = visit_list.pop_front()) != nullptr) {
710 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700711 continue;
712 }
713
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700714 if (!action(si)) {
715 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700716 }
717
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700718 visited.push_back(si);
719
720 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700721 visit_list.push_back(child);
722 });
723 }
724
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700725 return true;
726}
727
728
729// This is used by dlsym(3). It performs symbol lookup only within the
730// specified soinfo object and its dependencies in breadth first order.
731ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
732 ElfW(Sym)* result = nullptr;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800733 SymbolName symbol_name(name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700734
735
736 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800737 result = current_soinfo->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700738 if (result != nullptr) {
739 *found = current_soinfo;
740 return false;
741 }
742
743 return true;
744 });
745
746 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800747}
748
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800749/* This is used by dlsym(3) to performs a global symbol lookup. If the
750 start value is null (for RTLD_DEFAULT), the search starts at the
751 beginning of the global solist. Otherwise the search starts at the
752 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700753 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700754ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800755 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800756
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700757 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800758 start = solist;
759 }
760
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700761 ElfW(Sym)* s = nullptr;
762 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700763 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
764 continue;
765 }
766
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800767 s = si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700768 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800769 *found = si;
770 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600771 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800772 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600773
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700774 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700775 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
776 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800777 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800778
Elliott Hughescade4c32012-12-20 14:42:14 -0800779 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800780}
781
Kito Chengfa8c05d2013-03-12 14:58:06 +0800782soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800783 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700784 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800785 if (address >= si->base && address - si->base < si->size) {
786 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600787 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800788 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700789 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600790}
791
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800792ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
793 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
794}
795
796static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
797 return sym->st_shndx != SHN_UNDEF &&
798 soaddr >= sym->st_value &&
799 soaddr < sym->st_value + sym->st_size;
800}
801
802ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800803 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800804
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800805 for (size_t i = 0; i < nbucket_; ++i) {
806 uint32_t n = bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800807
808 if (n == 0) {
809 continue;
810 }
811
812 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800813 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800814 if (symbol_matches_soaddr(sym, soaddr)) {
815 return sym;
816 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800817 } while ((chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800818 }
819
820 return nullptr;
821}
822
823ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800824 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600825
Kito Chengfa8c05d2013-03-12 14:58:06 +0800826 // Search the library's symbol table for any defined symbol which
827 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800828 for (size_t i = 0; i < nchain_; ++i) {
829 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800830 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800831 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600832 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800833 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600834
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700835 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600836}
837
Elliott Hughes124fae92012-10-31 14:20:03 -0700838static int open_library_on_path(const char* name, const char* const paths[]) {
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +0000839 char buf[512];
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700840 for (size_t i = 0; paths[i] != nullptr; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800841 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700842 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700843 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700844 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800845 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700846 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
847 if (fd != -1) {
848 return fd;
849 }
850 }
851 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800852}
853
Elliott Hughes124fae92012-10-31 14:20:03 -0700854static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700855 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800856
Elliott Hughes124fae92012-10-31 14:20:03 -0700857 // 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 -0700858 if (strchr(name, '/') != nullptr) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700859 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
860 if (fd != -1) {
861 return fd;
862 }
863 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700864#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700865 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700866#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700867 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800868
Elliott Hughes124fae92012-10-31 14:20:03 -0700869 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Elliott Hughes1728b232014-05-14 10:02:03 -0700870 int fd = open_library_on_path(name, g_ld_library_paths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700871 if (fd == -1) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700872 fd = open_library_on_path(name, kDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700873 }
874 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800875}
876
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700877template<typename F>
878static void for_each_dt_needed(const soinfo* si, F action) {
879 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
880 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700881 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700882 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700883 }
884}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800885
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700886static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700887 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700888 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700889 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700890
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700891 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
892 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700893 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
894 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700895 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700896 } else {
897 // Open the file.
898 fd = open_library(name);
899 if (fd == -1) {
900 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700901 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700902 }
903
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700904 file_guard.reset(fd);
905 }
906
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700907 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700908 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700909 return nullptr;
910 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800911 if (file_offset < 0) {
912 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
913 return nullptr;
914 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700915
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700916 struct stat file_stat;
917 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700918 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700919 return nullptr;
920 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800921 if (file_offset >= file_stat.st_size) {
922 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64, name, file_offset, file_stat.st_size);
923 return nullptr;
924 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700925
926 // Check for symlink and other situations where
927 // file can have different names.
928 for (soinfo* si = solist; si != nullptr; si = si->next) {
929 if (si->get_st_dev() != 0 &&
930 si->get_st_ino() != 0 &&
931 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700932 si->get_st_ino() == file_stat.st_ino &&
933 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700934 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
935 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700936 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700937 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700938
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700939 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700940 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700941 return nullptr;
942 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700943
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700944 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700945 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700946 if (!elf_reader.Load(extinfo)) {
947 return nullptr;
948 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800949
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700950 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700951 if (si == nullptr) {
952 return nullptr;
953 }
954 si->base = elf_reader.load_start();
955 si->size = elf_reader.load_size();
956 si->load_bias = elf_reader.load_bias();
957 si->phnum = elf_reader.phdr_count();
958 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700959
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800960 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700961 soinfo_free(si);
962 return nullptr;
963 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700964
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700965 for_each_dt_needed(si, [&] (const char* name) {
966 load_tasks.push_back(LoadTask::create(name, si));
967 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700968
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700969 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800970}
971
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700972static soinfo *find_loaded_library_by_name(const char* name) {
973 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700974 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700975 if (!strcmp(search_name, si->name)) {
976 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200977 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700978 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700979 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200980}
981
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700982static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200983
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700984 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700985
986 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700987 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700988 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700989 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700990 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700991 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800992
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700993 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800994}
995
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700996static void soinfo_unload(soinfo* si);
997
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700998// TODO: this is slightly unusual way to construct
999// the global group for relocation. Not every RTLD_GLOBAL
1000// library is included in this group for backwards-compatibility
1001// reasons.
1002//
1003// This group consists of the main executable, LD_PRELOADs
1004// and libraries with the DF_1_GLOBAL flag set.
1005static soinfo::soinfo_list_t make_global_group() {
1006 soinfo::soinfo_list_t global_group;
1007 for (soinfo* si = somain; si != nullptr; si = si->next) {
1008 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1009 global_group.push_back(si);
1010 }
1011 }
1012
1013 return global_group;
1014}
1015
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001016static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
1017 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001018 // Step 0: prepare.
1019 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001020 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001021 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001022 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001023 }
1024
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001025 // Construct global_group.
1026 soinfo::soinfo_list_t global_group = make_global_group();
1027
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001028 // If soinfos array is null allocate one on stack.
1029 // The array is needed in case of failure; for example
1030 // when library_names[] = {libone.so, libtwo.so} and libone.so
1031 // is loaded correctly but libtwo.so failed for some reason.
1032 // In this case libone.so should be unloaded on return.
1033 // See also implementation of failure_guard below.
1034
1035 if (soinfos == nullptr) {
1036 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1037 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1038 memset(soinfos, 0, soinfos_size);
1039 }
1040
1041 // list of libraries to link - see step 2.
1042 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001043
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001044 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001045 // Housekeeping
1046 load_tasks.for_each([] (LoadTask* t) {
1047 LoadTask::deleter(t);
1048 });
1049
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001050 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001051 soinfo_unload(soinfos[i]);
1052 }
1053 });
1054
1055 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
1056 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 -07001057 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001058 if (si == nullptr) {
1059 return false;
1060 }
1061
1062 soinfo* needed_by = task->get_needed_by();
1063
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001064 if (needed_by != nullptr) {
1065 needed_by->add_child(si);
1066 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001067
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001068 if (si->is_linked()) {
1069 si->increment_ref_count();
1070 }
1071
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001072 // When ld_preloads is not null, the first
1073 // ld_preloads_count libs are in fact ld_preloads.
1074 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001075 // Add LD_PRELOADed libraries to the global group for future runs.
1076 // There is no need to explicitly add them to the global group
1077 // for this run because they are going to appear in the local
1078 // group in the correct order.
1079 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001080 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001081 }
1082
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001083 if (soinfos_count < library_names_count) {
1084 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001085 }
1086 }
1087
1088 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001089 soinfo::soinfo_list_t local_group;
1090 walk_dependencies_tree(
1091 start_with == nullptr ? soinfos : &start_with,
1092 start_with == nullptr ? soinfos_count : 1,
1093 [&] (soinfo* si) {
1094 local_group.push_back(si);
1095 return true;
1096 });
1097
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001098 // We need to increment ref_count in case
1099 // the root of the local group was not linked.
1100 bool was_local_group_root_linked = local_group.front()->is_linked();
1101
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001102 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001103 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001104 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001105 return false;
1106 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001107 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001108 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001109
1110 return true;
1111 });
1112
1113 if (linked) {
1114 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001115 }
1116
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001117 if (!was_local_group_root_linked) {
1118 local_group.front()->increment_ref_count();
1119 }
1120
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001121 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001122}
1123
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001124static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001125 soinfo* si;
1126
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001127 if (name == nullptr) {
1128 si = somain;
1129 } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001130 return nullptr;
1131 }
1132
Elliott Hughesd23736e2012-11-01 15:16:56 -07001133 return si;
1134}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001135
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001136static void soinfo_unload(soinfo* root) {
1137 // Note that the library can be loaded but not linked;
1138 // in which case there is no root but we still need
1139 // to walk the tree and unload soinfos involved.
1140 //
1141 // This happens on unsuccessful dlopen, when one of
1142 // the DT_NEEDED libraries could not be linked/found.
1143 if (root->is_linked()) {
1144 root = root->get_local_group_root();
1145 }
1146
1147 if (!root->can_unload()) {
1148 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->name);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001149 return;
1150 }
1151
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001152 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001153
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001154 if (ref_count == 0) {
1155 soinfo::soinfo_list_t local_unload_list;
1156 soinfo::soinfo_list_t external_unload_list;
1157 soinfo::soinfo_list_t depth_first_list;
1158 depth_first_list.push_back(root);
1159 soinfo* si = nullptr;
1160
1161 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001162 if (local_unload_list.contains(si)) {
1163 continue;
1164 }
1165
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001166 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001167
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001168 if (si->has_min_version(0)) {
1169 soinfo* child = nullptr;
1170 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001171 TRACE("%s@%p needs to unload %s@%p", si->name, si, child->name, child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001172 if (local_unload_list.contains(child)) {
1173 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001174 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001175 external_unload_list.push_back(child);
1176 } else {
1177 depth_first_list.push_front(child);
1178 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001179 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001180 } else {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001181#ifdef __LP64__
1182 __libc_fatal("soinfo for \"%s\"@%p has no version", si->name, si);
1183#else
1184 PRINT("warning: soinfo for \"%s\"@%p has no version", si->name, si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001185 for_each_dt_needed(si, [&] (const char* library_name) {
1186 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1187 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1188 if (needed != nullptr) {
1189 // Not found: for example if symlink was deleted between dlopen and dlclose
1190 // Since we cannot really handle errors at this point - print and continue.
1191 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
1192 return;
1193 } else if (local_unload_list.contains(needed)) {
1194 // already visited
1195 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001196 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001197 // external group
1198 external_unload_list.push_back(needed);
1199 } else {
1200 // local group
1201 depth_first_list.push_front(needed);
1202 }
1203 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001204#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001205 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001206 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001207
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001208 local_unload_list.for_each([](soinfo* si) {
1209 si->call_destructors();
1210 });
1211
1212 while ((si = local_unload_list.pop_front()) != nullptr) {
1213 notify_gdb_of_unload(si);
1214 soinfo_free(si);
1215 }
1216
1217 while ((si = external_unload_list.pop_front()) != nullptr) {
1218 soinfo_unload(si);
1219 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001220 } else {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001221 TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->name, ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001222 }
1223}
1224
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001225void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001226 // Use basic string manipulation calls to avoid snprintf.
1227 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1228 // When debug malloc is enabled, this call returns 0. This in turn causes
1229 // snprintf to do nothing, which causes libraries to fail to load.
1230 // See b/17302493 for further details.
1231 // Once the above bug is fixed, this code can be modified to use
1232 // snprintf again.
1233 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1234 if (buffer_size < required_len) {
1235 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1236 buffer_size, required_len);
1237 }
1238 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1239 *end = ':';
1240 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001241}
1242
Elliott Hughescade4c32012-12-20 14:42:14 -08001243void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
1244 if (!get_AT_SECURE()) {
1245 parse_LD_LIBRARY_PATH(ld_library_path);
1246 }
1247}
1248
Elliott Hughes1a586292014-06-03 16:23:08 -07001249soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001250 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001251 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001252 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001253 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001254 if (extinfo != nullptr) {
1255 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1256 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1257 return nullptr;
1258 }
1259 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001260 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1261 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 -07001262 return nullptr;
1263 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001264 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001265
1266 ProtectedDataGuard guard;
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001267 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001268 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001269 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001270 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001271 return si;
1272}
1273
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001274void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001275 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001276 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001277}
1278
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001279static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1280 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1281 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1282 ElfW(Addr) ifunc_addr = ifunc_resolver();
1283 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 -07001284
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001285 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001286}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001287
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001288#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001289#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001290static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
1291 return rela->r_addend;
1292}
1293#else
1294static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
1295 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE || ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
1296 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
1297 }
1298 return 0;
1299}
1300#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001301
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001302template<typename ElfRelIteratorT>
1303bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
1304 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
1305 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001306 if (rel == nullptr) {
1307 return false;
1308 }
1309
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001310 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
1311 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
1312
1313 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001314 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001315 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001316 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001317
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001318 DEBUG("Processing '%s' relocation at index %zd", this->name, idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001319 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001320 continue;
1321 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001322
1323 ElfW(Sym)* s = nullptr;
1324 soinfo* lsi = nullptr;
1325
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001326 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001327 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001328 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001329 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001330 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001331 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001332 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001333 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001334 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001335 }
1336
1337 /* IHI0044C AAELF 4.5.1.1:
1338
1339 Libraries are not searched to resolve weak references.
1340 It is not an error for a weak reference to remain unsatisfied.
1341
1342 During linking, the value of an undefined weak reference is:
1343 - Zero if the relocation type is absolute
1344 - The address of the place if the relocation is pc-relative
1345 - The address of nominal base address if the relocation
1346 type is base-relative.
1347 */
1348
1349 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001350 case R_GENERIC_JUMP_SLOT:
1351 case R_GENERIC_GLOB_DAT:
1352 case R_GENERIC_RELATIVE:
1353 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001354#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001355 case R_AARCH64_ABS64:
1356 case R_AARCH64_ABS32:
1357 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001358#elif defined(__x86_64__)
1359 case R_X86_64_32:
1360 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001361#elif defined(__arm__)
1362 case R_ARM_ABS32:
1363#elif defined(__i386__)
1364 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001365#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001366 /*
1367 * The sym_addr was initialized to be zero above, or the relocation
1368 * code below does not care about value of sym_addr.
1369 * No need to do anything.
1370 */
1371 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001372#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00001373 case R_X86_64_PC32:
1374 sym_addr = reloc;
1375 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001376#elif defined(__i386__)
1377 case R_386_PC32:
1378 sym_addr = reloc;
1379 break;
1380#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001381 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001382 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001383 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001384 }
1385 } else {
1386 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001387 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001388 }
1389 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001390 }
1391
1392 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001393 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001394 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001395 MARK(rel->r_offset);
1396 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
1397 reinterpret_cast<void*>(reloc),
1398 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1399
1400 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001401 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001402 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001403 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001404 MARK(rel->r_offset);
1405 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
1406 reinterpret_cast<void*>(reloc),
1407 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1408 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001409 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001410 case R_GENERIC_RELATIVE:
1411 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001412 MARK(rel->r_offset);
1413 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
1414 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001415 reinterpret_cast<void*>(load_bias + addend));
1416 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001417 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001418 case R_GENERIC_IRELATIVE:
1419 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001420 MARK(rel->r_offset);
1421 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
1422 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001423 reinterpret_cast<void*>(load_bias + addend));
1424 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001425 break;
1426
1427#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001428 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001429 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001430 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001431 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001432 reloc, (sym_addr + addend), sym_name);
1433 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001434 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001435 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001436 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001437 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001438 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001439 reloc, (sym_addr + addend), sym_name);
1440 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1441 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1442 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001443 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001444 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001445 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001446 static_cast<ElfW(Addr)>(INT32_MIN),
1447 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001448 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001449 }
1450 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001451 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001452 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001453 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001454 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001455 reloc, (sym_addr + addend), sym_name);
1456 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1457 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1458 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001459 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001460 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001461 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001462 static_cast<ElfW(Addr)>(INT16_MIN),
1463 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001464 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001465 }
1466 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001467 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001468 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001469 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001470 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001471 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1472 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend) - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001473 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001474 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001475 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001476 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001477 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001478 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1479 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1480 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1481 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001482 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001483 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001484 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001485 static_cast<ElfW(Addr)>(INT32_MIN),
1486 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001487 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001488 }
1489 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001490 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001491 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001492 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001493 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001494 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1495 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1496 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1497 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001498 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001499 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001500 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001501 static_cast<ElfW(Addr)>(INT16_MIN),
1502 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001503 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001504 }
1505 break;
1506
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001507 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001508 /*
1509 * ET_EXEC is not supported so this should not happen.
1510 *
1511 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1512 *
1513 * Section 4.7.1.10 "Dynamic relocations"
1514 * R_AARCH64_COPY may only appear in executable objects where e_type is
1515 * set to ET_EXEC.
1516 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001517 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001518 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001519 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001520 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001521 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001522 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001523 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001524 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001525 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001526 break;
1527#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001528 case R_X86_64_32:
1529 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001530 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001531 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1532 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001533 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001534 break;
1535 case R_X86_64_64:
1536 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001537 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001538 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1539 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001540 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001541 break;
1542 case R_X86_64_PC32:
1543 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001544 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001545 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1546 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1547 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001548 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001549 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001550#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001551 case R_ARM_ABS32:
1552 count_relocation(kRelocAbsolute);
1553 MARK(rel->r_offset);
1554 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1555 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1556 break;
1557 case R_ARM_REL32:
1558 count_relocation(kRelocRelative);
1559 MARK(rel->r_offset);
1560 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1561 reloc, sym_addr, rel->r_offset, sym_name);
1562 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1563 break;
1564 case R_ARM_COPY:
1565 /*
1566 * ET_EXEC is not supported so this should not happen.
1567 *
1568 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1569 *
1570 * Section 4.7.1.10 "Dynamic relocations"
1571 * R_ARM_COPY may only appear in executable objects where e_type is
1572 * set to ET_EXEC.
1573 */
1574 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001575 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001576#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001577 case R_386_32:
1578 count_relocation(kRelocRelative);
1579 MARK(rel->r_offset);
1580 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1581 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1582 break;
1583 case R_386_PC32:
1584 count_relocation(kRelocRelative);
1585 MARK(rel->r_offset);
1586 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1587 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1588 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1589 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001590#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001591 default:
1592 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001593 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001594 }
1595 }
1596 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001597}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001598#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001599
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001600void soinfo::call_array(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001601 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001602 return;
1603 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001604
Elliott Hughesc6200592013-09-30 18:43:46 -07001605 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001606
1607 int begin = reverse ? (count - 1) : 0;
1608 int end = reverse ? -1 : count;
1609 int step = reverse ? -1 : 1;
1610
1611 for (int i = begin; i != end; i += step) {
1612 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001613 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001614 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001615
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001616 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001617}
1618
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001619void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001620 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001621 return;
1622 }
1623
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001624 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001625 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001626 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001627}
1628
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001629void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001630 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1631 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001632 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001633}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001634
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001635void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001636 if (constructors_called) {
1637 return;
1638 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001639
Elliott Hughesd23736e2012-11-01 15:16:56 -07001640 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1641 // protect against recursive constructor calls. One simple example of constructor recursion
1642 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1643 // 1. The program depends on libc, so libc's constructor is called here.
1644 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1645 // 3. dlopen() calls the constructors on the newly created
1646 // soinfo for libc_malloc_debug_leak.so.
1647 // 4. The debug .so depends on libc, so CallConstructors is
1648 // called again with the libc soinfo. If it doesn't trigger the early-
1649 // out above, the libc constructor will be called again (recursively!).
1650 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001651
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001652 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001653 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001654 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001655 name, preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001656 }
1657
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001658 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001659 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001660 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001661
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001662 TRACE("\"%s\": calling constructors", name);
1663
1664 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001665 call_function("DT_INIT", init_func_);
1666 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001667}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001668
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001669void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001670 if (!constructors_called) {
1671 return;
1672 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001673 TRACE("\"%s\": calling destructors", name);
1674
1675 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001676 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001677
1678 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001679 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001680
1681 // This is needed on second call to dlopen
1682 // after library has been unloaded with RTLD_NODELETE
1683 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001684}
1685
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001686void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001687 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001688 child->parents_.push_back(this);
1689 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001690 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001691}
1692
1693void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001694 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001695 return;
1696 }
1697
1698 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001699 children_.for_each([&] (soinfo* child) {
1700 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001701 return parent == this;
1702 });
1703 });
1704
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001705 parents_.for_each([&] (soinfo* parent) {
1706 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001707 return child == this;
1708 });
1709 });
1710
1711 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001712 parents_.clear();
1713 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001714}
1715
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001716dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001717 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001718 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001719 }
1720
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001721 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001722};
1723
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001724ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001725 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001726 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001727 }
1728
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001729 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001730}
1731
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001732off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001733 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001734 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001735 }
1736
1737 return 0;
1738}
1739
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001740uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001741 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001742 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001743 }
1744
1745 return 0;
1746}
1747
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001748uint32_t soinfo::get_dt_flags_1() const {
1749 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001750 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001751 }
1752
1753 return 0;
1754}
1755void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1756 if (has_min_version(1)) {
1757 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001758 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001759 }
1760
1761 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001762 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001763 }
1764
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001765 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001766 }
1767}
1768
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001769// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001770// 'this->flags' does not have FLAG_NEW_SOINFO set.
1771static soinfo::soinfo_list_t g_empty_list;
1772
1773soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001774 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001775 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001776 }
1777
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001778 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001779}
1780
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001781soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001782 if (has_min_version(0)) {
1783 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001784 }
1785
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001786 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001787}
1788
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001789ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1790 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1791 return call_ifunc_resolver(s->st_value + load_bias);
1792 }
1793
1794 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1795}
1796
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001797const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001798 if (has_min_version(1) && (index >= strtab_size_)) {
1799 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001800 }
1801
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001802 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001803}
1804
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001805bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001806 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001807}
1808
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001809bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001810 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001811}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001812
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001813bool soinfo::is_linked() const {
1814 return (flags_ & FLAG_LINKED) != 0;
1815}
1816
1817bool soinfo::is_main_executable() const {
1818 return (flags_ & FLAG_EXE) != 0;
1819}
1820
1821void soinfo::set_linked() {
1822 flags_ |= FLAG_LINKED;
1823}
1824
1825void soinfo::set_linker_flag() {
1826 flags_ |= FLAG_LINKER;
1827}
1828
1829void soinfo::set_main_executable() {
1830 flags_ |= FLAG_EXE;
1831}
1832
1833void soinfo::increment_ref_count() {
1834 local_group_root_->ref_count_++;
1835}
1836
1837size_t soinfo::decrement_ref_count() {
1838 return --local_group_root_->ref_count_;
1839}
1840
1841soinfo* soinfo::get_local_group_root() const {
1842 return local_group_root_;
1843}
1844
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001845/* Force any of the closed stdin, stdout and stderr to be associated with
1846 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001847static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001848 int dev_null, i, status;
1849 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001850
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001851 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
1852 if (dev_null < 0) {
1853 DL_ERR("cannot open /dev/null: %s", strerror(errno));
1854 return -1;
1855 }
1856 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001857
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001858 /* If any of the stdio file descriptors is valid and not associated
1859 with /dev/null, dup /dev/null to it. */
1860 for (i = 0; i < 3; i++) {
1861 /* If it is /dev/null already, we are done. */
1862 if (i == dev_null) {
1863 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001864 }
1865
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001866 TRACE("[ Nullifying stdio file descriptor %d]", i);
1867 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
1868
1869 /* If file is opened, we are good. */
1870 if (status != -1) {
1871 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001872 }
1873
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001874 /* The only error we allow is that the file descriptor does not
1875 exist, in which case we dup /dev/null to it. */
1876 if (errno != EBADF) {
1877 DL_ERR("fcntl failed: %s", strerror(errno));
1878 return_value = -1;
1879 continue;
1880 }
1881
1882 /* Try dupping /dev/null to this stdio file descriptor and
1883 repeat if there is a signal. Note that any errors in closing
1884 the stdio descriptor are lost. */
1885 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
1886 if (status < 0) {
1887 DL_ERR("dup2 failed: %s", strerror(errno));
1888 return_value = -1;
1889 continue;
1890 }
1891 }
1892
1893 /* If /dev/null is not one of the stdio file descriptors, close it. */
1894 if (dev_null > 2) {
1895 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
1896 status = TEMP_FAILURE_RETRY(close(dev_null));
1897 if (status == -1) {
1898 DL_ERR("close failed: %s", strerror(errno));
1899 return_value = -1;
1900 }
1901 }
1902
1903 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001904}
1905
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001906bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08001907 /* Extract dynamic section */
1908 ElfW(Word) dynamic_flags = 0;
1909 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07001910
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001911 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001912 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001913 if (!relocating_linker) {
1914 INFO("[ linking %s ]", name);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001915 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001916 }
1917
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001918 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001919 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001920 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001921 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001922 return false;
1923 } else {
1924 if (!relocating_linker) {
1925 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001926 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001927 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001928
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001929#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001930 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
1931 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001932#endif
1933
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001934 // Extract useful information from dynamic section.
1935 uint32_t needed_count = 0;
1936 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
1937 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1938 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1939 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001940 case DT_SONAME:
1941 // TODO: glibc dynamic linker uses this name for
1942 // initial library lookup; consider doing the same here.
1943 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001944
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001945 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001946 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001947 // in case of --hash-style=both, we prefer gnu
1948 break;
1949 }
1950
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001951 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
1952 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
1953 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
1954 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001955 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001956
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001957 case DT_GNU_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001958 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001959 // in case of --hash-style=both, we prefer gnu
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001960 nchain_ = 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001961 }
1962
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001963 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001964 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001965 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
1966 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001967
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001968 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
1969 bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001970 // amend chain for symndx = header[1]
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001971 chain_ = bucket_ + nbucket_ - reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001972
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001973 if (!powerof2(gnu_maskwords_)) {
1974 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 -08001975 return false;
1976 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001977 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001978
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001979 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001980 break;
1981
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001982 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001983 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001984 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001985
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001986 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001987 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001988 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001989
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001990 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001991 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001992 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001993
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001994 case DT_SYMENT:
1995 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001996 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 -07001997 return false;
1998 }
1999 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002000
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002001 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002002#if defined(USE_RELA)
2003 if (d->d_un.d_val != DT_RELA) {
2004 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002005 return false;
2006 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002007#else
2008 if (d->d_un.d_val != DT_REL) {
2009 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
2010 return false;
2011 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002012#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002013 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002014
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002015 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002016#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002017 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002018#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002019 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002020#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002021 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002022
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002023 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002024#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002025 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002026#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002027 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002028#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002029 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002030
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002031 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002032#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002033 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002034 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002035#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002036 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2037 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002038
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002039 case DT_DEBUG:
2040 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2041 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002042// FIXME: not working currently for N64
2043// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002044// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002045// read-only, but the DYNAMIC header claims it is writable.
2046#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002047 if ((dynamic_flags & PF_W) != 0) {
2048 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2049 }
Chris Dearman99186652014-02-06 20:36:51 -08002050#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002051 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002052#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002053 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002054 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002055 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002056
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002057 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002058 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002059 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002060
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002061 case DT_ANDROID_RELA:
2062 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2063 break;
2064
2065 case DT_ANDROID_RELASZ:
2066 android_relocs_size_ = d->d_un.d_val;
2067 break;
2068
2069 case DT_ANDROID_REL:
2070 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", name);
2071 return false;
2072
2073 case DT_ANDROID_RELSZ:
2074 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", name);
2075 return false;
2076
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002077 case DT_RELAENT:
2078 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002079 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002080 return false;
2081 }
2082 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002083
2084 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002085 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002086 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002087
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002088 case DT_REL:
2089 DL_ERR("unsupported DT_REL in \"%s\"", name);
2090 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002091
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002092 case DT_RELSZ:
2093 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2094 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002095
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002096#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002097 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002098 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002099 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002100
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002101 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002102 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002103 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002104
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002105 case DT_RELENT:
2106 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002107 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002108 return false;
2109 }
2110 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002111
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002112 case DT_ANDROID_REL:
2113 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2114 break;
2115
2116 case DT_ANDROID_RELSZ:
2117 android_relocs_size_ = d->d_un.d_val;
2118 break;
2119
2120 case DT_ANDROID_RELA:
2121 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", name);
2122 return false;
2123
2124 case DT_ANDROID_RELASZ:
2125 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", name);
2126 return false;
2127
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002128 // "Indicates that all RELATIVE relocations have been concatenated together,
2129 // and specifies the RELATIVE relocation count."
2130 //
2131 // TODO: Spec also mentions that this can be used to optimize relocation process;
2132 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002133 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002134 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002135
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002136 case DT_RELA:
2137 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2138 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002139
2140 case DT_RELASZ:
2141 DL_ERR("unsupported DT_RELASZ in \"%s\"", name);
2142 return false;
2143
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002144#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002145 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002146 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2147 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002148 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002149
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002150 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002151 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2152 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002153 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002154
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002155 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002156 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2157 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002158 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002159
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002160 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002161 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002162 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002163
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002164 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002165 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2166 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002167 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002168
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002169 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002170 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002171 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002172
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002173 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002174 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2175 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002176 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002177
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002178 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002179 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002180 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002181
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002182 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002183#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002184 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2185 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002186#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002187 has_text_relocations = true;
2188 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002189#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002190
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002191 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002192 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002193 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002194
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002195 case DT_NEEDED:
2196 ++needed_count;
2197 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002198
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002199 case DT_FLAGS:
2200 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002201#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002202 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2203 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002204#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002205 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002206#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002207 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002208 if (d->d_un.d_val & DF_SYMBOLIC) {
2209 has_DT_SYMBOLIC = true;
2210 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002211 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002212
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002213 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002214 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002215
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002216 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002217 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2218 }
2219 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002220#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002221 case DT_MIPS_RLD_MAP:
2222 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2223 {
2224 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2225 *dp = &_r_debug;
2226 }
2227 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002228 case DT_MIPS_RLD_MAP2:
2229 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2230 {
2231 r_debug** dp = reinterpret_cast<r_debug**>(reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
2232 *dp = &_r_debug;
2233 }
2234 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002235
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002236 case DT_MIPS_RLD_VERSION:
2237 case DT_MIPS_FLAGS:
2238 case DT_MIPS_BASE_ADDRESS:
2239 case DT_MIPS_UNREFEXTNO:
2240 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002241
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002242 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002243 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002244 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002245
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002246 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002247 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002248 break;
2249
2250 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002251 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002252 break;
2253#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002254 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2255 case DT_BIND_NOW:
2256 break;
2257
2258 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002259 case DT_VERSYM:
2260 case DT_VERDEF:
2261 case DT_VERDEFNUM:
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002262 case DT_VERNEED:
2263 case DT_VERNEEDNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002264 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002265
2266 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002267 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002268 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002269 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2270 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002271 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002272 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002273 }
2274
2275 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002276 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002277
2278 // Sanity checks.
2279 if (relocating_linker && needed_count != 0) {
2280 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2281 return false;
2282 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002283 if (nbucket_ == 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002284 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 -07002285 return false;
2286 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002287 if (strtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002288 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2289 return false;
2290 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002291 if (symtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002292 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2293 return false;
2294 }
2295 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002296}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002297
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002298bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
2299 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002300
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002301 local_group_root_ = local_group.front();
2302 if (local_group_root_ == nullptr) {
2303 local_group_root_ = this;
2304 }
2305
Elliott Hughese4d792a2013-10-28 14:19:05 -07002306#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002307 if (has_text_relocations) {
2308 // Make segments writable to allow text relocations to work properly. We will later call
2309 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2310 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2311 "security hardening. Please fix.", name);
2312 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2313 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2314 name, strerror(errno));
2315 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002316 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002317 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002318#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002319
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002320 if (android_relocs_ != nullptr) {
2321 // check signature
2322 if (android_relocs_size_ > 3 &&
2323 android_relocs_[0] == 'A' &&
2324 android_relocs_[1] == 'P' &&
2325 (android_relocs_[2] == 'U' || android_relocs_[2] == 'S') &&
2326 android_relocs_[3] == '2') {
2327 DEBUG("[ android relocating %s ]", name);
2328
2329 bool relocated = false;
2330 const uint8_t* packed_relocs = android_relocs_ + 4;
2331 const size_t packed_relocs_size = android_relocs_size_ - 4;
2332
2333 if (android_relocs_[2] == 'U') {
2334 relocated = relocate(
2335 packed_reloc_iterator<leb128_decoder>(
2336 leb128_decoder(packed_relocs, packed_relocs_size)),
2337 global_group, local_group);
2338 } else { // android_relocs_[2] == 'S'
2339 relocated = relocate(
2340 packed_reloc_iterator<sleb128_decoder>(
2341 sleb128_decoder(packed_relocs, packed_relocs_size)),
2342 global_group, local_group);
2343 }
2344
2345 if (!relocated) {
2346 return false;
2347 }
2348 } else {
2349 DL_ERR("bad android relocation header.");
2350 return false;
2351 }
2352 }
2353
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002354#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002355 if (rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002356 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002357 if (!relocate(plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002358 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002359 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002360 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002361 if (plt_rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002362 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002363 if (!relocate(plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002364 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002365 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002366 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002367#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002368 if (rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002369 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002370 if (!relocate(plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002371 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002372 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002373 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002374 if (plt_rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002375 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002376 if (!relocate(plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002377 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002378 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002379 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002380#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002381
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002382#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08002383 if (!mips_relocate_got(global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002384 return false;
2385 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002386#endif
2387
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002388 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002389
Elliott Hughese4d792a2013-10-28 14:19:05 -07002390#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002391 if (has_text_relocations) {
2392 // All relocations are done, we can protect our segments back to read-only.
2393 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2394 DL_ERR("can't protect segments for \"%s\": %s",
2395 name, strerror(errno));
2396 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002397 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002398 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002399#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002400
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002401 /* We can also turn on GNU RELRO protection */
2402 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2403 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2404 name, strerror(errno));
2405 return false;
2406 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002407
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002408 /* Handle serializing/sharing the RELRO segment */
2409 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2410 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2411 extinfo->relro_fd) < 0) {
2412 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2413 name, strerror(errno));
2414 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002415 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002416 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2417 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2418 extinfo->relro_fd) < 0) {
2419 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2420 name, strerror(errno));
2421 return false;
2422 }
2423 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002424
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002425 notify_gdb_of_load(this);
2426 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002427}
2428
Nick Kralevich468319c2011-11-11 15:53:17 -08002429/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002430 * This function add vdso to internal dso list.
2431 * It helps to stack unwinding through signal handlers.
2432 * Also, it makes bionic more like glibc.
2433 */
Kito Cheng812fd422014-03-25 22:53:56 +08002434static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002435#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002436 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002437 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002438 return;
2439 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002440
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002441 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002442
Elliott Hughes0266ae52014-02-10 17:46:57 -08002443 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2444 si->phnum = ehdr_vdso->e_phnum;
2445 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2446 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002447 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002448
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002449 si->prelink_image();
2450 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002451#endif
2452}
2453
2454/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002455 * This is linker soinfo for GDB. See details below.
2456 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002457#if defined(__LP64__)
2458#define LINKER_PATH "/system/bin/linker64"
2459#else
2460#define LINKER_PATH "/system/bin/linker"
2461#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002462static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002463
2464/* gdb expects the linker to be in the debug shared object list.
2465 * Without this, gdb has trouble locating the linker's ".text"
2466 * and ".plt" sections. Gdb could also potentially use this to
2467 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2468 * Don't use soinfo_alloc(), because the linker shouldn't
2469 * be on the soinfo list.
2470 */
2471static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002472 linker_soinfo_for_gdb.base = linker_base;
2473
2474 /*
2475 * Set the dynamic field in the link map otherwise gdb will complain with
2476 * the following:
2477 * warning: .dynamic section for "/system/bin/linker" is not at the
2478 * expected address (wrong library or version mismatch?)
2479 */
2480 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2481 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2482 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002483 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002484 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2485}
2486
2487/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002488 * This code is called after the linker has linked itself and
2489 * fixed it's own GOT. It is safe to make references to externs
2490 * and other non-local data at this point.
2491 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002492static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002493#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002494 struct timeval t0, t1;
2495 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002496#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002497
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002498 // Initialize environment functions, and get to the ELF aux vectors table.
2499 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002500
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002501 // If this is a setuid/setgid program, close the security hole described in
2502 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2503 if (get_AT_SECURE()) {
2504 nullify_closed_stdio();
2505 }
2506
2507 debuggerd_init();
2508
2509 // Get a few environment variables.
2510 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2511 if (LD_DEBUG != nullptr) {
2512 g_ld_debug_verbosity = atoi(LD_DEBUG);
2513 }
2514
2515 // Normally, these are cleaned by linker_env_init, but the test
2516 // doesn't cost us anything.
2517 const char* ldpath_env = nullptr;
2518 const char* ldpreload_env = nullptr;
2519 if (!get_AT_SECURE()) {
2520 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2521 ldpreload_env = linker_env_get("LD_PRELOAD");
2522 }
2523
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -08002524#if !defined(__LP64__)
2525 if (personality(PER_LINUX32) == -1) {
2526 __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
2527 }
2528#endif
2529
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002530 INFO("[ android linker & debugger ]");
2531
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002532 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002533 if (si == nullptr) {
2534 exit(EXIT_FAILURE);
2535 }
2536
2537 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002538 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002539 link_map* map = &(si->link_map_head);
2540
2541 map->l_addr = 0;
2542 map->l_name = args.argv[0];
2543 map->l_prev = nullptr;
2544 map->l_next = nullptr;
2545
2546 _r_debug.r_map = map;
2547 r_debug_tail = map;
2548
2549 init_linker_info_for_gdb(linker_base);
2550
2551 // Extract information passed from the kernel.
2552 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2553 si->phnum = args.getauxval(AT_PHNUM);
2554 si->entry = args.getauxval(AT_ENTRY);
2555
2556 /* Compute the value of si->base. We can't rely on the fact that
2557 * the first entry is the PHDR because this will not be true
2558 * for certain executables (e.g. some in the NDK unit test suite)
2559 */
2560 si->base = 0;
2561 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2562 si->load_bias = 0;
2563 for (size_t i = 0; i < si->phnum; ++i) {
2564 if (si->phdr[i].p_type == PT_PHDR) {
2565 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2566 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2567 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002568 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002569 }
2570 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002571
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002572 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2573 if (elf_hdr->e_type != ET_DYN) {
2574 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2575 exit(EXIT_FAILURE);
2576 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002577
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002578 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2579 parse_LD_LIBRARY_PATH(ldpath_env);
2580 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002581
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002582 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002583
Dmitriy Ivanov67181252015-01-07 15:48:25 -08002584 if (!si->prelink_image()) {
2585 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2586 exit(EXIT_FAILURE);
2587 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002588
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002589 // add somain to global group
2590 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2591
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002592 // Load ld_preloads and dependencies.
2593 StringLinkedList needed_library_name_list;
2594 size_t needed_libraries_count = 0;
2595 size_t ld_preloads_count = 0;
2596 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2597 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2598 ++needed_libraries_count;
2599 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002600
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002601 for_each_dt_needed(si, [&](const char* name) {
2602 needed_library_name_list.push_back(name);
2603 ++needed_libraries_count;
2604 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002605
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002606 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002607
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002608 memset(needed_library_names, 0, sizeof(needed_library_names));
2609 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002610
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002611 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 -07002612 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2613 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002614 } else if (needed_libraries_count == 0) {
2615 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
2616 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2617 exit(EXIT_FAILURE);
2618 }
2619 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002620 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002621
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002622 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002623
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002624 {
2625 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06002626
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002627 si->call_pre_init_constructors();
2628
2629 /* After the prelink_image, the si->load_bias is initialized.
2630 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2631 * We need to update this value for so exe here. So Unwind_Backtrace
2632 * for some arch like x86 could work correctly within so exe.
2633 */
2634 map->l_addr = si->load_bias;
2635 si->call_constructors();
2636 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002637
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002638#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002639 gettimeofday(&t1, nullptr);
2640 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2641 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2642 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002643#endif
2644#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002645 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2646 linker_stats.count[kRelocAbsolute],
2647 linker_stats.count[kRelocRelative],
2648 linker_stats.count[kRelocCopy],
2649 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002650#endif
2651#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002652 {
2653 unsigned n;
2654 unsigned i;
2655 unsigned count = 0;
2656 for (n = 0; n < 4096; n++) {
2657 if (bitmask[n]) {
2658 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002659#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002660 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002661#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002662 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002663#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002664 if (x & 1) {
2665 count++;
2666 }
2667 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002668 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002669 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002670 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002671 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2672 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002673#endif
2674
2675#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002676 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002677#endif
2678
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002679 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2680 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002681}
Nick Kralevich468319c2011-11-11 15:53:17 -08002682
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002683/* Compute the load-bias of an existing executable. This shall only
2684 * be used to compute the load bias of an executable or shared library
2685 * that was loaded by the kernel itself.
2686 *
2687 * Input:
2688 * elf -> address of ELF header, assumed to be at the start of the file.
2689 * Return:
2690 * load bias, i.e. add the value of any p_vaddr in the file to get
2691 * the corresponding address in memory.
2692 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002693static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2694 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002695 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002696 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002697
Elliott Hughes0266ae52014-02-10 17:46:57 -08002698 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002699 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002700 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002701 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002702 }
2703 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002704}
2705
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002706extern "C" void _start();
2707
Nick Kralevich468319c2011-11-11 15:53:17 -08002708/*
2709 * This is the entry point for the linker, called from begin.S. This
2710 * method is responsible for fixing the linker's own relocations, and
2711 * then calling __linker_init_post_relocation().
2712 *
2713 * Because this method is called before the linker has fixed it's own
2714 * relocations, any attempt to reference an extern variable, extern
2715 * function, or other GOT reference will generate a segfault.
2716 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002717extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002718 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002719
Elliott Hughes0266ae52014-02-10 17:46:57 -08002720 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002721 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002722 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002723 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002724
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002725 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002726
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002727 // If the linker is not acting as PT_INTERP entry_point is equal to
2728 // _start. Which means that the linker is running as an executable and
2729 // already linked by PT_INTERP.
2730 //
2731 // This happens when user tries to run 'adb shell /system/bin/linker'
2732 // see also https://code.google.com/p/android/issues/detail?id=63174
2733 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2734 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2735 }
2736
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002737 linker_so.base = linker_addr;
2738 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2739 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002740 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002741 linker_so.phdr = phdr;
2742 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002743 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07002744
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002745 // This might not be obvious... The reasons why we pass g_empty_list
2746 // in place of local_group here are (1) we do not really need it, because
2747 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2748 // itself without having to look into local_group and (2) allocators
2749 // are not yet initialized, and therefore we cannot use linked_list.push_*
2750 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002751 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002752 // It would be nice to print an error message, but if the linker
2753 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002754 // call write() (because it involves a GOT reference). We may as
2755 // well try though...
2756 const char* msg = "CANNOT LINK EXECUTABLE: ";
2757 write(2, msg, strlen(msg));
2758 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2759 write(2, "\n", 1);
2760 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002761 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002762
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002763 __libc_init_tls(args);
2764
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002765 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002766 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002767
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002768 // Initialize static variables. Note that in order to
2769 // get correct libdl_info we need to call constructors
2770 // before get_libdl_info().
2771 solist = get_libdl_info();
2772 sonext = get_libdl_info();
2773
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002774 // We have successfully fixed our own relocations. It's safe to run
2775 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002776 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002777 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002778
Elliott Hughes611f9562015-01-23 10:43:58 -08002779 INFO("[ jumping to _start ]");
2780
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002781 // Return the address that the calling assembly stub should jump to.
2782 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002783}