blob: 493b03515c59a79405a4c3a72925ec4a16d6f07b [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 Ivanovc9ce70d2015-03-10 15:30:26 -070053#include "linker_block_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
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700424 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
425 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
426
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800427 // test against bloom filter
428 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700429 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
430 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
431
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800432 return nullptr;
433 }
434
435 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700436 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800437
438 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700439 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
440 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
441
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800442 return nullptr;
443 }
444
445 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800446 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700447 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800448 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
449 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700450 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
451 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
452 static_cast<size_t>(s->st_size));
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800453 return s;
454 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700455 } while ((gnu_chain_[n++] & 1) == 0);
456
457 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
458 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800459
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800460 return nullptr;
461}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800462
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800463ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name) {
464 uint32_t hash = symbol_name.elf_hash();
465
466 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800467 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800468
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800469 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
470 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800471 if (strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && is_symbol_global_and_defined(this, s)) {
472 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
473 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
474 static_cast<size_t>(s->st_size));
475 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800476 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800477 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800478
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700479 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800480 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700481
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700482 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800483}
484
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700485soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700486 memset(this, 0, sizeof(*this));
487
488 strlcpy(this->name, name, sizeof(this->name));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800489 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800490 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700491
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700492 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800493 this->st_dev_ = file_stat->st_dev;
494 this->st_ino_ = file_stat->st_ino;
495 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700496 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700497
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800498 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700499}
500
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800501
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800502uint32_t SymbolName::elf_hash() {
503 if (!has_elf_hash_) {
504 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
505 uint32_t h = 0, g;
506
507 while (*name) {
508 h = (h << 4) + *name++;
509 g = h & 0xf0000000;
510 h ^= g;
511 h ^= g >> 24;
512 }
513
514 elf_hash_ = h;
515 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700516 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800517
518 return elf_hash_;
519}
520
521uint32_t SymbolName::gnu_hash() {
522 if (!has_gnu_hash_) {
523 uint32_t h = 5381;
524 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
525 while (*name != 0) {
526 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
527 }
528
529 gnu_hash_ = h;
530 has_gnu_hash_ = true;
531 }
532
533 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534}
535
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800536ElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700537 const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800538 SymbolName symbol_name(name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700539 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700540
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700541 /* "This element's presence in a shared object library alters the dynamic linker's
542 * symbol resolution algorithm for references within the library. Instead of starting
543 * a symbol search with the executable file, the dynamic linker starts from the shared
544 * object itself. If the shared object fails to supply the referenced symbol, the
545 * dynamic linker then searches the executable file and other shared objects as usual."
546 *
547 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
548 *
549 * Note that this is unlikely since static linker avoids generating
550 * relocations for -Bsymbolic linked dynamic executables.
551 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700552 if (si_from->has_DT_SYMBOLIC) {
553 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->name, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800554 s = si_from->find_symbol_by_name(symbol_name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700555 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700556 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700557 }
558 }
559
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700560 // 1. Look for it in global_group
561 if (s == nullptr) {
562 global_group.visit([&](soinfo* global_si) {
563 DEBUG("%s: looking up %s in %s (from global group)", si_from->name, name, global_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800564 s = global_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700565 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700566 *si_found_in = global_si;
567 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700568 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700569
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700570 return true;
571 });
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700572 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700573
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700574 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700575 if (s == nullptr) {
576 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700577 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700578 // we already did this - skip
579 return true;
580 }
581
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700582 DEBUG("%s: looking up %s in %s (from local group)", si_from->name, name, local_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800583 s = local_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700584 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700585 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700586 return false;
587 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700588
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700589 return true;
590 });
591 }
592
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700593 if (s != nullptr) {
594 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
595 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700596 si_from->name, name, reinterpret_cast<void*>(s->st_value),
597 (*si_found_in)->name, reinterpret_cast<void*>((*si_found_in)->base),
598 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700599 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700600
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700601 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700602}
603
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800604class ProtectedDataGuard {
605 public:
606 ProtectedDataGuard() {
607 if (ref_count_++ == 0) {
608 protect_data(PROT_READ | PROT_WRITE);
609 }
610 }
611
612 ~ProtectedDataGuard() {
613 if (ref_count_ == 0) { // overflow
614 __libc_fatal("Too many nested calls to dlopen()");
615 }
616
617 if (--ref_count_ == 0) {
618 protect_data(PROT_READ);
619 }
620 }
621 private:
622 void protect_data(int protection) {
623 g_soinfo_allocator.protect_all(protection);
624 g_soinfo_links_allocator.protect_all(protection);
625 }
626
627 static size_t ref_count_;
628};
629
630size_t ProtectedDataGuard::ref_count_ = 0;
631
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700632// Each size has it's own allocator.
633template<size_t size>
634class SizeBasedAllocator {
635 public:
636 static void* alloc() {
637 return allocator_.alloc();
638 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700639
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700640 static void free(void* ptr) {
641 allocator_.free(ptr);
642 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700643
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700644 private:
645 static LinkerBlockAllocator allocator_;
646};
647
648template<size_t size>
649LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
650
651template<typename T>
652class TypeBasedAllocator {
653 public:
654 static T* alloc() {
655 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
656 }
657
658 static void free(T* ptr) {
659 SizeBasedAllocator<sizeof(T)>::free(ptr);
660 }
661};
662
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700663class LoadTask {
664 public:
665 struct deleter_t {
666 void operator()(LoadTask* t) {
667 TypeBasedAllocator<LoadTask>::free(t);
668 }
669 };
670
671 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
672
673 static deleter_t deleter;
674
675 static LoadTask* create(const char* name, soinfo* needed_by) {
676 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
677 return new (ptr) LoadTask(name, needed_by);
678 }
679
680 const char* get_name() const {
681 return name_;
682 }
683
684 soinfo* get_needed_by() const {
685 return needed_by_;
686 }
687 private:
688 LoadTask(const char* name, soinfo* needed_by)
689 : name_(name), needed_by_(needed_by) {}
690
691 const char* name_;
692 soinfo* needed_by_;
693
694 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
695};
696
Ningsheng Jiane93be992014-09-16 15:22:10 +0800697LoadTask::deleter_t LoadTask::deleter;
698
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700699template <typename T>
700using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
701
702typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700703typedef linked_list_t<const char> StringLinkedList;
704typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700705
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700706
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700707// This function walks down the tree of soinfo dependencies
708// in breadth-first order and
709// * calls action(soinfo* si) for each node, and
710// * terminates walk if action returns false.
711//
712// walk_dependencies_tree returns false if walk was terminated
713// by the action and true otherwise.
714template<typename F>
715static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700716 SoinfoLinkedList visit_list;
717 SoinfoLinkedList visited;
718
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700719 for (size_t i = 0; i < root_soinfos_size; ++i) {
720 visit_list.push_back(root_soinfos[i]);
721 }
722
723 soinfo* si;
724 while ((si = visit_list.pop_front()) != nullptr) {
725 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700726 continue;
727 }
728
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700729 if (!action(si)) {
730 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700731 }
732
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700733 visited.push_back(si);
734
735 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700736 visit_list.push_back(child);
737 });
738 }
739
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700740 return true;
741}
742
743
744// This is used by dlsym(3). It performs symbol lookup only within the
745// specified soinfo object and its dependencies in breadth first order.
746ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
747 ElfW(Sym)* result = nullptr;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800748 SymbolName symbol_name(name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700749
750
751 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800752 result = current_soinfo->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700753 if (result != nullptr) {
754 *found = current_soinfo;
755 return false;
756 }
757
758 return true;
759 });
760
761 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800762}
763
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800764/* This is used by dlsym(3) to performs a global symbol lookup. If the
765 start value is null (for RTLD_DEFAULT), the search starts at the
766 beginning of the global solist. Otherwise the search starts at the
767 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700768 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700769ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800770 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800771
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700772 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800773 start = solist;
774 }
775
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700776 ElfW(Sym)* s = nullptr;
777 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700778 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
779 continue;
780 }
781
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800782 s = si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700783 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800784 *found = si;
785 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600786 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800787 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600788
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700789 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700790 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
791 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800792 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800793
Elliott Hughescade4c32012-12-20 14:42:14 -0800794 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800795}
796
Kito Chengfa8c05d2013-03-12 14:58:06 +0800797soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800798 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700799 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800800 if (address >= si->base && address - si->base < si->size) {
801 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600802 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800803 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700804 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600805}
806
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800807ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
808 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
809}
810
811static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
812 return sym->st_shndx != SHN_UNDEF &&
813 soaddr >= sym->st_value &&
814 soaddr < sym->st_value + sym->st_size;
815}
816
817ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800818 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800819
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700820 for (size_t i = 0; i < gnu_nbucket_; ++i) {
821 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800822
823 if (n == 0) {
824 continue;
825 }
826
827 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800828 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800829 if (symbol_matches_soaddr(sym, soaddr)) {
830 return sym;
831 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700832 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800833 }
834
835 return nullptr;
836}
837
838ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800839 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600840
Kito Chengfa8c05d2013-03-12 14:58:06 +0800841 // Search the library's symbol table for any defined symbol which
842 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800843 for (size_t i = 0; i < nchain_; ++i) {
844 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800845 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800846 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600847 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800848 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600849
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700850 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600851}
852
Elliott Hughes124fae92012-10-31 14:20:03 -0700853static int open_library_on_path(const char* name, const char* const paths[]) {
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +0000854 char buf[512];
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700855 for (size_t i = 0; paths[i] != nullptr; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800856 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700857 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700858 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700859 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800860 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700861 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
862 if (fd != -1) {
863 return fd;
864 }
865 }
866 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800867}
868
Elliott Hughes124fae92012-10-31 14:20:03 -0700869static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700870 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800871
Elliott Hughes124fae92012-10-31 14:20:03 -0700872 // 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 -0700873 if (strchr(name, '/') != nullptr) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700874 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
875 if (fd != -1) {
876 return fd;
877 }
878 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700879#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700880 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700881#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700882 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800883
Elliott Hughes124fae92012-10-31 14:20:03 -0700884 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Elliott Hughes1728b232014-05-14 10:02:03 -0700885 int fd = open_library_on_path(name, g_ld_library_paths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700886 if (fd == -1) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700887 fd = open_library_on_path(name, kDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700888 }
889 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800890}
891
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700892template<typename F>
893static void for_each_dt_needed(const soinfo* si, F action) {
894 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
895 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700896 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700897 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700898 }
899}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800900
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700901static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700902 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700903 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700904 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700905
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700906 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
907 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700908 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
909 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700910 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700911 } else {
912 // Open the file.
913 fd = open_library(name);
914 if (fd == -1) {
915 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700916 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700917 }
918
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700919 file_guard.reset(fd);
920 }
921
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700922 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700923 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700924 return nullptr;
925 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800926 if (file_offset < 0) {
927 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
928 return nullptr;
929 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700930
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700931 struct stat file_stat;
932 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700933 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700934 return nullptr;
935 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800936 if (file_offset >= file_stat.st_size) {
937 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64, name, file_offset, file_stat.st_size);
938 return nullptr;
939 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700940
941 // Check for symlink and other situations where
942 // file can have different names.
943 for (soinfo* si = solist; si != nullptr; si = si->next) {
944 if (si->get_st_dev() != 0 &&
945 si->get_st_ino() != 0 &&
946 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700947 si->get_st_ino() == file_stat.st_ino &&
948 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700949 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
950 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700951 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700952 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700953
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700954 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700955 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700956 return nullptr;
957 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700958
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700959 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700960 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700961 if (!elf_reader.Load(extinfo)) {
962 return nullptr;
963 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800964
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700965 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700966 if (si == nullptr) {
967 return nullptr;
968 }
969 si->base = elf_reader.load_start();
970 si->size = elf_reader.load_size();
971 si->load_bias = elf_reader.load_bias();
972 si->phnum = elf_reader.phdr_count();
973 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700974
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800975 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700976 soinfo_free(si);
977 return nullptr;
978 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700979
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700980 for_each_dt_needed(si, [&] (const char* name) {
981 load_tasks.push_back(LoadTask::create(name, si));
982 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700983
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700984 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800985}
986
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700987static soinfo *find_loaded_library_by_name(const char* name) {
988 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700989 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700990 if (!strcmp(search_name, si->name)) {
991 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200992 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700993 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700994 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200995}
996
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700997static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200998
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700999 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001000
1001 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001002 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001003 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001004 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001005 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001006 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001007
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001008 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001009}
1010
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001011static void soinfo_unload(soinfo* si);
1012
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001013// TODO: this is slightly unusual way to construct
1014// the global group for relocation. Not every RTLD_GLOBAL
1015// library is included in this group for backwards-compatibility
1016// reasons.
1017//
1018// This group consists of the main executable, LD_PRELOADs
1019// and libraries with the DF_1_GLOBAL flag set.
1020static soinfo::soinfo_list_t make_global_group() {
1021 soinfo::soinfo_list_t global_group;
1022 for (soinfo* si = somain; si != nullptr; si = si->next) {
1023 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1024 global_group.push_back(si);
1025 }
1026 }
1027
1028 return global_group;
1029}
1030
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001031static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
1032 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001033 // Step 0: prepare.
1034 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001035 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001036 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001037 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001038 }
1039
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001040 // Construct global_group.
1041 soinfo::soinfo_list_t global_group = make_global_group();
1042
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001043 // If soinfos array is null allocate one on stack.
1044 // The array is needed in case of failure; for example
1045 // when library_names[] = {libone.so, libtwo.so} and libone.so
1046 // is loaded correctly but libtwo.so failed for some reason.
1047 // In this case libone.so should be unloaded on return.
1048 // See also implementation of failure_guard below.
1049
1050 if (soinfos == nullptr) {
1051 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1052 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1053 memset(soinfos, 0, soinfos_size);
1054 }
1055
1056 // list of libraries to link - see step 2.
1057 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001058
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001059 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001060 // Housekeeping
1061 load_tasks.for_each([] (LoadTask* t) {
1062 LoadTask::deleter(t);
1063 });
1064
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001065 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001066 soinfo_unload(soinfos[i]);
1067 }
1068 });
1069
1070 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
1071 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 -07001072 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001073 if (si == nullptr) {
1074 return false;
1075 }
1076
1077 soinfo* needed_by = task->get_needed_by();
1078
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001079 if (needed_by != nullptr) {
1080 needed_by->add_child(si);
1081 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001082
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001083 if (si->is_linked()) {
1084 si->increment_ref_count();
1085 }
1086
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001087 // When ld_preloads is not null, the first
1088 // ld_preloads_count libs are in fact ld_preloads.
1089 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001090 // Add LD_PRELOADed libraries to the global group for future runs.
1091 // There is no need to explicitly add them to the global group
1092 // for this run because they are going to appear in the local
1093 // group in the correct order.
1094 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001095 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001096 }
1097
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001098 if (soinfos_count < library_names_count) {
1099 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001100 }
1101 }
1102
1103 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001104 soinfo::soinfo_list_t local_group;
1105 walk_dependencies_tree(
1106 start_with == nullptr ? soinfos : &start_with,
1107 start_with == nullptr ? soinfos_count : 1,
1108 [&] (soinfo* si) {
1109 local_group.push_back(si);
1110 return true;
1111 });
1112
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001113 // We need to increment ref_count in case
1114 // the root of the local group was not linked.
1115 bool was_local_group_root_linked = local_group.front()->is_linked();
1116
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001117 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001118 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001119 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001120 return false;
1121 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001122 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001123 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001124
1125 return true;
1126 });
1127
1128 if (linked) {
1129 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001130 }
1131
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001132 if (!was_local_group_root_linked) {
1133 local_group.front()->increment_ref_count();
1134 }
1135
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001136 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001137}
1138
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001139static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001140 soinfo* si;
1141
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001142 if (name == nullptr) {
1143 si = somain;
1144 } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001145 return nullptr;
1146 }
1147
Elliott Hughesd23736e2012-11-01 15:16:56 -07001148 return si;
1149}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001150
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001151static void soinfo_unload(soinfo* root) {
1152 // Note that the library can be loaded but not linked;
1153 // in which case there is no root but we still need
1154 // to walk the tree and unload soinfos involved.
1155 //
1156 // This happens on unsuccessful dlopen, when one of
1157 // the DT_NEEDED libraries could not be linked/found.
1158 if (root->is_linked()) {
1159 root = root->get_local_group_root();
1160 }
1161
1162 if (!root->can_unload()) {
1163 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->name);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001164 return;
1165 }
1166
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001167 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001168
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001169 if (ref_count == 0) {
1170 soinfo::soinfo_list_t local_unload_list;
1171 soinfo::soinfo_list_t external_unload_list;
1172 soinfo::soinfo_list_t depth_first_list;
1173 depth_first_list.push_back(root);
1174 soinfo* si = nullptr;
1175
1176 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001177 if (local_unload_list.contains(si)) {
1178 continue;
1179 }
1180
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001181 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001182
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001183 if (si->has_min_version(0)) {
1184 soinfo* child = nullptr;
1185 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001186 TRACE("%s@%p needs to unload %s@%p", si->name, si, child->name, child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001187 if (local_unload_list.contains(child)) {
1188 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001189 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001190 external_unload_list.push_back(child);
1191 } else {
1192 depth_first_list.push_front(child);
1193 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001194 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001195 } else {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001196#ifdef __LP64__
1197 __libc_fatal("soinfo for \"%s\"@%p has no version", si->name, si);
1198#else
1199 PRINT("warning: soinfo for \"%s\"@%p has no version", si->name, si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001200 for_each_dt_needed(si, [&] (const char* library_name) {
1201 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1202 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1203 if (needed != nullptr) {
1204 // Not found: for example if symlink was deleted between dlopen and dlclose
1205 // Since we cannot really handle errors at this point - print and continue.
1206 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
1207 return;
1208 } else if (local_unload_list.contains(needed)) {
1209 // already visited
1210 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001211 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001212 // external group
1213 external_unload_list.push_back(needed);
1214 } else {
1215 // local group
1216 depth_first_list.push_front(needed);
1217 }
1218 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001219#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001220 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001221 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001222
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001223 local_unload_list.for_each([](soinfo* si) {
1224 si->call_destructors();
1225 });
1226
1227 while ((si = local_unload_list.pop_front()) != nullptr) {
1228 notify_gdb_of_unload(si);
1229 soinfo_free(si);
1230 }
1231
1232 while ((si = external_unload_list.pop_front()) != nullptr) {
1233 soinfo_unload(si);
1234 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001235 } else {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001236 TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->name, ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001237 }
1238}
1239
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001240void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001241 // Use basic string manipulation calls to avoid snprintf.
1242 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1243 // When debug malloc is enabled, this call returns 0. This in turn causes
1244 // snprintf to do nothing, which causes libraries to fail to load.
1245 // See b/17302493 for further details.
1246 // Once the above bug is fixed, this code can be modified to use
1247 // snprintf again.
1248 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1249 if (buffer_size < required_len) {
1250 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1251 buffer_size, required_len);
1252 }
1253 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1254 *end = ':';
1255 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001256}
1257
Elliott Hughescade4c32012-12-20 14:42:14 -08001258void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001259 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001260}
1261
Elliott Hughes1a586292014-06-03 16:23:08 -07001262soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001263 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001264 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001265 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001266 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001267 if (extinfo != nullptr) {
1268 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1269 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1270 return nullptr;
1271 }
1272 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001273 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1274 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 -07001275 return nullptr;
1276 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001277 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001278
1279 ProtectedDataGuard guard;
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001280 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001281 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001282 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001283 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001284 return si;
1285}
1286
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001287void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001288 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001289 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001290}
1291
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001292static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1293 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1294 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1295 ElfW(Addr) ifunc_addr = ifunc_resolver();
1296 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 -07001297
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001298 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001299}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001300
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001301#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001302#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001303static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
1304 return rela->r_addend;
1305}
1306#else
1307static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
1308 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE || ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
1309 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
1310 }
1311 return 0;
1312}
1313#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001314
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001315template<typename ElfRelIteratorT>
1316bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
1317 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
1318 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001319 if (rel == nullptr) {
1320 return false;
1321 }
1322
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001323 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
1324 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
1325
1326 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001327 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001328 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001329 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001330
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001331 DEBUG("Processing '%s' relocation at index %zd", this->name, idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001332 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001333 continue;
1334 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001335
1336 ElfW(Sym)* s = nullptr;
1337 soinfo* lsi = nullptr;
1338
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001339 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001340 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001341 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001342 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001343 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001344 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001345 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001346 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001347 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001348 }
1349
1350 /* IHI0044C AAELF 4.5.1.1:
1351
1352 Libraries are not searched to resolve weak references.
1353 It is not an error for a weak reference to remain unsatisfied.
1354
1355 During linking, the value of an undefined weak reference is:
1356 - Zero if the relocation type is absolute
1357 - The address of the place if the relocation is pc-relative
1358 - The address of nominal base address if the relocation
1359 type is base-relative.
1360 */
1361
1362 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001363 case R_GENERIC_JUMP_SLOT:
1364 case R_GENERIC_GLOB_DAT:
1365 case R_GENERIC_RELATIVE:
1366 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001367#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001368 case R_AARCH64_ABS64:
1369 case R_AARCH64_ABS32:
1370 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001371#elif defined(__x86_64__)
1372 case R_X86_64_32:
1373 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001374#elif defined(__arm__)
1375 case R_ARM_ABS32:
1376#elif defined(__i386__)
1377 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001378#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001379 /*
1380 * The sym_addr was initialized to be zero above, or the relocation
1381 * code below does not care about value of sym_addr.
1382 * No need to do anything.
1383 */
1384 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001385#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00001386 case R_X86_64_PC32:
1387 sym_addr = reloc;
1388 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001389#elif defined(__i386__)
1390 case R_386_PC32:
1391 sym_addr = reloc;
1392 break;
1393#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001394 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001395 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001396 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001397 }
1398 } else {
1399 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001400 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001401 }
1402 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001403 }
1404
1405 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001406 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001407 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001408 MARK(rel->r_offset);
1409 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
1410 reinterpret_cast<void*>(reloc),
1411 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1412
1413 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001414 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001415 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001416 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001417 MARK(rel->r_offset);
1418 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
1419 reinterpret_cast<void*>(reloc),
1420 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1421 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001422 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001423 case R_GENERIC_RELATIVE:
1424 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001425 MARK(rel->r_offset);
1426 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
1427 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001428 reinterpret_cast<void*>(load_bias + addend));
1429 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001430 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001431 case R_GENERIC_IRELATIVE:
1432 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001433 MARK(rel->r_offset);
1434 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
1435 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001436 reinterpret_cast<void*>(load_bias + addend));
1437 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001438 break;
1439
1440#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001441 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001442 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001443 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001444 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001445 reloc, (sym_addr + addend), sym_name);
1446 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001447 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001448 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001449 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001450 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001451 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001452 reloc, (sym_addr + addend), sym_name);
1453 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1454 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1455 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001456 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001457 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001458 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001459 static_cast<ElfW(Addr)>(INT32_MIN),
1460 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001461 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001462 }
1463 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001464 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001465 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001466 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001467 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001468 reloc, (sym_addr + addend), sym_name);
1469 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1470 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1471 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001472 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001473 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001474 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001475 static_cast<ElfW(Addr)>(INT16_MIN),
1476 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001477 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001478 }
1479 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001480 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001481 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001482 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001483 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001484 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1485 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend) - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001486 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001487 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001488 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001489 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001490 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001491 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1492 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1493 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1494 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001495 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001496 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001497 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001498 static_cast<ElfW(Addr)>(INT32_MIN),
1499 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001500 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001501 }
1502 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001503 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001504 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001505 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001506 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001507 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1508 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1509 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1510 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001511 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001512 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001513 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001514 static_cast<ElfW(Addr)>(INT16_MIN),
1515 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001516 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001517 }
1518 break;
1519
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001520 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001521 /*
1522 * ET_EXEC is not supported so this should not happen.
1523 *
1524 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1525 *
1526 * Section 4.7.1.10 "Dynamic relocations"
1527 * R_AARCH64_COPY may only appear in executable objects where e_type is
1528 * set to ET_EXEC.
1529 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001530 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001531 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001532 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001533 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001534 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001535 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001536 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001537 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001538 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001539 break;
1540#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001541 case R_X86_64_32:
1542 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001543 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001544 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1545 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001546 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001547 break;
1548 case R_X86_64_64:
1549 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001550 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001551 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1552 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001553 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001554 break;
1555 case R_X86_64_PC32:
1556 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001557 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001558 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1559 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1560 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001561 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001562 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001563#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001564 case R_ARM_ABS32:
1565 count_relocation(kRelocAbsolute);
1566 MARK(rel->r_offset);
1567 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1568 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1569 break;
1570 case R_ARM_REL32:
1571 count_relocation(kRelocRelative);
1572 MARK(rel->r_offset);
1573 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1574 reloc, sym_addr, rel->r_offset, sym_name);
1575 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1576 break;
1577 case R_ARM_COPY:
1578 /*
1579 * ET_EXEC is not supported so this should not happen.
1580 *
1581 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1582 *
1583 * Section 4.7.1.10 "Dynamic relocations"
1584 * R_ARM_COPY may only appear in executable objects where e_type is
1585 * set to ET_EXEC.
1586 */
1587 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001588 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001589#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001590 case R_386_32:
1591 count_relocation(kRelocRelative);
1592 MARK(rel->r_offset);
1593 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1594 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1595 break;
1596 case R_386_PC32:
1597 count_relocation(kRelocRelative);
1598 MARK(rel->r_offset);
1599 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1600 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1601 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1602 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001603#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001604 default:
1605 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001606 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001607 }
1608 }
1609 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001610}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001611#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001612
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001613void soinfo::call_array(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001614 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001615 return;
1616 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001617
Elliott Hughesc6200592013-09-30 18:43:46 -07001618 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001619
1620 int begin = reverse ? (count - 1) : 0;
1621 int end = reverse ? -1 : count;
1622 int step = reverse ? -1 : 1;
1623
1624 for (int i = begin; i != end; i += step) {
1625 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001626 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001627 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001628
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001629 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001630}
1631
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001632void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001633 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001634 return;
1635 }
1636
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001637 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001638 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001639 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001640}
1641
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001642void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001643 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1644 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001645 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001646}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001647
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001648void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001649 if (constructors_called) {
1650 return;
1651 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001652
Elliott Hughesd23736e2012-11-01 15:16:56 -07001653 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1654 // protect against recursive constructor calls. One simple example of constructor recursion
1655 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1656 // 1. The program depends on libc, so libc's constructor is called here.
1657 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1658 // 3. dlopen() calls the constructors on the newly created
1659 // soinfo for libc_malloc_debug_leak.so.
1660 // 4. The debug .so depends on libc, so CallConstructors is
1661 // called again with the libc soinfo. If it doesn't trigger the early-
1662 // out above, the libc constructor will be called again (recursively!).
1663 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001664
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001665 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001666 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001667 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001668 name, preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001669 }
1670
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001671 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001672 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001673 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001674
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001675 TRACE("\"%s\": calling constructors", name);
1676
1677 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001678 call_function("DT_INIT", init_func_);
1679 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001680}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001681
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001682void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001683 if (!constructors_called) {
1684 return;
1685 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001686 TRACE("\"%s\": calling destructors", name);
1687
1688 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001689 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001690
1691 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001692 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001693
1694 // This is needed on second call to dlopen
1695 // after library has been unloaded with RTLD_NODELETE
1696 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001697}
1698
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001699void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001700 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001701 child->parents_.push_back(this);
1702 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001703 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001704}
1705
1706void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001707 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001708 return;
1709 }
1710
1711 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001712 children_.for_each([&] (soinfo* child) {
1713 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001714 return parent == this;
1715 });
1716 });
1717
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001718 parents_.for_each([&] (soinfo* parent) {
1719 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001720 return child == this;
1721 });
1722 });
1723
1724 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001725 parents_.clear();
1726 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001727}
1728
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001729dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001730 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001731 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001732 }
1733
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001734 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001735};
1736
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001737ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001738 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001739 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001740 }
1741
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001742 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001743}
1744
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001745off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001746 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001747 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001748 }
1749
1750 return 0;
1751}
1752
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001753uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001754 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001755 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001756 }
1757
1758 return 0;
1759}
1760
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001761uint32_t soinfo::get_dt_flags_1() const {
1762 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001763 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001764 }
1765
1766 return 0;
1767}
1768void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1769 if (has_min_version(1)) {
1770 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001771 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001772 }
1773
1774 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001775 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001776 }
1777
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001778 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001779 }
1780}
1781
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001782// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001783// 'this->flags' does not have FLAG_NEW_SOINFO set.
1784static soinfo::soinfo_list_t g_empty_list;
1785
1786soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001787 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001788 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001789 }
1790
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001791 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001792}
1793
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001794soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001795 if (has_min_version(0)) {
1796 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001797 }
1798
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001799 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001800}
1801
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001802ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1803 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1804 return call_ifunc_resolver(s->st_value + load_bias);
1805 }
1806
1807 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1808}
1809
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001810const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001811 if (has_min_version(1) && (index >= strtab_size_)) {
1812 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001813 }
1814
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001815 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001816}
1817
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001818bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001819 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001820}
1821
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001822bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001823 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001824}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001825
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001826bool soinfo::is_linked() const {
1827 return (flags_ & FLAG_LINKED) != 0;
1828}
1829
1830bool soinfo::is_main_executable() const {
1831 return (flags_ & FLAG_EXE) != 0;
1832}
1833
1834void soinfo::set_linked() {
1835 flags_ |= FLAG_LINKED;
1836}
1837
1838void soinfo::set_linker_flag() {
1839 flags_ |= FLAG_LINKER;
1840}
1841
1842void soinfo::set_main_executable() {
1843 flags_ |= FLAG_EXE;
1844}
1845
1846void soinfo::increment_ref_count() {
1847 local_group_root_->ref_count_++;
1848}
1849
1850size_t soinfo::decrement_ref_count() {
1851 return --local_group_root_->ref_count_;
1852}
1853
1854soinfo* soinfo::get_local_group_root() const {
1855 return local_group_root_;
1856}
1857
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001858/* Force any of the closed stdin, stdout and stderr to be associated with
1859 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001860static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001861 int dev_null, i, status;
1862 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001863
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001864 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
1865 if (dev_null < 0) {
1866 DL_ERR("cannot open /dev/null: %s", strerror(errno));
1867 return -1;
1868 }
1869 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001870
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001871 /* If any of the stdio file descriptors is valid and not associated
1872 with /dev/null, dup /dev/null to it. */
1873 for (i = 0; i < 3; i++) {
1874 /* If it is /dev/null already, we are done. */
1875 if (i == dev_null) {
1876 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001877 }
1878
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001879 TRACE("[ Nullifying stdio file descriptor %d]", i);
1880 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
1881
1882 /* If file is opened, we are good. */
1883 if (status != -1) {
1884 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001885 }
1886
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001887 /* The only error we allow is that the file descriptor does not
1888 exist, in which case we dup /dev/null to it. */
1889 if (errno != EBADF) {
1890 DL_ERR("fcntl failed: %s", strerror(errno));
1891 return_value = -1;
1892 continue;
1893 }
1894
1895 /* Try dupping /dev/null to this stdio file descriptor and
1896 repeat if there is a signal. Note that any errors in closing
1897 the stdio descriptor are lost. */
1898 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
1899 if (status < 0) {
1900 DL_ERR("dup2 failed: %s", strerror(errno));
1901 return_value = -1;
1902 continue;
1903 }
1904 }
1905
1906 /* If /dev/null is not one of the stdio file descriptors, close it. */
1907 if (dev_null > 2) {
1908 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
1909 status = TEMP_FAILURE_RETRY(close(dev_null));
1910 if (status == -1) {
1911 DL_ERR("close failed: %s", strerror(errno));
1912 return_value = -1;
1913 }
1914 }
1915
1916 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001917}
1918
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001919bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08001920 /* Extract dynamic section */
1921 ElfW(Word) dynamic_flags = 0;
1922 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07001923
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001924 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001925 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001926 if (!relocating_linker) {
1927 INFO("[ linking %s ]", name);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001928 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001929 }
1930
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001931 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001932 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001933 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001934 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001935 return false;
1936 } else {
1937 if (!relocating_linker) {
1938 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001939 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001940 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001941
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001942#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001943 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
1944 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001945#endif
1946
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001947 // Extract useful information from dynamic section.
1948 uint32_t needed_count = 0;
1949 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
1950 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1951 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1952 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001953 case DT_SONAME:
1954 // TODO: glibc dynamic linker uses this name for
1955 // initial library lookup; consider doing the same here.
1956 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001957
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001958 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001959 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
1960 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
1961 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
1962 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001963 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001964
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001965 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001966 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001967 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001968 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
1969 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001970
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001971 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001972 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001973 // amend chain for symndx = header[1]
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001974 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ - reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001975
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001976 if (!powerof2(gnu_maskwords_)) {
1977 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 -08001978 return false;
1979 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001980 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001981
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001982 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001983 break;
1984
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001985 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001986 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001987 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001988
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001989 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001990 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001991 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001992
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001993 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001994 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001995 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001996
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001997 case DT_SYMENT:
1998 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001999 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 -07002000 return false;
2001 }
2002 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002003
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002004 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002005#if defined(USE_RELA)
2006 if (d->d_un.d_val != DT_RELA) {
2007 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002008 return false;
2009 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002010#else
2011 if (d->d_un.d_val != DT_REL) {
2012 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
2013 return false;
2014 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002015#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002016 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002017
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002018 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002019#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002020 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002021#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002022 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002023#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002024 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002025
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002026 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002027#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002028 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002029#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002030 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002031#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002032 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002033
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002034 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002035#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002036 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002037 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002038#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002039 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2040 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002041
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002042 case DT_DEBUG:
2043 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2044 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002045// FIXME: not working currently for N64
2046// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002047// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002048// read-only, but the DYNAMIC header claims it is writable.
2049#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002050 if ((dynamic_flags & PF_W) != 0) {
2051 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2052 }
Chris Dearman99186652014-02-06 20:36:51 -08002053#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002054 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002055#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002056 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002057 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002058 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002059
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002060 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002061 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002062 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002063
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002064 case DT_ANDROID_RELA:
2065 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2066 break;
2067
2068 case DT_ANDROID_RELASZ:
2069 android_relocs_size_ = d->d_un.d_val;
2070 break;
2071
2072 case DT_ANDROID_REL:
2073 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", name);
2074 return false;
2075
2076 case DT_ANDROID_RELSZ:
2077 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", name);
2078 return false;
2079
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002080 case DT_RELAENT:
2081 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002082 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002083 return false;
2084 }
2085 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002086
2087 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002088 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002089 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002090
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002091 case DT_REL:
2092 DL_ERR("unsupported DT_REL in \"%s\"", name);
2093 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002094
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002095 case DT_RELSZ:
2096 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2097 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002098
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002099#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002100 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002101 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002102 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002103
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002104 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002105 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002106 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002107
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002108 case DT_RELENT:
2109 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002110 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002111 return false;
2112 }
2113 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002114
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002115 case DT_ANDROID_REL:
2116 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2117 break;
2118
2119 case DT_ANDROID_RELSZ:
2120 android_relocs_size_ = d->d_un.d_val;
2121 break;
2122
2123 case DT_ANDROID_RELA:
2124 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", name);
2125 return false;
2126
2127 case DT_ANDROID_RELASZ:
2128 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", name);
2129 return false;
2130
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002131 // "Indicates that all RELATIVE relocations have been concatenated together,
2132 // and specifies the RELATIVE relocation count."
2133 //
2134 // TODO: Spec also mentions that this can be used to optimize relocation process;
2135 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002136 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002137 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002138
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002139 case DT_RELA:
2140 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2141 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002142
2143 case DT_RELASZ:
2144 DL_ERR("unsupported DT_RELASZ in \"%s\"", name);
2145 return false;
2146
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002147#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002148 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002149 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2150 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002151 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002152
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002153 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002154 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2155 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002156 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002157
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002158 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002159 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2160 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002161 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002162
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002163 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002164 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002165 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002166
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002167 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002168 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2169 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002170 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002171
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002172 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002173 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002174 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002175
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002176 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002177 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2178 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002179 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002180
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002181 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002182 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002183 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002184
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002185 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002186#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002187 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2188 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002189#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002190 has_text_relocations = true;
2191 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002192#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002193
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002194 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002195 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002196 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002197
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002198 case DT_NEEDED:
2199 ++needed_count;
2200 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002201
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002202 case DT_FLAGS:
2203 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002204#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002205 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2206 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002207#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002208 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002209#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002210 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002211 if (d->d_un.d_val & DF_SYMBOLIC) {
2212 has_DT_SYMBOLIC = true;
2213 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002214 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002215
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002216 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002217 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002218
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002219 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002220 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2221 }
2222 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002223#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002224 case DT_MIPS_RLD_MAP:
2225 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2226 {
2227 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2228 *dp = &_r_debug;
2229 }
2230 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002231 case DT_MIPS_RLD_MAP2:
2232 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2233 {
2234 r_debug** dp = reinterpret_cast<r_debug**>(reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
2235 *dp = &_r_debug;
2236 }
2237 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002238
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002239 case DT_MIPS_RLD_VERSION:
2240 case DT_MIPS_FLAGS:
2241 case DT_MIPS_BASE_ADDRESS:
2242 case DT_MIPS_UNREFEXTNO:
2243 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002244
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002245 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002246 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002247 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002248
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002249 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002250 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002251 break;
2252
2253 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002254 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002255 break;
2256#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002257 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2258 case DT_BIND_NOW:
2259 break;
2260
2261 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002262 case DT_VERSYM:
2263 case DT_VERDEF:
2264 case DT_VERDEFNUM:
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002265 case DT_VERNEED:
2266 case DT_VERNEEDNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002267 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002268
2269 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002270 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002271 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002272 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2273 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002274 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002275 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002276 }
2277
2278 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002279 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002280
2281 // Sanity checks.
2282 if (relocating_linker && needed_count != 0) {
2283 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2284 return false;
2285 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002286 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002287 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 -07002288 return false;
2289 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002290 if (strtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002291 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2292 return false;
2293 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002294 if (symtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002295 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2296 return false;
2297 }
2298 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002299}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002300
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002301bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
2302 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002303
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002304 local_group_root_ = local_group.front();
2305 if (local_group_root_ == nullptr) {
2306 local_group_root_ = this;
2307 }
2308
Elliott Hughese4d792a2013-10-28 14:19:05 -07002309#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002310 if (has_text_relocations) {
2311 // Make segments writable to allow text relocations to work properly. We will later call
2312 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2313 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2314 "security hardening. Please fix.", name);
2315 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2316 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2317 name, strerror(errno));
2318 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002319 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002320 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002321#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002322
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002323 if (android_relocs_ != nullptr) {
2324 // check signature
2325 if (android_relocs_size_ > 3 &&
2326 android_relocs_[0] == 'A' &&
2327 android_relocs_[1] == 'P' &&
2328 (android_relocs_[2] == 'U' || android_relocs_[2] == 'S') &&
2329 android_relocs_[3] == '2') {
2330 DEBUG("[ android relocating %s ]", name);
2331
2332 bool relocated = false;
2333 const uint8_t* packed_relocs = android_relocs_ + 4;
2334 const size_t packed_relocs_size = android_relocs_size_ - 4;
2335
2336 if (android_relocs_[2] == 'U') {
2337 relocated = relocate(
2338 packed_reloc_iterator<leb128_decoder>(
2339 leb128_decoder(packed_relocs, packed_relocs_size)),
2340 global_group, local_group);
2341 } else { // android_relocs_[2] == 'S'
2342 relocated = relocate(
2343 packed_reloc_iterator<sleb128_decoder>(
2344 sleb128_decoder(packed_relocs, packed_relocs_size)),
2345 global_group, local_group);
2346 }
2347
2348 if (!relocated) {
2349 return false;
2350 }
2351 } else {
2352 DL_ERR("bad android relocation header.");
2353 return false;
2354 }
2355 }
2356
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002357#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002358 if (rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002359 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002360 if (!relocate(plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002361 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002362 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002363 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002364 if (plt_rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002365 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002366 if (!relocate(plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002367 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002368 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002369 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002370#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002371 if (rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002372 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002373 if (!relocate(plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002374 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002375 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002376 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002377 if (plt_rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002378 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002379 if (!relocate(plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002380 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002381 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002382 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002383#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002384
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002385#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08002386 if (!mips_relocate_got(global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002387 return false;
2388 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002389#endif
2390
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002391 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002392
Elliott Hughese4d792a2013-10-28 14:19:05 -07002393#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002394 if (has_text_relocations) {
2395 // All relocations are done, we can protect our segments back to read-only.
2396 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2397 DL_ERR("can't protect segments for \"%s\": %s",
2398 name, strerror(errno));
2399 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002400 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002401 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002402#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002403
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002404 /* We can also turn on GNU RELRO protection */
2405 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2406 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2407 name, strerror(errno));
2408 return false;
2409 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002410
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002411 /* Handle serializing/sharing the RELRO segment */
2412 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2413 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2414 extinfo->relro_fd) < 0) {
2415 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2416 name, strerror(errno));
2417 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002418 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002419 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2420 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2421 extinfo->relro_fd) < 0) {
2422 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2423 name, strerror(errno));
2424 return false;
2425 }
2426 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002427
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002428 notify_gdb_of_load(this);
2429 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002430}
2431
Nick Kralevich468319c2011-11-11 15:53:17 -08002432/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002433 * This function add vdso to internal dso list.
2434 * It helps to stack unwinding through signal handlers.
2435 * Also, it makes bionic more like glibc.
2436 */
Kito Cheng812fd422014-03-25 22:53:56 +08002437static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002438#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002439 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002440 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002441 return;
2442 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002443
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002444 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002445
Elliott Hughes0266ae52014-02-10 17:46:57 -08002446 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2447 si->phnum = ehdr_vdso->e_phnum;
2448 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2449 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002450 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002451
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002452 si->prelink_image();
2453 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002454#endif
2455}
2456
2457/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002458 * This is linker soinfo for GDB. See details below.
2459 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002460#if defined(__LP64__)
2461#define LINKER_PATH "/system/bin/linker64"
2462#else
2463#define LINKER_PATH "/system/bin/linker"
2464#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002465static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002466
2467/* gdb expects the linker to be in the debug shared object list.
2468 * Without this, gdb has trouble locating the linker's ".text"
2469 * and ".plt" sections. Gdb could also potentially use this to
2470 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2471 * Don't use soinfo_alloc(), because the linker shouldn't
2472 * be on the soinfo list.
2473 */
2474static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002475 linker_soinfo_for_gdb.base = linker_base;
2476
2477 /*
2478 * Set the dynamic field in the link map otherwise gdb will complain with
2479 * the following:
2480 * warning: .dynamic section for "/system/bin/linker" is not at the
2481 * expected address (wrong library or version mismatch?)
2482 */
2483 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2484 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2485 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002486 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002487 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2488}
2489
2490/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002491 * This code is called after the linker has linked itself and
2492 * fixed it's own GOT. It is safe to make references to externs
2493 * and other non-local data at this point.
2494 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002495static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002496#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002497 struct timeval t0, t1;
2498 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002499#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002500
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002501 // Initialize environment functions, and get to the ELF aux vectors table.
2502 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002503
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002504 // If this is a setuid/setgid program, close the security hole described in
2505 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2506 if (get_AT_SECURE()) {
2507 nullify_closed_stdio();
2508 }
2509
2510 debuggerd_init();
2511
2512 // Get a few environment variables.
2513 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2514 if (LD_DEBUG != nullptr) {
2515 g_ld_debug_verbosity = atoi(LD_DEBUG);
2516 }
2517
2518 // Normally, these are cleaned by linker_env_init, but the test
2519 // doesn't cost us anything.
2520 const char* ldpath_env = nullptr;
2521 const char* ldpreload_env = nullptr;
2522 if (!get_AT_SECURE()) {
2523 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2524 ldpreload_env = linker_env_get("LD_PRELOAD");
2525 }
2526
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -08002527#if !defined(__LP64__)
2528 if (personality(PER_LINUX32) == -1) {
2529 __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
2530 }
2531#endif
2532
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002533 INFO("[ android linker & debugger ]");
2534
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002535 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002536 if (si == nullptr) {
2537 exit(EXIT_FAILURE);
2538 }
2539
2540 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002541 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002542 link_map* map = &(si->link_map_head);
2543
2544 map->l_addr = 0;
2545 map->l_name = args.argv[0];
2546 map->l_prev = nullptr;
2547 map->l_next = nullptr;
2548
2549 _r_debug.r_map = map;
2550 r_debug_tail = map;
2551
2552 init_linker_info_for_gdb(linker_base);
2553
2554 // Extract information passed from the kernel.
2555 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2556 si->phnum = args.getauxval(AT_PHNUM);
2557 si->entry = args.getauxval(AT_ENTRY);
2558
2559 /* Compute the value of si->base. We can't rely on the fact that
2560 * the first entry is the PHDR because this will not be true
2561 * for certain executables (e.g. some in the NDK unit test suite)
2562 */
2563 si->base = 0;
2564 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2565 si->load_bias = 0;
2566 for (size_t i = 0; i < si->phnum; ++i) {
2567 if (si->phdr[i].p_type == PT_PHDR) {
2568 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2569 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2570 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002571 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002572 }
2573 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002574
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002575 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2576 if (elf_hdr->e_type != ET_DYN) {
2577 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2578 exit(EXIT_FAILURE);
2579 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002580
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002581 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2582 parse_LD_LIBRARY_PATH(ldpath_env);
2583 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002584
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002585 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002586
Dmitriy Ivanov67181252015-01-07 15:48:25 -08002587 if (!si->prelink_image()) {
2588 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2589 exit(EXIT_FAILURE);
2590 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002591
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002592 // add somain to global group
2593 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2594
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002595 // Load ld_preloads and dependencies.
2596 StringLinkedList needed_library_name_list;
2597 size_t needed_libraries_count = 0;
2598 size_t ld_preloads_count = 0;
2599 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2600 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2601 ++needed_libraries_count;
2602 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002603
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002604 for_each_dt_needed(si, [&](const char* name) {
2605 needed_library_name_list.push_back(name);
2606 ++needed_libraries_count;
2607 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002608
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002609 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002610
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002611 memset(needed_library_names, 0, sizeof(needed_library_names));
2612 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002613
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002614 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 -07002615 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2616 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002617 } else if (needed_libraries_count == 0) {
2618 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
2619 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2620 exit(EXIT_FAILURE);
2621 }
2622 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002623 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002624
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002625 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002626
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002627 {
2628 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06002629
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002630 si->call_pre_init_constructors();
2631
2632 /* After the prelink_image, the si->load_bias is initialized.
2633 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2634 * We need to update this value for so exe here. So Unwind_Backtrace
2635 * for some arch like x86 could work correctly within so exe.
2636 */
2637 map->l_addr = si->load_bias;
2638 si->call_constructors();
2639 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002640
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002641#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002642 gettimeofday(&t1, nullptr);
2643 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2644 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2645 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002646#endif
2647#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002648 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2649 linker_stats.count[kRelocAbsolute],
2650 linker_stats.count[kRelocRelative],
2651 linker_stats.count[kRelocCopy],
2652 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002653#endif
2654#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002655 {
2656 unsigned n;
2657 unsigned i;
2658 unsigned count = 0;
2659 for (n = 0; n < 4096; n++) {
2660 if (bitmask[n]) {
2661 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002662#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002663 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002664#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002665 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002666#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002667 if (x & 1) {
2668 count++;
2669 }
2670 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002671 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002672 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002673 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002674 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2675 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002676#endif
2677
2678#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002679 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002680#endif
2681
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002682 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2683 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002684}
Nick Kralevich468319c2011-11-11 15:53:17 -08002685
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002686/* Compute the load-bias of an existing executable. This shall only
2687 * be used to compute the load bias of an executable or shared library
2688 * that was loaded by the kernel itself.
2689 *
2690 * Input:
2691 * elf -> address of ELF header, assumed to be at the start of the file.
2692 * Return:
2693 * load bias, i.e. add the value of any p_vaddr in the file to get
2694 * the corresponding address in memory.
2695 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002696static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2697 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002698 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002699 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002700
Elliott Hughes0266ae52014-02-10 17:46:57 -08002701 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002702 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002703 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002704 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002705 }
2706 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002707}
2708
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002709extern "C" void _start();
2710
Nick Kralevich468319c2011-11-11 15:53:17 -08002711/*
2712 * This is the entry point for the linker, called from begin.S. This
2713 * method is responsible for fixing the linker's own relocations, and
2714 * then calling __linker_init_post_relocation().
2715 *
2716 * Because this method is called before the linker has fixed it's own
2717 * relocations, any attempt to reference an extern variable, extern
2718 * function, or other GOT reference will generate a segfault.
2719 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002720extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002721 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002722
Elliott Hughes0266ae52014-02-10 17:46:57 -08002723 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002724 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002725 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002726 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002727
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002728 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002729
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002730 // If the linker is not acting as PT_INTERP entry_point is equal to
2731 // _start. Which means that the linker is running as an executable and
2732 // already linked by PT_INTERP.
2733 //
2734 // This happens when user tries to run 'adb shell /system/bin/linker'
2735 // see also https://code.google.com/p/android/issues/detail?id=63174
2736 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2737 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2738 }
2739
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002740 linker_so.base = linker_addr;
2741 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2742 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002743 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002744 linker_so.phdr = phdr;
2745 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002746 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07002747
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002748 // This might not be obvious... The reasons why we pass g_empty_list
2749 // in place of local_group here are (1) we do not really need it, because
2750 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2751 // itself without having to look into local_group and (2) allocators
2752 // are not yet initialized, and therefore we cannot use linked_list.push_*
2753 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002754 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002755 // It would be nice to print an error message, but if the linker
2756 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002757 // call write() (because it involves a GOT reference). We may as
2758 // well try though...
2759 const char* msg = "CANNOT LINK EXECUTABLE: ";
2760 write(2, msg, strlen(msg));
2761 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2762 write(2, "\n", 1);
2763 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002764 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002765
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002766 __libc_init_tls(args);
2767
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002768 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002769 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002770
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002771 // Initialize static variables. Note that in order to
2772 // get correct libdl_info we need to call constructors
2773 // before get_libdl_info().
2774 solist = get_libdl_info();
2775 sonext = get_libdl_info();
2776
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002777 // We have successfully fixed our own relocations. It's safe to run
2778 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002779 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002780 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002781
Elliott Hughes611f9562015-01-23 10:43:58 -08002782 INFO("[ jumping to _start ]");
2783
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002784 // Return the address that the calling assembly stub should jump to.
2785 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002786}