blob: 1dae343aa412c2be34be795a6d9a8ca1c95e6dde [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 Ivanov07e5bc12014-10-03 17:52:44 -0700287static soinfo* soinfo_alloc(const char* name, struct stat* file_stat, off64_t file_offset, int 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 Ivanovcfa97f12014-10-21 09:23:18 -0700486static ElfW(Sym)* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700487 unsigned elf_hash = elfhash(name);
488 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700489
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700490 /* "This element's presence in a shared object library alters the dynamic linker's
491 * symbol resolution algorithm for references within the library. Instead of starting
492 * a symbol search with the executable file, the dynamic linker starts from the shared
493 * object itself. If the shared object fails to supply the referenced symbol, the
494 * dynamic linker then searches the executable file and other shared objects as usual."
495 *
496 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
497 *
498 * Note that this is unlikely since static linker avoids generating
499 * relocations for -Bsymbolic linked dynamic executables.
500 */
501 if (si->has_DT_SYMBOLIC) {
502 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si->name, name);
503 s = soinfo_elf_lookup(si, elf_hash, name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700504 if (s != nullptr) {
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700505 *lsi = si;
506 }
507 }
508
509 if (s == nullptr && somain != nullptr) {
510 // 1. Look for it in the main executable unless we already did.
511 if (si != somain || !si->has_DT_SYMBOLIC) {
512 DEBUG("%s: looking up %s in executable %s",
513 si->name, name, somain->name);
514 s = soinfo_elf_lookup(somain, elf_hash, name);
515 if (s != nullptr) {
516 *lsi = somain;
517 }
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700518 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700519
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700520 // 2. Look for it in the ld_preloads
521 if (s == nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700522 for (int i = 0; g_ld_preloads[i] != NULL; i++) {
523 s = soinfo_elf_lookup(g_ld_preloads[i], elf_hash, name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700524 if (s != nullptr) {
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700525 *lsi = g_ld_preloads[i];
526 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700527 }
528 }
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700529 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700530 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700531
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700532 // 3. Look for it in the local group
533 if (s == nullptr) {
534 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700535 if (local_si == si && si->has_DT_SYMBOLIC) {
536 // we already did this - skip
537 return true;
538 }
539
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700540 DEBUG("%s: looking up %s in %s (from local group)", si->name, name, local_si->name);
541 s = soinfo_elf_lookup(local_si, elf_hash, name);
542 if (s != nullptr) {
543 *lsi = local_si;
544 return false;
545 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700546
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700547 return true;
548 });
549 }
550
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700551 if (s != nullptr) {
552 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
553 "found in %s, base = %p, load bias = %p",
554 si->name, name, reinterpret_cast<void*>(s->st_value),
555 (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
556 reinterpret_cast<void*>((*lsi)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700557 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700558
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700559 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700560}
561
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700562// Each size has it's own allocator.
563template<size_t size>
564class SizeBasedAllocator {
565 public:
566 static void* alloc() {
567 return allocator_.alloc();
568 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700569
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700570 static void free(void* ptr) {
571 allocator_.free(ptr);
572 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700573
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700574 private:
575 static LinkerBlockAllocator allocator_;
576};
577
578template<size_t size>
579LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
580
581template<typename T>
582class TypeBasedAllocator {
583 public:
584 static T* alloc() {
585 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
586 }
587
588 static void free(T* ptr) {
589 SizeBasedAllocator<sizeof(T)>::free(ptr);
590 }
591};
592
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700593class LoadTask {
594 public:
595 struct deleter_t {
596 void operator()(LoadTask* t) {
597 TypeBasedAllocator<LoadTask>::free(t);
598 }
599 };
600
601 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
602
603 static deleter_t deleter;
604
605 static LoadTask* create(const char* name, soinfo* needed_by) {
606 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
607 return new (ptr) LoadTask(name, needed_by);
608 }
609
610 const char* get_name() const {
611 return name_;
612 }
613
614 soinfo* get_needed_by() const {
615 return needed_by_;
616 }
617 private:
618 LoadTask(const char* name, soinfo* needed_by)
619 : name_(name), needed_by_(needed_by) {}
620
621 const char* name_;
622 soinfo* needed_by_;
623
624 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
625};
626
Ningsheng Jiane93be992014-09-16 15:22:10 +0800627LoadTask::deleter_t LoadTask::deleter;
628
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700629template <typename T>
630using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
631
632typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700633typedef linked_list_t<const char> StringLinkedList;
634typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700635
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700636
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700637// This function walks down the tree of soinfo dependencies
638// in breadth-first order and
639// * calls action(soinfo* si) for each node, and
640// * terminates walk if action returns false.
641//
642// walk_dependencies_tree returns false if walk was terminated
643// by the action and true otherwise.
644template<typename F>
645static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700646 SoinfoLinkedList visit_list;
647 SoinfoLinkedList visited;
648
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700649 for (size_t i = 0; i < root_soinfos_size; ++i) {
650 visit_list.push_back(root_soinfos[i]);
651 }
652
653 soinfo* si;
654 while ((si = visit_list.pop_front()) != nullptr) {
655 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700656 continue;
657 }
658
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700659 if (!action(si)) {
660 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700661 }
662
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700663 visited.push_back(si);
664
665 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700666 visit_list.push_back(child);
667 });
668 }
669
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700670 return true;
671}
672
673
674// This is used by dlsym(3). It performs symbol lookup only within the
675// specified soinfo object and its dependencies in breadth first order.
676ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
677 ElfW(Sym)* result = nullptr;
678 uint32_t elf_hash = elfhash(name);
679
680
681 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
682 result = soinfo_elf_lookup(current_soinfo, elf_hash, name);
683 if (result != nullptr) {
684 *found = current_soinfo;
685 return false;
686 }
687
688 return true;
689 });
690
691 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800692}
693
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800694/* This is used by dlsym(3) to performs a global symbol lookup. If the
695 start value is null (for RTLD_DEFAULT), the search starts at the
696 beginning of the global solist. Otherwise the search starts at the
697 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700698 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700699ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800700 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800701
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700702 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800703 start = solist;
704 }
705
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700706 ElfW(Sym)* s = nullptr;
707 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700708 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
709 continue;
710 }
711
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700712 s = soinfo_elf_lookup(si, elf_hash, name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700713 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800714 *found = si;
715 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600716 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800717 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600718
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700719 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700720 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
721 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800722 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800723
Elliott Hughescade4c32012-12-20 14:42:14 -0800724 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800725}
726
Kito Chengfa8c05d2013-03-12 14:58:06 +0800727soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800728 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700729 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800730 if (address >= si->base && address - si->base < si->size) {
731 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600732 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800733 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700734 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600735}
736
Elliott Hughes0266ae52014-02-10 17:46:57 -0800737ElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr) {
738 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - si->base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600739
Kito Chengfa8c05d2013-03-12 14:58:06 +0800740 // Search the library's symbol table for any defined symbol which
741 // contains this address.
742 for (size_t i = 0; i < si->nchain; ++i) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800743 ElfW(Sym)* sym = &si->symtab[i];
Kito Chengfa8c05d2013-03-12 14:58:06 +0800744 if (sym->st_shndx != SHN_UNDEF &&
745 soaddr >= sym->st_value &&
746 soaddr < sym->st_value + sym->st_size) {
747 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600748 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800749 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600750
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700751 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600752}
753
Elliott Hughes124fae92012-10-31 14:20:03 -0700754static int open_library_on_path(const char* name, const char* const paths[]) {
Dmitriy Ivanovda8e5912014-10-31 13:46:42 -0700755 char buf[MAX_PATH_LEN];
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700756 for (size_t i = 0; paths[i] != nullptr; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800757 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700758 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700759 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700760 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800761 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700762 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
763 if (fd != -1) {
764 return fd;
765 }
766 }
767 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800768}
769
Elliott Hughes124fae92012-10-31 14:20:03 -0700770static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700771 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800772
Elliott Hughes124fae92012-10-31 14:20:03 -0700773 // 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 -0700774 if (strchr(name, '/') != nullptr) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700775 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
776 if (fd != -1) {
777 return fd;
778 }
779 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700780#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700781 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700782#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700783 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800784
Elliott Hughes124fae92012-10-31 14:20:03 -0700785 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Elliott Hughes1728b232014-05-14 10:02:03 -0700786 int fd = open_library_on_path(name, g_ld_library_paths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700787 if (fd == -1) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700788 fd = open_library_on_path(name, kDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700789 }
790 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800791}
792
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700793template<typename F>
794static void for_each_dt_needed(const soinfo* si, F action) {
795 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
796 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700797 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700798 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700799 }
800}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800801
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700802static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700803 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700804 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700805 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700806
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700807 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
808 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700809 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
810 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700811 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700812 } else {
813 // Open the file.
814 fd = open_library(name);
815 if (fd == -1) {
816 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700817 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700818 }
819
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700820 file_guard.reset(fd);
821 }
822
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700823 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700824 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700825 return nullptr;
826 }
827
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700828 struct stat file_stat;
829 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700830 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700831 return nullptr;
832 }
833
834 // Check for symlink and other situations where
835 // file can have different names.
836 for (soinfo* si = solist; si != nullptr; si = si->next) {
837 if (si->get_st_dev() != 0 &&
838 si->get_st_ino() != 0 &&
839 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700840 si->get_st_ino() == file_stat.st_ino &&
841 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700842 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
843 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700844 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700845 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700846
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700847 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700848 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700849 return nullptr;
850 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700851
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700852 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700853 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700854 if (!elf_reader.Load(extinfo)) {
855 return nullptr;
856 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800857
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700858 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700859 if (si == nullptr) {
860 return nullptr;
861 }
862 si->base = elf_reader.load_start();
863 si->size = elf_reader.load_size();
864 si->load_bias = elf_reader.load_bias();
865 si->phnum = elf_reader.phdr_count();
866 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700867
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700868 if (!si->PrelinkImage()) {
869 soinfo_free(si);
870 return nullptr;
871 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700872
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700873 for_each_dt_needed(si, [&] (const char* name) {
874 load_tasks.push_back(LoadTask::create(name, si));
875 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700876
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700877 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800878}
879
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700880static soinfo *find_loaded_library_by_name(const char* name) {
881 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700882 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700883 if (!strcmp(search_name, si->name)) {
884 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200885 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700886 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700887 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200888}
889
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700890static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200891
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700892 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700893
894 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700895 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700896 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700897 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700898 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700899 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800900
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700901 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800902}
903
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700904static void soinfo_unload(soinfo* si);
905
906static bool is_recursive(soinfo* si, soinfo* parent) {
907 if (parent == nullptr) {
908 return false;
Dmitriy Ivanova3ad4502014-07-29 14:21:45 -0700909 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700910
911 if (si == parent) {
912 DL_ERR("recursive link to \"%s\"", si->name);
913 return true;
914 }
915
916 return !parent->get_parents().visit([&](soinfo* grandparent) {
917 return !is_recursive(si, grandparent);
918 });
919}
920
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700921static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
922 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700923 // Step 0: prepare.
924 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700925 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700926 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700927 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700928 }
929
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700930 // If soinfos array is null allocate one on stack.
931 // The array is needed in case of failure; for example
932 // when library_names[] = {libone.so, libtwo.so} and libone.so
933 // is loaded correctly but libtwo.so failed for some reason.
934 // In this case libone.so should be unloaded on return.
935 // See also implementation of failure_guard below.
936
937 if (soinfos == nullptr) {
938 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
939 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
940 memset(soinfos, 0, soinfos_size);
941 }
942
943 // list of libraries to link - see step 2.
944 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700945
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700946 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700947 // Housekeeping
948 load_tasks.for_each([] (LoadTask* t) {
949 LoadTask::deleter(t);
950 });
951
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700952 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700953 soinfo_unload(soinfos[i]);
954 }
955 });
956
957 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
958 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 -0700959 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700960 if (si == nullptr) {
961 return false;
962 }
963
964 soinfo* needed_by = task->get_needed_by();
965
966 if (is_recursive(si, needed_by)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700967 return false;
968 }
969
970 si->ref_count++;
971 if (needed_by != nullptr) {
972 needed_by->add_child(si);
973 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700974
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700975 // When ld_preloads is not null, the first
976 // ld_preloads_count libs are in fact ld_preloads.
977 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
978 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700979 }
980
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700981 if (soinfos_count < library_names_count) {
982 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700983 }
984 }
985
986 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700987 soinfo::soinfo_list_t local_group;
988 walk_dependencies_tree(
989 start_with == nullptr ? soinfos : &start_with,
990 start_with == nullptr ? soinfos_count : 1,
991 [&] (soinfo* si) {
992 local_group.push_back(si);
993 return true;
994 });
995
996 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700997 if ((si->flags & FLAG_LINKED) == 0) {
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700998 if (!si->LinkImage(local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700999 return false;
1000 }
1001 si->flags |= FLAG_LINKED;
1002 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001003
1004 return true;
1005 });
1006
1007 if (linked) {
1008 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001009 }
1010
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001011 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001012}
1013
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001014static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001015 if (name == nullptr) {
1016 somain->ref_count++;
1017 return somain;
1018 }
1019
1020 soinfo* si;
1021
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001022 if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001023 return nullptr;
1024 }
1025
Elliott Hughesd23736e2012-11-01 15:16:56 -07001026 return si;
1027}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001028
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001029static void soinfo_unload(soinfo* si) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001030 if (!si->can_unload()) {
1031 TRACE("not unloading '%s' - the binary is flagged with NODELETE", si->name);
1032 return;
1033 }
1034
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001035 if (si->ref_count == 1) {
1036 TRACE("unloading '%s'", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001037 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001038
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001039 if (si->has_min_version(0)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001040 soinfo* child = nullptr;
1041 while ((child = si->get_children().pop_front()) != nullptr) {
1042 TRACE("%s needs to unload %s", si->name, child->name);
1043 soinfo_unload(child);
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001044 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001045 } else {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001046 for_each_dt_needed(si, [&] (const char* library_name) {
1047 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1048 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1049 if (needed != nullptr) {
1050 soinfo_unload(needed);
1051 } else {
1052 // Not found: for example if symlink was deleted between dlopen and dlclose
1053 // Since we cannot really handle errors at this point - print and continue.
1054 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001055 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001056 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001057 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001058
Elliott Hughesd23736e2012-11-01 15:16:56 -07001059 notify_gdb_of_unload(si);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001060 si->ref_count = 0;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -07001061 soinfo_free(si);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001062 } else {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001063 si->ref_count--;
Elliott Hughesc6200592013-09-30 18:43:46 -07001064 TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001065 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001066}
1067
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001068void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001069 // Use basic string manipulation calls to avoid snprintf.
1070 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1071 // When debug malloc is enabled, this call returns 0. This in turn causes
1072 // snprintf to do nothing, which causes libraries to fail to load.
1073 // See b/17302493 for further details.
1074 // Once the above bug is fixed, this code can be modified to use
1075 // snprintf again.
1076 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1077 if (buffer_size < required_len) {
1078 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1079 buffer_size, required_len);
1080 }
1081 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1082 *end = ':';
1083 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001084}
1085
Elliott Hughescade4c32012-12-20 14:42:14 -08001086void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
1087 if (!get_AT_SECURE()) {
1088 parse_LD_LIBRARY_PATH(ld_library_path);
1089 }
1090}
1091
Elliott Hughes1a586292014-06-03 16:23:08 -07001092soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001093 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001094 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001095 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001096 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001097 if (extinfo != nullptr) {
1098 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1099 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1100 return nullptr;
1101 }
1102 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001103 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1104 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 -07001105 return nullptr;
1106 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001107 }
Dmitriy Ivanovda8e5912014-10-31 13:46:42 -07001108
1109 size_t name_len = strlen(name);
1110 if (name_len >= MAX_PATH_LEN) {
1111 DL_ERR("library name \"%s\" is too long", name);
1112 return nullptr;
1113 }
1114
1115 char local_name[name_len+1];
1116 strlcpy(local_name, name, name_len+1);
1117
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001118 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanovda8e5912014-10-31 13:46:42 -07001119 soinfo* si = find_library(local_name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001120 if (si != nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001121 si->CallConstructors();
1122 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001123 protect_data(PROT_READ);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001124 return si;
1125}
1126
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001127void do_dlclose(soinfo* si) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001128 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001129 soinfo_unload(si);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001130 protect_data(PROT_READ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001131}
1132
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001133static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1134 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1135 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1136 ElfW(Addr) ifunc_addr = ifunc_resolver();
1137 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 -07001138
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001139 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001140}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001141
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001142#if defined(USE_RELA)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001143int soinfo::Relocate(ElfW(Rela)* rela, unsigned count, const soinfo_list_t& local_group) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001144 for (size_t idx = 0; idx < count; ++idx, ++rela) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001145 unsigned type = ELFW(R_TYPE)(rela->r_info);
1146 unsigned sym = ELFW(R_SYM)(rela->r_info);
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001147 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001148 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001149 const char* sym_name = nullptr;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001150
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001151 DEBUG("Processing '%s' relocation at index %zd", name, idx);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001152 if (type == 0) { // R_*_NONE
1153 continue;
1154 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001155
1156 ElfW(Sym)* s = nullptr;
1157 soinfo* lsi = nullptr;
1158
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001159 if (sym != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001160 sym_name = get_string(symtab[sym].st_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001161 s = soinfo_do_lookup(this, sym_name, &lsi, local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001162 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001163 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001164 s = &symtab[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001165 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001166 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001167 return -1;
1168 }
1169
1170 /* IHI0044C AAELF 4.5.1.1:
1171
1172 Libraries are not searched to resolve weak references.
1173 It is not an error for a weak reference to remain unsatisfied.
1174
1175 During linking, the value of an undefined weak reference is:
1176 - Zero if the relocation type is absolute
1177 - The address of the place if the relocation is pc-relative
1178 - The address of nominal base address if the relocation
1179 type is base-relative.
1180 */
1181
1182 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001183#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001184 case R_AARCH64_JUMP_SLOT:
1185 case R_AARCH64_GLOB_DAT:
1186 case R_AARCH64_ABS64:
1187 case R_AARCH64_ABS32:
1188 case R_AARCH64_ABS16:
1189 case R_AARCH64_RELATIVE:
1190 case R_AARCH64_IRELATIVE:
1191 /*
1192 * The sym_addr was initialized to be zero above, or the relocation
1193 * code below does not care about value of sym_addr.
1194 * No need to do anything.
1195 */
1196 break;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001197#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001198 case R_X86_64_JUMP_SLOT:
1199 case R_X86_64_GLOB_DAT:
1200 case R_X86_64_32:
1201 case R_X86_64_64:
1202 case R_X86_64_RELATIVE:
1203 case R_X86_64_IRELATIVE:
1204 // No need to do anything.
1205 break;
1206 case R_X86_64_PC32:
1207 sym_addr = reloc;
1208 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001209#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001210 default:
1211 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
1212 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001213 }
1214 } else {
1215 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001216 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001217 }
1218 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001219 }
1220
1221 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001222#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001223 case R_AARCH64_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001224 count_relocation(kRelocAbsolute);
1225 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001226 TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
1227 reloc, (sym_addr + rela->r_addend), sym_name);
1228 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001229 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001230 case R_AARCH64_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001231 count_relocation(kRelocAbsolute);
1232 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001233 TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
1234 reloc, (sym_addr + rela->r_addend), sym_name);
1235 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001236 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001237 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001238 count_relocation(kRelocAbsolute);
1239 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001240 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
1241 reloc, (sym_addr + rela->r_addend), sym_name);
1242 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001243 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001244 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001245 count_relocation(kRelocAbsolute);
1246 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001247 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
1248 reloc, (sym_addr + rela->r_addend), sym_name);
1249 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1250 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001251 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001252 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001253 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1254 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1255 static_cast<ElfW(Addr)>(INT32_MIN),
1256 static_cast<ElfW(Addr)>(UINT32_MAX));
1257 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001258 }
1259 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001260 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001261 count_relocation(kRelocAbsolute);
1262 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001263 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
1264 reloc, (sym_addr + rela->r_addend), sym_name);
1265 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1266 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001267 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001268 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001269 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1270 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1271 static_cast<ElfW(Addr)>(INT16_MIN),
1272 static_cast<ElfW(Addr)>(UINT16_MAX));
1273 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001274 }
1275 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001276 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001277 count_relocation(kRelocRelative);
1278 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001279 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
1280 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1281 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001282 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001283 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001284 count_relocation(kRelocRelative);
1285 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001286 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
1287 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1288 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1289 ((*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 -07001290 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001291 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001292 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1293 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1294 static_cast<ElfW(Addr)>(INT32_MIN),
1295 static_cast<ElfW(Addr)>(UINT32_MAX));
1296 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001297 }
1298 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001299 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001300 count_relocation(kRelocRelative);
1301 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001302 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
1303 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1304 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1305 ((*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 -07001306 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001307 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001308 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1309 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1310 static_cast<ElfW(Addr)>(INT16_MIN),
1311 static_cast<ElfW(Addr)>(UINT16_MAX));
1312 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001313 }
1314 break;
1315
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001316 case R_AARCH64_RELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001317 count_relocation(kRelocRelative);
1318 MARK(rela->r_offset);
1319 if (sym) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001320 DL_ERR("odd RELATIVE form...");
1321 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001322 }
Elliott Hughes0266ae52014-02-10 17:46:57 -08001323 TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001324 reloc, (base + rela->r_addend));
1325 *reinterpret_cast<ElfW(Addr)*>(reloc) = (base + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001326 break;
1327
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001328 case R_AARCH64_IRELATIVE:
1329 count_relocation(kRelocRelative);
1330 MARK(rela->r_offset);
1331 TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
1332 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
1333 break;
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001334
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001335 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001336 /*
1337 * ET_EXEC is not supported so this should not happen.
1338 *
1339 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1340 *
1341 * Section 4.7.1.10 "Dynamic relocations"
1342 * R_AARCH64_COPY may only appear in executable objects where e_type is
1343 * set to ET_EXEC.
1344 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001345 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Nick Kralevich76e289c2014-07-03 12:04:31 -07001346 return -1;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001347 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001348 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
1349 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001350 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001351 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001352 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
1353 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001354 break;
1355#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001356 case R_X86_64_JUMP_SLOT:
1357 count_relocation(kRelocAbsolute);
1358 MARK(rela->r_offset);
1359 TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1360 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1361 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1362 break;
1363 case R_X86_64_GLOB_DAT:
1364 count_relocation(kRelocAbsolute);
1365 MARK(rela->r_offset);
1366 TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1367 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1368 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1369 break;
1370 case R_X86_64_RELATIVE:
1371 count_relocation(kRelocRelative);
1372 MARK(rela->r_offset);
1373 if (sym) {
1374 DL_ERR("odd RELATIVE form...");
1375 return -1;
1376 }
1377 TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1378 static_cast<size_t>(base));
1379 *reinterpret_cast<ElfW(Addr)*>(reloc) = base + rela->r_addend;
1380 break;
1381 case R_X86_64_IRELATIVE:
1382 count_relocation(kRelocRelative);
1383 MARK(rela->r_offset);
1384 TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
1385 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
1386 break;
1387 case R_X86_64_32:
1388 count_relocation(kRelocRelative);
1389 MARK(rela->r_offset);
1390 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1391 static_cast<size_t>(sym_addr), sym_name);
1392 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1393 break;
1394 case R_X86_64_64:
1395 count_relocation(kRelocRelative);
1396 MARK(rela->r_offset);
1397 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1398 static_cast<size_t>(sym_addr), sym_name);
1399 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1400 break;
1401 case R_X86_64_PC32:
1402 count_relocation(kRelocRelative);
1403 MARK(rela->r_offset);
1404 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1405 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1406 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
1407 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
1408 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001409#endif
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001410
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001411 default:
1412 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
1413 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001414 }
1415 }
1416 return 0;
1417}
Chris Dearman99186652014-02-06 20:36:51 -08001418
1419#else // REL, not RELA.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001420int soinfo::Relocate(ElfW(Rel)* rel, unsigned count, const soinfo_list_t& local_group) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001421 for (size_t idx = 0; idx < count; ++idx, ++rel) {
1422 unsigned type = ELFW(R_TYPE)(rel->r_info);
1423 // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
1424 unsigned sym = ELFW(R_SYM)(rel->r_info);
1425 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
1426 ElfW(Addr) sym_addr = 0;
1427 const char* sym_name = nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001428
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001429 DEBUG("Processing '%s' relocation at index %zd", name, idx);
1430 if (type == 0) { // R_*_NONE
1431 continue;
1432 }
1433
1434 ElfW(Sym)* s = nullptr;
1435 soinfo* lsi = nullptr;
1436
1437 if (sym != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001438 sym_name = get_string(symtab[sym].st_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001439 s = soinfo_do_lookup(this, sym_name, &lsi, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001440 if (s == nullptr) {
1441 // We only allow an undefined symbol if this is a weak reference...
1442 s = &symtab[sym];
1443 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1444 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
1445 return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001446 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001447
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001448 /* IHI0044C AAELF 4.5.1.1:
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001449
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001450 Libraries are not searched to resolve weak references.
1451 It is not an error for a weak reference to remain
1452 unsatisfied.
Doug Kwane8238072009-10-26 12:05:23 -07001453
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001454 During linking, the value of an undefined weak reference is:
1455 - Zero if the relocation type is absolute
1456 - The address of the place if the relocation is pc-relative
1457 - The address of nominal base address if the relocation
1458 type is base-relative.
1459 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001460
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001461 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001462#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001463 case R_ARM_JUMP_SLOT:
1464 case R_ARM_GLOB_DAT:
1465 case R_ARM_ABS32:
1466 case R_ARM_RELATIVE: /* Don't care. */
1467 // sym_addr was initialized to be zero above or relocation
1468 // code below does not care about value of sym_addr.
1469 // No need to do anything.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001470 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001471#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001472 case R_386_JMP_SLOT:
1473 case R_386_GLOB_DAT:
1474 case R_386_32:
1475 case R_386_RELATIVE: /* Don't care. */
1476 case R_386_IRELATIVE:
1477 // sym_addr was initialized to be zero above or relocation
1478 // code below does not care about value of sym_addr.
1479 // No need to do anything.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001480 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001481 case R_386_PC32:
1482 sym_addr = reloc;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001483 break;
1484#endif
1485
1486#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001487 case R_ARM_COPY:
1488 // Fall through. Can't really copy if weak symbol is not found at run-time.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001489#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001490 default:
1491 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001492 return -1;
1493 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001494 } else {
1495 // We got a definition.
1496 sym_addr = lsi->resolve_symbol_address(s);
1497 }
1498 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001499 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001500
1501 switch (type) {
1502#if defined(__arm__)
1503 case R_ARM_JUMP_SLOT:
1504 count_relocation(kRelocAbsolute);
1505 MARK(rel->r_offset);
1506 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1507 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1508 break;
1509 case R_ARM_GLOB_DAT:
1510 count_relocation(kRelocAbsolute);
1511 MARK(rel->r_offset);
1512 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1513 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1514 break;
1515 case R_ARM_ABS32:
1516 count_relocation(kRelocAbsolute);
1517 MARK(rel->r_offset);
1518 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1519 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1520 break;
1521 case R_ARM_REL32:
1522 count_relocation(kRelocRelative);
1523 MARK(rel->r_offset);
1524 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1525 reloc, sym_addr, rel->r_offset, sym_name);
1526 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1527 break;
1528 case R_ARM_COPY:
1529 /*
1530 * ET_EXEC is not supported so this should not happen.
1531 *
1532 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1533 *
1534 * Section 4.7.1.10 "Dynamic relocations"
1535 * R_ARM_COPY may only appear in executable objects where e_type is
1536 * set to ET_EXEC.
1537 */
1538 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
1539 return -1;
1540#elif defined(__i386__)
1541 case R_386_JMP_SLOT:
1542 count_relocation(kRelocAbsolute);
1543 MARK(rel->r_offset);
1544 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1545 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1546 break;
1547 case R_386_GLOB_DAT:
1548 count_relocation(kRelocAbsolute);
1549 MARK(rel->r_offset);
1550 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1551 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1552 break;
1553 case R_386_32:
1554 count_relocation(kRelocRelative);
1555 MARK(rel->r_offset);
1556 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1557 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1558 break;
1559 case R_386_PC32:
1560 count_relocation(kRelocRelative);
1561 MARK(rel->r_offset);
1562 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1563 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1564 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1565 break;
1566#elif defined(__mips__)
1567 case R_MIPS_REL32:
1568#if defined(__LP64__)
1569 // MIPS Elf64_Rel entries contain compound relocations
1570 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
1571 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
1572 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
1573 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
1574 type, (unsigned)ELF64_R_TYPE2(rel->r_info),
1575 (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
1576 return -1;
1577 }
1578#endif
1579 count_relocation(kRelocAbsolute);
1580 MARK(rel->r_offset);
1581 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
1582 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
1583 if (s) {
1584 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1585 } else {
1586 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1587 }
1588 break;
1589#endif
1590
1591#if defined(__arm__)
1592 case R_ARM_RELATIVE:
1593#elif defined(__i386__)
1594 case R_386_RELATIVE:
1595#endif
1596 count_relocation(kRelocRelative);
1597 MARK(rel->r_offset);
1598 if (sym) {
1599 DL_ERR("odd RELATIVE form...");
1600 return -1;
1601 }
1602 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1603 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1604 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1605 break;
1606#if defined(__i386__)
1607 case R_386_IRELATIVE:
1608 count_relocation(kRelocRelative);
1609 MARK(rel->r_offset);
1610 TRACE_TYPE(RELO, "RELO IRELATIVE %p <- %p", reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1611 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + *reinterpret_cast<ElfW(Addr)*>(reloc));
1612 break;
1613#endif
1614
1615 default:
1616 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
1617 return -1;
1618 }
1619 }
1620 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001621}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001622#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001623
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001624#if defined(__mips__)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001625static bool mips_relocate_got(soinfo* si, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001626 ElfW(Addr)** got = si->plt_got;
1627 if (got == nullptr) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001628 return true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001629 }
1630 unsigned local_gotno = si->mips_local_gotno;
1631 unsigned gotsym = si->mips_gotsym;
1632 unsigned symtabno = si->mips_symtabno;
1633 ElfW(Sym)* symtab = si->symtab;
1634
1635 // got[0] is the address of the lazy resolver function.
1636 // got[1] may be used for a GNU extension.
1637 // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
1638 // FIXME: maybe this should be in a separate routine?
1639 if ((si->flags & FLAG_LINKER) == 0) {
1640 size_t g = 0;
1641 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
1642 if (reinterpret_cast<intptr_t>(got[g]) < 0) {
1643 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
1644 }
1645 // Relocate the local GOT entries.
1646 for (; g < local_gotno; g++) {
1647 got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias);
1648 }
1649 }
1650
1651 // Now for the global GOT entries...
1652 ElfW(Sym)* sym = symtab + gotsym;
1653 got = si->plt_got + local_gotno;
1654 for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
1655 // This is an undefined reference... try to locate it.
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001656 const char* sym_name = si->get_string(sym->st_name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001657 soinfo* lsi = nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001658 ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001659 if (s == nullptr) {
1660 // We only allow an undefined symbol if this is a weak reference.
1661 s = &symtab[g];
1662 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1663 DL_ERR("cannot locate \"%s\"...", sym_name);
1664 return false;
1665 }
1666 *got = 0;
1667 } else {
1668 // FIXME: is this sufficient?
1669 // For reference see NetBSD link loader
1670 // 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
1671 *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s));
1672 }
1673 }
1674 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001675}
1676#endif
1677
Kito Cheng812fd422014-03-25 22:53:56 +08001678void soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001679 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001680 return;
1681 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001682
Elliott Hughesc6200592013-09-30 18:43:46 -07001683 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001684
1685 int begin = reverse ? (count - 1) : 0;
1686 int end = reverse ? -1 : count;
1687 int step = reverse ? -1 : 1;
1688
1689 for (int i = begin; i != end; i += step) {
1690 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1691 CallFunction("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001692 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001693
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001694 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001695}
1696
Kito Cheng812fd422014-03-25 22:53:56 +08001697void soinfo::CallFunction(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001698 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001699 return;
1700 }
1701
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001702 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001703 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001704 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001705
1706 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1707 // are still writable. This happens with our debug malloc (see http://b/7941716).
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001708 protect_data(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001709}
1710
Elliott Hughesd23736e2012-11-01 15:16:56 -07001711void soinfo::CallPreInitConstructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001712 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1713 // but ignored in a shared library.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001714 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1715}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001716
Elliott Hughesd23736e2012-11-01 15:16:56 -07001717void soinfo::CallConstructors() {
1718 if (constructors_called) {
1719 return;
1720 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001721
Elliott Hughesd23736e2012-11-01 15:16:56 -07001722 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1723 // protect against recursive constructor calls. One simple example of constructor recursion
1724 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1725 // 1. The program depends on libc, so libc's constructor is called here.
1726 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1727 // 3. dlopen() calls the constructors on the newly created
1728 // soinfo for libc_malloc_debug_leak.so.
1729 // 4. The debug .so depends on libc, so CallConstructors is
1730 // called again with the libc soinfo. If it doesn't trigger the early-
1731 // out above, the libc constructor will be called again (recursively!).
1732 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001733
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001734 if ((flags & FLAG_EXE) == 0 && preinit_array != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001735 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001736 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001737 name, preinit_array_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001738 }
1739
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001740 get_children().for_each([] (soinfo* si) {
1741 si->CallConstructors();
1742 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001743
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001744 TRACE("\"%s\": calling constructors", name);
1745
1746 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001747 CallFunction("DT_INIT", init_func);
1748 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001749}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001750
Elliott Hughesd23736e2012-11-01 15:16:56 -07001751void soinfo::CallDestructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001752 if (!constructors_called) {
1753 return;
1754 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001755 TRACE("\"%s\": calling destructors", name);
1756
1757 // DT_FINI_ARRAY must be parsed in reverse order.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001758 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001759
1760 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001761 CallFunction("DT_FINI", fini_func);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001762
1763 // This is needed on second call to dlopen
1764 // after library has been unloaded with RTLD_NODELETE
1765 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001766}
1767
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001768void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001769 if (has_min_version(0)) {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -07001770 child->parents.push_back(this);
1771 this->children.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001772 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001773}
1774
1775void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001776 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001777 return;
1778 }
1779
1780 // 1. Untie connected soinfos from 'this'.
1781 children.for_each([&] (soinfo* child) {
1782 child->parents.remove_if([&] (const soinfo* parent) {
1783 return parent == this;
1784 });
1785 });
1786
1787 parents.for_each([&] (soinfo* parent) {
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001788 parent->children.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001789 return child == this;
1790 });
1791 });
1792
1793 // 2. Once everything untied - clear local lists.
1794 parents.clear();
1795 children.clear();
1796}
1797
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001798dev_t soinfo::get_st_dev() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001799 if (has_min_version(0)) {
1800 return st_dev;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001801 }
1802
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001803 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001804};
1805
1806ino_t soinfo::get_st_ino() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001807 if (has_min_version(0)) {
1808 return st_ino;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001809 }
1810
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001811 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001812}
1813
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001814off64_t soinfo::get_file_offset() {
1815 if (has_min_version(1)) {
1816 return file_offset;
1817 }
1818
1819 return 0;
1820}
1821
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001822int soinfo::get_rtld_flags() {
1823 if (has_min_version(1)) {
1824 return rtld_flags;
1825 }
1826
1827 return 0;
1828}
1829
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001830// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001831// 'this->flags' does not have FLAG_NEW_SOINFO set.
1832static soinfo::soinfo_list_t g_empty_list;
1833
1834soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001835 if (has_min_version(0)) {
1836 return this->children;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001837 }
1838
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001839 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001840}
1841
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001842soinfo::soinfo_list_t& soinfo::get_parents() {
1843 if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1844 return g_empty_list;
1845 }
1846
1847 return this->parents;
1848}
1849
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001850ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1851 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1852 return call_ifunc_resolver(s->st_value + load_bias);
1853 }
1854
1855 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1856}
1857
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001858const char* soinfo::get_string(ElfW(Word) index) const {
1859 if (has_min_version(1) && (index >= strtab_size)) {
1860 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size, index);
1861 }
1862
1863 return strtab + index;
1864}
1865
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001866bool soinfo::can_unload() const {
1867 return (rtld_flags & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
1868}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001869/* Force any of the closed stdin, stdout and stderr to be associated with
1870 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001871static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001872 int dev_null, i, status;
1873 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001874
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001875 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
1876 if (dev_null < 0) {
1877 DL_ERR("cannot open /dev/null: %s", strerror(errno));
1878 return -1;
1879 }
1880 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001881
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001882 /* If any of the stdio file descriptors is valid and not associated
1883 with /dev/null, dup /dev/null to it. */
1884 for (i = 0; i < 3; i++) {
1885 /* If it is /dev/null already, we are done. */
1886 if (i == dev_null) {
1887 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001888 }
1889
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001890 TRACE("[ Nullifying stdio file descriptor %d]", i);
1891 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
1892
1893 /* If file is opened, we are good. */
1894 if (status != -1) {
1895 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001896 }
1897
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001898 /* The only error we allow is that the file descriptor does not
1899 exist, in which case we dup /dev/null to it. */
1900 if (errno != EBADF) {
1901 DL_ERR("fcntl failed: %s", strerror(errno));
1902 return_value = -1;
1903 continue;
1904 }
1905
1906 /* Try dupping /dev/null to this stdio file descriptor and
1907 repeat if there is a signal. Note that any errors in closing
1908 the stdio descriptor are lost. */
1909 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
1910 if (status < 0) {
1911 DL_ERR("dup2 failed: %s", strerror(errno));
1912 return_value = -1;
1913 continue;
1914 }
1915 }
1916
1917 /* If /dev/null is not one of the stdio file descriptors, close it. */
1918 if (dev_null > 2) {
1919 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
1920 status = TEMP_FAILURE_RETRY(close(dev_null));
1921 if (status == -1) {
1922 DL_ERR("close failed: %s", strerror(errno));
1923 return_value = -1;
1924 }
1925 }
1926
1927 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001928}
1929
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001930bool soinfo::PrelinkImage() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08001931 /* Extract dynamic section */
1932 ElfW(Word) dynamic_flags = 0;
1933 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07001934
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001935 /* We can't log anything until the linker is relocated */
1936 bool relocating_linker = (flags & FLAG_LINKER) != 0;
1937 if (!relocating_linker) {
1938 INFO("[ linking %s ]", name);
1939 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags);
1940 }
1941
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001942 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001943 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001944 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001945 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001946 return false;
1947 } else {
1948 if (!relocating_linker) {
1949 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001950 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001951 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001952
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001953#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001954 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
1955 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001956#endif
1957
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001958 // Extract useful information from dynamic section.
1959 uint32_t needed_count = 0;
1960 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
1961 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1962 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1963 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001964 case DT_SONAME:
1965 // TODO: glibc dynamic linker uses this name for
1966 // initial library lookup; consider doing the same here.
1967 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001968
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001969 case DT_HASH:
1970 nbucket = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
1971 nchain = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
1972 bucket = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
1973 chain = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket * 4);
1974 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001975
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001976 case DT_STRTAB:
1977 strtab = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
1978 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001979
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001980 case DT_STRSZ:
1981 strtab_size = d->d_un.d_val;
1982 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001983
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001984 case DT_SYMTAB:
1985 symtab = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
1986 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001987
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001988 case DT_SYMENT:
1989 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07001990 DL_ERR("invalid DT_SYMENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001991 return false;
1992 }
1993 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001994
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001995 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07001996#if defined(USE_RELA)
1997 if (d->d_un.d_val != DT_RELA) {
1998 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001999 return false;
2000 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002001#else
2002 if (d->d_un.d_val != DT_REL) {
2003 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
2004 return false;
2005 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002006#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002007 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002008
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002009 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002010#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002011 plt_rela = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002012#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002013 plt_rel = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002014#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002015 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002016
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002017 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002018#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002019 plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002020#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002021 plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002022#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002023 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002024
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002025 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002026#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002027 // Used by mips and mips64.
2028 plt_got = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002029#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002030 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2031 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002032
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002033 case DT_DEBUG:
2034 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2035 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002036// FIXME: not working currently for N64
2037// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002038// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002039// read-only, but the DYNAMIC header claims it is writable.
2040#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002041 if ((dynamic_flags & PF_W) != 0) {
2042 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2043 }
2044 break;
Chris Dearman99186652014-02-06 20:36:51 -08002045#endif
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002046#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002047 case DT_RELA:
2048 rela = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
2049 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002050
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002051 case DT_RELASZ:
2052 rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
2053 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002054
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002055 case DT_RELAENT:
2056 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002057 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002058 return false;
2059 }
2060 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002061
2062 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002063 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002064 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002065
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002066 case DT_REL:
2067 DL_ERR("unsupported DT_REL in \"%s\"", name);
2068 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002069
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002070 case DT_RELSZ:
2071 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2072 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002073#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002074 case DT_REL:
2075 rel = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
2076 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002077
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002078 case DT_RELSZ:
2079 rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
2080 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002081
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002082 case DT_RELENT:
2083 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002084 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002085 return false;
2086 }
2087 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002088
2089 // "Indicates that all RELATIVE relocations have been concatenated together,
2090 // and specifies the RELATIVE relocation count."
2091 //
2092 // TODO: Spec also mentions that this can be used to optimize relocation process;
2093 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002094 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002095 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002096 case DT_RELA:
2097 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2098 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002099#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002100 case DT_INIT:
2101 init_func = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2102 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func);
2103 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002104
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002105 case DT_FINI:
2106 fini_func = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2107 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func);
2108 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002109
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002110 case DT_INIT_ARRAY:
2111 init_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2112 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array);
2113 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002114
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002115 case DT_INIT_ARRAYSZ:
2116 init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
2117 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002118
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002119 case DT_FINI_ARRAY:
2120 fini_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2121 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array);
2122 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002123
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002124 case DT_FINI_ARRAYSZ:
2125 fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
2126 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002127
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002128 case DT_PREINIT_ARRAY:
2129 preinit_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2130 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array);
2131 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002132
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002133 case DT_PREINIT_ARRAYSZ:
2134 preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
2135 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002136
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002137 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002138#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002139 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2140 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002141#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002142 has_text_relocations = true;
2143 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002144#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002145
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002146 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002147 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002148 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002149
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002150 case DT_NEEDED:
2151 ++needed_count;
2152 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002153
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002154 case DT_FLAGS:
2155 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002156#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002157 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2158 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002159#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002160 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002161#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002162 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002163 if (d->d_un.d_val & DF_SYMBOLIC) {
2164 has_DT_SYMBOLIC = true;
2165 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002166 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002167
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002168 case DT_FLAGS_1:
2169 if ((d->d_un.d_val & DF_1_GLOBAL) != 0) {
2170 rtld_flags |= RTLD_GLOBAL;
2171 }
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002172
2173 if ((d->d_un.d_val & DF_1_NODELETE) != 0) {
2174 rtld_flags |= RTLD_NODELETE;
2175 }
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002176 // TODO: Implement other flags
2177
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002178 if ((d->d_un.d_val & ~(DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE)) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002179 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2180 }
2181 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002182#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002183 case DT_MIPS_RLD_MAP:
2184 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2185 {
2186 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2187 *dp = &_r_debug;
2188 }
2189 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002190
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002191 case DT_MIPS_RLD_VERSION:
2192 case DT_MIPS_FLAGS:
2193 case DT_MIPS_BASE_ADDRESS:
2194 case DT_MIPS_UNREFEXTNO:
2195 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002196
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002197 case DT_MIPS_SYMTABNO:
2198 mips_symtabno = d->d_un.d_val;
2199 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002200
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002201 case DT_MIPS_LOCAL_GOTNO:
2202 mips_local_gotno = d->d_un.d_val;
2203 break;
2204
2205 case DT_MIPS_GOTSYM:
2206 mips_gotsym = d->d_un.d_val;
2207 break;
2208#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002209 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2210 case DT_BIND_NOW:
2211 break;
2212
2213 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002214 case DT_VERSYM:
2215 case DT_VERDEF:
2216 case DT_VERDEFNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002217 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002218
2219 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002220 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002221 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002222 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2223 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002224 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002225 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002226 }
2227
2228 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
2229 reinterpret_cast<void*>(base), strtab, symtab);
2230
2231 // Sanity checks.
2232 if (relocating_linker && needed_count != 0) {
2233 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2234 return false;
2235 }
2236 if (nbucket == 0) {
2237 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", name);
2238 return false;
2239 }
2240 if (strtab == 0) {
2241 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2242 return false;
2243 }
2244 if (symtab == 0) {
2245 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2246 return false;
2247 }
2248 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002249}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002250
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002251bool soinfo::LinkImage(const soinfo_list_t& local_group, const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002252
Elliott Hughese4d792a2013-10-28 14:19:05 -07002253#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002254 if (has_text_relocations) {
2255 // Make segments writable to allow text relocations to work properly. We will later call
2256 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2257 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2258 "security hardening. Please fix.", name);
2259 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2260 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2261 name, strerror(errno));
2262 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002263 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002264 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002265#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002266
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002267#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002268 if (rela != nullptr) {
2269 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002270 if (Relocate(rela, rela_count, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002271 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002272 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002273 }
2274 if (plt_rela != nullptr) {
2275 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002276 if (Relocate(plt_rela, plt_rela_count, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002277 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002278 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002279 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002280#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002281 if (rel != nullptr) {
2282 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002283 if (Relocate(rel, rel_count, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002284 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002285 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002286 }
2287 if (plt_rel != nullptr) {
2288 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002289 if (Relocate(plt_rel, plt_rel_count, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002290 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002291 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002292 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002293#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002294
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002295#if defined(__mips__)
Dmitriy Ivanov90b74fb2014-10-23 14:34:12 -07002296 if (!mips_relocate_got(this, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002297 return false;
2298 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002299#endif
2300
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002301 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002302
Elliott Hughese4d792a2013-10-28 14:19:05 -07002303#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002304 if (has_text_relocations) {
2305 // All relocations are done, we can protect our segments back to read-only.
2306 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2307 DL_ERR("can't protect segments for \"%s\": %s",
2308 name, strerror(errno));
2309 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002310 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002311 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002312#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002313
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002314 /* We can also turn on GNU RELRO protection */
2315 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2316 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2317 name, strerror(errno));
2318 return false;
2319 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002320
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002321 /* Handle serializing/sharing the RELRO segment */
2322 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2323 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2324 extinfo->relro_fd) < 0) {
2325 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2326 name, strerror(errno));
2327 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002328 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002329 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2330 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2331 extinfo->relro_fd) < 0) {
2332 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2333 name, strerror(errno));
2334 return false;
2335 }
2336 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002337
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002338 notify_gdb_of_load(this);
2339 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002340}
2341
Nick Kralevich468319c2011-11-11 15:53:17 -08002342/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002343 * This function add vdso to internal dso list.
2344 * It helps to stack unwinding through signal handlers.
2345 * Also, it makes bionic more like glibc.
2346 */
Kito Cheng812fd422014-03-25 22:53:56 +08002347static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002348#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002349 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002350 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002351 return;
2352 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002353
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002354 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002355
Elliott Hughes0266ae52014-02-10 17:46:57 -08002356 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2357 si->phnum = ehdr_vdso->e_phnum;
2358 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2359 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002360 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002361
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002362 si->PrelinkImage();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002363 si->LinkImage(g_empty_list, nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002364#endif
2365}
2366
2367/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002368 * This is linker soinfo for GDB. See details below.
2369 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002370#if defined(__LP64__)
2371#define LINKER_PATH "/system/bin/linker64"
2372#else
2373#define LINKER_PATH "/system/bin/linker"
2374#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002375static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002376
2377/* gdb expects the linker to be in the debug shared object list.
2378 * Without this, gdb has trouble locating the linker's ".text"
2379 * and ".plt" sections. Gdb could also potentially use this to
2380 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2381 * Don't use soinfo_alloc(), because the linker shouldn't
2382 * be on the soinfo list.
2383 */
2384static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002385 linker_soinfo_for_gdb.base = linker_base;
2386
2387 /*
2388 * Set the dynamic field in the link map otherwise gdb will complain with
2389 * the following:
2390 * warning: .dynamic section for "/system/bin/linker" is not at the
2391 * expected address (wrong library or version mismatch?)
2392 */
2393 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2394 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2395 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002396 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002397 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2398}
2399
2400/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002401 * This code is called after the linker has linked itself and
2402 * fixed it's own GOT. It is safe to make references to externs
2403 * and other non-local data at this point.
2404 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002405static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002406#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002407 struct timeval t0, t1;
2408 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002409#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002410
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002411 // Initialize environment functions, and get to the ELF aux vectors table.
2412 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002413
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002414 // If this is a setuid/setgid program, close the security hole described in
2415 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2416 if (get_AT_SECURE()) {
2417 nullify_closed_stdio();
2418 }
2419
2420 debuggerd_init();
2421
2422 // Get a few environment variables.
2423 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2424 if (LD_DEBUG != nullptr) {
2425 g_ld_debug_verbosity = atoi(LD_DEBUG);
2426 }
2427
2428 // Normally, these are cleaned by linker_env_init, but the test
2429 // doesn't cost us anything.
2430 const char* ldpath_env = nullptr;
2431 const char* ldpreload_env = nullptr;
2432 if (!get_AT_SECURE()) {
2433 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2434 ldpreload_env = linker_env_get("LD_PRELOAD");
2435 }
2436
2437 INFO("[ android linker & debugger ]");
2438
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002439 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002440 if (si == nullptr) {
2441 exit(EXIT_FAILURE);
2442 }
2443
2444 /* bootstrap the link map, the main exe always needs to be first */
2445 si->flags |= FLAG_EXE;
2446 link_map* map = &(si->link_map_head);
2447
2448 map->l_addr = 0;
2449 map->l_name = args.argv[0];
2450 map->l_prev = nullptr;
2451 map->l_next = nullptr;
2452
2453 _r_debug.r_map = map;
2454 r_debug_tail = map;
2455
2456 init_linker_info_for_gdb(linker_base);
2457
2458 // Extract information passed from the kernel.
2459 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2460 si->phnum = args.getauxval(AT_PHNUM);
2461 si->entry = args.getauxval(AT_ENTRY);
2462
2463 /* Compute the value of si->base. We can't rely on the fact that
2464 * the first entry is the PHDR because this will not be true
2465 * for certain executables (e.g. some in the NDK unit test suite)
2466 */
2467 si->base = 0;
2468 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2469 si->load_bias = 0;
2470 for (size_t i = 0; i < si->phnum; ++i) {
2471 if (si->phdr[i].p_type == PT_PHDR) {
2472 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2473 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2474 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002475 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002476 }
2477 si->dynamic = nullptr;
2478 si->ref_count = 1;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002479
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002480 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2481 if (elf_hdr->e_type != ET_DYN) {
2482 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2483 exit(EXIT_FAILURE);
2484 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002485
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002486 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2487 parse_LD_LIBRARY_PATH(ldpath_env);
2488 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002489
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002490 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002491
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002492 si->PrelinkImage();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002493
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002494 // Load ld_preloads and dependencies.
2495 StringLinkedList needed_library_name_list;
2496 size_t needed_libraries_count = 0;
2497 size_t ld_preloads_count = 0;
2498 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2499 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2500 ++needed_libraries_count;
2501 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002502
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002503 for_each_dt_needed(si, [&](const char* name) {
2504 needed_library_name_list.push_back(name);
2505 ++needed_libraries_count;
2506 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002507
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002508 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002509
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002510 memset(needed_library_names, 0, sizeof(needed_library_names));
2511 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002512
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002513 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 -07002514 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2515 exit(EXIT_FAILURE);
2516 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002517
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002518 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002519
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002520 si->CallPreInitConstructors();
Matt Fischer4fd42c12009-12-31 12:09:10 -06002521
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002522 /* After the PrelinkImage, the si->load_bias is initialized.
2523 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2524 * We need to update this value for so exe here. So Unwind_Backtrace
2525 * for some arch like x86 could work correctly within so exe.
2526 */
2527 map->l_addr = si->load_bias;
2528 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002529
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002530#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002531 gettimeofday(&t1, nullptr);
2532 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2533 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2534 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002535#endif
2536#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002537 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2538 linker_stats.count[kRelocAbsolute],
2539 linker_stats.count[kRelocRelative],
2540 linker_stats.count[kRelocCopy],
2541 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002542#endif
2543#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002544 {
2545 unsigned n;
2546 unsigned i;
2547 unsigned count = 0;
2548 for (n = 0; n < 4096; n++) {
2549 if (bitmask[n]) {
2550 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002551#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002552 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002553#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002554 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002555#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002556 if (x & 1) {
2557 count++;
2558 }
2559 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002560 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002561 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002562 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002563 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2564 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002565#endif
2566
2567#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002568 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002569#endif
2570
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002571 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2572 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002573}
Nick Kralevich468319c2011-11-11 15:53:17 -08002574
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002575/* Compute the load-bias of an existing executable. This shall only
2576 * be used to compute the load bias of an executable or shared library
2577 * that was loaded by the kernel itself.
2578 *
2579 * Input:
2580 * elf -> address of ELF header, assumed to be at the start of the file.
2581 * Return:
2582 * load bias, i.e. add the value of any p_vaddr in the file to get
2583 * the corresponding address in memory.
2584 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002585static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2586 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002587 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002588 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002589
Elliott Hughes0266ae52014-02-10 17:46:57 -08002590 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002591 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002592 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002593 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002594 }
2595 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002596}
2597
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002598extern "C" void _start();
2599
Nick Kralevich468319c2011-11-11 15:53:17 -08002600/*
2601 * This is the entry point for the linker, called from begin.S. This
2602 * method is responsible for fixing the linker's own relocations, and
2603 * then calling __linker_init_post_relocation().
2604 *
2605 * Because this method is called before the linker has fixed it's own
2606 * relocations, any attempt to reference an extern variable, extern
2607 * function, or other GOT reference will generate a segfault.
2608 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002609extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002610 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002611
Elliott Hughes0266ae52014-02-10 17:46:57 -08002612 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002613 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002614 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002615 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002616
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002617 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002618
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002619 // If the linker is not acting as PT_INTERP entry_point is equal to
2620 // _start. Which means that the linker is running as an executable and
2621 // already linked by PT_INTERP.
2622 //
2623 // This happens when user tries to run 'adb shell /system/bin/linker'
2624 // see also https://code.google.com/p/android/issues/detail?id=63174
2625 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2626 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2627 }
2628
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002629 linker_so.base = linker_addr;
2630 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2631 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002632 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002633 linker_so.phdr = phdr;
2634 linker_so.phnum = elf_hdr->e_phnum;
2635 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07002636
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002637 if (!(linker_so.PrelinkImage() && linker_so.LinkImage(g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002638 // It would be nice to print an error message, but if the linker
2639 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002640 // call write() (because it involves a GOT reference). We may as
2641 // well try though...
2642 const char* msg = "CANNOT LINK EXECUTABLE: ";
2643 write(2, msg, strlen(msg));
2644 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2645 write(2, "\n", 1);
2646 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002647 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002648
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002649 __libc_init_tls(args);
2650
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002651 // Initialize the linker's own global variables
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002652 linker_so.CallConstructors();
2653
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002654 // Initialize static variables. Note that in order to
2655 // get correct libdl_info we need to call constructors
2656 // before get_libdl_info().
2657 solist = get_libdl_info();
2658 sonext = get_libdl_info();
2659
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002660 // We have successfully fixed our own relocations. It's safe to run
2661 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002662 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002663 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002664
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002665 protect_data(PROT_READ);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002666
2667 // Return the address that the calling assembly stub should jump to.
2668 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002669}