blob: b8a88004d0999073dba48d5692fca7a76c7c718a [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Doug Kwan94304352009-10-23 18:11:40 -07002 * Copyright (C) 2008, 2009 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughes46882792012-08-03 16:49:39 -070029#include <dlfcn.h>
30#include <errno.h>
31#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080032#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070037#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080038#include <sys/param.h>
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -080039#include <sys/personality.h>
Elliott Hughes46882792012-08-03 16:49:39 -070040#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070042#include <new>
43
Elliott Hughes46882792012-08-03 16:49:39 -070044// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070045#include "private/bionic_tls.h"
46#include "private/KernelArgumentBlock.h"
47#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070048#include "private/ScopedFd.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070049#include "private/ScopeGuard.h"
50#include "private/UniquePtr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051
52#include "linker.h"
53#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010054#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020055#include "linker_phdr.h"
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070056#include "linker_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080058/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
59 *
60 * Do NOT use malloc() and friends or pthread_*() code here.
61 * Don't use printf() either; it's caused mysterious memory
62 * corruption in the past.
63 * The linker runs before we bring up libc and it's easiest
64 * to make sure it does not depend on any complex libc features
65 *
66 * open issues / todo:
67 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069 * - after linking, set as much stuff as possible to READONLY
70 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070071 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072
Dmitriy Ivanov489e4982014-05-19 15:19:52 -070073#if defined(__LP64__)
74#define SEARCH_NAME(x) x
75#else
76// Nvidia drivers are relying on the bug:
77// http://code.google.com/p/android/issues/detail?id=6670
78// so we continue to use base-name lookup for lp32
79static const char* get_base_name(const char* name) {
80 const char* bname = strrchr(name, '/');
81 return bname ? bname + 1 : name;
82}
83#define SEARCH_NAME(x) get_base_name(x)
84#endif
85
Elliott Hughes0266ae52014-02-10 17:46:57 -080086static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
Elliott Hughes1728b232014-05-14 10:02:03 -070088static LinkerAllocator<soinfo> g_soinfo_allocator;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070089static LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020090
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070091static soinfo* solist;
92static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -070093static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094
Elliott Hughes1728b232014-05-14 10:02:03 -070095static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070096#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070097 "/vendor/lib64",
98 "/system/lib64",
99#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700100 "/vendor/lib",
101 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700102#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700103 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700104};
David Bartleybc3a5c22009-06-02 18:27:28 -0700105
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800106#define LDPATH_BUFSIZE (LDPATH_MAX*64)
107#define LDPATH_MAX 8
108
109#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
110#define LDPRELOAD_MAX 8
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 Ivanovab972b92014-11-29 13:57:41 -0800234 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700235 // 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 Ivanovab972b92014-11-29 13:57:41 -0800251 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700252 // GDB already knows about the main executable
253 return;
254 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700255
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700256 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700257
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700258 _r_debug.r_state = r_debug::RT_DELETE;
259 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700260
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700261 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700262
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700263 _r_debug.r_state = r_debug::RT_CONSISTENT;
264 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800265}
266
Elliott Hughes18a206c2012-10-29 17:37:13 -0700267void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800268 _r_debug.r_state = r_debug::RT_ADD;
269 rtld_db_dlactivity();
270 _r_debug.r_state = r_debug::RT_CONSISTENT;
271 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800272}
273
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700274LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
275 return g_soinfo_links_allocator.alloc();
276}
277
278void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
279 g_soinfo_links_allocator.free(entry);
280}
281
282static void protect_data(int protection) {
283 g_soinfo_allocator.protect_all(protection);
284 g_soinfo_links_allocator.protect_all(protection);
285}
286
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700287static soinfo* soinfo_alloc(const char* name, struct stat* file_stat, off64_t file_offset, uint32_t rtld_flags) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200288 if (strlen(name) >= SOINFO_NAME_LEN) {
289 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700290 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200291 }
292
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700293 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700294
Magnus Malmbornba98d922012-09-12 13:00:55 +0200295 sonext->next = si;
296 sonext = si;
297
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700298 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200299 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800300}
301
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800302static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700303 if (si == nullptr) {
304 return;
305 }
306
307 if (si->base != 0 && si->size != 0) {
308 munmap(reinterpret_cast<void*>(si->base), si->size);
309 }
310
311 soinfo *prev = nullptr, *trav;
312
313 TRACE("name %s: freeing soinfo @ %p", si->name, si);
314
315 for (trav = solist; trav != nullptr; trav = trav->next) {
316 if (trav == si) {
317 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700318 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700319 prev = trav;
320 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800321
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700322 if (trav == nullptr) {
323 // si was not in solist
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -0800324 DL_ERR("name \"%s\"@%p is not in solist!", si->name, si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700325 return;
326 }
Elliott Hughes46882792012-08-03 16:49:39 -0700327
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700328 // clear links to/from si
329 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700330
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700331 // prev will never be null, because the first entry in solist is
332 // always the static libdl_info.
333 prev->next = si->next;
334 if (si == sonext) {
335 sonext = prev;
336 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800337
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700338 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339}
340
Elliott Hughescade4c32012-12-20 14:42:14 -0800341static 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 Ivanovec18ce02014-11-09 19:27:20 -0800420ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name) {
421 return is_gnu_hash() ? gnu_lookup(symbol_name) : elf_lookup(symbol_name);
422}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800423
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800424static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
425 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
426 ELF_ST_BIND(s->st_info) == STB_WEAK) {
427 return s->st_shndx != SHN_UNDEF;
428 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
429 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
430 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->name);
431 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800432
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800433 return false;
434}
435
436ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name) {
437 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800438 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800439
440 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800441 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
442 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800443
444 // test against bloom filter
445 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
446 return nullptr;
447 }
448
449 // bloom test says "probably yes"...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800450 uint32_t n = bucket_[hash % nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800451
452 if (n == 0) {
453 return nullptr;
454 }
455
456 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800457 ElfW(Sym)* s = symtab_ + n;
458 if (((chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800459 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
460 is_symbol_global_and_defined(this, s)) {
461 return s;
462 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800463 } while ((chain_[n++] & 1) == 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800464
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800465 return nullptr;
466}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800467
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800468ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name) {
469 uint32_t hash = symbol_name.elf_hash();
470
471 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800472 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800473
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800474 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
475 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800476 if (strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && is_symbol_global_and_defined(this, s)) {
477 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
478 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
479 static_cast<size_t>(s->st_size));
480 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800481 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800482 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800483
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700484 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800485 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700486
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700487 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800488}
489
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700490soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700491 memset(this, 0, sizeof(*this));
492
493 strlcpy(this->name, name, sizeof(this->name));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800494 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800495 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700496
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700497 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800498 this->st_dev_ = file_stat->st_dev;
499 this->st_ino_ = file_stat->st_ino;
500 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700501 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700502
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800503 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700504}
505
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800506
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800507uint32_t SymbolName::elf_hash() {
508 if (!has_elf_hash_) {
509 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
510 uint32_t h = 0, g;
511
512 while (*name) {
513 h = (h << 4) + *name++;
514 g = h & 0xf0000000;
515 h ^= g;
516 h ^= g >> 24;
517 }
518
519 elf_hash_ = h;
520 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700521 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800522
523 return elf_hash_;
524}
525
526uint32_t SymbolName::gnu_hash() {
527 if (!has_gnu_hash_) {
528 uint32_t h = 5381;
529 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
530 while (*name != 0) {
531 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
532 }
533
534 gnu_hash_ = h;
535 has_gnu_hash_ = true;
536 }
537
538 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800539}
540
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700541static ElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
542 const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800543 SymbolName symbol_name(name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700544 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700545
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700546 /* "This element's presence in a shared object library alters the dynamic linker's
547 * symbol resolution algorithm for references within the library. Instead of starting
548 * a symbol search with the executable file, the dynamic linker starts from the shared
549 * object itself. If the shared object fails to supply the referenced symbol, the
550 * dynamic linker then searches the executable file and other shared objects as usual."
551 *
552 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
553 *
554 * Note that this is unlikely since static linker avoids generating
555 * relocations for -Bsymbolic linked dynamic executables.
556 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700557 if (si_from->has_DT_SYMBOLIC) {
558 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->name, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800559 s = si_from->find_symbol_by_name(symbol_name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700560 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700561 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700562 }
563 }
564
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700565 // 1. Look for it in global_group
566 if (s == nullptr) {
567 global_group.visit([&](soinfo* global_si) {
568 DEBUG("%s: looking up %s in %s (from global group)", si_from->name, name, global_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800569 s = global_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700570 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700571 *si_found_in = global_si;
572 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700573 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700574
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700575 return true;
576 });
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700577 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700578
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700579 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700580 if (s == nullptr) {
581 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700582 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700583 // we already did this - skip
584 return true;
585 }
586
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700587 DEBUG("%s: looking up %s in %s (from local group)", si_from->name, name, local_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800588 s = local_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700589 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700590 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700591 return false;
592 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700593
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700594 return true;
595 });
596 }
597
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700598 if (s != nullptr) {
599 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
600 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700601 si_from->name, name, reinterpret_cast<void*>(s->st_value),
602 (*si_found_in)->name, reinterpret_cast<void*>((*si_found_in)->base),
603 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700604 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700605
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700606 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700607}
608
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700609// Each size has it's own allocator.
610template<size_t size>
611class SizeBasedAllocator {
612 public:
613 static void* alloc() {
614 return allocator_.alloc();
615 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700616
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700617 static void free(void* ptr) {
618 allocator_.free(ptr);
619 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700620
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700621 private:
622 static LinkerBlockAllocator allocator_;
623};
624
625template<size_t size>
626LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
627
628template<typename T>
629class TypeBasedAllocator {
630 public:
631 static T* alloc() {
632 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
633 }
634
635 static void free(T* ptr) {
636 SizeBasedAllocator<sizeof(T)>::free(ptr);
637 }
638};
639
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700640class LoadTask {
641 public:
642 struct deleter_t {
643 void operator()(LoadTask* t) {
644 TypeBasedAllocator<LoadTask>::free(t);
645 }
646 };
647
648 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
649
650 static deleter_t deleter;
651
652 static LoadTask* create(const char* name, soinfo* needed_by) {
653 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
654 return new (ptr) LoadTask(name, needed_by);
655 }
656
657 const char* get_name() const {
658 return name_;
659 }
660
661 soinfo* get_needed_by() const {
662 return needed_by_;
663 }
664 private:
665 LoadTask(const char* name, soinfo* needed_by)
666 : name_(name), needed_by_(needed_by) {}
667
668 const char* name_;
669 soinfo* needed_by_;
670
671 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
672};
673
Ningsheng Jiane93be992014-09-16 15:22:10 +0800674LoadTask::deleter_t LoadTask::deleter;
675
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700676template <typename T>
677using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
678
679typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700680typedef linked_list_t<const char> StringLinkedList;
681typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700682
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700683
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700684// This function walks down the tree of soinfo dependencies
685// in breadth-first order and
686// * calls action(soinfo* si) for each node, and
687// * terminates walk if action returns false.
688//
689// walk_dependencies_tree returns false if walk was terminated
690// by the action and true otherwise.
691template<typename F>
692static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700693 SoinfoLinkedList visit_list;
694 SoinfoLinkedList visited;
695
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700696 for (size_t i = 0; i < root_soinfos_size; ++i) {
697 visit_list.push_back(root_soinfos[i]);
698 }
699
700 soinfo* si;
701 while ((si = visit_list.pop_front()) != nullptr) {
702 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700703 continue;
704 }
705
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700706 if (!action(si)) {
707 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700708 }
709
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700710 visited.push_back(si);
711
712 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700713 visit_list.push_back(child);
714 });
715 }
716
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700717 return true;
718}
719
720
721// This is used by dlsym(3). It performs symbol lookup only within the
722// specified soinfo object and its dependencies in breadth first order.
723ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
724 ElfW(Sym)* result = nullptr;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800725 SymbolName symbol_name(name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700726
727
728 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800729 result = current_soinfo->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700730 if (result != nullptr) {
731 *found = current_soinfo;
732 return false;
733 }
734
735 return true;
736 });
737
738 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800739}
740
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800741/* This is used by dlsym(3) to performs a global symbol lookup. If the
742 start value is null (for RTLD_DEFAULT), the search starts at the
743 beginning of the global solist. Otherwise the search starts at the
744 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700745 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700746ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800747 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800748
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700749 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800750 start = solist;
751 }
752
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700753 ElfW(Sym)* s = nullptr;
754 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700755 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
756 continue;
757 }
758
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800759 s = si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700760 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800761 *found = si;
762 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600763 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800764 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600765
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700766 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700767 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
768 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800769 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800770
Elliott Hughescade4c32012-12-20 14:42:14 -0800771 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800772}
773
Kito Chengfa8c05d2013-03-12 14:58:06 +0800774soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800775 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700776 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800777 if (address >= si->base && address - si->base < si->size) {
778 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600779 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800780 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700781 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600782}
783
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800784ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
785 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
786}
787
788static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
789 return sym->st_shndx != SHN_UNDEF &&
790 soaddr >= sym->st_value &&
791 soaddr < sym->st_value + sym->st_size;
792}
793
794ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
795 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - base;
796
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800797 for (size_t i = 0; i < nbucket_; ++i) {
798 uint32_t n = bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800799
800 if (n == 0) {
801 continue;
802 }
803
804 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800805 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800806 if (symbol_matches_soaddr(sym, soaddr)) {
807 return sym;
808 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800809 } while ((chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800810 }
811
812 return nullptr;
813}
814
815ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
816 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600817
Kito Chengfa8c05d2013-03-12 14:58:06 +0800818 // Search the library's symbol table for any defined symbol which
819 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800820 for (size_t i = 0; i < nchain_; ++i) {
821 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800822 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800823 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600824 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800825 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600826
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700827 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600828}
829
Elliott Hughes124fae92012-10-31 14:20:03 -0700830static int open_library_on_path(const char* name, const char* const paths[]) {
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +0000831 char buf[512];
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700832 for (size_t i = 0; paths[i] != nullptr; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800833 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700834 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700835 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700836 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800837 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700838 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
839 if (fd != -1) {
840 return fd;
841 }
842 }
843 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800844}
845
Elliott Hughes124fae92012-10-31 14:20:03 -0700846static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700847 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800848
Elliott Hughes124fae92012-10-31 14:20:03 -0700849 // 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 -0700850 if (strchr(name, '/') != nullptr) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700851 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
852 if (fd != -1) {
853 return fd;
854 }
855 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700856#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700857 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700858#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700859 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800860
Elliott Hughes124fae92012-10-31 14:20:03 -0700861 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Elliott Hughes1728b232014-05-14 10:02:03 -0700862 int fd = open_library_on_path(name, g_ld_library_paths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700863 if (fd == -1) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700864 fd = open_library_on_path(name, kDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700865 }
866 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800867}
868
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700869template<typename F>
870static void for_each_dt_needed(const soinfo* si, F action) {
871 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
872 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700873 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700874 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700875 }
876}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800877
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700878static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700879 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700880 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700881 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700882
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700883 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
884 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700885 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
886 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700887 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700888 } else {
889 // Open the file.
890 fd = open_library(name);
891 if (fd == -1) {
892 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700893 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700894 }
895
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700896 file_guard.reset(fd);
897 }
898
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700899 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700900 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700901 return nullptr;
902 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800903 if (file_offset < 0) {
904 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
905 return nullptr;
906 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700907
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700908 struct stat file_stat;
909 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700910 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700911 return nullptr;
912 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800913 if (file_offset >= file_stat.st_size) {
914 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64, name, file_offset, file_stat.st_size);
915 return nullptr;
916 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700917
918 // Check for symlink and other situations where
919 // file can have different names.
920 for (soinfo* si = solist; si != nullptr; si = si->next) {
921 if (si->get_st_dev() != 0 &&
922 si->get_st_ino() != 0 &&
923 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700924 si->get_st_ino() == file_stat.st_ino &&
925 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700926 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
927 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700928 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700929 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700930
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700931 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700932 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700933 return nullptr;
934 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700935
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700936 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700937 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700938 if (!elf_reader.Load(extinfo)) {
939 return nullptr;
940 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800941
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700942 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700943 if (si == nullptr) {
944 return nullptr;
945 }
946 si->base = elf_reader.load_start();
947 si->size = elf_reader.load_size();
948 si->load_bias = elf_reader.load_bias();
949 si->phnum = elf_reader.phdr_count();
950 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700951
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800952 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700953 soinfo_free(si);
954 return nullptr;
955 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700956
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700957 for_each_dt_needed(si, [&] (const char* name) {
958 load_tasks.push_back(LoadTask::create(name, si));
959 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700960
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700961 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800962}
963
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700964static soinfo *find_loaded_library_by_name(const char* name) {
965 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700966 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700967 if (!strcmp(search_name, si->name)) {
968 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200969 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700970 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700971 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200972}
973
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700974static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200975
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700976 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700977
978 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700979 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700980 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700981 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700982 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700983 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800984
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700985 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800986}
987
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700988static void soinfo_unload(soinfo* si);
989
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700990// TODO: this is slightly unusual way to construct
991// the global group for relocation. Not every RTLD_GLOBAL
992// library is included in this group for backwards-compatibility
993// reasons.
994//
995// This group consists of the main executable, LD_PRELOADs
996// and libraries with the DF_1_GLOBAL flag set.
997static soinfo::soinfo_list_t make_global_group() {
998 soinfo::soinfo_list_t global_group;
999 for (soinfo* si = somain; si != nullptr; si = si->next) {
1000 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1001 global_group.push_back(si);
1002 }
1003 }
1004
1005 return global_group;
1006}
1007
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001008static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
1009 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001010 // Step 0: prepare.
1011 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001012 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001013 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001014 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001015 }
1016
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001017 // Construct global_group.
1018 soinfo::soinfo_list_t global_group = make_global_group();
1019
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001020 // If soinfos array is null allocate one on stack.
1021 // The array is needed in case of failure; for example
1022 // when library_names[] = {libone.so, libtwo.so} and libone.so
1023 // is loaded correctly but libtwo.so failed for some reason.
1024 // In this case libone.so should be unloaded on return.
1025 // See also implementation of failure_guard below.
1026
1027 if (soinfos == nullptr) {
1028 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1029 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1030 memset(soinfos, 0, soinfos_size);
1031 }
1032
1033 // list of libraries to link - see step 2.
1034 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001035
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001036 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001037 // Housekeeping
1038 load_tasks.for_each([] (LoadTask* t) {
1039 LoadTask::deleter(t);
1040 });
1041
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001042 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001043 soinfo_unload(soinfos[i]);
1044 }
1045 });
1046
1047 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
1048 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 -07001049 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001050 if (si == nullptr) {
1051 return false;
1052 }
1053
1054 soinfo* needed_by = task->get_needed_by();
1055
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001056 if (needed_by != nullptr) {
1057 needed_by->add_child(si);
1058 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001059
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001060 if (si->is_linked()) {
1061 si->increment_ref_count();
1062 }
1063
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001064 // When ld_preloads is not null, the first
1065 // ld_preloads_count libs are in fact ld_preloads.
1066 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001067 // Add LD_PRELOADed libraries to the global group for future runs.
1068 // There is no need to explicitly add them to the global group
1069 // for this run because they are going to appear in the local
1070 // group in the correct order.
1071 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001072 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001073 }
1074
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001075 if (soinfos_count < library_names_count) {
1076 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001077 }
1078 }
1079
1080 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001081 soinfo::soinfo_list_t local_group;
1082 walk_dependencies_tree(
1083 start_with == nullptr ? soinfos : &start_with,
1084 start_with == nullptr ? soinfos_count : 1,
1085 [&] (soinfo* si) {
1086 local_group.push_back(si);
1087 return true;
1088 });
1089
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001090 // We need to increment ref_count in case
1091 // the root of the local group was not linked.
1092 bool was_local_group_root_linked = local_group.front()->is_linked();
1093
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001094 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001095 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001096 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001097 return false;
1098 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001099 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001100 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001101
1102 return true;
1103 });
1104
1105 if (linked) {
1106 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001107 }
1108
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001109 if (!was_local_group_root_linked) {
1110 local_group.front()->increment_ref_count();
1111 }
1112
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001113 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001114}
1115
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001116static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001117 soinfo* si;
1118
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001119 if (name == nullptr) {
1120 si = somain;
1121 } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001122 return nullptr;
1123 }
1124
Elliott Hughesd23736e2012-11-01 15:16:56 -07001125 return si;
1126}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001127
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001128static void soinfo_unload(soinfo* root) {
1129 // Note that the library can be loaded but not linked;
1130 // in which case there is no root but we still need
1131 // to walk the tree and unload soinfos involved.
1132 //
1133 // This happens on unsuccessful dlopen, when one of
1134 // the DT_NEEDED libraries could not be linked/found.
1135 if (root->is_linked()) {
1136 root = root->get_local_group_root();
1137 }
1138
1139 if (!root->can_unload()) {
1140 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->name);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001141 return;
1142 }
1143
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001144 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001145
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001146 if (ref_count == 0) {
1147 soinfo::soinfo_list_t local_unload_list;
1148 soinfo::soinfo_list_t external_unload_list;
1149 soinfo::soinfo_list_t depth_first_list;
1150 depth_first_list.push_back(root);
1151 soinfo* si = nullptr;
1152
1153 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001154 if (local_unload_list.contains(si)) {
1155 continue;
1156 }
1157
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001158 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001159
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001160 if (si->has_min_version(0)) {
1161 soinfo* child = nullptr;
1162 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001163 TRACE("%s@%p needs to unload %s@%p", si->name, si, child->name, child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001164 if (local_unload_list.contains(child)) {
1165 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001166 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001167 external_unload_list.push_back(child);
1168 } else {
1169 depth_first_list.push_front(child);
1170 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001171 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001172 } else {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001173#ifdef __LP64__
1174 __libc_fatal("soinfo for \"%s\"@%p has no version", si->name, si);
1175#else
1176 PRINT("warning: soinfo for \"%s\"@%p has no version", si->name, si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001177 for_each_dt_needed(si, [&] (const char* library_name) {
1178 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1179 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1180 if (needed != nullptr) {
1181 // Not found: for example if symlink was deleted between dlopen and dlclose
1182 // Since we cannot really handle errors at this point - print and continue.
1183 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
1184 return;
1185 } else if (local_unload_list.contains(needed)) {
1186 // already visited
1187 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001188 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001189 // external group
1190 external_unload_list.push_back(needed);
1191 } else {
1192 // local group
1193 depth_first_list.push_front(needed);
1194 }
1195 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001196#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001197 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001198 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001199
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001200 local_unload_list.for_each([](soinfo* si) {
1201 si->call_destructors();
1202 });
1203
1204 while ((si = local_unload_list.pop_front()) != nullptr) {
1205 notify_gdb_of_unload(si);
1206 soinfo_free(si);
1207 }
1208
1209 while ((si = external_unload_list.pop_front()) != nullptr) {
1210 soinfo_unload(si);
1211 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001212 } else {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001213 TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->name, ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001214 }
1215}
1216
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001217void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001218 // Use basic string manipulation calls to avoid snprintf.
1219 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1220 // When debug malloc is enabled, this call returns 0. This in turn causes
1221 // snprintf to do nothing, which causes libraries to fail to load.
1222 // See b/17302493 for further details.
1223 // Once the above bug is fixed, this code can be modified to use
1224 // snprintf again.
1225 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1226 if (buffer_size < required_len) {
1227 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1228 buffer_size, required_len);
1229 }
1230 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1231 *end = ':';
1232 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001233}
1234
Elliott Hughescade4c32012-12-20 14:42:14 -08001235void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
1236 if (!get_AT_SECURE()) {
1237 parse_LD_LIBRARY_PATH(ld_library_path);
1238 }
1239}
1240
Elliott Hughes1a586292014-06-03 16:23:08 -07001241soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001242 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001243 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001244 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001245 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001246 if (extinfo != nullptr) {
1247 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1248 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1249 return nullptr;
1250 }
1251 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001252 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1253 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 -07001254 return nullptr;
1255 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001256 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001257 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001258 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001259 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001260 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001261 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001262 protect_data(PROT_READ);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001263 return si;
1264}
1265
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001266void do_dlclose(soinfo* si) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001267 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001268 soinfo_unload(si);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001269 protect_data(PROT_READ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001270}
1271
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001272static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1273 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1274 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1275 ElfW(Addr) ifunc_addr = ifunc_resolver();
1276 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 -07001277
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001278 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001279}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001280
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001281#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001282int soinfo::relocate(ElfW(Rela)* rela, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001283 for (size_t idx = 0; idx < count; ++idx, ++rela) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001284 unsigned type = ELFW(R_TYPE)(rela->r_info);
1285 unsigned sym = ELFW(R_SYM)(rela->r_info);
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001286 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001287 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001288 const char* sym_name = nullptr;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001289
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001290 DEBUG("Processing '%s' relocation at index %zd", name, idx);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001291 if (type == 0) { // R_*_NONE
1292 continue;
1293 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001294
1295 ElfW(Sym)* s = nullptr;
1296 soinfo* lsi = nullptr;
1297
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001298 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001299 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001300 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001301 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001302 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001303 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001304 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001305 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001306 return -1;
1307 }
1308
1309 /* IHI0044C AAELF 4.5.1.1:
1310
1311 Libraries are not searched to resolve weak references.
1312 It is not an error for a weak reference to remain unsatisfied.
1313
1314 During linking, the value of an undefined weak reference is:
1315 - Zero if the relocation type is absolute
1316 - The address of the place if the relocation is pc-relative
1317 - The address of nominal base address if the relocation
1318 type is base-relative.
1319 */
1320
1321 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001322#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001323 case R_AARCH64_JUMP_SLOT:
1324 case R_AARCH64_GLOB_DAT:
1325 case R_AARCH64_ABS64:
1326 case R_AARCH64_ABS32:
1327 case R_AARCH64_ABS16:
1328 case R_AARCH64_RELATIVE:
1329 case R_AARCH64_IRELATIVE:
1330 /*
1331 * The sym_addr was initialized to be zero above, or the relocation
1332 * code below does not care about value of sym_addr.
1333 * No need to do anything.
1334 */
1335 break;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001336#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001337 case R_X86_64_JUMP_SLOT:
1338 case R_X86_64_GLOB_DAT:
1339 case R_X86_64_32:
1340 case R_X86_64_64:
1341 case R_X86_64_RELATIVE:
1342 case R_X86_64_IRELATIVE:
1343 // No need to do anything.
1344 break;
1345 case R_X86_64_PC32:
1346 sym_addr = reloc;
1347 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001348#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001349 default:
1350 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
1351 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001352 }
1353 } else {
1354 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001355 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001356 }
1357 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001358 }
1359
1360 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001361#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001362 case R_AARCH64_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001363 count_relocation(kRelocAbsolute);
1364 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001365 TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
1366 reloc, (sym_addr + rela->r_addend), sym_name);
1367 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001368 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001369 case R_AARCH64_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001370 count_relocation(kRelocAbsolute);
1371 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001372 TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
1373 reloc, (sym_addr + rela->r_addend), sym_name);
1374 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001375 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001376 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001377 count_relocation(kRelocAbsolute);
1378 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001379 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
1380 reloc, (sym_addr + rela->r_addend), sym_name);
1381 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001382 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001383 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001384 count_relocation(kRelocAbsolute);
1385 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001386 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
1387 reloc, (sym_addr + rela->r_addend), sym_name);
1388 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1389 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001390 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001391 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001392 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1393 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1394 static_cast<ElfW(Addr)>(INT32_MIN),
1395 static_cast<ElfW(Addr)>(UINT32_MAX));
1396 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001397 }
1398 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001399 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001400 count_relocation(kRelocAbsolute);
1401 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001402 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
1403 reloc, (sym_addr + rela->r_addend), sym_name);
1404 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1405 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001406 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001407 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001408 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1409 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1410 static_cast<ElfW(Addr)>(INT16_MIN),
1411 static_cast<ElfW(Addr)>(UINT16_MAX));
1412 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001413 }
1414 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001415 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001416 count_relocation(kRelocRelative);
1417 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001418 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
1419 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1420 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001421 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001422 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001423 count_relocation(kRelocRelative);
1424 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001425 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
1426 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1427 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1428 ((*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 -07001429 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001430 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001431 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1432 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1433 static_cast<ElfW(Addr)>(INT32_MIN),
1434 static_cast<ElfW(Addr)>(UINT32_MAX));
1435 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001436 }
1437 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001438 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001439 count_relocation(kRelocRelative);
1440 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001441 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
1442 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1443 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1444 ((*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 -07001445 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001446 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001447 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1448 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1449 static_cast<ElfW(Addr)>(INT16_MIN),
1450 static_cast<ElfW(Addr)>(UINT16_MAX));
1451 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001452 }
1453 break;
1454
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001455 case R_AARCH64_RELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001456 count_relocation(kRelocRelative);
1457 MARK(rela->r_offset);
1458 if (sym) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001459 DL_ERR("odd RELATIVE form...");
1460 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001461 }
Elliott Hughes0266ae52014-02-10 17:46:57 -08001462 TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001463 reloc, (base + rela->r_addend));
1464 *reinterpret_cast<ElfW(Addr)*>(reloc) = (base + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001465 break;
1466
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001467 case R_AARCH64_IRELATIVE:
1468 count_relocation(kRelocRelative);
1469 MARK(rela->r_offset);
1470 TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
1471 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
1472 break;
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001473
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001474 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001475 /*
1476 * ET_EXEC is not supported so this should not happen.
1477 *
1478 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1479 *
1480 * Section 4.7.1.10 "Dynamic relocations"
1481 * R_AARCH64_COPY may only appear in executable objects where e_type is
1482 * set to ET_EXEC.
1483 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001484 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Nick Kralevich76e289c2014-07-03 12:04:31 -07001485 return -1;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001486 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001487 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
1488 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001489 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001490 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001491 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
1492 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001493 break;
1494#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001495 case R_X86_64_JUMP_SLOT:
1496 count_relocation(kRelocAbsolute);
1497 MARK(rela->r_offset);
1498 TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1499 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1500 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1501 break;
1502 case R_X86_64_GLOB_DAT:
1503 count_relocation(kRelocAbsolute);
1504 MARK(rela->r_offset);
1505 TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1506 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1507 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1508 break;
1509 case R_X86_64_RELATIVE:
1510 count_relocation(kRelocRelative);
1511 MARK(rela->r_offset);
1512 if (sym) {
1513 DL_ERR("odd RELATIVE form...");
1514 return -1;
1515 }
1516 TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1517 static_cast<size_t>(base));
1518 *reinterpret_cast<ElfW(Addr)*>(reloc) = base + rela->r_addend;
1519 break;
1520 case R_X86_64_IRELATIVE:
1521 count_relocation(kRelocRelative);
1522 MARK(rela->r_offset);
1523 TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
1524 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
1525 break;
1526 case R_X86_64_32:
1527 count_relocation(kRelocRelative);
1528 MARK(rela->r_offset);
1529 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1530 static_cast<size_t>(sym_addr), sym_name);
1531 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1532 break;
1533 case R_X86_64_64:
1534 count_relocation(kRelocRelative);
1535 MARK(rela->r_offset);
1536 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1537 static_cast<size_t>(sym_addr), sym_name);
1538 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1539 break;
1540 case R_X86_64_PC32:
1541 count_relocation(kRelocRelative);
1542 MARK(rela->r_offset);
1543 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1544 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1545 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
1546 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
1547 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001548#endif
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001549
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001550 default:
1551 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
1552 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001553 }
1554 }
1555 return 0;
1556}
Chris Dearman99186652014-02-06 20:36:51 -08001557
1558#else // REL, not RELA.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001559int soinfo::relocate(ElfW(Rel)* rel, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001560 for (size_t idx = 0; idx < count; ++idx, ++rel) {
1561 unsigned type = ELFW(R_TYPE)(rel->r_info);
1562 // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
1563 unsigned sym = ELFW(R_SYM)(rel->r_info);
1564 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
1565 ElfW(Addr) sym_addr = 0;
1566 const char* sym_name = nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001567
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001568 DEBUG("Processing '%s' relocation at index %zd", name, idx);
1569 if (type == 0) { // R_*_NONE
1570 continue;
1571 }
1572
1573 ElfW(Sym)* s = nullptr;
1574 soinfo* lsi = nullptr;
1575
1576 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001577 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001578 s = soinfo_do_lookup(this, sym_name, &lsi, global_group, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001579 if (s == nullptr) {
1580 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001581 s = &symtab_[sym];
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001582 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1583 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
1584 return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001585 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001586
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001587 /* IHI0044C AAELF 4.5.1.1:
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001588
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001589 Libraries are not searched to resolve weak references.
1590 It is not an error for a weak reference to remain
1591 unsatisfied.
Doug Kwane8238072009-10-26 12:05:23 -07001592
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001593 During linking, the value of an undefined weak reference is:
1594 - Zero if the relocation type is absolute
1595 - The address of the place if the relocation is pc-relative
1596 - The address of nominal base address if the relocation
1597 type is base-relative.
1598 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001599
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001600 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001601#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001602 case R_ARM_JUMP_SLOT:
1603 case R_ARM_GLOB_DAT:
1604 case R_ARM_ABS32:
1605 case R_ARM_RELATIVE: /* Don't care. */
1606 // sym_addr was initialized to be zero above or relocation
1607 // code below does not care about value of sym_addr.
1608 // No need to do anything.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001609 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001610#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001611 case R_386_JMP_SLOT:
1612 case R_386_GLOB_DAT:
1613 case R_386_32:
1614 case R_386_RELATIVE: /* Don't care. */
1615 case R_386_IRELATIVE:
1616 // sym_addr was initialized to be zero above or relocation
1617 // code below does not care about value of sym_addr.
1618 // No need to do anything.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001619 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001620 case R_386_PC32:
1621 sym_addr = reloc;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001622 break;
1623#endif
1624
1625#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001626 case R_ARM_COPY:
1627 // Fall through. Can't really copy if weak symbol is not found at run-time.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001628#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001629 default:
1630 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001631 return -1;
1632 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001633 } else {
1634 // We got a definition.
1635 sym_addr = lsi->resolve_symbol_address(s);
1636 }
1637 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001638 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001639
1640 switch (type) {
1641#if defined(__arm__)
1642 case R_ARM_JUMP_SLOT:
1643 count_relocation(kRelocAbsolute);
1644 MARK(rel->r_offset);
1645 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1646 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1647 break;
1648 case R_ARM_GLOB_DAT:
1649 count_relocation(kRelocAbsolute);
1650 MARK(rel->r_offset);
1651 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1652 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1653 break;
1654 case R_ARM_ABS32:
1655 count_relocation(kRelocAbsolute);
1656 MARK(rel->r_offset);
1657 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1658 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1659 break;
1660 case R_ARM_REL32:
1661 count_relocation(kRelocRelative);
1662 MARK(rel->r_offset);
1663 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1664 reloc, sym_addr, rel->r_offset, sym_name);
1665 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1666 break;
1667 case R_ARM_COPY:
1668 /*
1669 * ET_EXEC is not supported so this should not happen.
1670 *
1671 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1672 *
1673 * Section 4.7.1.10 "Dynamic relocations"
1674 * R_ARM_COPY may only appear in executable objects where e_type is
1675 * set to ET_EXEC.
1676 */
1677 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
1678 return -1;
1679#elif defined(__i386__)
1680 case R_386_JMP_SLOT:
1681 count_relocation(kRelocAbsolute);
1682 MARK(rel->r_offset);
1683 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1684 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1685 break;
1686 case R_386_GLOB_DAT:
1687 count_relocation(kRelocAbsolute);
1688 MARK(rel->r_offset);
1689 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1690 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1691 break;
1692 case R_386_32:
1693 count_relocation(kRelocRelative);
1694 MARK(rel->r_offset);
1695 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1696 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1697 break;
1698 case R_386_PC32:
1699 count_relocation(kRelocRelative);
1700 MARK(rel->r_offset);
1701 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1702 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1703 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1704 break;
1705#elif defined(__mips__)
1706 case R_MIPS_REL32:
1707#if defined(__LP64__)
1708 // MIPS Elf64_Rel entries contain compound relocations
1709 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
1710 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
1711 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
1712 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
1713 type, (unsigned)ELF64_R_TYPE2(rel->r_info),
1714 (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
1715 return -1;
1716 }
1717#endif
1718 count_relocation(kRelocAbsolute);
1719 MARK(rel->r_offset);
1720 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
1721 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
1722 if (s) {
1723 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1724 } else {
1725 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1726 }
1727 break;
1728#endif
1729
1730#if defined(__arm__)
1731 case R_ARM_RELATIVE:
1732#elif defined(__i386__)
1733 case R_386_RELATIVE:
1734#endif
1735 count_relocation(kRelocRelative);
1736 MARK(rel->r_offset);
1737 if (sym) {
1738 DL_ERR("odd RELATIVE form...");
1739 return -1;
1740 }
1741 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1742 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1743 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1744 break;
1745#if defined(__i386__)
1746 case R_386_IRELATIVE:
1747 count_relocation(kRelocRelative);
1748 MARK(rel->r_offset);
1749 TRACE_TYPE(RELO, "RELO IRELATIVE %p <- %p", reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1750 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + *reinterpret_cast<ElfW(Addr)*>(reloc));
1751 break;
1752#endif
1753
1754 default:
1755 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
1756 return -1;
1757 }
1758 }
1759 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001760}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001761#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001762
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001763#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001764bool soinfo::mips_relocate_got(const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001765 ElfW(Addr)** got = plt_got_;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001766 if (got == nullptr) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001767 return true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001768 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001769
1770 // got[0] is the address of the lazy resolver function.
1771 // got[1] may be used for a GNU extension.
1772 // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
1773 // FIXME: maybe this should be in a separate routine?
Dmitriy Ivanov20463e32014-12-02 13:27:40 -08001774 if ((flags_ & FLAG_LINKER) == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001775 size_t g = 0;
1776 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
1777 if (reinterpret_cast<intptr_t>(got[g]) < 0) {
1778 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
1779 }
1780 // Relocate the local GOT entries.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001781 for (; g < mips_local_gotno_; g++) {
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001782 got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + load_bias);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001783 }
1784 }
1785
1786 // Now for the global GOT entries...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001787 ElfW(Sym)* sym = symtab_ + mips_gotsym_;
1788 got = plt_got_ + mips_local_gotno_;
1789 for (size_t g = mips_gotsym_; g < mips_symtabno_; g++, sym++, got++) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001790 // This is an undefined reference... try to locate it.
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001791 const char* sym_name = get_string(sym->st_name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001792 soinfo* lsi = nullptr;
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001793 ElfW(Sym)* s = soinfo_do_lookup(this, sym_name, &lsi, global_group, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001794 if (s == nullptr) {
1795 // We only allow an undefined symbol if this is a weak reference.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001796 s = &symtab_[g];
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001797 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1798 DL_ERR("cannot locate \"%s\"...", sym_name);
1799 return false;
1800 }
1801 *got = 0;
1802 } else {
1803 // FIXME: is this sufficient?
1804 // For reference see NetBSD link loader
1805 // 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
1806 *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s));
1807 }
1808 }
1809 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001810}
1811#endif
1812
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001813void soinfo::call_array(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001814 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001815 return;
1816 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001817
Elliott Hughesc6200592013-09-30 18:43:46 -07001818 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001819
1820 int begin = reverse ? (count - 1) : 0;
1821 int end = reverse ? -1 : count;
1822 int step = reverse ? -1 : 1;
1823
1824 for (int i = begin; i != end; i += step) {
1825 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001826 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001827 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001828
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001829 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001830}
1831
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001832void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001833 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001834 return;
1835 }
1836
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001837 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001838 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001839 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001840
1841 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1842 // are still writable. This happens with our debug malloc (see http://b/7941716).
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001843 protect_data(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001844}
1845
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001846void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001847 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1848 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001849 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001850}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001851
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001852void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001853 if (constructors_called) {
1854 return;
1855 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001856
Elliott Hughesd23736e2012-11-01 15:16:56 -07001857 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1858 // protect against recursive constructor calls. One simple example of constructor recursion
1859 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1860 // 1. The program depends on libc, so libc's constructor is called here.
1861 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1862 // 3. dlopen() calls the constructors on the newly created
1863 // soinfo for libc_malloc_debug_leak.so.
1864 // 4. The debug .so depends on libc, so CallConstructors is
1865 // called again with the libc soinfo. If it doesn't trigger the early-
1866 // out above, the libc constructor will be called again (recursively!).
1867 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001868
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001869 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001870 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001871 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001872 name, preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001873 }
1874
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001875 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001876 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001877 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001878
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001879 TRACE("\"%s\": calling constructors", name);
1880
1881 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001882 call_function("DT_INIT", init_func_);
1883 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001884}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001885
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001886void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001887 if (!constructors_called) {
1888 return;
1889 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001890 TRACE("\"%s\": calling destructors", name);
1891
1892 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001893 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001894
1895 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001896 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001897
1898 // This is needed on second call to dlopen
1899 // after library has been unloaded with RTLD_NODELETE
1900 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001901}
1902
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001903void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001904 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001905 child->parents_.push_back(this);
1906 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001907 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001908}
1909
1910void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001911 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001912 return;
1913 }
1914
1915 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001916 children_.for_each([&] (soinfo* child) {
1917 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001918 return parent == this;
1919 });
1920 });
1921
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001922 parents_.for_each([&] (soinfo* parent) {
1923 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001924 return child == this;
1925 });
1926 });
1927
1928 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001929 parents_.clear();
1930 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001931}
1932
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001933dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001934 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001935 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001936 }
1937
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001938 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001939};
1940
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001941ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001942 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001943 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001944 }
1945
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001946 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001947}
1948
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001949off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001950 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001951 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001952 }
1953
1954 return 0;
1955}
1956
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001957uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001958 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001959 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001960 }
1961
1962 return 0;
1963}
1964
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001965uint32_t soinfo::get_dt_flags_1() const {
1966 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001967 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001968 }
1969
1970 return 0;
1971}
1972void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1973 if (has_min_version(1)) {
1974 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001975 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001976 }
1977
1978 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001979 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001980 }
1981
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001982 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001983 }
1984}
1985
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001986// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001987// 'this->flags' does not have FLAG_NEW_SOINFO set.
1988static soinfo::soinfo_list_t g_empty_list;
1989
1990soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001991 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001992 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001993 }
1994
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001995 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001996}
1997
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001998soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001999 if (has_min_version(0)) {
2000 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002001 }
2002
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002003 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002004}
2005
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002006ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
2007 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
2008 return call_ifunc_resolver(s->st_value + load_bias);
2009 }
2010
2011 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
2012}
2013
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002014const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002015 if (has_min_version(1) && (index >= strtab_size_)) {
2016 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002017 }
2018
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002019 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002020}
2021
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002022bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002023 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002024}
2025
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002026bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002027 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002028}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002029
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002030bool soinfo::is_linked() const {
2031 return (flags_ & FLAG_LINKED) != 0;
2032}
2033
2034bool soinfo::is_main_executable() const {
2035 return (flags_ & FLAG_EXE) != 0;
2036}
2037
2038void soinfo::set_linked() {
2039 flags_ |= FLAG_LINKED;
2040}
2041
2042void soinfo::set_linker_flag() {
2043 flags_ |= FLAG_LINKER;
2044}
2045
2046void soinfo::set_main_executable() {
2047 flags_ |= FLAG_EXE;
2048}
2049
2050void soinfo::increment_ref_count() {
2051 local_group_root_->ref_count_++;
2052}
2053
2054size_t soinfo::decrement_ref_count() {
2055 return --local_group_root_->ref_count_;
2056}
2057
2058soinfo* soinfo::get_local_group_root() const {
2059 return local_group_root_;
2060}
2061
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002062/* Force any of the closed stdin, stdout and stderr to be associated with
2063 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07002064static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002065 int dev_null, i, status;
2066 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002067
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002068 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
2069 if (dev_null < 0) {
2070 DL_ERR("cannot open /dev/null: %s", strerror(errno));
2071 return -1;
2072 }
2073 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002074
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002075 /* If any of the stdio file descriptors is valid and not associated
2076 with /dev/null, dup /dev/null to it. */
2077 for (i = 0; i < 3; i++) {
2078 /* If it is /dev/null already, we are done. */
2079 if (i == dev_null) {
2080 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002081 }
2082
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002083 TRACE("[ Nullifying stdio file descriptor %d]", i);
2084 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
2085
2086 /* If file is opened, we are good. */
2087 if (status != -1) {
2088 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002089 }
2090
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002091 /* The only error we allow is that the file descriptor does not
2092 exist, in which case we dup /dev/null to it. */
2093 if (errno != EBADF) {
2094 DL_ERR("fcntl failed: %s", strerror(errno));
2095 return_value = -1;
2096 continue;
2097 }
2098
2099 /* Try dupping /dev/null to this stdio file descriptor and
2100 repeat if there is a signal. Note that any errors in closing
2101 the stdio descriptor are lost. */
2102 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
2103 if (status < 0) {
2104 DL_ERR("dup2 failed: %s", strerror(errno));
2105 return_value = -1;
2106 continue;
2107 }
2108 }
2109
2110 /* If /dev/null is not one of the stdio file descriptors, close it. */
2111 if (dev_null > 2) {
2112 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
2113 status = TEMP_FAILURE_RETRY(close(dev_null));
2114 if (status == -1) {
2115 DL_ERR("close failed: %s", strerror(errno));
2116 return_value = -1;
2117 }
2118 }
2119
2120 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002121}
2122
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002123bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002124 /* Extract dynamic section */
2125 ElfW(Word) dynamic_flags = 0;
2126 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002127
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002128 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002129 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002130 if (!relocating_linker) {
2131 INFO("[ linking %s ]", name);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002132 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002133 }
2134
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002135 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002136 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002137 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002138 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002139 return false;
2140 } else {
2141 if (!relocating_linker) {
2142 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002143 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002144 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002145
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002146#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002147 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2148 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002149#endif
2150
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002151 // Extract useful information from dynamic section.
2152 uint32_t needed_count = 0;
2153 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2154 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2155 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2156 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002157 case DT_SONAME:
2158 // TODO: glibc dynamic linker uses this name for
2159 // initial library lookup; consider doing the same here.
2160 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002161
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002162 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002163 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002164 // in case of --hash-style=both, we prefer gnu
2165 break;
2166 }
2167
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002168 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2169 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2170 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2171 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002172 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002173
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002174 case DT_GNU_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002175 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002176 // in case of --hash-style=both, we prefer gnu
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002177 nchain_ = 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002178 }
2179
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002180 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002181 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002182 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2183 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002184
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002185 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
2186 bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002187 // amend chain for symndx = header[1]
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002188 chain_ = bucket_ + nbucket_ - reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002189
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002190 if (!powerof2(gnu_maskwords_)) {
2191 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two", gnu_maskwords_, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002192 return false;
2193 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002194 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002195
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002196 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002197 break;
2198
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002199 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002200 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002201 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002202
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002203 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002204 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002205 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002206
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002207 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002208 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002209 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002210
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002211 case DT_SYMENT:
2212 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002213 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"", static_cast<size_t>(d->d_un.d_val), name);
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002214 return false;
2215 }
2216 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002217
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002218 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002219#if defined(USE_RELA)
2220 if (d->d_un.d_val != DT_RELA) {
2221 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002222 return false;
2223 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002224#else
2225 if (d->d_un.d_val != DT_REL) {
2226 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
2227 return false;
2228 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002229#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002230 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002231
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002232 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002233#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002234 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002235#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002236 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002237#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002238 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002239
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002240 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002241#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002242 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002243#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002244 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002245#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002246 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002247
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002248 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002249#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002250 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002251 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002252#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002253 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2254 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002255
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002256 case DT_DEBUG:
2257 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2258 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002259// FIXME: not working currently for N64
2260// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002261// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002262// read-only, but the DYNAMIC header claims it is writable.
2263#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002264 if ((dynamic_flags & PF_W) != 0) {
2265 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2266 }
2267 break;
Chris Dearman99186652014-02-06 20:36:51 -08002268#endif
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002269#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002270 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002271 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002272 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002273
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002274 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002275 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002276 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002277
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002278 case DT_RELAENT:
2279 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002280 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002281 return false;
2282 }
2283 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002284
2285 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002286 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002287 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002288
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002289 case DT_REL:
2290 DL_ERR("unsupported DT_REL in \"%s\"", name);
2291 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002292
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002293 case DT_RELSZ:
2294 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2295 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002296#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002297 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002298 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002299 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002300
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002301 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002302 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002303 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002304
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002305 case DT_RELENT:
2306 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002307 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002308 return false;
2309 }
2310 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002311
2312 // "Indicates that all RELATIVE relocations have been concatenated together,
2313 // and specifies the RELATIVE relocation count."
2314 //
2315 // TODO: Spec also mentions that this can be used to optimize relocation process;
2316 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002317 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002318 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002319 case DT_RELA:
2320 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2321 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002322#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002323 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002324 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2325 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002326 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002327
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002328 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002329 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2330 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002331 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002332
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002333 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002334 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2335 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002336 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002337
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002338 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002339 init_array_count_ = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002340 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002341
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002342 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002343 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2344 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002345 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002346
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002347 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002348 fini_array_count_ = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002349 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002350
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002351 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002352 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2353 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002354 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002355
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002356 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002357 preinit_array_count_ = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002358 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002359
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002360 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002361#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002362 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2363 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002364#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002365 has_text_relocations = true;
2366 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002367#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002368
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002369 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002370 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002371 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002372
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002373 case DT_NEEDED:
2374 ++needed_count;
2375 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002376
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002377 case DT_FLAGS:
2378 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002379#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002380 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2381 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002382#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002383 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002384#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002385 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002386 if (d->d_un.d_val & DF_SYMBOLIC) {
2387 has_DT_SYMBOLIC = true;
2388 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002389 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002390
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002391 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002392 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002393
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002394 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002395 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2396 }
2397 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002398#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002399 case DT_MIPS_RLD_MAP:
2400 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2401 {
2402 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2403 *dp = &_r_debug;
2404 }
2405 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002406 case DT_MIPS_RLD_MAP2:
2407 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2408 {
2409 r_debug** dp = reinterpret_cast<r_debug**>(reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
2410 *dp = &_r_debug;
2411 }
2412 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002413
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002414 case DT_MIPS_RLD_VERSION:
2415 case DT_MIPS_FLAGS:
2416 case DT_MIPS_BASE_ADDRESS:
2417 case DT_MIPS_UNREFEXTNO:
2418 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002419
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002420 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002421 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002422 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002423
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002424 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002425 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002426 break;
2427
2428 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002429 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002430 break;
2431#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002432 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2433 case DT_BIND_NOW:
2434 break;
2435
2436 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002437 case DT_VERSYM:
2438 case DT_VERDEF:
2439 case DT_VERDEFNUM:
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002440 case DT_VERNEED:
2441 case DT_VERNEEDNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002442 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002443
2444 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002445 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002446 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002447 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2448 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002449 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002450 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002451 }
2452
2453 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002454 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002455
2456 // Sanity checks.
2457 if (relocating_linker && needed_count != 0) {
2458 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2459 return false;
2460 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002461 if (nbucket_ == 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002462 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" (new hash type from the future?)", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002463 return false;
2464 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002465 if (strtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002466 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2467 return false;
2468 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002469 if (symtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002470 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2471 return false;
2472 }
2473 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002474}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002475
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002476bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group, const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002477
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002478 local_group_root_ = local_group.front();
2479 if (local_group_root_ == nullptr) {
2480 local_group_root_ = this;
2481 }
2482
Elliott Hughese4d792a2013-10-28 14:19:05 -07002483#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002484 if (has_text_relocations) {
2485 // Make segments writable to allow text relocations to work properly. We will later call
2486 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2487 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2488 "security hardening. Please fix.", name);
2489 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2490 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2491 name, strerror(errno));
2492 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002493 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002494 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002495#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002496
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002497#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002498 if (rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002499 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002500 if (relocate(rela_, rela_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002501 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002502 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002503 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002504 if (plt_rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002505 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002506 if (relocate(plt_rela_, plt_rela_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002507 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002508 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002509 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002510#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002511 if (rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002512 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002513 if (relocate(rel_, rel_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002514 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002515 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002516 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002517 if (plt_rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002518 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002519 if (relocate(plt_rel_, plt_rel_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002520 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002521 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002522 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002523#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002524
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002525#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08002526 if (!mips_relocate_got(global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002527 return false;
2528 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002529#endif
2530
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002531 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002532
Elliott Hughese4d792a2013-10-28 14:19:05 -07002533#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002534 if (has_text_relocations) {
2535 // All relocations are done, we can protect our segments back to read-only.
2536 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2537 DL_ERR("can't protect segments for \"%s\": %s",
2538 name, strerror(errno));
2539 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002540 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002541 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002542#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002543
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002544 /* We can also turn on GNU RELRO protection */
2545 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2546 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2547 name, strerror(errno));
2548 return false;
2549 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002550
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002551 /* Handle serializing/sharing the RELRO segment */
2552 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2553 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2554 extinfo->relro_fd) < 0) {
2555 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2556 name, strerror(errno));
2557 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002558 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002559 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2560 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2561 extinfo->relro_fd) < 0) {
2562 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2563 name, strerror(errno));
2564 return false;
2565 }
2566 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002567
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002568 notify_gdb_of_load(this);
2569 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002570}
2571
Nick Kralevich468319c2011-11-11 15:53:17 -08002572/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002573 * This function add vdso to internal dso list.
2574 * It helps to stack unwinding through signal handlers.
2575 * Also, it makes bionic more like glibc.
2576 */
Kito Cheng812fd422014-03-25 22:53:56 +08002577static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002578#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002579 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002580 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002581 return;
2582 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002583
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002584 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002585
Elliott Hughes0266ae52014-02-10 17:46:57 -08002586 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2587 si->phnum = ehdr_vdso->e_phnum;
2588 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2589 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002590 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002591
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002592 si->prelink_image();
2593 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002594#endif
2595}
2596
2597/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002598 * This is linker soinfo for GDB. See details below.
2599 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002600#if defined(__LP64__)
2601#define LINKER_PATH "/system/bin/linker64"
2602#else
2603#define LINKER_PATH "/system/bin/linker"
2604#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002605static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002606
2607/* gdb expects the linker to be in the debug shared object list.
2608 * Without this, gdb has trouble locating the linker's ".text"
2609 * and ".plt" sections. Gdb could also potentially use this to
2610 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2611 * Don't use soinfo_alloc(), because the linker shouldn't
2612 * be on the soinfo list.
2613 */
2614static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002615 linker_soinfo_for_gdb.base = linker_base;
2616
2617 /*
2618 * Set the dynamic field in the link map otherwise gdb will complain with
2619 * the following:
2620 * warning: .dynamic section for "/system/bin/linker" is not at the
2621 * expected address (wrong library or version mismatch?)
2622 */
2623 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2624 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2625 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002626 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002627 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2628}
2629
2630/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002631 * This code is called after the linker has linked itself and
2632 * fixed it's own GOT. It is safe to make references to externs
2633 * and other non-local data at this point.
2634 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002635static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002636#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002637 struct timeval t0, t1;
2638 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002639#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002640
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002641 // Initialize environment functions, and get to the ELF aux vectors table.
2642 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002643
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002644 // If this is a setuid/setgid program, close the security hole described in
2645 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2646 if (get_AT_SECURE()) {
2647 nullify_closed_stdio();
2648 }
2649
2650 debuggerd_init();
2651
2652 // Get a few environment variables.
2653 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2654 if (LD_DEBUG != nullptr) {
2655 g_ld_debug_verbosity = atoi(LD_DEBUG);
2656 }
2657
2658 // Normally, these are cleaned by linker_env_init, but the test
2659 // doesn't cost us anything.
2660 const char* ldpath_env = nullptr;
2661 const char* ldpreload_env = nullptr;
2662 if (!get_AT_SECURE()) {
2663 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2664 ldpreload_env = linker_env_get("LD_PRELOAD");
2665 }
2666
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -08002667#if !defined(__LP64__)
2668 if (personality(PER_LINUX32) == -1) {
2669 __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
2670 }
2671#endif
2672
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002673 INFO("[ android linker & debugger ]");
2674
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002675 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002676 if (si == nullptr) {
2677 exit(EXIT_FAILURE);
2678 }
2679
2680 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002681 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002682 link_map* map = &(si->link_map_head);
2683
2684 map->l_addr = 0;
2685 map->l_name = args.argv[0];
2686 map->l_prev = nullptr;
2687 map->l_next = nullptr;
2688
2689 _r_debug.r_map = map;
2690 r_debug_tail = map;
2691
2692 init_linker_info_for_gdb(linker_base);
2693
2694 // Extract information passed from the kernel.
2695 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2696 si->phnum = args.getauxval(AT_PHNUM);
2697 si->entry = args.getauxval(AT_ENTRY);
2698
2699 /* Compute the value of si->base. We can't rely on the fact that
2700 * the first entry is the PHDR because this will not be true
2701 * for certain executables (e.g. some in the NDK unit test suite)
2702 */
2703 si->base = 0;
2704 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2705 si->load_bias = 0;
2706 for (size_t i = 0; i < si->phnum; ++i) {
2707 if (si->phdr[i].p_type == PT_PHDR) {
2708 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2709 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2710 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002711 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002712 }
2713 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002714
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002715 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2716 if (elf_hdr->e_type != ET_DYN) {
2717 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2718 exit(EXIT_FAILURE);
2719 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002720
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002721 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2722 parse_LD_LIBRARY_PATH(ldpath_env);
2723 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002724
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002725 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002726
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002727 si->prelink_image();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002728
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002729 // add somain to global group
2730 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2731
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002732 // Load ld_preloads and dependencies.
2733 StringLinkedList needed_library_name_list;
2734 size_t needed_libraries_count = 0;
2735 size_t ld_preloads_count = 0;
2736 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2737 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2738 ++needed_libraries_count;
2739 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002740
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002741 for_each_dt_needed(si, [&](const char* name) {
2742 needed_library_name_list.push_back(name);
2743 ++needed_libraries_count;
2744 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002745
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002746 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002747
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002748 memset(needed_library_names, 0, sizeof(needed_library_names));
2749 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002750
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002751 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 -07002752 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2753 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002754 } else if (needed_libraries_count == 0) {
2755 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
2756 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2757 exit(EXIT_FAILURE);
2758 }
2759 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002760 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002761
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002762 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002763
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002764 si->call_pre_init_constructors();
Matt Fischer4fd42c12009-12-31 12:09:10 -06002765
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002766 /* After the prelink_image, the si->load_bias is initialized.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002767 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2768 * We need to update this value for so exe here. So Unwind_Backtrace
2769 * for some arch like x86 could work correctly within so exe.
2770 */
2771 map->l_addr = si->load_bias;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002772 si->call_constructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002773
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002774#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002775 gettimeofday(&t1, nullptr);
2776 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2777 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2778 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002779#endif
2780#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002781 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2782 linker_stats.count[kRelocAbsolute],
2783 linker_stats.count[kRelocRelative],
2784 linker_stats.count[kRelocCopy],
2785 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002786#endif
2787#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002788 {
2789 unsigned n;
2790 unsigned i;
2791 unsigned count = 0;
2792 for (n = 0; n < 4096; n++) {
2793 if (bitmask[n]) {
2794 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002795#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002796 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002797#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002798 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002799#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002800 if (x & 1) {
2801 count++;
2802 }
2803 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002804 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002805 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002806 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002807 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2808 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002809#endif
2810
2811#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002812 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002813#endif
2814
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002815 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2816 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002817}
Nick Kralevich468319c2011-11-11 15:53:17 -08002818
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002819/* Compute the load-bias of an existing executable. This shall only
2820 * be used to compute the load bias of an executable or shared library
2821 * that was loaded by the kernel itself.
2822 *
2823 * Input:
2824 * elf -> address of ELF header, assumed to be at the start of the file.
2825 * Return:
2826 * load bias, i.e. add the value of any p_vaddr in the file to get
2827 * the corresponding address in memory.
2828 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002829static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2830 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002831 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002832 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002833
Elliott Hughes0266ae52014-02-10 17:46:57 -08002834 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002835 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002836 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002837 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002838 }
2839 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002840}
2841
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002842extern "C" void _start();
2843
Nick Kralevich468319c2011-11-11 15:53:17 -08002844/*
2845 * This is the entry point for the linker, called from begin.S. This
2846 * method is responsible for fixing the linker's own relocations, and
2847 * then calling __linker_init_post_relocation().
2848 *
2849 * Because this method is called before the linker has fixed it's own
2850 * relocations, any attempt to reference an extern variable, extern
2851 * function, or other GOT reference will generate a segfault.
2852 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002853extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002854 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002855
Elliott Hughes0266ae52014-02-10 17:46:57 -08002856 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002857 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002858 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002859 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002860
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002861 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002862
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002863 // If the linker is not acting as PT_INTERP entry_point is equal to
2864 // _start. Which means that the linker is running as an executable and
2865 // already linked by PT_INTERP.
2866 //
2867 // This happens when user tries to run 'adb shell /system/bin/linker'
2868 // see also https://code.google.com/p/android/issues/detail?id=63174
2869 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2870 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2871 }
2872
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002873 linker_so.base = linker_addr;
2874 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2875 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002876 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002877 linker_so.phdr = phdr;
2878 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002879 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07002880
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002881 // This might not be obvious... The reasons why we pass g_empty_list
2882 // in place of local_group here are (1) we do not really need it, because
2883 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2884 // itself without having to look into local_group and (2) allocators
2885 // are not yet initialized, and therefore we cannot use linked_list.push_*
2886 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002887 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002888 // It would be nice to print an error message, but if the linker
2889 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002890 // call write() (because it involves a GOT reference). We may as
2891 // well try though...
2892 const char* msg = "CANNOT LINK EXECUTABLE: ";
2893 write(2, msg, strlen(msg));
2894 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2895 write(2, "\n", 1);
2896 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002897 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002898
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002899 __libc_init_tls(args);
2900
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002901 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002902 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002903
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002904 // Initialize static variables. Note that in order to
2905 // get correct libdl_info we need to call constructors
2906 // before get_libdl_info().
2907 solist = get_libdl_info();
2908 sonext = get_libdl_info();
2909
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002910 // We have successfully fixed our own relocations. It's safe to run
2911 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002912 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002913 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002914
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002915 protect_data(PROT_READ);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002916
2917 // Return the address that the calling assembly stub should jump to.
2918 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002919}