blob: 008833f955b8544d216479cb40b6dd09b5bf8491 [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>
Elliott Hughes46882792012-08-03 16:49:39 -070038#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070040#include <new>
41
Elliott Hughes46882792012-08-03 16:49:39 -070042// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070043#include "private/bionic_tls.h"
44#include "private/KernelArgumentBlock.h"
45#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070046#include "private/ScopedFd.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070047#include "private/ScopeGuard.h"
48#include "private/UniquePtr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049
50#include "linker.h"
51#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010052#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020053#include "linker_phdr.h"
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070054#include "linker_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
57 *
58 * Do NOT use malloc() and friends or pthread_*() code here.
59 * Don't use printf() either; it's caused mysterious memory
60 * corruption in the past.
61 * The linker runs before we bring up libc and it's easiest
62 * to make sure it does not depend on any complex libc features
63 *
64 * open issues / todo:
65 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067 * - after linking, set as much stuff as possible to READONLY
68 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070069 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070
Dmitriy Ivanov489e4982014-05-19 15:19:52 -070071#if defined(__LP64__)
72#define SEARCH_NAME(x) x
73#else
74// Nvidia drivers are relying on the bug:
75// http://code.google.com/p/android/issues/detail?id=6670
76// so we continue to use base-name lookup for lp32
77static const char* get_base_name(const char* name) {
78 const char* bname = strrchr(name, '/');
79 return bname ? bname + 1 : name;
80}
81#define SEARCH_NAME(x) get_base_name(x)
82#endif
83
Elliott Hughes0266ae52014-02-10 17:46:57 -080084static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085
Elliott Hughes1728b232014-05-14 10:02:03 -070086static LinkerAllocator<soinfo> g_soinfo_allocator;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070087static LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020088
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070089static soinfo* solist;
90static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -070091static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092
Elliott Hughes1728b232014-05-14 10:02:03 -070093static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070094#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070095 "/vendor/lib64",
96 "/system/lib64",
97#else
Elliott Hughes124fae92012-10-31 14:20:03 -070098 "/vendor/lib",
99 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700100#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700101 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700102};
David Bartleybc3a5c22009-06-02 18:27:28 -0700103
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800104#define LDPATH_BUFSIZE (LDPATH_MAX*64)
105#define LDPATH_MAX 8
106
107#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
108#define LDPRELOAD_MAX 8
109
Dmitriy Ivanovda8e5912014-10-31 13:46:42 -0700110#define MAX_PATH_LEN 512
111
Elliott Hughes1728b232014-05-14 10:02:03 -0700112static char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
113static const char* g_ld_library_paths[LDPATH_MAX + 1];
Elliott Hughes124fae92012-10-31 14:20:03 -0700114
Elliott Hughes1728b232014-05-14 10:02:03 -0700115static char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
116static const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600117
Elliott Hughes1728b232014-05-14 10:02:03 -0700118static soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600119
Elliott Hughes1728b232014-05-14 10:02:03 -0700120__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700122__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700123
Elliott Hughesbedfe382012-08-14 14:07:59 -0700124enum RelocationKind {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700125 kRelocAbsolute = 0,
126 kRelocRelative,
127 kRelocCopy,
128 kRelocSymbol,
129 kRelocMax
Elliott Hughesbedfe382012-08-14 14:07:59 -0700130};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100131
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700133struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700134 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700135};
136
137static linker_stats_t linker_stats;
138
139static void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700140 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700141}
142#else
143static void count_relocation(RelocationKind) {
144}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145#endif
146
147#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700148static unsigned bitmask[4096];
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100149#if defined(__LP64__)
150#define MARK(offset) \
151 do { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700152 if ((((offset) >> 12) >> 5) < 4096) \
153 bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800154 } while (0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100155#else
Elliott Hughesbedfe382012-08-14 14:07:59 -0700156#define MARK(offset) \
157 do { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700158 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800159 } while (0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100160#endif
Elliott Hughesbedfe382012-08-14 14:07:59 -0700161#else
162#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163#endif
164
Elliott Hughes46882792012-08-03 16:49:39 -0700165// You shouldn't try to call memory-allocating functions in the dynamic linker.
166// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700167#define DISALLOW_ALLOCATION(return_type, name, ...) \
168 return_type name __VA_ARGS__ \
169 { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700170 __libc_fatal("ERROR: " #name " called from the dynamic linker!\n"); \
Dima Zavin2e855792009-05-20 18:28:09 -0700171 }
Kito Cheng812fd422014-03-25 22:53:56 +0800172DISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
173DISALLOW_ALLOCATION(void, free, (void* u __unused));
174DISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
175DISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
Dima Zavin2e855792009-05-20 18:28:09 -0700176
177static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700178
Elliott Hughes650be4e2013-03-05 18:47:58 -0800179char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700180 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700181}
182
Elliott Hughes650be4e2013-03-05 18:47:58 -0800183size_t linker_get_error_buffer_size() {
184 return sizeof(__linker_dl_err_buf);
185}
186
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700187// This function is an empty stub where GDB locates a breakpoint to get notified
188// about linker activity.
Elliott Hughes5419b942012-10-16 15:54:46 -0700189extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190
Elliott Hughes1728b232014-05-14 10:02:03 -0700191static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700192static 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 -0800193static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800194
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800195static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700196 // Copy the necessary fields into the debug structure.
197 link_map* map = &(info->link_map_head);
198 map->l_addr = info->load_bias;
Dmitriy Ivanovc9d16582014-10-24 14:46:12 -0700199 map->l_name = info->name;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700200 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700202 // Stick the new library at the end of the list.
203 // gdb tends to care more about libc than it does
204 // about leaf libraries, and ordering it this way
205 // reduces the back-and-forth over the wire.
206 if (r_debug_tail) {
207 r_debug_tail->l_next = map;
208 map->l_prev = r_debug_tail;
209 map->l_next = 0;
210 } else {
211 _r_debug.r_map = map;
212 map->l_prev = 0;
213 map->l_next = 0;
214 }
215 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216}
217
Elliott Hughesbedfe382012-08-14 14:07:59 -0700218static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700219 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700220
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700221 if (r_debug_tail == map) {
222 r_debug_tail = map->l_prev;
223 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700224
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700225 if (map->l_prev) {
226 map->l_prev->l_next = map->l_next;
227 }
228 if (map->l_next) {
229 map->l_next->l_prev = map->l_prev;
230 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700231}
232
Elliott Hughesbedfe382012-08-14 14:07:59 -0700233static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700234 if (info->flags & FLAG_EXE) {
235 // GDB already knows about the main executable
236 return;
237 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700239 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700241 _r_debug.r_state = r_debug::RT_ADD;
242 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700244 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700246 _r_debug.r_state = r_debug::RT_CONSISTENT;
247 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700248}
249
Elliott Hughesbedfe382012-08-14 14:07:59 -0700250static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700251 if (info->flags & FLAG_EXE) {
252 // GDB already knows about the main executable
253 return;
254 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700255
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700256 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700257
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700258 _r_debug.r_state = r_debug::RT_DELETE;
259 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700260
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700261 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700262
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700263 _r_debug.r_state = r_debug::RT_CONSISTENT;
264 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800265}
266
Elliott Hughes18a206c2012-10-29 17:37:13 -0700267void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800268 _r_debug.r_state = r_debug::RT_ADD;
269 rtld_db_dlactivity();
270 _r_debug.r_state = r_debug::RT_CONSISTENT;
271 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800272}
273
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700274LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
275 return g_soinfo_links_allocator.alloc();
276}
277
278void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
279 g_soinfo_links_allocator.free(entry);
280}
281
282static void protect_data(int protection) {
283 g_soinfo_allocator.protect_all(protection);
284 g_soinfo_links_allocator.protect_all(protection);
285}
286
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700287static 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 +0200288 if (strlen(name) >= SOINFO_NAME_LEN) {
289 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700290 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200291 }
292
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700293 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700294
Magnus Malmbornba98d922012-09-12 13:00:55 +0200295 sonext->next = si;
296 sonext = si;
297
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700298 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200299 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800300}
301
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800302static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700303 if (si == nullptr) {
304 return;
305 }
306
307 if (si->base != 0 && si->size != 0) {
308 munmap(reinterpret_cast<void*>(si->base), si->size);
309 }
310
311 soinfo *prev = nullptr, *trav;
312
313 TRACE("name %s: freeing soinfo @ %p", si->name, si);
314
315 for (trav = solist; trav != nullptr; trav = trav->next) {
316 if (trav == si) {
317 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700318 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700319 prev = trav;
320 }
321 if (trav == nullptr) {
322 // si was not in solist
323 DL_ERR("name \"%s\" is not in solist!", si->name);
324 return;
325 }
Elliott Hughes46882792012-08-03 16:49:39 -0700326
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700327 // clear links to/from si
328 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700329
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700330 // prev will never be null, because the first entry in solist is
331 // always the static libdl_info.
332 prev->next = si->next;
333 if (si == sonext) {
334 sonext = prev;
335 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700337 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338}
339
Elliott Hughescade4c32012-12-20 14:42:14 -0800340
341static void parse_path(const char* path, const char* delimiters,
342 const char** array, char* buf, size_t buf_size, size_t max_count) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700343 if (path == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800344 return;
345 }
346
347 size_t len = strlcpy(buf, path, buf_size);
348
349 size_t i = 0;
350 char* buf_p = buf;
351 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
352 if (*array[i] != '\0') {
353 ++i;
354 }
355 }
356
357 // Forget the last path if we had to truncate; this occurs if the 2nd to
358 // last char isn't '\0' (i.e. wasn't originally a delimiter).
359 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700360 array[i - 1] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800361 } else {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700362 array[i] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800363 }
364}
365
366static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700367 parse_path(path, ":", g_ld_library_paths,
368 g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800369}
370
371static void parse_LD_PRELOAD(const char* path) {
372 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes1728b232014-05-14 10:02:03 -0700373 parse_path(path, " :", g_ld_preload_names,
374 g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800375}
376
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700377#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700378
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700379// For a given PC, find the .so that it belongs to.
380// Returns the base address of the .ARM.exidx section
381// for that .so, and the number of 8-byte entries
382// in that section (via *pcount).
383//
384// Intended to be called by libc's __gnu_Unwind_Find_exidx().
385//
386// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800387_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700388 unsigned addr = (unsigned)pc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800389
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700390 for (soinfo* si = solist; si != 0; si = si->next) {
391 if ((addr >= si->base) && (addr < (si->base + si->size))) {
392 *pcount = si->ARM_exidx_count;
393 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800394 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700395 }
396 *pcount = 0;
397 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398}
Elliott Hughes46882792012-08-03 16:49:39 -0700399
Christopher Ferris24053a42013-08-19 17:45:09 -0700400#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700401
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700402// Here, we only have to provide a callback to iterate across all the
403// loaded libraries. gcc_eh does the rest.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800404int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700405 int rv = 0;
406 for (soinfo* si = solist; si != nullptr; si = si->next) {
407 dl_phdr_info dl_info;
408 dl_info.dlpi_addr = si->link_map_head.l_addr;
409 dl_info.dlpi_name = si->link_map_head.l_name;
410 dl_info.dlpi_phdr = si->phdr;
411 dl_info.dlpi_phnum = si->phnum;
412 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
413 if (rv != 0) {
414 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800415 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700416 }
417 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800418}
Elliott Hughes46882792012-08-03 16:49:39 -0700419
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700420static ElfW(Sym)* soinfo_elf_lookup(const soinfo* si, unsigned hash, const char* name) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800421 ElfW(Sym)* symtab = si->symtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800422
Elliott Hughes0266ae52014-02-10 17:46:57 -0800423 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
424 name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800425
Elliott Hughes0266ae52014-02-10 17:46:57 -0800426 for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
427 ElfW(Sym)* s = symtab + n;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700428 if (strcmp(si->get_string(s->st_name), name)) continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800429
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700430 // only concern ourselves with global and weak symbol definitions
Elliott Hughes0266ae52014-02-10 17:46:57 -0800431 switch (ELF_ST_BIND(s->st_info)) {
432 case STB_GLOBAL:
433 case STB_WEAK:
434 if (s->st_shndx == SHN_UNDEF) {
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -0700435 continue;
436 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800437
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -0700438 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Elliott Hughes0266ae52014-02-10 17:46:57 -0800439 name, si->name, reinterpret_cast<void*>(s->st_value),
440 static_cast<size_t>(s->st_size));
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -0700441 return s;
442 case STB_LOCAL:
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700443 continue;
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -0700444 default:
Dmitriy Ivanov12bf3bc2014-07-01 14:24:45 -0700445 __libc_fatal("ERROR: Unexpected ST_BIND value: %d for '%s' in '%s'",
446 ELF_ST_BIND(s->st_info), name, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800447 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800448 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800449
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700450 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
451 name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
452
453
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700454 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800455}
456
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700457soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700458 memset(this, 0, sizeof(*this));
459
460 strlcpy(this->name, name, sizeof(this->name));
461 flags = FLAG_NEW_SOINFO;
462 version = SOINFO_VERSION;
463
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700464 if (file_stat != nullptr) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700465 this->st_dev = file_stat->st_dev;
466 this->st_ino = file_stat->st_ino;
467 this->file_offset = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700468 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700469
470 this->rtld_flags = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700471}
472
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800473static unsigned elfhash(const char* _name) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700474 const unsigned char* name = reinterpret_cast<const unsigned char*>(_name);
475 unsigned h = 0, g;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800476
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700477 while (*name) {
478 h = (h << 4) + *name++;
479 g = h & 0xf0000000;
480 h ^= g;
481 h ^= g >> 24;
482 }
483 return h;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800484}
485
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700486static ElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
487 const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700488 unsigned elf_hash = elfhash(name);
489 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700490
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700491 /* "This element's presence in a shared object library alters the dynamic linker's
492 * symbol resolution algorithm for references within the library. Instead of starting
493 * a symbol search with the executable file, the dynamic linker starts from the shared
494 * object itself. If the shared object fails to supply the referenced symbol, the
495 * dynamic linker then searches the executable file and other shared objects as usual."
496 *
497 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
498 *
499 * Note that this is unlikely since static linker avoids generating
500 * relocations for -Bsymbolic linked dynamic executables.
501 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700502 if (si_from->has_DT_SYMBOLIC) {
503 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->name, name);
504 s = soinfo_elf_lookup(si_from, elf_hash, name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700505 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700506 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700507 }
508 }
509
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700510 // 1. Look for it in global_group
511 if (s == nullptr) {
512 global_group.visit([&](soinfo* global_si) {
513 DEBUG("%s: looking up %s in %s (from global group)", si_from->name, name, global_si->name);
514 s = soinfo_elf_lookup(global_si, elf_hash, name);
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700515 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700516 *si_found_in = global_si;
517 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700518 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700519
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700520 return true;
521 });
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700522 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700523
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700524 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700525 if (s == nullptr) {
526 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700527 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700528 // we already did this - skip
529 return true;
530 }
531
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700532 DEBUG("%s: looking up %s in %s (from local group)", si_from->name, name, local_si->name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700533 s = soinfo_elf_lookup(local_si, elf_hash, name);
534 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700535 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700536 return false;
537 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700538
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700539 return true;
540 });
541 }
542
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700543 if (s != nullptr) {
544 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
545 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700546 si_from->name, name, reinterpret_cast<void*>(s->st_value),
547 (*si_found_in)->name, reinterpret_cast<void*>((*si_found_in)->base),
548 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700549 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700550
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700551 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700552}
553
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700554// Each size has it's own allocator.
555template<size_t size>
556class SizeBasedAllocator {
557 public:
558 static void* alloc() {
559 return allocator_.alloc();
560 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700561
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700562 static void free(void* ptr) {
563 allocator_.free(ptr);
564 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700565
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700566 private:
567 static LinkerBlockAllocator allocator_;
568};
569
570template<size_t size>
571LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
572
573template<typename T>
574class TypeBasedAllocator {
575 public:
576 static T* alloc() {
577 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
578 }
579
580 static void free(T* ptr) {
581 SizeBasedAllocator<sizeof(T)>::free(ptr);
582 }
583};
584
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700585class LoadTask {
586 public:
587 struct deleter_t {
588 void operator()(LoadTask* t) {
589 TypeBasedAllocator<LoadTask>::free(t);
590 }
591 };
592
593 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
594
595 static deleter_t deleter;
596
597 static LoadTask* create(const char* name, soinfo* needed_by) {
598 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
599 return new (ptr) LoadTask(name, needed_by);
600 }
601
602 const char* get_name() const {
603 return name_;
604 }
605
606 soinfo* get_needed_by() const {
607 return needed_by_;
608 }
609 private:
610 LoadTask(const char* name, soinfo* needed_by)
611 : name_(name), needed_by_(needed_by) {}
612
613 const char* name_;
614 soinfo* needed_by_;
615
616 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
617};
618
Ningsheng Jiane93be992014-09-16 15:22:10 +0800619LoadTask::deleter_t LoadTask::deleter;
620
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700621template <typename T>
622using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
623
624typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700625typedef linked_list_t<const char> StringLinkedList;
626typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700627
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700628
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700629// This function walks down the tree of soinfo dependencies
630// in breadth-first order and
631// * calls action(soinfo* si) for each node, and
632// * terminates walk if action returns false.
633//
634// walk_dependencies_tree returns false if walk was terminated
635// by the action and true otherwise.
636template<typename F>
637static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700638 SoinfoLinkedList visit_list;
639 SoinfoLinkedList visited;
640
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700641 for (size_t i = 0; i < root_soinfos_size; ++i) {
642 visit_list.push_back(root_soinfos[i]);
643 }
644
645 soinfo* si;
646 while ((si = visit_list.pop_front()) != nullptr) {
647 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700648 continue;
649 }
650
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700651 if (!action(si)) {
652 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700653 }
654
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700655 visited.push_back(si);
656
657 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700658 visit_list.push_back(child);
659 });
660 }
661
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700662 return true;
663}
664
665
666// This is used by dlsym(3). It performs symbol lookup only within the
667// specified soinfo object and its dependencies in breadth first order.
668ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
669 ElfW(Sym)* result = nullptr;
670 uint32_t elf_hash = elfhash(name);
671
672
673 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
674 result = soinfo_elf_lookup(current_soinfo, elf_hash, name);
675 if (result != nullptr) {
676 *found = current_soinfo;
677 return false;
678 }
679
680 return true;
681 });
682
683 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800684}
685
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800686/* This is used by dlsym(3) to performs a global symbol lookup. If the
687 start value is null (for RTLD_DEFAULT), the search starts at the
688 beginning of the global solist. Otherwise the search starts at the
689 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700690 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700691ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800692 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800693
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700694 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800695 start = solist;
696 }
697
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700698 ElfW(Sym)* s = nullptr;
699 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700700 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
701 continue;
702 }
703
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700704 s = soinfo_elf_lookup(si, elf_hash, name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700705 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800706 *found = si;
707 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600708 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800709 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600710
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700711 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700712 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
713 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800714 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800715
Elliott Hughescade4c32012-12-20 14:42:14 -0800716 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800717}
718
Kito Chengfa8c05d2013-03-12 14:58:06 +0800719soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800720 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700721 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800722 if (address >= si->base && address - si->base < si->size) {
723 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600724 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800725 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700726 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600727}
728
Elliott Hughes0266ae52014-02-10 17:46:57 -0800729ElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr) {
730 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - si->base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600731
Kito Chengfa8c05d2013-03-12 14:58:06 +0800732 // Search the library's symbol table for any defined symbol which
733 // contains this address.
734 for (size_t i = 0; i < si->nchain; ++i) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800735 ElfW(Sym)* sym = &si->symtab[i];
Kito Chengfa8c05d2013-03-12 14:58:06 +0800736 if (sym->st_shndx != SHN_UNDEF &&
737 soaddr >= sym->st_value &&
738 soaddr < sym->st_value + sym->st_size) {
739 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600740 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800741 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600742
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700743 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600744}
745
Elliott Hughes124fae92012-10-31 14:20:03 -0700746static int open_library_on_path(const char* name, const char* const paths[]) {
Dmitriy Ivanovda8e5912014-10-31 13:46:42 -0700747 char buf[MAX_PATH_LEN];
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700748 for (size_t i = 0; paths[i] != nullptr; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800749 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700750 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700751 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700752 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800753 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700754 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
755 if (fd != -1) {
756 return fd;
757 }
758 }
759 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800760}
761
Elliott Hughes124fae92012-10-31 14:20:03 -0700762static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700763 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800764
Elliott Hughes124fae92012-10-31 14:20:03 -0700765 // 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 -0700766 if (strchr(name, '/') != nullptr) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700767 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
768 if (fd != -1) {
769 return fd;
770 }
771 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700772#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700773 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700774#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700775 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800776
Elliott Hughes124fae92012-10-31 14:20:03 -0700777 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Elliott Hughes1728b232014-05-14 10:02:03 -0700778 int fd = open_library_on_path(name, g_ld_library_paths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700779 if (fd == -1) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700780 fd = open_library_on_path(name, kDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700781 }
782 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800783}
784
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700785template<typename F>
786static void for_each_dt_needed(const soinfo* si, F action) {
787 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
788 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700789 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700790 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700791 }
792}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800793
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700794static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700795 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700796 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700797 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700798
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700799 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
800 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700801 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
802 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700803 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700804 } else {
805 // Open the file.
806 fd = open_library(name);
807 if (fd == -1) {
808 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700809 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700810 }
811
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700812 file_guard.reset(fd);
813 }
814
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700815 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700816 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700817 return nullptr;
818 }
819
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700820 struct stat file_stat;
821 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700822 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700823 return nullptr;
824 }
825
826 // Check for symlink and other situations where
827 // file can have different names.
828 for (soinfo* si = solist; si != nullptr; si = si->next) {
829 if (si->get_st_dev() != 0 &&
830 si->get_st_ino() != 0 &&
831 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700832 si->get_st_ino() == file_stat.st_ino &&
833 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700834 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
835 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700836 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700837 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700838
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700839 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700840 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700841 return nullptr;
842 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700843
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700844 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700845 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700846 if (!elf_reader.Load(extinfo)) {
847 return nullptr;
848 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800849
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700850 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700851 if (si == nullptr) {
852 return nullptr;
853 }
854 si->base = elf_reader.load_start();
855 si->size = elf_reader.load_size();
856 si->load_bias = elf_reader.load_bias();
857 si->phnum = elf_reader.phdr_count();
858 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700859
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700860 if (!si->PrelinkImage()) {
861 soinfo_free(si);
862 return nullptr;
863 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700864
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700865 for_each_dt_needed(si, [&] (const char* name) {
866 load_tasks.push_back(LoadTask::create(name, si));
867 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700868
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700869 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800870}
871
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700872static soinfo *find_loaded_library_by_name(const char* name) {
873 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700874 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700875 if (!strcmp(search_name, si->name)) {
876 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200877 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700878 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700879 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200880}
881
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700882static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200883
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700884 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700885
886 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700887 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700888 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700889 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700890 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700891 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800892
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700893 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800894}
895
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700896static void soinfo_unload(soinfo* si);
897
898static bool is_recursive(soinfo* si, soinfo* parent) {
899 if (parent == nullptr) {
900 return false;
Dmitriy Ivanova3ad4502014-07-29 14:21:45 -0700901 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700902
903 if (si == parent) {
904 DL_ERR("recursive link to \"%s\"", si->name);
905 return true;
906 }
907
908 return !parent->get_parents().visit([&](soinfo* grandparent) {
909 return !is_recursive(si, grandparent);
910 });
911}
912
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700913// TODO: this is slightly unusual way to construct
914// the global group for relocation. Not every RTLD_GLOBAL
915// library is included in this group for backwards-compatibility
916// reasons.
917//
918// This group consists of the main executable, LD_PRELOADs
919// and libraries with the DF_1_GLOBAL flag set.
920static soinfo::soinfo_list_t make_global_group() {
921 soinfo::soinfo_list_t global_group;
922 for (soinfo* si = somain; si != nullptr; si = si->next) {
923 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
924 global_group.push_back(si);
925 }
926 }
927
928 return global_group;
929}
930
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700931static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
932 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700933 // Step 0: prepare.
934 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700935 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700936 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700937 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700938 }
939
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700940 // Construct global_group.
941 soinfo::soinfo_list_t global_group = make_global_group();
942
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700943 // If soinfos array is null allocate one on stack.
944 // The array is needed in case of failure; for example
945 // when library_names[] = {libone.so, libtwo.so} and libone.so
946 // is loaded correctly but libtwo.so failed for some reason.
947 // In this case libone.so should be unloaded on return.
948 // See also implementation of failure_guard below.
949
950 if (soinfos == nullptr) {
951 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
952 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
953 memset(soinfos, 0, soinfos_size);
954 }
955
956 // list of libraries to link - see step 2.
957 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700958
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700959 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700960 // Housekeeping
961 load_tasks.for_each([] (LoadTask* t) {
962 LoadTask::deleter(t);
963 });
964
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700965 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700966 soinfo_unload(soinfos[i]);
967 }
968 });
969
970 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
971 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 -0700972 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700973 if (si == nullptr) {
974 return false;
975 }
976
977 soinfo* needed_by = task->get_needed_by();
978
979 if (is_recursive(si, needed_by)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700980 return false;
981 }
982
983 si->ref_count++;
984 if (needed_by != nullptr) {
985 needed_by->add_child(si);
986 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700987
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700988 // When ld_preloads is not null, the first
989 // ld_preloads_count libs are in fact ld_preloads.
990 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700991 // Add LD_PRELOADed libraries to the global group for future runs.
992 // There is no need to explicitly add them to the global group
993 // for this run because they are going to appear in the local
994 // group in the correct order.
995 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700996 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700997 }
998
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700999 if (soinfos_count < library_names_count) {
1000 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001001 }
1002 }
1003
1004 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001005 soinfo::soinfo_list_t local_group;
1006 walk_dependencies_tree(
1007 start_with == nullptr ? soinfos : &start_with,
1008 start_with == nullptr ? soinfos_count : 1,
1009 [&] (soinfo* si) {
1010 local_group.push_back(si);
1011 return true;
1012 });
1013
1014 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001015 if ((si->flags & FLAG_LINKED) == 0) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001016 if (!si->LinkImage(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001017 return false;
1018 }
1019 si->flags |= FLAG_LINKED;
1020 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001021
1022 return true;
1023 });
1024
1025 if (linked) {
1026 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001027 }
1028
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001029 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001030}
1031
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001032static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001033 if (name == nullptr) {
1034 somain->ref_count++;
1035 return somain;
1036 }
1037
1038 soinfo* si;
1039
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001040 if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001041 return nullptr;
1042 }
1043
Elliott Hughesd23736e2012-11-01 15:16:56 -07001044 return si;
1045}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001046
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001047static void soinfo_unload(soinfo* si) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001048 if (!si->can_unload()) {
1049 TRACE("not unloading '%s' - the binary is flagged with NODELETE", si->name);
1050 return;
1051 }
1052
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001053 if (si->ref_count == 1) {
1054 TRACE("unloading '%s'", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001055 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001056
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001057 if (si->has_min_version(0)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001058 soinfo* child = nullptr;
1059 while ((child = si->get_children().pop_front()) != nullptr) {
1060 TRACE("%s needs to unload %s", si->name, child->name);
1061 soinfo_unload(child);
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001062 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001063 } else {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001064 for_each_dt_needed(si, [&] (const char* library_name) {
1065 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1066 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1067 if (needed != nullptr) {
1068 soinfo_unload(needed);
1069 } else {
1070 // Not found: for example if symlink was deleted between dlopen and dlclose
1071 // Since we cannot really handle errors at this point - print and continue.
1072 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001073 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001074 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001075 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001076
Elliott Hughesd23736e2012-11-01 15:16:56 -07001077 notify_gdb_of_unload(si);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001078 si->ref_count = 0;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -07001079 soinfo_free(si);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001080 } else {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001081 si->ref_count--;
Elliott Hughesc6200592013-09-30 18:43:46 -07001082 TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001083 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001084}
1085
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001086void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001087 // Use basic string manipulation calls to avoid snprintf.
1088 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1089 // When debug malloc is enabled, this call returns 0. This in turn causes
1090 // snprintf to do nothing, which causes libraries to fail to load.
1091 // See b/17302493 for further details.
1092 // Once the above bug is fixed, this code can be modified to use
1093 // snprintf again.
1094 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1095 if (buffer_size < required_len) {
1096 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1097 buffer_size, required_len);
1098 }
1099 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1100 *end = ':';
1101 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001102}
1103
Elliott Hughescade4c32012-12-20 14:42:14 -08001104void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
1105 if (!get_AT_SECURE()) {
1106 parse_LD_LIBRARY_PATH(ld_library_path);
1107 }
1108}
1109
Elliott Hughes1a586292014-06-03 16:23:08 -07001110soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001111 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001112 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001113 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001114 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001115 if (extinfo != nullptr) {
1116 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1117 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1118 return nullptr;
1119 }
1120 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001121 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1122 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 -07001123 return nullptr;
1124 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001125 }
Dmitriy Ivanovda8e5912014-10-31 13:46:42 -07001126
1127 size_t name_len = strlen(name);
1128 if (name_len >= MAX_PATH_LEN) {
1129 DL_ERR("library name \"%s\" is too long", name);
1130 return nullptr;
1131 }
1132
1133 char local_name[name_len+1];
1134 strlcpy(local_name, name, name_len+1);
1135
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001136 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanovda8e5912014-10-31 13:46:42 -07001137 soinfo* si = find_library(local_name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001138 if (si != nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001139 si->CallConstructors();
1140 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001141 protect_data(PROT_READ);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001142 return si;
1143}
1144
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001145void do_dlclose(soinfo* si) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001146 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001147 soinfo_unload(si);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001148 protect_data(PROT_READ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001149}
1150
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001151static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1152 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1153 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1154 ElfW(Addr) ifunc_addr = ifunc_resolver();
1155 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 -07001156
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001157 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001158}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001159
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001160#if defined(USE_RELA)
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001161int soinfo::Relocate(ElfW(Rela)* rela, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001162 for (size_t idx = 0; idx < count; ++idx, ++rela) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001163 unsigned type = ELFW(R_TYPE)(rela->r_info);
1164 unsigned sym = ELFW(R_SYM)(rela->r_info);
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001165 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001166 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001167 const char* sym_name = nullptr;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001168
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001169 DEBUG("Processing '%s' relocation at index %zd", name, idx);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001170 if (type == 0) { // R_*_NONE
1171 continue;
1172 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001173
1174 ElfW(Sym)* s = nullptr;
1175 soinfo* lsi = nullptr;
1176
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001177 if (sym != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001178 sym_name = get_string(symtab[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001179 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001180 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001181 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001182 s = &symtab[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001183 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001184 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001185 return -1;
1186 }
1187
1188 /* IHI0044C AAELF 4.5.1.1:
1189
1190 Libraries are not searched to resolve weak references.
1191 It is not an error for a weak reference to remain unsatisfied.
1192
1193 During linking, the value of an undefined weak reference is:
1194 - Zero if the relocation type is absolute
1195 - The address of the place if the relocation is pc-relative
1196 - The address of nominal base address if the relocation
1197 type is base-relative.
1198 */
1199
1200 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001201#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001202 case R_AARCH64_JUMP_SLOT:
1203 case R_AARCH64_GLOB_DAT:
1204 case R_AARCH64_ABS64:
1205 case R_AARCH64_ABS32:
1206 case R_AARCH64_ABS16:
1207 case R_AARCH64_RELATIVE:
1208 case R_AARCH64_IRELATIVE:
1209 /*
1210 * The sym_addr was initialized to be zero above, or the relocation
1211 * code below does not care about value of sym_addr.
1212 * No need to do anything.
1213 */
1214 break;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001215#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001216 case R_X86_64_JUMP_SLOT:
1217 case R_X86_64_GLOB_DAT:
1218 case R_X86_64_32:
1219 case R_X86_64_64:
1220 case R_X86_64_RELATIVE:
1221 case R_X86_64_IRELATIVE:
1222 // No need to do anything.
1223 break;
1224 case R_X86_64_PC32:
1225 sym_addr = reloc;
1226 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001227#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001228 default:
1229 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
1230 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001231 }
1232 } else {
1233 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001234 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001235 }
1236 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001237 }
1238
1239 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001240#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001241 case R_AARCH64_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001242 count_relocation(kRelocAbsolute);
1243 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001244 TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
1245 reloc, (sym_addr + rela->r_addend), sym_name);
1246 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001247 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001248 case R_AARCH64_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001249 count_relocation(kRelocAbsolute);
1250 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001251 TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
1252 reloc, (sym_addr + rela->r_addend), sym_name);
1253 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001254 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001255 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001256 count_relocation(kRelocAbsolute);
1257 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001258 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
1259 reloc, (sym_addr + rela->r_addend), sym_name);
1260 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001261 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001262 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001263 count_relocation(kRelocAbsolute);
1264 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001265 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
1266 reloc, (sym_addr + rela->r_addend), sym_name);
1267 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1268 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001269 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001270 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001271 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1272 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1273 static_cast<ElfW(Addr)>(INT32_MIN),
1274 static_cast<ElfW(Addr)>(UINT32_MAX));
1275 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001276 }
1277 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001278 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001279 count_relocation(kRelocAbsolute);
1280 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001281 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
1282 reloc, (sym_addr + rela->r_addend), sym_name);
1283 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1284 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001285 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001286 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001287 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1288 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1289 static_cast<ElfW(Addr)>(INT16_MIN),
1290 static_cast<ElfW(Addr)>(UINT16_MAX));
1291 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001292 }
1293 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001294 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001295 count_relocation(kRelocRelative);
1296 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001297 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
1298 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1299 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001300 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001301 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001302 count_relocation(kRelocRelative);
1303 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001304 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
1305 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1306 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1307 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001308 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001309 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001310 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1311 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1312 static_cast<ElfW(Addr)>(INT32_MIN),
1313 static_cast<ElfW(Addr)>(UINT32_MAX));
1314 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001315 }
1316 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001317 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001318 count_relocation(kRelocRelative);
1319 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001320 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
1321 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1322 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1323 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001324 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001325 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001326 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1327 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1328 static_cast<ElfW(Addr)>(INT16_MIN),
1329 static_cast<ElfW(Addr)>(UINT16_MAX));
1330 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001331 }
1332 break;
1333
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001334 case R_AARCH64_RELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001335 count_relocation(kRelocRelative);
1336 MARK(rela->r_offset);
1337 if (sym) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001338 DL_ERR("odd RELATIVE form...");
1339 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001340 }
Elliott Hughes0266ae52014-02-10 17:46:57 -08001341 TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001342 reloc, (base + rela->r_addend));
1343 *reinterpret_cast<ElfW(Addr)*>(reloc) = (base + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001344 break;
1345
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001346 case R_AARCH64_IRELATIVE:
1347 count_relocation(kRelocRelative);
1348 MARK(rela->r_offset);
1349 TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
1350 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
1351 break;
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001352
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001353 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001354 /*
1355 * ET_EXEC is not supported so this should not happen.
1356 *
1357 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1358 *
1359 * Section 4.7.1.10 "Dynamic relocations"
1360 * R_AARCH64_COPY may only appear in executable objects where e_type is
1361 * set to ET_EXEC.
1362 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001363 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Nick Kralevich76e289c2014-07-03 12:04:31 -07001364 return -1;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001365 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001366 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
1367 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001368 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001369 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001370 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
1371 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001372 break;
1373#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001374 case R_X86_64_JUMP_SLOT:
1375 count_relocation(kRelocAbsolute);
1376 MARK(rela->r_offset);
1377 TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1378 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1379 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1380 break;
1381 case R_X86_64_GLOB_DAT:
1382 count_relocation(kRelocAbsolute);
1383 MARK(rela->r_offset);
1384 TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1385 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1386 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1387 break;
1388 case R_X86_64_RELATIVE:
1389 count_relocation(kRelocRelative);
1390 MARK(rela->r_offset);
1391 if (sym) {
1392 DL_ERR("odd RELATIVE form...");
1393 return -1;
1394 }
1395 TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1396 static_cast<size_t>(base));
1397 *reinterpret_cast<ElfW(Addr)*>(reloc) = base + rela->r_addend;
1398 break;
1399 case R_X86_64_IRELATIVE:
1400 count_relocation(kRelocRelative);
1401 MARK(rela->r_offset);
1402 TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
1403 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
1404 break;
1405 case R_X86_64_32:
1406 count_relocation(kRelocRelative);
1407 MARK(rela->r_offset);
1408 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1409 static_cast<size_t>(sym_addr), sym_name);
1410 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1411 break;
1412 case R_X86_64_64:
1413 count_relocation(kRelocRelative);
1414 MARK(rela->r_offset);
1415 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1416 static_cast<size_t>(sym_addr), sym_name);
1417 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1418 break;
1419 case R_X86_64_PC32:
1420 count_relocation(kRelocRelative);
1421 MARK(rela->r_offset);
1422 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1423 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1424 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
1425 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
1426 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001427#endif
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001428
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001429 default:
1430 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
1431 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001432 }
1433 }
1434 return 0;
1435}
Chris Dearman99186652014-02-06 20:36:51 -08001436
1437#else // REL, not RELA.
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001438int soinfo::Relocate(ElfW(Rel)* rel, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001439 for (size_t idx = 0; idx < count; ++idx, ++rel) {
1440 unsigned type = ELFW(R_TYPE)(rel->r_info);
1441 // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
1442 unsigned sym = ELFW(R_SYM)(rel->r_info);
1443 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
1444 ElfW(Addr) sym_addr = 0;
1445 const char* sym_name = nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001446
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001447 DEBUG("Processing '%s' relocation at index %zd", name, idx);
1448 if (type == 0) { // R_*_NONE
1449 continue;
1450 }
1451
1452 ElfW(Sym)* s = nullptr;
1453 soinfo* lsi = nullptr;
1454
1455 if (sym != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001456 sym_name = get_string(symtab[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001457 s = soinfo_do_lookup(this, sym_name, &lsi, global_group, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001458 if (s == nullptr) {
1459 // We only allow an undefined symbol if this is a weak reference...
1460 s = &symtab[sym];
1461 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1462 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
1463 return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001464 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001465
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001466 /* IHI0044C AAELF 4.5.1.1:
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001467
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001468 Libraries are not searched to resolve weak references.
1469 It is not an error for a weak reference to remain
1470 unsatisfied.
Doug Kwane8238072009-10-26 12:05:23 -07001471
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001472 During linking, the value of an undefined weak reference is:
1473 - Zero if the relocation type is absolute
1474 - The address of the place if the relocation is pc-relative
1475 - The address of nominal base address if the relocation
1476 type is base-relative.
1477 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001478
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001479 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001480#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001481 case R_ARM_JUMP_SLOT:
1482 case R_ARM_GLOB_DAT:
1483 case R_ARM_ABS32:
1484 case R_ARM_RELATIVE: /* Don't care. */
1485 // sym_addr was initialized to be zero above or relocation
1486 // code below does not care about value of sym_addr.
1487 // No need to do anything.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001488 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001489#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001490 case R_386_JMP_SLOT:
1491 case R_386_GLOB_DAT:
1492 case R_386_32:
1493 case R_386_RELATIVE: /* Don't care. */
1494 case R_386_IRELATIVE:
1495 // sym_addr was initialized to be zero above or relocation
1496 // code below does not care about value of sym_addr.
1497 // No need to do anything.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001498 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001499 case R_386_PC32:
1500 sym_addr = reloc;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001501 break;
1502#endif
1503
1504#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001505 case R_ARM_COPY:
1506 // Fall through. Can't really copy if weak symbol is not found at run-time.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001507#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001508 default:
1509 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001510 return -1;
1511 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001512 } else {
1513 // We got a definition.
1514 sym_addr = lsi->resolve_symbol_address(s);
1515 }
1516 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001517 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001518
1519 switch (type) {
1520#if defined(__arm__)
1521 case R_ARM_JUMP_SLOT:
1522 count_relocation(kRelocAbsolute);
1523 MARK(rel->r_offset);
1524 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1525 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1526 break;
1527 case R_ARM_GLOB_DAT:
1528 count_relocation(kRelocAbsolute);
1529 MARK(rel->r_offset);
1530 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1531 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1532 break;
1533 case R_ARM_ABS32:
1534 count_relocation(kRelocAbsolute);
1535 MARK(rel->r_offset);
1536 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1537 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1538 break;
1539 case R_ARM_REL32:
1540 count_relocation(kRelocRelative);
1541 MARK(rel->r_offset);
1542 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1543 reloc, sym_addr, rel->r_offset, sym_name);
1544 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1545 break;
1546 case R_ARM_COPY:
1547 /*
1548 * ET_EXEC is not supported so this should not happen.
1549 *
1550 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1551 *
1552 * Section 4.7.1.10 "Dynamic relocations"
1553 * R_ARM_COPY may only appear in executable objects where e_type is
1554 * set to ET_EXEC.
1555 */
1556 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
1557 return -1;
1558#elif defined(__i386__)
1559 case R_386_JMP_SLOT:
1560 count_relocation(kRelocAbsolute);
1561 MARK(rel->r_offset);
1562 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1563 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1564 break;
1565 case R_386_GLOB_DAT:
1566 count_relocation(kRelocAbsolute);
1567 MARK(rel->r_offset);
1568 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1569 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1570 break;
1571 case R_386_32:
1572 count_relocation(kRelocRelative);
1573 MARK(rel->r_offset);
1574 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1575 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1576 break;
1577 case R_386_PC32:
1578 count_relocation(kRelocRelative);
1579 MARK(rel->r_offset);
1580 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1581 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1582 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1583 break;
1584#elif defined(__mips__)
1585 case R_MIPS_REL32:
1586#if defined(__LP64__)
1587 // MIPS Elf64_Rel entries contain compound relocations
1588 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
1589 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
1590 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
1591 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
1592 type, (unsigned)ELF64_R_TYPE2(rel->r_info),
1593 (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
1594 return -1;
1595 }
1596#endif
1597 count_relocation(kRelocAbsolute);
1598 MARK(rel->r_offset);
1599 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
1600 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
1601 if (s) {
1602 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1603 } else {
1604 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1605 }
1606 break;
1607#endif
1608
1609#if defined(__arm__)
1610 case R_ARM_RELATIVE:
1611#elif defined(__i386__)
1612 case R_386_RELATIVE:
1613#endif
1614 count_relocation(kRelocRelative);
1615 MARK(rel->r_offset);
1616 if (sym) {
1617 DL_ERR("odd RELATIVE form...");
1618 return -1;
1619 }
1620 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1621 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1622 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1623 break;
1624#if defined(__i386__)
1625 case R_386_IRELATIVE:
1626 count_relocation(kRelocRelative);
1627 MARK(rel->r_offset);
1628 TRACE_TYPE(RELO, "RELO IRELATIVE %p <- %p", reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1629 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + *reinterpret_cast<ElfW(Addr)*>(reloc));
1630 break;
1631#endif
1632
1633 default:
1634 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
1635 return -1;
1636 }
1637 }
1638 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001639}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001640#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001641
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001642#if defined(__mips__)
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001643static bool mips_relocate_got(soinfo* si, const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001644 ElfW(Addr)** got = si->plt_got;
1645 if (got == nullptr) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001646 return true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001647 }
1648 unsigned local_gotno = si->mips_local_gotno;
1649 unsigned gotsym = si->mips_gotsym;
1650 unsigned symtabno = si->mips_symtabno;
1651 ElfW(Sym)* symtab = si->symtab;
1652
1653 // got[0] is the address of the lazy resolver function.
1654 // got[1] may be used for a GNU extension.
1655 // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
1656 // FIXME: maybe this should be in a separate routine?
1657 if ((si->flags & FLAG_LINKER) == 0) {
1658 size_t g = 0;
1659 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
1660 if (reinterpret_cast<intptr_t>(got[g]) < 0) {
1661 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
1662 }
1663 // Relocate the local GOT entries.
1664 for (; g < local_gotno; g++) {
1665 got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias);
1666 }
1667 }
1668
1669 // Now for the global GOT entries...
1670 ElfW(Sym)* sym = symtab + gotsym;
1671 got = si->plt_got + local_gotno;
1672 for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
1673 // This is an undefined reference... try to locate it.
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001674 const char* sym_name = si->get_string(sym->st_name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001675 soinfo* lsi = nullptr;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001676 ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi, global_group, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001677 if (s == nullptr) {
1678 // We only allow an undefined symbol if this is a weak reference.
1679 s = &symtab[g];
1680 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1681 DL_ERR("cannot locate \"%s\"...", sym_name);
1682 return false;
1683 }
1684 *got = 0;
1685 } else {
1686 // FIXME: is this sufficient?
1687 // For reference see NetBSD link loader
1688 // http://cvsweb.netbsd.org/bsdweb.cgi/src/libexec/ld.elf_so/arch/mips/mips_reloc.c?rev=1.53&content-type=text/x-cvsweb-markup
1689 *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s));
1690 }
1691 }
1692 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001693}
1694#endif
1695
Kito Cheng812fd422014-03-25 22:53:56 +08001696void soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001697 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001698 return;
1699 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001700
Elliott Hughesc6200592013-09-30 18:43:46 -07001701 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001702
1703 int begin = reverse ? (count - 1) : 0;
1704 int end = reverse ? -1 : count;
1705 int step = reverse ? -1 : 1;
1706
1707 for (int i = begin; i != end; i += step) {
1708 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1709 CallFunction("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001710 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001711
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001712 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001713}
1714
Kito Cheng812fd422014-03-25 22:53:56 +08001715void soinfo::CallFunction(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001716 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001717 return;
1718 }
1719
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001720 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001721 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001722 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001723
1724 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1725 // are still writable. This happens with our debug malloc (see http://b/7941716).
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001726 protect_data(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001727}
1728
Elliott Hughesd23736e2012-11-01 15:16:56 -07001729void soinfo::CallPreInitConstructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001730 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1731 // but ignored in a shared library.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001732 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1733}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001734
Elliott Hughesd23736e2012-11-01 15:16:56 -07001735void soinfo::CallConstructors() {
1736 if (constructors_called) {
1737 return;
1738 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001739
Elliott Hughesd23736e2012-11-01 15:16:56 -07001740 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1741 // protect against recursive constructor calls. One simple example of constructor recursion
1742 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1743 // 1. The program depends on libc, so libc's constructor is called here.
1744 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1745 // 3. dlopen() calls the constructors on the newly created
1746 // soinfo for libc_malloc_debug_leak.so.
1747 // 4. The debug .so depends on libc, so CallConstructors is
1748 // called again with the libc soinfo. If it doesn't trigger the early-
1749 // out above, the libc constructor will be called again (recursively!).
1750 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001751
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001752 if ((flags & FLAG_EXE) == 0 && preinit_array != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001753 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001754 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001755 name, preinit_array_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001756 }
1757
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001758 get_children().for_each([] (soinfo* si) {
1759 si->CallConstructors();
1760 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001761
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001762 TRACE("\"%s\": calling constructors", name);
1763
1764 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001765 CallFunction("DT_INIT", init_func);
1766 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001767}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001768
Elliott Hughesd23736e2012-11-01 15:16:56 -07001769void soinfo::CallDestructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001770 if (!constructors_called) {
1771 return;
1772 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001773 TRACE("\"%s\": calling destructors", name);
1774
1775 // DT_FINI_ARRAY must be parsed in reverse order.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001776 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001777
1778 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001779 CallFunction("DT_FINI", fini_func);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001780
1781 // This is needed on second call to dlopen
1782 // after library has been unloaded with RTLD_NODELETE
1783 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001784}
1785
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001786void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001787 if (has_min_version(0)) {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -07001788 child->parents.push_back(this);
1789 this->children.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001790 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001791}
1792
1793void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001794 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001795 return;
1796 }
1797
1798 // 1. Untie connected soinfos from 'this'.
1799 children.for_each([&] (soinfo* child) {
1800 child->parents.remove_if([&] (const soinfo* parent) {
1801 return parent == this;
1802 });
1803 });
1804
1805 parents.for_each([&] (soinfo* parent) {
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001806 parent->children.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001807 return child == this;
1808 });
1809 });
1810
1811 // 2. Once everything untied - clear local lists.
1812 parents.clear();
1813 children.clear();
1814}
1815
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001816dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001817 if (has_min_version(0)) {
1818 return st_dev;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001819 }
1820
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001821 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001822};
1823
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001824ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001825 if (has_min_version(0)) {
1826 return st_ino;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001827 }
1828
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001829 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001830}
1831
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001832off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001833 if (has_min_version(1)) {
1834 return file_offset;
1835 }
1836
1837 return 0;
1838}
1839
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001840uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001841 if (has_min_version(1)) {
1842 return rtld_flags;
1843 }
1844
1845 return 0;
1846}
1847
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001848uint32_t soinfo::get_dt_flags_1() const {
1849 if (has_min_version(1)) {
1850 return dt_flags_1;
1851 }
1852
1853 return 0;
1854}
1855void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1856 if (has_min_version(1)) {
1857 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
1858 rtld_flags |= RTLD_GLOBAL;
1859 }
1860
1861 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
1862 rtld_flags |= RTLD_NODELETE;
1863 }
1864
1865 this->dt_flags_1 = dt_flags_1;
1866 }
1867}
1868
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001869// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001870// 'this->flags' does not have FLAG_NEW_SOINFO set.
1871static soinfo::soinfo_list_t g_empty_list;
1872
1873soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001874 if (has_min_version(0)) {
1875 return this->children;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001876 }
1877
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001878 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001879}
1880
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001881soinfo::soinfo_list_t& soinfo::get_parents() {
1882 if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1883 return g_empty_list;
1884 }
1885
1886 return this->parents;
1887}
1888
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001889ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1890 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1891 return call_ifunc_resolver(s->st_value + load_bias);
1892 }
1893
1894 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1895}
1896
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001897const char* soinfo::get_string(ElfW(Word) index) const {
1898 if (has_min_version(1) && (index >= strtab_size)) {
1899 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size, index);
1900 }
1901
1902 return strtab + index;
1903}
1904
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001905bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001906 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001907}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001908
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001909/* Force any of the closed stdin, stdout and stderr to be associated with
1910 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001911static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001912 int dev_null, i, status;
1913 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001914
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001915 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
1916 if (dev_null < 0) {
1917 DL_ERR("cannot open /dev/null: %s", strerror(errno));
1918 return -1;
1919 }
1920 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001921
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001922 /* If any of the stdio file descriptors is valid and not associated
1923 with /dev/null, dup /dev/null to it. */
1924 for (i = 0; i < 3; i++) {
1925 /* If it is /dev/null already, we are done. */
1926 if (i == dev_null) {
1927 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001928 }
1929
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001930 TRACE("[ Nullifying stdio file descriptor %d]", i);
1931 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
1932
1933 /* If file is opened, we are good. */
1934 if (status != -1) {
1935 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001936 }
1937
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001938 /* The only error we allow is that the file descriptor does not
1939 exist, in which case we dup /dev/null to it. */
1940 if (errno != EBADF) {
1941 DL_ERR("fcntl failed: %s", strerror(errno));
1942 return_value = -1;
1943 continue;
1944 }
1945
1946 /* Try dupping /dev/null to this stdio file descriptor and
1947 repeat if there is a signal. Note that any errors in closing
1948 the stdio descriptor are lost. */
1949 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
1950 if (status < 0) {
1951 DL_ERR("dup2 failed: %s", strerror(errno));
1952 return_value = -1;
1953 continue;
1954 }
1955 }
1956
1957 /* If /dev/null is not one of the stdio file descriptors, close it. */
1958 if (dev_null > 2) {
1959 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
1960 status = TEMP_FAILURE_RETRY(close(dev_null));
1961 if (status == -1) {
1962 DL_ERR("close failed: %s", strerror(errno));
1963 return_value = -1;
1964 }
1965 }
1966
1967 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001968}
1969
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001970bool soinfo::PrelinkImage() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08001971 /* Extract dynamic section */
1972 ElfW(Word) dynamic_flags = 0;
1973 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07001974
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001975 /* We can't log anything until the linker is relocated */
1976 bool relocating_linker = (flags & FLAG_LINKER) != 0;
1977 if (!relocating_linker) {
1978 INFO("[ linking %s ]", name);
1979 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags);
1980 }
1981
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001982 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001983 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001984 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001985 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001986 return false;
1987 } else {
1988 if (!relocating_linker) {
1989 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001990 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001991 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001992
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001993#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001994 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
1995 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001996#endif
1997
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001998 // Extract useful information from dynamic section.
1999 uint32_t needed_count = 0;
2000 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2001 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2002 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2003 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002004 case DT_SONAME:
2005 // TODO: glibc dynamic linker uses this name for
2006 // initial library lookup; consider doing the same here.
2007 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002008
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002009 case DT_HASH:
2010 nbucket = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2011 nchain = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2012 bucket = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2013 chain = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket * 4);
2014 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002015
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002016 case DT_STRTAB:
2017 strtab = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
2018 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002019
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002020 case DT_STRSZ:
2021 strtab_size = d->d_un.d_val;
2022 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002023
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002024 case DT_SYMTAB:
2025 symtab = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
2026 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002027
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002028 case DT_SYMENT:
2029 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002030 DL_ERR("invalid DT_SYMENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002031 return false;
2032 }
2033 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002034
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002035 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002036#if defined(USE_RELA)
2037 if (d->d_un.d_val != DT_RELA) {
2038 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002039 return false;
2040 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002041#else
2042 if (d->d_un.d_val != DT_REL) {
2043 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
2044 return false;
2045 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002046#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002047 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002048
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002049 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002050#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002051 plt_rela = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002052#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002053 plt_rel = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002054#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002055 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002056
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002057 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002058#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002059 plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002060#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002061 plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002062#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002063 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002064
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002065 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002066#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002067 // Used by mips and mips64.
2068 plt_got = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002069#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002070 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2071 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002072
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002073 case DT_DEBUG:
2074 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2075 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002076// FIXME: not working currently for N64
2077// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002078// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002079// read-only, but the DYNAMIC header claims it is writable.
2080#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002081 if ((dynamic_flags & PF_W) != 0) {
2082 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2083 }
2084 break;
Chris Dearman99186652014-02-06 20:36:51 -08002085#endif
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002086#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002087 case DT_RELA:
2088 rela = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
2089 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002090
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002091 case DT_RELASZ:
2092 rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
2093 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002094
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002095 case DT_RELAENT:
2096 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002097 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002098 return false;
2099 }
2100 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002101
2102 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002103 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002104 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002105
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002106 case DT_REL:
2107 DL_ERR("unsupported DT_REL in \"%s\"", name);
2108 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002109
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002110 case DT_RELSZ:
2111 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2112 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002113#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002114 case DT_REL:
2115 rel = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
2116 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002117
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002118 case DT_RELSZ:
2119 rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
2120 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002121
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002122 case DT_RELENT:
2123 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002124 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002125 return false;
2126 }
2127 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002128
2129 // "Indicates that all RELATIVE relocations have been concatenated together,
2130 // and specifies the RELATIVE relocation count."
2131 //
2132 // TODO: Spec also mentions that this can be used to optimize relocation process;
2133 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002134 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002135 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002136 case DT_RELA:
2137 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2138 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002139#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002140 case DT_INIT:
2141 init_func = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2142 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func);
2143 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002144
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002145 case DT_FINI:
2146 fini_func = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2147 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func);
2148 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002149
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002150 case DT_INIT_ARRAY:
2151 init_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2152 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array);
2153 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002154
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002155 case DT_INIT_ARRAYSZ:
2156 init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
2157 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002158
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002159 case DT_FINI_ARRAY:
2160 fini_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2161 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array);
2162 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002163
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002164 case DT_FINI_ARRAYSZ:
2165 fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
2166 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002167
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002168 case DT_PREINIT_ARRAY:
2169 preinit_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2170 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array);
2171 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002172
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002173 case DT_PREINIT_ARRAYSZ:
2174 preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
2175 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002176
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002177 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002178#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002179 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2180 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002181#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002182 has_text_relocations = true;
2183 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002184#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002185
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002186 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002187 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002188 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002189
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002190 case DT_NEEDED:
2191 ++needed_count;
2192 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002193
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002194 case DT_FLAGS:
2195 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002196#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002197 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2198 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002199#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002200 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002201#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002202 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002203 if (d->d_un.d_val & DF_SYMBOLIC) {
2204 has_DT_SYMBOLIC = true;
2205 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002206 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002207
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002208 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002209 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002210
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002211 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002212 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2213 }
2214 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002215#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002216 case DT_MIPS_RLD_MAP:
2217 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2218 {
2219 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2220 *dp = &_r_debug;
2221 }
2222 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002223
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002224 case DT_MIPS_RLD_VERSION:
2225 case DT_MIPS_FLAGS:
2226 case DT_MIPS_BASE_ADDRESS:
2227 case DT_MIPS_UNREFEXTNO:
2228 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002229
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002230 case DT_MIPS_SYMTABNO:
2231 mips_symtabno = d->d_un.d_val;
2232 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002233
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002234 case DT_MIPS_LOCAL_GOTNO:
2235 mips_local_gotno = d->d_un.d_val;
2236 break;
2237
2238 case DT_MIPS_GOTSYM:
2239 mips_gotsym = d->d_un.d_val;
2240 break;
2241#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002242 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2243 case DT_BIND_NOW:
2244 break;
2245
2246 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002247 case DT_VERSYM:
2248 case DT_VERDEF:
2249 case DT_VERDEFNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002250 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002251
2252 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002253 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002254 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002255 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2256 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002257 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002258 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002259 }
2260
2261 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
2262 reinterpret_cast<void*>(base), strtab, symtab);
2263
2264 // Sanity checks.
2265 if (relocating_linker && needed_count != 0) {
2266 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2267 return false;
2268 }
2269 if (nbucket == 0) {
2270 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", name);
2271 return false;
2272 }
2273 if (strtab == 0) {
2274 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2275 return false;
2276 }
2277 if (symtab == 0) {
2278 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2279 return false;
2280 }
2281 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002282}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002283
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002284bool soinfo::LinkImage(const soinfo_list_t& global_group, const soinfo_list_t& local_group, const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002285
Elliott Hughese4d792a2013-10-28 14:19:05 -07002286#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002287 if (has_text_relocations) {
2288 // Make segments writable to allow text relocations to work properly. We will later call
2289 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2290 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2291 "security hardening. Please fix.", name);
2292 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2293 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2294 name, strerror(errno));
2295 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002296 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002297 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002298#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002299
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002300#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002301 if (rela != nullptr) {
2302 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002303 if (Relocate(rela, rela_count, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002304 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002305 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002306 }
2307 if (plt_rela != nullptr) {
2308 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002309 if (Relocate(plt_rela, plt_rela_count, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002310 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002311 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002312 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002313#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002314 if (rel != nullptr) {
2315 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002316 if (Relocate(rel, rel_count, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002317 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002318 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002319 }
2320 if (plt_rel != nullptr) {
2321 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002322 if (Relocate(plt_rel, plt_rel_count, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002323 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002324 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002325 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002326#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002327
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002328#if defined(__mips__)
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002329 if (!mips_relocate_got(this, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002330 return false;
2331 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002332#endif
2333
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002334 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002335
Elliott Hughese4d792a2013-10-28 14:19:05 -07002336#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002337 if (has_text_relocations) {
2338 // All relocations are done, we can protect our segments back to read-only.
2339 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2340 DL_ERR("can't protect segments for \"%s\": %s",
2341 name, strerror(errno));
2342 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002343 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002344 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002345#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002346
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002347 /* We can also turn on GNU RELRO protection */
2348 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2349 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2350 name, strerror(errno));
2351 return false;
2352 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002353
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002354 /* Handle serializing/sharing the RELRO segment */
2355 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2356 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2357 extinfo->relro_fd) < 0) {
2358 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2359 name, strerror(errno));
2360 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002361 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002362 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2363 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2364 extinfo->relro_fd) < 0) {
2365 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2366 name, strerror(errno));
2367 return false;
2368 }
2369 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002370
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002371 notify_gdb_of_load(this);
2372 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002373}
2374
Nick Kralevich468319c2011-11-11 15:53:17 -08002375/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002376 * This function add vdso to internal dso list.
2377 * It helps to stack unwinding through signal handlers.
2378 * Also, it makes bionic more like glibc.
2379 */
Kito Cheng812fd422014-03-25 22:53:56 +08002380static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002381#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002382 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002383 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002384 return;
2385 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002386
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002387 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002388
Elliott Hughes0266ae52014-02-10 17:46:57 -08002389 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2390 si->phnum = ehdr_vdso->e_phnum;
2391 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2392 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002393 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002394
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002395 si->PrelinkImage();
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002396 si->LinkImage(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002397#endif
2398}
2399
2400/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002401 * This is linker soinfo for GDB. See details below.
2402 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002403#if defined(__LP64__)
2404#define LINKER_PATH "/system/bin/linker64"
2405#else
2406#define LINKER_PATH "/system/bin/linker"
2407#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002408static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002409
2410/* gdb expects the linker to be in the debug shared object list.
2411 * Without this, gdb has trouble locating the linker's ".text"
2412 * and ".plt" sections. Gdb could also potentially use this to
2413 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2414 * Don't use soinfo_alloc(), because the linker shouldn't
2415 * be on the soinfo list.
2416 */
2417static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002418 linker_soinfo_for_gdb.base = linker_base;
2419
2420 /*
2421 * Set the dynamic field in the link map otherwise gdb will complain with
2422 * the following:
2423 * warning: .dynamic section for "/system/bin/linker" is not at the
2424 * expected address (wrong library or version mismatch?)
2425 */
2426 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2427 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2428 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002429 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002430 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2431}
2432
2433/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002434 * This code is called after the linker has linked itself and
2435 * fixed it's own GOT. It is safe to make references to externs
2436 * and other non-local data at this point.
2437 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002438static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002439#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002440 struct timeval t0, t1;
2441 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002442#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002443
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002444 // Initialize environment functions, and get to the ELF aux vectors table.
2445 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002446
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002447 // If this is a setuid/setgid program, close the security hole described in
2448 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2449 if (get_AT_SECURE()) {
2450 nullify_closed_stdio();
2451 }
2452
2453 debuggerd_init();
2454
2455 // Get a few environment variables.
2456 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2457 if (LD_DEBUG != nullptr) {
2458 g_ld_debug_verbosity = atoi(LD_DEBUG);
2459 }
2460
2461 // Normally, these are cleaned by linker_env_init, but the test
2462 // doesn't cost us anything.
2463 const char* ldpath_env = nullptr;
2464 const char* ldpreload_env = nullptr;
2465 if (!get_AT_SECURE()) {
2466 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2467 ldpreload_env = linker_env_get("LD_PRELOAD");
2468 }
2469
2470 INFO("[ android linker & debugger ]");
2471
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002472 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002473 if (si == nullptr) {
2474 exit(EXIT_FAILURE);
2475 }
2476
2477 /* bootstrap the link map, the main exe always needs to be first */
2478 si->flags |= FLAG_EXE;
2479 link_map* map = &(si->link_map_head);
2480
2481 map->l_addr = 0;
2482 map->l_name = args.argv[0];
2483 map->l_prev = nullptr;
2484 map->l_next = nullptr;
2485
2486 _r_debug.r_map = map;
2487 r_debug_tail = map;
2488
2489 init_linker_info_for_gdb(linker_base);
2490
2491 // Extract information passed from the kernel.
2492 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2493 si->phnum = args.getauxval(AT_PHNUM);
2494 si->entry = args.getauxval(AT_ENTRY);
2495
2496 /* Compute the value of si->base. We can't rely on the fact that
2497 * the first entry is the PHDR because this will not be true
2498 * for certain executables (e.g. some in the NDK unit test suite)
2499 */
2500 si->base = 0;
2501 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2502 si->load_bias = 0;
2503 for (size_t i = 0; i < si->phnum; ++i) {
2504 if (si->phdr[i].p_type == PT_PHDR) {
2505 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2506 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2507 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002508 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002509 }
2510 si->dynamic = nullptr;
2511 si->ref_count = 1;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002512
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002513 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2514 if (elf_hdr->e_type != ET_DYN) {
2515 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2516 exit(EXIT_FAILURE);
2517 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002518
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002519 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2520 parse_LD_LIBRARY_PATH(ldpath_env);
2521 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002522
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002523 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002524
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002525 si->PrelinkImage();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002526
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002527 // add somain to global group
2528 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2529
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002530 // Load ld_preloads and dependencies.
2531 StringLinkedList needed_library_name_list;
2532 size_t needed_libraries_count = 0;
2533 size_t ld_preloads_count = 0;
2534 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2535 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2536 ++needed_libraries_count;
2537 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002538
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002539 for_each_dt_needed(si, [&](const char* name) {
2540 needed_library_name_list.push_back(name);
2541 ++needed_libraries_count;
2542 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002543
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002544 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002545
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002546 memset(needed_library_names, 0, sizeof(needed_library_names));
2547 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002548
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002549 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 -07002550 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2551 exit(EXIT_FAILURE);
2552 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002553
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002554 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002555
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002556 si->CallPreInitConstructors();
Matt Fischer4fd42c12009-12-31 12:09:10 -06002557
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002558 /* After the PrelinkImage, the si->load_bias is initialized.
2559 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2560 * We need to update this value for so exe here. So Unwind_Backtrace
2561 * for some arch like x86 could work correctly within so exe.
2562 */
2563 map->l_addr = si->load_bias;
2564 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002565
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002566#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002567 gettimeofday(&t1, nullptr);
2568 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2569 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2570 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002571#endif
2572#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002573 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2574 linker_stats.count[kRelocAbsolute],
2575 linker_stats.count[kRelocRelative],
2576 linker_stats.count[kRelocCopy],
2577 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002578#endif
2579#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002580 {
2581 unsigned n;
2582 unsigned i;
2583 unsigned count = 0;
2584 for (n = 0; n < 4096; n++) {
2585 if (bitmask[n]) {
2586 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002587#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002588 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002589#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002590 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002591#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002592 if (x & 1) {
2593 count++;
2594 }
2595 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002596 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002597 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002598 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002599 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2600 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002601#endif
2602
2603#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002604 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002605#endif
2606
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002607 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2608 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002609}
Nick Kralevich468319c2011-11-11 15:53:17 -08002610
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002611/* Compute the load-bias of an existing executable. This shall only
2612 * be used to compute the load bias of an executable or shared library
2613 * that was loaded by the kernel itself.
2614 *
2615 * Input:
2616 * elf -> address of ELF header, assumed to be at the start of the file.
2617 * Return:
2618 * load bias, i.e. add the value of any p_vaddr in the file to get
2619 * the corresponding address in memory.
2620 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002621static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2622 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002623 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002624 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002625
Elliott Hughes0266ae52014-02-10 17:46:57 -08002626 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002627 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002628 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002629 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002630 }
2631 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002632}
2633
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002634extern "C" void _start();
2635
Nick Kralevich468319c2011-11-11 15:53:17 -08002636/*
2637 * This is the entry point for the linker, called from begin.S. This
2638 * method is responsible for fixing the linker's own relocations, and
2639 * then calling __linker_init_post_relocation().
2640 *
2641 * Because this method is called before the linker has fixed it's own
2642 * relocations, any attempt to reference an extern variable, extern
2643 * function, or other GOT reference will generate a segfault.
2644 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002645extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002646 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002647
Elliott Hughes0266ae52014-02-10 17:46:57 -08002648 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002649 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002650 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002651 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002652
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002653 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002654
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002655 // If the linker is not acting as PT_INTERP entry_point is equal to
2656 // _start. Which means that the linker is running as an executable and
2657 // already linked by PT_INTERP.
2658 //
2659 // This happens when user tries to run 'adb shell /system/bin/linker'
2660 // see also https://code.google.com/p/android/issues/detail?id=63174
2661 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2662 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2663 }
2664
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002665 linker_so.base = linker_addr;
2666 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2667 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002668 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002669 linker_so.phdr = phdr;
2670 linker_so.phnum = elf_hdr->e_phnum;
2671 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07002672
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002673 // This might not be obvious... The reasons why we pass g_empty_list
2674 // in place of local_group here are (1) we do not really need it, because
2675 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2676 // itself without having to look into local_group and (2) allocators
2677 // are not yet initialized, and therefore we cannot use linked_list.push_*
2678 // functions at this point.
2679 if (!(linker_so.PrelinkImage() && linker_so.LinkImage(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002680 // It would be nice to print an error message, but if the linker
2681 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002682 // call write() (because it involves a GOT reference). We may as
2683 // well try though...
2684 const char* msg = "CANNOT LINK EXECUTABLE: ";
2685 write(2, msg, strlen(msg));
2686 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2687 write(2, "\n", 1);
2688 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002689 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002690
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002691 __libc_init_tls(args);
2692
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002693 // Initialize the linker's own global variables
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002694 linker_so.CallConstructors();
2695
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002696 // Initialize static variables. Note that in order to
2697 // get correct libdl_info we need to call constructors
2698 // before get_libdl_info().
2699 solist = get_libdl_info();
2700 sonext = get_libdl_info();
2701
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002702 // We have successfully fixed our own relocations. It's safe to run
2703 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002704 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002705 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002706
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002707 protect_data(PROT_READ);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002708
2709 // Return the address that the calling assembly stub should jump to.
2710 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002711}