blob: babefeb3c1fb259fd27da4abec33f913e96b29ea [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>
Elliott Hughes46882792012-08-03 16:49:39 -070039#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070041#include <new>
42
Elliott Hughes46882792012-08-03 16:49:39 -070043// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070044#include "private/bionic_tls.h"
45#include "private/KernelArgumentBlock.h"
46#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070047#include "private/ScopedFd.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070048#include "private/ScopeGuard.h"
49#include "private/UniquePtr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050
51#include "linker.h"
52#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010053#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020054#include "linker_phdr.h"
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070055#include "linker_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
58 *
59 * Do NOT use malloc() and friends or pthread_*() code here.
60 * Don't use printf() either; it's caused mysterious memory
61 * corruption in the past.
62 * The linker runs before we bring up libc and it's easiest
63 * to make sure it does not depend on any complex libc features
64 *
65 * open issues / todo:
66 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068 * - after linking, set as much stuff as possible to READONLY
69 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070070 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071
Dmitriy Ivanov489e4982014-05-19 15:19:52 -070072#if defined(__LP64__)
73#define SEARCH_NAME(x) x
74#else
75// Nvidia drivers are relying on the bug:
76// http://code.google.com/p/android/issues/detail?id=6670
77// so we continue to use base-name lookup for lp32
78static const char* get_base_name(const char* name) {
79 const char* bname = strrchr(name, '/');
80 return bname ? bname + 1 : name;
81}
82#define SEARCH_NAME(x) get_base_name(x)
83#endif
84
Elliott Hughes0266ae52014-02-10 17:46:57 -080085static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
Elliott Hughes1728b232014-05-14 10:02:03 -070087static LinkerAllocator<soinfo> g_soinfo_allocator;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070088static LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020089
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070090static soinfo* solist;
91static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -070092static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
Elliott Hughes1728b232014-05-14 10:02:03 -070094static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070095#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070096 "/vendor/lib64",
97 "/system/lib64",
98#else
Elliott Hughes124fae92012-10-31 14:20:03 -070099 "/vendor/lib",
100 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700101#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700102 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700103};
David Bartleybc3a5c22009-06-02 18:27:28 -0700104
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800105#define LDPATH_BUFSIZE (LDPATH_MAX*64)
106#define LDPATH_MAX 8
107
108#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
109#define LDPRELOAD_MAX 8
110
Elliott Hughes1728b232014-05-14 10:02:03 -0700111static char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
112static const char* g_ld_library_paths[LDPATH_MAX + 1];
Elliott Hughes124fae92012-10-31 14:20:03 -0700113
Elliott Hughes1728b232014-05-14 10:02:03 -0700114static char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
115static const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600116
Elliott Hughes1728b232014-05-14 10:02:03 -0700117static soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600118
Elliott Hughes1728b232014-05-14 10:02:03 -0700119__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700121__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700122
Elliott Hughesbedfe382012-08-14 14:07:59 -0700123enum RelocationKind {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700124 kRelocAbsolute = 0,
125 kRelocRelative,
126 kRelocCopy,
127 kRelocSymbol,
128 kRelocMax
Elliott Hughesbedfe382012-08-14 14:07:59 -0700129};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100130
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700132struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700133 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700134};
135
136static linker_stats_t linker_stats;
137
138static void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700139 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700140}
141#else
142static void count_relocation(RelocationKind) {
143}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144#endif
145
146#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700147static unsigned bitmask[4096];
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100148#if defined(__LP64__)
149#define MARK(offset) \
150 do { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700151 if ((((offset) >> 12) >> 5) < 4096) \
152 bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800153 } while (0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100154#else
Elliott Hughesbedfe382012-08-14 14:07:59 -0700155#define MARK(offset) \
156 do { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700157 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800158 } while (0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100159#endif
Elliott Hughesbedfe382012-08-14 14:07:59 -0700160#else
161#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162#endif
163
Elliott Hughes46882792012-08-03 16:49:39 -0700164// You shouldn't try to call memory-allocating functions in the dynamic linker.
165// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700166#define DISALLOW_ALLOCATION(return_type, name, ...) \
167 return_type name __VA_ARGS__ \
168 { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700169 __libc_fatal("ERROR: " #name " called from the dynamic linker!\n"); \
Dima Zavin2e855792009-05-20 18:28:09 -0700170 }
Kito Cheng812fd422014-03-25 22:53:56 +0800171DISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
172DISALLOW_ALLOCATION(void, free, (void* u __unused));
173DISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
174DISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
Dima Zavin2e855792009-05-20 18:28:09 -0700175
176static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700177
Elliott Hughes650be4e2013-03-05 18:47:58 -0800178char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700179 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700180}
181
Elliott Hughes650be4e2013-03-05 18:47:58 -0800182size_t linker_get_error_buffer_size() {
183 return sizeof(__linker_dl_err_buf);
184}
185
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700186// This function is an empty stub where GDB locates a breakpoint to get notified
187// about linker activity.
Elliott Hughes5419b942012-10-16 15:54:46 -0700188extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189
Elliott Hughes1728b232014-05-14 10:02:03 -0700190static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700191static 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 -0800192static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800194static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700195 // Copy the necessary fields into the debug structure.
196 link_map* map = &(info->link_map_head);
197 map->l_addr = info->load_bias;
Dmitriy Ivanovc9d16582014-10-24 14:46:12 -0700198 map->l_name = info->name;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700199 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700201 // Stick the new library at the end of the list.
202 // gdb tends to care more about libc than it does
203 // about leaf libraries, and ordering it this way
204 // reduces the back-and-forth over the wire.
205 if (r_debug_tail) {
206 r_debug_tail->l_next = map;
207 map->l_prev = r_debug_tail;
208 map->l_next = 0;
209 } else {
210 _r_debug.r_map = map;
211 map->l_prev = 0;
212 map->l_next = 0;
213 }
214 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800215}
216
Elliott Hughesbedfe382012-08-14 14:07:59 -0700217static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700218 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700219
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700220 if (r_debug_tail == map) {
221 r_debug_tail = map->l_prev;
222 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700223
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700224 if (map->l_prev) {
225 map->l_prev->l_next = map->l_next;
226 }
227 if (map->l_next) {
228 map->l_next->l_prev = map->l_prev;
229 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700230}
231
Elliott Hughesbedfe382012-08-14 14:07:59 -0700232static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800233 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700234 // GDB already knows about the main executable
235 return;
236 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700238 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700240 _r_debug.r_state = r_debug::RT_ADD;
241 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800242
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700243 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700245 _r_debug.r_state = r_debug::RT_CONSISTENT;
246 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700247}
248
Elliott Hughesbedfe382012-08-14 14:07:59 -0700249static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800250 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700251 // GDB already knows about the main executable
252 return;
253 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700254
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700255 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700256
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700257 _r_debug.r_state = r_debug::RT_DELETE;
258 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700259
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700260 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700261
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700262 _r_debug.r_state = r_debug::RT_CONSISTENT;
263 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264}
265
Elliott Hughes18a206c2012-10-29 17:37:13 -0700266void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800267 _r_debug.r_state = r_debug::RT_ADD;
268 rtld_db_dlactivity();
269 _r_debug.r_state = r_debug::RT_CONSISTENT;
270 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271}
272
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700273LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
274 return g_soinfo_links_allocator.alloc();
275}
276
277void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
278 g_soinfo_links_allocator.free(entry);
279}
280
281static void protect_data(int protection) {
282 g_soinfo_allocator.protect_all(protection);
283 g_soinfo_links_allocator.protect_all(protection);
284}
285
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700286static 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 +0200287 if (strlen(name) >= SOINFO_NAME_LEN) {
288 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700289 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200290 }
291
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700292 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700293
Magnus Malmbornba98d922012-09-12 13:00:55 +0200294 sonext->next = si;
295 sonext = si;
296
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700297 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200298 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800299}
300
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800301static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700302 if (si == nullptr) {
303 return;
304 }
305
306 if (si->base != 0 && si->size != 0) {
307 munmap(reinterpret_cast<void*>(si->base), si->size);
308 }
309
310 soinfo *prev = nullptr, *trav;
311
312 TRACE("name %s: freeing soinfo @ %p", si->name, si);
313
314 for (trav = solist; trav != nullptr; trav = trav->next) {
315 if (trav == si) {
316 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700317 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700318 prev = trav;
319 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800320
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700321 if (trav == nullptr) {
322 // si was not in solist
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -0800323 DL_ERR("name \"%s\"@%p is not in solist!", si->name, si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700324 return;
325 }
Elliott Hughes46882792012-08-03 16:49:39 -0700326
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700327 // clear links to/from si
328 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700329
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700330 // prev will never be null, because the first entry in solist is
331 // always the static libdl_info.
332 prev->next = si->next;
333 if (si == sonext) {
334 sonext = prev;
335 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700337 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338}
339
Elliott Hughescade4c32012-12-20 14:42:14 -0800340static void parse_path(const char* path, const char* delimiters,
341 const char** array, char* buf, size_t buf_size, size_t max_count) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700342 if (path == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800343 return;
344 }
345
346 size_t len = strlcpy(buf, path, buf_size);
347
348 size_t i = 0;
349 char* buf_p = buf;
350 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
351 if (*array[i] != '\0') {
352 ++i;
353 }
354 }
355
356 // Forget the last path if we had to truncate; this occurs if the 2nd to
357 // last char isn't '\0' (i.e. wasn't originally a delimiter).
358 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700359 array[i - 1] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800360 } else {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700361 array[i] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800362 }
363}
364
365static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700366 parse_path(path, ":", g_ld_library_paths,
367 g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800368}
369
370static void parse_LD_PRELOAD(const char* path) {
371 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes1728b232014-05-14 10:02:03 -0700372 parse_path(path, " :", g_ld_preload_names,
373 g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800374}
375
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700376#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700377
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700378// For a given PC, find the .so that it belongs to.
379// Returns the base address of the .ARM.exidx section
380// for that .so, and the number of 8-byte entries
381// in that section (via *pcount).
382//
383// Intended to be called by libc's __gnu_Unwind_Find_exidx().
384//
385// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800386_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700387 unsigned addr = (unsigned)pc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800388
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700389 for (soinfo* si = solist; si != 0; si = si->next) {
390 if ((addr >= si->base) && (addr < (si->base + si->size))) {
391 *pcount = si->ARM_exidx_count;
392 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700394 }
395 *pcount = 0;
396 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800397}
Elliott Hughes46882792012-08-03 16:49:39 -0700398
Christopher Ferris24053a42013-08-19 17:45:09 -0700399#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700400
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700401// Here, we only have to provide a callback to iterate across all the
402// loaded libraries. gcc_eh does the rest.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800403int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700404 int rv = 0;
405 for (soinfo* si = solist; si != nullptr; si = si->next) {
406 dl_phdr_info dl_info;
407 dl_info.dlpi_addr = si->link_map_head.l_addr;
408 dl_info.dlpi_name = si->link_map_head.l_name;
409 dl_info.dlpi_phdr = si->phdr;
410 dl_info.dlpi_phnum = si->phnum;
411 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
412 if (rv != 0) {
413 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800414 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700415 }
416 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800417}
Elliott Hughes46882792012-08-03 16:49:39 -0700418
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800419ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name) {
420 return is_gnu_hash() ? gnu_lookup(symbol_name) : elf_lookup(symbol_name);
421}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800422
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800423static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
424 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
425 ELF_ST_BIND(s->st_info) == STB_WEAK) {
426 return s->st_shndx != SHN_UNDEF;
427 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
428 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
429 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->name);
430 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800431
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800432 return false;
433}
434
435ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name) {
436 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800437 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800438
439 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800440 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
441 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800442
443 // test against bloom filter
444 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
445 return nullptr;
446 }
447
448 // bloom test says "probably yes"...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800449 uint32_t n = bucket_[hash % nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800450
451 if (n == 0) {
452 return nullptr;
453 }
454
455 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800456 ElfW(Sym)* s = symtab_ + n;
457 if (((chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800458 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
459 is_symbol_global_and_defined(this, s)) {
460 return s;
461 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800462 } while ((chain_[n++] & 1) == 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800463
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800464 return nullptr;
465}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800466
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800467ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name) {
468 uint32_t hash = symbol_name.elf_hash();
469
470 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800471 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800472
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800473 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
474 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800475 if (strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && is_symbol_global_and_defined(this, s)) {
476 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
477 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
478 static_cast<size_t>(s->st_size));
479 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800480 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800481 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800482
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700483 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800484 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700485
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700486 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800487}
488
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700489soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700490 memset(this, 0, sizeof(*this));
491
492 strlcpy(this->name, name, sizeof(this->name));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800493 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800494 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700495
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700496 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800497 this->st_dev_ = file_stat->st_dev;
498 this->st_ino_ = file_stat->st_ino;
499 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700500 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700501
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800502 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700503}
504
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800505
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800506uint32_t SymbolName::elf_hash() {
507 if (!has_elf_hash_) {
508 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
509 uint32_t h = 0, g;
510
511 while (*name) {
512 h = (h << 4) + *name++;
513 g = h & 0xf0000000;
514 h ^= g;
515 h ^= g >> 24;
516 }
517
518 elf_hash_ = h;
519 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700520 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800521
522 return elf_hash_;
523}
524
525uint32_t SymbolName::gnu_hash() {
526 if (!has_gnu_hash_) {
527 uint32_t h = 5381;
528 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
529 while (*name != 0) {
530 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
531 }
532
533 gnu_hash_ = h;
534 has_gnu_hash_ = true;
535 }
536
537 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800538}
539
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700540static ElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
541 const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800542 SymbolName symbol_name(name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700543 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700544
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700545 /* "This element's presence in a shared object library alters the dynamic linker's
546 * symbol resolution algorithm for references within the library. Instead of starting
547 * a symbol search with the executable file, the dynamic linker starts from the shared
548 * object itself. If the shared object fails to supply the referenced symbol, the
549 * dynamic linker then searches the executable file and other shared objects as usual."
550 *
551 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
552 *
553 * Note that this is unlikely since static linker avoids generating
554 * relocations for -Bsymbolic linked dynamic executables.
555 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700556 if (si_from->has_DT_SYMBOLIC) {
557 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->name, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800558 s = si_from->find_symbol_by_name(symbol_name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700559 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700560 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700561 }
562 }
563
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700564 // 1. Look for it in global_group
565 if (s == nullptr) {
566 global_group.visit([&](soinfo* global_si) {
567 DEBUG("%s: looking up %s in %s (from global group)", si_from->name, name, global_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800568 s = global_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700569 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700570 *si_found_in = global_si;
571 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700572 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700573
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700574 return true;
575 });
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700576 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700577
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700578 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700579 if (s == nullptr) {
580 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700581 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700582 // we already did this - skip
583 return true;
584 }
585
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700586 DEBUG("%s: looking up %s in %s (from local group)", si_from->name, name, local_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800587 s = local_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700588 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700589 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700590 return false;
591 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700592
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700593 return true;
594 });
595 }
596
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700597 if (s != nullptr) {
598 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
599 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700600 si_from->name, name, reinterpret_cast<void*>(s->st_value),
601 (*si_found_in)->name, reinterpret_cast<void*>((*si_found_in)->base),
602 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700603 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700604
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700605 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700606}
607
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700608// Each size has it's own allocator.
609template<size_t size>
610class SizeBasedAllocator {
611 public:
612 static void* alloc() {
613 return allocator_.alloc();
614 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700615
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700616 static void free(void* ptr) {
617 allocator_.free(ptr);
618 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700619
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700620 private:
621 static LinkerBlockAllocator allocator_;
622};
623
624template<size_t size>
625LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
626
627template<typename T>
628class TypeBasedAllocator {
629 public:
630 static T* alloc() {
631 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
632 }
633
634 static void free(T* ptr) {
635 SizeBasedAllocator<sizeof(T)>::free(ptr);
636 }
637};
638
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700639class LoadTask {
640 public:
641 struct deleter_t {
642 void operator()(LoadTask* t) {
643 TypeBasedAllocator<LoadTask>::free(t);
644 }
645 };
646
647 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
648
649 static deleter_t deleter;
650
651 static LoadTask* create(const char* name, soinfo* needed_by) {
652 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
653 return new (ptr) LoadTask(name, needed_by);
654 }
655
656 const char* get_name() const {
657 return name_;
658 }
659
660 soinfo* get_needed_by() const {
661 return needed_by_;
662 }
663 private:
664 LoadTask(const char* name, soinfo* needed_by)
665 : name_(name), needed_by_(needed_by) {}
666
667 const char* name_;
668 soinfo* needed_by_;
669
670 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
671};
672
Ningsheng Jiane93be992014-09-16 15:22:10 +0800673LoadTask::deleter_t LoadTask::deleter;
674
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700675template <typename T>
676using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
677
678typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700679typedef linked_list_t<const char> StringLinkedList;
680typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700681
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700682
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700683// This function walks down the tree of soinfo dependencies
684// in breadth-first order and
685// * calls action(soinfo* si) for each node, and
686// * terminates walk if action returns false.
687//
688// walk_dependencies_tree returns false if walk was terminated
689// by the action and true otherwise.
690template<typename F>
691static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700692 SoinfoLinkedList visit_list;
693 SoinfoLinkedList visited;
694
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700695 for (size_t i = 0; i < root_soinfos_size; ++i) {
696 visit_list.push_back(root_soinfos[i]);
697 }
698
699 soinfo* si;
700 while ((si = visit_list.pop_front()) != nullptr) {
701 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700702 continue;
703 }
704
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700705 if (!action(si)) {
706 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700707 }
708
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700709 visited.push_back(si);
710
711 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700712 visit_list.push_back(child);
713 });
714 }
715
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700716 return true;
717}
718
719
720// This is used by dlsym(3). It performs symbol lookup only within the
721// specified soinfo object and its dependencies in breadth first order.
722ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
723 ElfW(Sym)* result = nullptr;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800724 SymbolName symbol_name(name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700725
726
727 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800728 result = current_soinfo->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700729 if (result != nullptr) {
730 *found = current_soinfo;
731 return false;
732 }
733
734 return true;
735 });
736
737 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800738}
739
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800740/* This is used by dlsym(3) to performs a global symbol lookup. If the
741 start value is null (for RTLD_DEFAULT), the search starts at the
742 beginning of the global solist. Otherwise the search starts at the
743 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700744 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700745ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800746 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800747
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700748 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800749 start = solist;
750 }
751
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700752 ElfW(Sym)* s = nullptr;
753 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700754 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
755 continue;
756 }
757
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800758 s = si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700759 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800760 *found = si;
761 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600762 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800763 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600764
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700765 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700766 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
767 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800768 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800769
Elliott Hughescade4c32012-12-20 14:42:14 -0800770 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800771}
772
Kito Chengfa8c05d2013-03-12 14:58:06 +0800773soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800774 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700775 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800776 if (address >= si->base && address - si->base < si->size) {
777 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600778 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800779 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700780 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600781}
782
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800783ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
784 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
785}
786
787static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
788 return sym->st_shndx != SHN_UNDEF &&
789 soaddr >= sym->st_value &&
790 soaddr < sym->st_value + sym->st_size;
791}
792
793ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
794 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - base;
795
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800796 for (size_t i = 0; i < nbucket_; ++i) {
797 uint32_t n = bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800798
799 if (n == 0) {
800 continue;
801 }
802
803 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800804 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800805 if (symbol_matches_soaddr(sym, soaddr)) {
806 return sym;
807 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800808 } while ((chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800809 }
810
811 return nullptr;
812}
813
814ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
815 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600816
Kito Chengfa8c05d2013-03-12 14:58:06 +0800817 // Search the library's symbol table for any defined symbol which
818 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800819 for (size_t i = 0; i < nchain_; ++i) {
820 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800821 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800822 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600823 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800824 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600825
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700826 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600827}
828
Elliott Hughes124fae92012-10-31 14:20:03 -0700829static int open_library_on_path(const char* name, const char* const paths[]) {
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +0000830 char buf[512];
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700831 for (size_t i = 0; paths[i] != nullptr; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800832 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700833 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700834 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700835 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800836 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700837 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
838 if (fd != -1) {
839 return fd;
840 }
841 }
842 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800843}
844
Elliott Hughes124fae92012-10-31 14:20:03 -0700845static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700846 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800847
Elliott Hughes124fae92012-10-31 14:20:03 -0700848 // 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 -0700849 if (strchr(name, '/') != nullptr) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700850 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
851 if (fd != -1) {
852 return fd;
853 }
854 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700855#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700856 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700857#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700858 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800859
Elliott Hughes124fae92012-10-31 14:20:03 -0700860 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Elliott Hughes1728b232014-05-14 10:02:03 -0700861 int fd = open_library_on_path(name, g_ld_library_paths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700862 if (fd == -1) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700863 fd = open_library_on_path(name, kDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700864 }
865 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800866}
867
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700868template<typename F>
869static void for_each_dt_needed(const soinfo* si, F action) {
870 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
871 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700872 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700873 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700874 }
875}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800876
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700877static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700878 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700879 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700880 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700881
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700882 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
883 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700884 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
885 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700886 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700887 } else {
888 // Open the file.
889 fd = open_library(name);
890 if (fd == -1) {
891 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700892 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700893 }
894
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700895 file_guard.reset(fd);
896 }
897
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700898 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700899 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700900 return nullptr;
901 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800902 if (file_offset < 0) {
903 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
904 return nullptr;
905 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700906
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700907 struct stat file_stat;
908 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700909 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700910 return nullptr;
911 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800912 if (file_offset >= file_stat.st_size) {
913 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64, name, file_offset, file_stat.st_size);
914 return nullptr;
915 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700916
917 // Check for symlink and other situations where
918 // file can have different names.
919 for (soinfo* si = solist; si != nullptr; si = si->next) {
920 if (si->get_st_dev() != 0 &&
921 si->get_st_ino() != 0 &&
922 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700923 si->get_st_ino() == file_stat.st_ino &&
924 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700925 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
926 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700927 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700928 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700929
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700930 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700931 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700932 return nullptr;
933 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700934
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700935 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700936 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700937 if (!elf_reader.Load(extinfo)) {
938 return nullptr;
939 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800940
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700941 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700942 if (si == nullptr) {
943 return nullptr;
944 }
945 si->base = elf_reader.load_start();
946 si->size = elf_reader.load_size();
947 si->load_bias = elf_reader.load_bias();
948 si->phnum = elf_reader.phdr_count();
949 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700950
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800951 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700952 soinfo_free(si);
953 return nullptr;
954 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700955
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700956 for_each_dt_needed(si, [&] (const char* name) {
957 load_tasks.push_back(LoadTask::create(name, si));
958 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700959
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700960 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800961}
962
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700963static soinfo *find_loaded_library_by_name(const char* name) {
964 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700965 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700966 if (!strcmp(search_name, si->name)) {
967 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200968 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700969 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700970 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200971}
972
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700973static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200974
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700975 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700976
977 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700978 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700979 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700980 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700981 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700982 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800983
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700984 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800985}
986
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700987static void soinfo_unload(soinfo* si);
988
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700989// TODO: this is slightly unusual way to construct
990// the global group for relocation. Not every RTLD_GLOBAL
991// library is included in this group for backwards-compatibility
992// reasons.
993//
994// This group consists of the main executable, LD_PRELOADs
995// and libraries with the DF_1_GLOBAL flag set.
996static soinfo::soinfo_list_t make_global_group() {
997 soinfo::soinfo_list_t global_group;
998 for (soinfo* si = somain; si != nullptr; si = si->next) {
999 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1000 global_group.push_back(si);
1001 }
1002 }
1003
1004 return global_group;
1005}
1006
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001007static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
1008 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001009 // Step 0: prepare.
1010 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001011 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001012 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001013 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001014 }
1015
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001016 // Construct global_group.
1017 soinfo::soinfo_list_t global_group = make_global_group();
1018
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001019 // If soinfos array is null allocate one on stack.
1020 // The array is needed in case of failure; for example
1021 // when library_names[] = {libone.so, libtwo.so} and libone.so
1022 // is loaded correctly but libtwo.so failed for some reason.
1023 // In this case libone.so should be unloaded on return.
1024 // See also implementation of failure_guard below.
1025
1026 if (soinfos == nullptr) {
1027 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1028 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1029 memset(soinfos, 0, soinfos_size);
1030 }
1031
1032 // list of libraries to link - see step 2.
1033 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001034
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001035 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001036 // Housekeeping
1037 load_tasks.for_each([] (LoadTask* t) {
1038 LoadTask::deleter(t);
1039 });
1040
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001041 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001042 soinfo_unload(soinfos[i]);
1043 }
1044 });
1045
1046 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
1047 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 -07001048 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001049 if (si == nullptr) {
1050 return false;
1051 }
1052
1053 soinfo* needed_by = task->get_needed_by();
1054
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001055 if (needed_by != nullptr) {
1056 needed_by->add_child(si);
1057 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001058
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001059 if (si->is_linked()) {
1060 si->increment_ref_count();
1061 }
1062
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001063 // When ld_preloads is not null, the first
1064 // ld_preloads_count libs are in fact ld_preloads.
1065 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001066 // Add LD_PRELOADed libraries to the global group for future runs.
1067 // There is no need to explicitly add them to the global group
1068 // for this run because they are going to appear in the local
1069 // group in the correct order.
1070 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001071 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001072 }
1073
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001074 if (soinfos_count < library_names_count) {
1075 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001076 }
1077 }
1078
1079 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001080 soinfo::soinfo_list_t local_group;
1081 walk_dependencies_tree(
1082 start_with == nullptr ? soinfos : &start_with,
1083 start_with == nullptr ? soinfos_count : 1,
1084 [&] (soinfo* si) {
1085 local_group.push_back(si);
1086 return true;
1087 });
1088
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001089 // We need to increment ref_count in case
1090 // the root of the local group was not linked.
1091 bool was_local_group_root_linked = local_group.front()->is_linked();
1092
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001093 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001094 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001095 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001096 return false;
1097 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001098 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001099 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001100
1101 return true;
1102 });
1103
1104 if (linked) {
1105 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001106 }
1107
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001108 if (!was_local_group_root_linked) {
1109 local_group.front()->increment_ref_count();
1110 }
1111
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001112 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001113}
1114
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001115static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001116 soinfo* si;
1117
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001118 if (name == nullptr) {
1119 si = somain;
1120 } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001121 return nullptr;
1122 }
1123
Elliott Hughesd23736e2012-11-01 15:16:56 -07001124 return si;
1125}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001126
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001127static void soinfo_unload(soinfo* root) {
1128 // Note that the library can be loaded but not linked;
1129 // in which case there is no root but we still need
1130 // to walk the tree and unload soinfos involved.
1131 //
1132 // This happens on unsuccessful dlopen, when one of
1133 // the DT_NEEDED libraries could not be linked/found.
1134 if (root->is_linked()) {
1135 root = root->get_local_group_root();
1136 }
1137
1138 if (!root->can_unload()) {
1139 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->name);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001140 return;
1141 }
1142
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001143 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001144
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001145 if (ref_count == 0) {
1146 soinfo::soinfo_list_t local_unload_list;
1147 soinfo::soinfo_list_t external_unload_list;
1148 soinfo::soinfo_list_t depth_first_list;
1149 depth_first_list.push_back(root);
1150 soinfo* si = nullptr;
1151
1152 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001153 if (local_unload_list.contains(si)) {
1154 continue;
1155 }
1156
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001157 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001158
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001159 if (si->has_min_version(0)) {
1160 soinfo* child = nullptr;
1161 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001162 TRACE("%s@%p needs to unload %s@%p", si->name, si, child->name, child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001163 if (local_unload_list.contains(child)) {
1164 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001165 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001166 external_unload_list.push_back(child);
1167 } else {
1168 depth_first_list.push_front(child);
1169 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001170 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001171 } else {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001172#ifdef __LP64__
1173 __libc_fatal("soinfo for \"%s\"@%p has no version", si->name, si);
1174#else
1175 PRINT("warning: soinfo for \"%s\"@%p has no version", si->name, si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001176 for_each_dt_needed(si, [&] (const char* library_name) {
1177 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1178 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1179 if (needed != nullptr) {
1180 // Not found: for example if symlink was deleted between dlopen and dlclose
1181 // Since we cannot really handle errors at this point - print and continue.
1182 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
1183 return;
1184 } else if (local_unload_list.contains(needed)) {
1185 // already visited
1186 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001187 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001188 // external group
1189 external_unload_list.push_back(needed);
1190 } else {
1191 // local group
1192 depth_first_list.push_front(needed);
1193 }
1194 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001195#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001196 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001197 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001198
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001199 local_unload_list.for_each([](soinfo* si) {
1200 si->call_destructors();
1201 });
1202
1203 while ((si = local_unload_list.pop_front()) != nullptr) {
1204 notify_gdb_of_unload(si);
1205 soinfo_free(si);
1206 }
1207
1208 while ((si = external_unload_list.pop_front()) != nullptr) {
1209 soinfo_unload(si);
1210 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001211 } else {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001212 TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->name, ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001213 }
1214}
1215
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001216void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001217 // Use basic string manipulation calls to avoid snprintf.
1218 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1219 // When debug malloc is enabled, this call returns 0. This in turn causes
1220 // snprintf to do nothing, which causes libraries to fail to load.
1221 // See b/17302493 for further details.
1222 // Once the above bug is fixed, this code can be modified to use
1223 // snprintf again.
1224 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1225 if (buffer_size < required_len) {
1226 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1227 buffer_size, required_len);
1228 }
1229 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1230 *end = ':';
1231 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001232}
1233
Elliott Hughescade4c32012-12-20 14:42:14 -08001234void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
1235 if (!get_AT_SECURE()) {
1236 parse_LD_LIBRARY_PATH(ld_library_path);
1237 }
1238}
1239
Elliott Hughes1a586292014-06-03 16:23:08 -07001240soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001241 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001242 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001243 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001244 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001245 if (extinfo != nullptr) {
1246 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1247 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1248 return nullptr;
1249 }
1250 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001251 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1252 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 -07001253 return nullptr;
1254 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001255 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001256 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001257 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001258 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001259 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001260 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001261 protect_data(PROT_READ);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001262 return si;
1263}
1264
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001265void do_dlclose(soinfo* si) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001266 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001267 soinfo_unload(si);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001268 protect_data(PROT_READ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001269}
1270
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001271static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1272 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1273 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1274 ElfW(Addr) ifunc_addr = ifunc_resolver();
1275 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 -07001276
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001277 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001278}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001279
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001280#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001281int 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 -07001282 for (size_t idx = 0; idx < count; ++idx, ++rela) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001283 unsigned type = ELFW(R_TYPE)(rela->r_info);
1284 unsigned sym = ELFW(R_SYM)(rela->r_info);
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001285 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001286 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001287 const char* sym_name = nullptr;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001288
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001289 DEBUG("Processing '%s' relocation at index %zd", name, idx);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001290 if (type == 0) { // R_*_NONE
1291 continue;
1292 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001293
1294 ElfW(Sym)* s = nullptr;
1295 soinfo* lsi = nullptr;
1296
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001297 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001298 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001299 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001300 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001301 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001302 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001303 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001304 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001305 return -1;
1306 }
1307
1308 /* IHI0044C AAELF 4.5.1.1:
1309
1310 Libraries are not searched to resolve weak references.
1311 It is not an error for a weak reference to remain unsatisfied.
1312
1313 During linking, the value of an undefined weak reference is:
1314 - Zero if the relocation type is absolute
1315 - The address of the place if the relocation is pc-relative
1316 - The address of nominal base address if the relocation
1317 type is base-relative.
1318 */
1319
1320 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001321#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001322 case R_AARCH64_JUMP_SLOT:
1323 case R_AARCH64_GLOB_DAT:
1324 case R_AARCH64_ABS64:
1325 case R_AARCH64_ABS32:
1326 case R_AARCH64_ABS16:
1327 case R_AARCH64_RELATIVE:
1328 case R_AARCH64_IRELATIVE:
1329 /*
1330 * The sym_addr was initialized to be zero above, or the relocation
1331 * code below does not care about value of sym_addr.
1332 * No need to do anything.
1333 */
1334 break;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001335#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001336 case R_X86_64_JUMP_SLOT:
1337 case R_X86_64_GLOB_DAT:
1338 case R_X86_64_32:
1339 case R_X86_64_64:
1340 case R_X86_64_RELATIVE:
1341 case R_X86_64_IRELATIVE:
1342 // No need to do anything.
1343 break;
1344 case R_X86_64_PC32:
1345 sym_addr = reloc;
1346 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001347#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001348 default:
1349 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
1350 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001351 }
1352 } else {
1353 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001354 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001355 }
1356 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001357 }
1358
1359 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001360#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001361 case R_AARCH64_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001362 count_relocation(kRelocAbsolute);
1363 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001364 TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
1365 reloc, (sym_addr + rela->r_addend), sym_name);
1366 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001367 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001368 case R_AARCH64_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001369 count_relocation(kRelocAbsolute);
1370 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001371 TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
1372 reloc, (sym_addr + rela->r_addend), sym_name);
1373 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001374 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001375 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001376 count_relocation(kRelocAbsolute);
1377 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001378 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
1379 reloc, (sym_addr + rela->r_addend), sym_name);
1380 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001381 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001382 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001383 count_relocation(kRelocAbsolute);
1384 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001385 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
1386 reloc, (sym_addr + rela->r_addend), sym_name);
1387 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1388 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001389 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001390 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001391 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1392 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1393 static_cast<ElfW(Addr)>(INT32_MIN),
1394 static_cast<ElfW(Addr)>(UINT32_MAX));
1395 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001396 }
1397 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001398 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001399 count_relocation(kRelocAbsolute);
1400 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001401 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
1402 reloc, (sym_addr + rela->r_addend), sym_name);
1403 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1404 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001405 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001406 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001407 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1408 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1409 static_cast<ElfW(Addr)>(INT16_MIN),
1410 static_cast<ElfW(Addr)>(UINT16_MAX));
1411 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001412 }
1413 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001414 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001415 count_relocation(kRelocRelative);
1416 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001417 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
1418 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1419 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001420 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001421 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001422 count_relocation(kRelocRelative);
1423 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001424 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
1425 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1426 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1427 ((*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 -07001428 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001429 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001430 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1431 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1432 static_cast<ElfW(Addr)>(INT32_MIN),
1433 static_cast<ElfW(Addr)>(UINT32_MAX));
1434 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001435 }
1436 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001437 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001438 count_relocation(kRelocRelative);
1439 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001440 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
1441 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1442 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1443 ((*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 -07001444 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001445 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001446 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1447 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1448 static_cast<ElfW(Addr)>(INT16_MIN),
1449 static_cast<ElfW(Addr)>(UINT16_MAX));
1450 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001451 }
1452 break;
1453
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001454 case R_AARCH64_RELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001455 count_relocation(kRelocRelative);
1456 MARK(rela->r_offset);
1457 if (sym) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001458 DL_ERR("odd RELATIVE form...");
1459 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001460 }
Elliott Hughes0266ae52014-02-10 17:46:57 -08001461 TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001462 reloc, (base + rela->r_addend));
1463 *reinterpret_cast<ElfW(Addr)*>(reloc) = (base + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001464 break;
1465
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001466 case R_AARCH64_IRELATIVE:
1467 count_relocation(kRelocRelative);
1468 MARK(rela->r_offset);
1469 TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
1470 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
1471 break;
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001472
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001473 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001474 /*
1475 * ET_EXEC is not supported so this should not happen.
1476 *
1477 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1478 *
1479 * Section 4.7.1.10 "Dynamic relocations"
1480 * R_AARCH64_COPY may only appear in executable objects where e_type is
1481 * set to ET_EXEC.
1482 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001483 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Nick Kralevich76e289c2014-07-03 12:04:31 -07001484 return -1;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001485 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001486 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
1487 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001488 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001489 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001490 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
1491 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001492 break;
1493#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001494 case R_X86_64_JUMP_SLOT:
1495 count_relocation(kRelocAbsolute);
1496 MARK(rela->r_offset);
1497 TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1498 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1499 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1500 break;
1501 case R_X86_64_GLOB_DAT:
1502 count_relocation(kRelocAbsolute);
1503 MARK(rela->r_offset);
1504 TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1505 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1506 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1507 break;
1508 case R_X86_64_RELATIVE:
1509 count_relocation(kRelocRelative);
1510 MARK(rela->r_offset);
1511 if (sym) {
1512 DL_ERR("odd RELATIVE form...");
1513 return -1;
1514 }
1515 TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1516 static_cast<size_t>(base));
1517 *reinterpret_cast<ElfW(Addr)*>(reloc) = base + rela->r_addend;
1518 break;
1519 case R_X86_64_IRELATIVE:
1520 count_relocation(kRelocRelative);
1521 MARK(rela->r_offset);
1522 TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
1523 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
1524 break;
1525 case R_X86_64_32:
1526 count_relocation(kRelocRelative);
1527 MARK(rela->r_offset);
1528 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1529 static_cast<size_t>(sym_addr), sym_name);
1530 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1531 break;
1532 case R_X86_64_64:
1533 count_relocation(kRelocRelative);
1534 MARK(rela->r_offset);
1535 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1536 static_cast<size_t>(sym_addr), sym_name);
1537 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1538 break;
1539 case R_X86_64_PC32:
1540 count_relocation(kRelocRelative);
1541 MARK(rela->r_offset);
1542 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1543 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1544 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
1545 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
1546 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001547#endif
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001548
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001549 default:
1550 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
1551 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001552 }
1553 }
1554 return 0;
1555}
Chris Dearman99186652014-02-06 20:36:51 -08001556
1557#else // REL, not RELA.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001558int 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 -07001559 for (size_t idx = 0; idx < count; ++idx, ++rel) {
1560 unsigned type = ELFW(R_TYPE)(rel->r_info);
1561 // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
1562 unsigned sym = ELFW(R_SYM)(rel->r_info);
1563 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
1564 ElfW(Addr) sym_addr = 0;
1565 const char* sym_name = nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001566
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001567 DEBUG("Processing '%s' relocation at index %zd", name, idx);
1568 if (type == 0) { // R_*_NONE
1569 continue;
1570 }
1571
1572 ElfW(Sym)* s = nullptr;
1573 soinfo* lsi = nullptr;
1574
1575 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001576 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001577 s = soinfo_do_lookup(this, sym_name, &lsi, global_group, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001578 if (s == nullptr) {
1579 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001580 s = &symtab_[sym];
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001581 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1582 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
1583 return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001584 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001585
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001586 /* IHI0044C AAELF 4.5.1.1:
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001587
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001588 Libraries are not searched to resolve weak references.
1589 It is not an error for a weak reference to remain
1590 unsatisfied.
Doug Kwane8238072009-10-26 12:05:23 -07001591
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001592 During linking, the value of an undefined weak reference is:
1593 - Zero if the relocation type is absolute
1594 - The address of the place if the relocation is pc-relative
1595 - The address of nominal base address if the relocation
1596 type is base-relative.
1597 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001598
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001599 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001600#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001601 case R_ARM_JUMP_SLOT:
1602 case R_ARM_GLOB_DAT:
1603 case R_ARM_ABS32:
1604 case R_ARM_RELATIVE: /* Don't care. */
1605 // sym_addr was initialized to be zero above or relocation
1606 // code below does not care about value of sym_addr.
1607 // No need to do anything.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001608 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001609#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001610 case R_386_JMP_SLOT:
1611 case R_386_GLOB_DAT:
1612 case R_386_32:
1613 case R_386_RELATIVE: /* Don't care. */
1614 case R_386_IRELATIVE:
1615 // sym_addr was initialized to be zero above or relocation
1616 // code below does not care about value of sym_addr.
1617 // No need to do anything.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001618 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001619 case R_386_PC32:
1620 sym_addr = reloc;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001621 break;
1622#endif
1623
1624#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001625 case R_ARM_COPY:
1626 // Fall through. Can't really copy if weak symbol is not found at run-time.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001627#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001628 default:
1629 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001630 return -1;
1631 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001632 } else {
1633 // We got a definition.
1634 sym_addr = lsi->resolve_symbol_address(s);
1635 }
1636 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001637 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001638
1639 switch (type) {
1640#if defined(__arm__)
1641 case R_ARM_JUMP_SLOT:
1642 count_relocation(kRelocAbsolute);
1643 MARK(rel->r_offset);
1644 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1645 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1646 break;
1647 case R_ARM_GLOB_DAT:
1648 count_relocation(kRelocAbsolute);
1649 MARK(rel->r_offset);
1650 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1651 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1652 break;
1653 case R_ARM_ABS32:
1654 count_relocation(kRelocAbsolute);
1655 MARK(rel->r_offset);
1656 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1657 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1658 break;
1659 case R_ARM_REL32:
1660 count_relocation(kRelocRelative);
1661 MARK(rel->r_offset);
1662 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1663 reloc, sym_addr, rel->r_offset, sym_name);
1664 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1665 break;
1666 case R_ARM_COPY:
1667 /*
1668 * ET_EXEC is not supported so this should not happen.
1669 *
1670 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1671 *
1672 * Section 4.7.1.10 "Dynamic relocations"
1673 * R_ARM_COPY may only appear in executable objects where e_type is
1674 * set to ET_EXEC.
1675 */
1676 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
1677 return -1;
1678#elif defined(__i386__)
1679 case R_386_JMP_SLOT:
1680 count_relocation(kRelocAbsolute);
1681 MARK(rel->r_offset);
1682 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1683 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1684 break;
1685 case R_386_GLOB_DAT:
1686 count_relocation(kRelocAbsolute);
1687 MARK(rel->r_offset);
1688 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1689 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1690 break;
1691 case R_386_32:
1692 count_relocation(kRelocRelative);
1693 MARK(rel->r_offset);
1694 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1695 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1696 break;
1697 case R_386_PC32:
1698 count_relocation(kRelocRelative);
1699 MARK(rel->r_offset);
1700 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1701 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1702 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1703 break;
1704#elif defined(__mips__)
1705 case R_MIPS_REL32:
1706#if defined(__LP64__)
1707 // MIPS Elf64_Rel entries contain compound relocations
1708 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
1709 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
1710 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
1711 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
1712 type, (unsigned)ELF64_R_TYPE2(rel->r_info),
1713 (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
1714 return -1;
1715 }
1716#endif
1717 count_relocation(kRelocAbsolute);
1718 MARK(rel->r_offset);
1719 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
1720 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
1721 if (s) {
1722 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1723 } else {
1724 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1725 }
1726 break;
1727#endif
1728
1729#if defined(__arm__)
1730 case R_ARM_RELATIVE:
1731#elif defined(__i386__)
1732 case R_386_RELATIVE:
1733#endif
1734 count_relocation(kRelocRelative);
1735 MARK(rel->r_offset);
1736 if (sym) {
1737 DL_ERR("odd RELATIVE form...");
1738 return -1;
1739 }
1740 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1741 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1742 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1743 break;
1744#if defined(__i386__)
1745 case R_386_IRELATIVE:
1746 count_relocation(kRelocRelative);
1747 MARK(rel->r_offset);
1748 TRACE_TYPE(RELO, "RELO IRELATIVE %p <- %p", reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1749 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + *reinterpret_cast<ElfW(Addr)*>(reloc));
1750 break;
1751#endif
1752
1753 default:
1754 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
1755 return -1;
1756 }
1757 }
1758 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001759}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001760#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001761
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001762#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001763bool soinfo::mips_relocate_got(const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001764 ElfW(Addr)** got = plt_got_;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001765 if (got == nullptr) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001766 return true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001767 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001768
1769 // got[0] is the address of the lazy resolver function.
1770 // got[1] may be used for a GNU extension.
1771 // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
1772 // FIXME: maybe this should be in a separate routine?
Dmitriy Ivanov20463e32014-12-02 13:27:40 -08001773 if ((flags_ & FLAG_LINKER) == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001774 size_t g = 0;
1775 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
1776 if (reinterpret_cast<intptr_t>(got[g]) < 0) {
1777 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
1778 }
1779 // Relocate the local GOT entries.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001780 for (; g < mips_local_gotno_; g++) {
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001781 got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + load_bias);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001782 }
1783 }
1784
1785 // Now for the global GOT entries...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001786 ElfW(Sym)* sym = symtab_ + mips_gotsym_;
1787 got = plt_got_ + mips_local_gotno_;
1788 for (size_t g = mips_gotsym_; g < mips_symtabno_; g++, sym++, got++) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001789 // This is an undefined reference... try to locate it.
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001790 const char* sym_name = get_string(sym->st_name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001791 soinfo* lsi = nullptr;
Dmitriy Ivanov88940912014-11-12 18:20:39 -08001792 ElfW(Sym)* s = soinfo_do_lookup(this, sym_name, &lsi, global_group, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001793 if (s == nullptr) {
1794 // We only allow an undefined symbol if this is a weak reference.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001795 s = &symtab_[g];
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001796 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1797 DL_ERR("cannot locate \"%s\"...", sym_name);
1798 return false;
1799 }
1800 *got = 0;
1801 } else {
1802 // FIXME: is this sufficient?
1803 // For reference see NetBSD link loader
1804 // 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
1805 *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s));
1806 }
1807 }
1808 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001809}
1810#endif
1811
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001812void soinfo::call_array(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001813 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001814 return;
1815 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001816
Elliott Hughesc6200592013-09-30 18:43:46 -07001817 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001818
1819 int begin = reverse ? (count - 1) : 0;
1820 int end = reverse ? -1 : count;
1821 int step = reverse ? -1 : 1;
1822
1823 for (int i = begin; i != end; i += step) {
1824 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001825 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001826 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001827
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001828 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001829}
1830
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001831void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001832 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001833 return;
1834 }
1835
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001836 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001837 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001838 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001839
1840 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1841 // are still writable. This happens with our debug malloc (see http://b/7941716).
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001842 protect_data(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001843}
1844
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001845void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001846 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1847 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001848 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001849}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001850
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001851void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001852 if (constructors_called) {
1853 return;
1854 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001855
Elliott Hughesd23736e2012-11-01 15:16:56 -07001856 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1857 // protect against recursive constructor calls. One simple example of constructor recursion
1858 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1859 // 1. The program depends on libc, so libc's constructor is called here.
1860 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1861 // 3. dlopen() calls the constructors on the newly created
1862 // soinfo for libc_malloc_debug_leak.so.
1863 // 4. The debug .so depends on libc, so CallConstructors is
1864 // called again with the libc soinfo. If it doesn't trigger the early-
1865 // out above, the libc constructor will be called again (recursively!).
1866 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001867
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001868 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001869 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001870 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001871 name, preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001872 }
1873
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001874 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001875 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001876 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001877
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001878 TRACE("\"%s\": calling constructors", name);
1879
1880 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001881 call_function("DT_INIT", init_func_);
1882 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001883}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001884
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001885void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001886 if (!constructors_called) {
1887 return;
1888 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001889 TRACE("\"%s\": calling destructors", name);
1890
1891 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001892 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001893
1894 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001895 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001896
1897 // This is needed on second call to dlopen
1898 // after library has been unloaded with RTLD_NODELETE
1899 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001900}
1901
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001902void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001903 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001904 child->parents_.push_back(this);
1905 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001906 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001907}
1908
1909void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001910 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001911 return;
1912 }
1913
1914 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001915 children_.for_each([&] (soinfo* child) {
1916 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001917 return parent == this;
1918 });
1919 });
1920
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001921 parents_.for_each([&] (soinfo* parent) {
1922 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001923 return child == this;
1924 });
1925 });
1926
1927 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001928 parents_.clear();
1929 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001930}
1931
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001932dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001933 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001934 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001935 }
1936
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001937 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001938};
1939
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001940ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001941 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001942 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001943 }
1944
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001945 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001946}
1947
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001948off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001949 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001950 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001951 }
1952
1953 return 0;
1954}
1955
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001956uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001957 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001958 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001959 }
1960
1961 return 0;
1962}
1963
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001964uint32_t soinfo::get_dt_flags_1() const {
1965 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001966 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001967 }
1968
1969 return 0;
1970}
1971void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1972 if (has_min_version(1)) {
1973 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001974 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001975 }
1976
1977 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001978 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001979 }
1980
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001981 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001982 }
1983}
1984
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001985// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001986// 'this->flags' does not have FLAG_NEW_SOINFO set.
1987static soinfo::soinfo_list_t g_empty_list;
1988
1989soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001990 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001991 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001992 }
1993
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001994 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001995}
1996
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001997soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001998 if (has_min_version(0)) {
1999 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002000 }
2001
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002002 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002003}
2004
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002005ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
2006 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
2007 return call_ifunc_resolver(s->st_value + load_bias);
2008 }
2009
2010 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
2011}
2012
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002013const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002014 if (has_min_version(1) && (index >= strtab_size_)) {
2015 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002016 }
2017
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002018 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002019}
2020
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002021bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002022 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002023}
2024
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002025bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002026 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002027}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002028
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002029bool soinfo::is_linked() const {
2030 return (flags_ & FLAG_LINKED) != 0;
2031}
2032
2033bool soinfo::is_main_executable() const {
2034 return (flags_ & FLAG_EXE) != 0;
2035}
2036
2037void soinfo::set_linked() {
2038 flags_ |= FLAG_LINKED;
2039}
2040
2041void soinfo::set_linker_flag() {
2042 flags_ |= FLAG_LINKER;
2043}
2044
2045void soinfo::set_main_executable() {
2046 flags_ |= FLAG_EXE;
2047}
2048
2049void soinfo::increment_ref_count() {
2050 local_group_root_->ref_count_++;
2051}
2052
2053size_t soinfo::decrement_ref_count() {
2054 return --local_group_root_->ref_count_;
2055}
2056
2057soinfo* soinfo::get_local_group_root() const {
2058 return local_group_root_;
2059}
2060
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002061/* Force any of the closed stdin, stdout and stderr to be associated with
2062 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07002063static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002064 int dev_null, i, status;
2065 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002066
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002067 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
2068 if (dev_null < 0) {
2069 DL_ERR("cannot open /dev/null: %s", strerror(errno));
2070 return -1;
2071 }
2072 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002073
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002074 /* If any of the stdio file descriptors is valid and not associated
2075 with /dev/null, dup /dev/null to it. */
2076 for (i = 0; i < 3; i++) {
2077 /* If it is /dev/null already, we are done. */
2078 if (i == dev_null) {
2079 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002080 }
2081
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002082 TRACE("[ Nullifying stdio file descriptor %d]", i);
2083 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
2084
2085 /* If file is opened, we are good. */
2086 if (status != -1) {
2087 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002088 }
2089
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002090 /* The only error we allow is that the file descriptor does not
2091 exist, in which case we dup /dev/null to it. */
2092 if (errno != EBADF) {
2093 DL_ERR("fcntl failed: %s", strerror(errno));
2094 return_value = -1;
2095 continue;
2096 }
2097
2098 /* Try dupping /dev/null to this stdio file descriptor and
2099 repeat if there is a signal. Note that any errors in closing
2100 the stdio descriptor are lost. */
2101 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
2102 if (status < 0) {
2103 DL_ERR("dup2 failed: %s", strerror(errno));
2104 return_value = -1;
2105 continue;
2106 }
2107 }
2108
2109 /* If /dev/null is not one of the stdio file descriptors, close it. */
2110 if (dev_null > 2) {
2111 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
2112 status = TEMP_FAILURE_RETRY(close(dev_null));
2113 if (status == -1) {
2114 DL_ERR("close failed: %s", strerror(errno));
2115 return_value = -1;
2116 }
2117 }
2118
2119 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002120}
2121
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002122bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002123 /* Extract dynamic section */
2124 ElfW(Word) dynamic_flags = 0;
2125 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002126
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002127 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002128 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002129 if (!relocating_linker) {
2130 INFO("[ linking %s ]", name);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002131 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002132 }
2133
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002134 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002135 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002136 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002137 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002138 return false;
2139 } else {
2140 if (!relocating_linker) {
2141 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002142 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002143 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002144
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002145#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002146 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2147 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002148#endif
2149
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002150 // Extract useful information from dynamic section.
2151 uint32_t needed_count = 0;
2152 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2153 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2154 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2155 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002156 case DT_SONAME:
2157 // TODO: glibc dynamic linker uses this name for
2158 // initial library lookup; consider doing the same here.
2159 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002160
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002161 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002162 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002163 // in case of --hash-style=both, we prefer gnu
2164 break;
2165 }
2166
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002167 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2168 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2169 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2170 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002171 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002172
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002173 case DT_GNU_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002174 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002175 // in case of --hash-style=both, we prefer gnu
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002176 nchain_ = 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002177 }
2178
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002179 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002180 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002181 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2182 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002183
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002184 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
2185 bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002186 // amend chain for symndx = header[1]
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002187 chain_ = bucket_ + nbucket_ - reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002188
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002189 if (!powerof2(gnu_maskwords_)) {
2190 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 -08002191 return false;
2192 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002193 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002194
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002195 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002196 break;
2197
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002198 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002199 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002200 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002201
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002202 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002203 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002204 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002205
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002206 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002207 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002208 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002209
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002210 case DT_SYMENT:
2211 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002212 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 -07002213 return false;
2214 }
2215 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002216
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002217 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002218#if defined(USE_RELA)
2219 if (d->d_un.d_val != DT_RELA) {
2220 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002221 return false;
2222 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002223#else
2224 if (d->d_un.d_val != DT_REL) {
2225 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
2226 return false;
2227 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002228#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002229 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002230
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002231 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002232#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002233 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002234#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002235 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002236#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002237 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002238
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002239 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002240#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002241 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002242#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002243 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002244#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002245 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002246
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002247 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002248#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002249 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002250 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002251#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002252 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2253 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002254
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002255 case DT_DEBUG:
2256 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2257 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002258// FIXME: not working currently for N64
2259// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002260// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002261// read-only, but the DYNAMIC header claims it is writable.
2262#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002263 if ((dynamic_flags & PF_W) != 0) {
2264 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2265 }
2266 break;
Chris Dearman99186652014-02-06 20:36:51 -08002267#endif
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002268#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002269 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002270 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002271 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002272
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002273 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002274 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002275 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002276
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002277 case DT_RELAENT:
2278 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002279 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002280 return false;
2281 }
2282 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002283
2284 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002285 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002286 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002287
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002288 case DT_REL:
2289 DL_ERR("unsupported DT_REL in \"%s\"", name);
2290 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002291
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002292 case DT_RELSZ:
2293 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2294 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002295#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002296 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002297 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002298 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002299
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002300 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002301 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002302 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002303
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002304 case DT_RELENT:
2305 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002306 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002307 return false;
2308 }
2309 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002310
2311 // "Indicates that all RELATIVE relocations have been concatenated together,
2312 // and specifies the RELATIVE relocation count."
2313 //
2314 // TODO: Spec also mentions that this can be used to optimize relocation process;
2315 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002316 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002317 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002318 case DT_RELA:
2319 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2320 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002321#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002322 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002323 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2324 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002325 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002326
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002327 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002328 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2329 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002330 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002331
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002332 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002333 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2334 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002335 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002336
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002337 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002338 init_array_count_ = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002339 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002340
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002341 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002342 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2343 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002344 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002345
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002346 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002347 fini_array_count_ = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002348 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002349
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002350 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002351 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2352 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002353 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002354
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002355 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002356 preinit_array_count_ = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002357 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002358
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002359 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002360#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002361 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2362 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002363#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002364 has_text_relocations = true;
2365 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002366#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002367
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002368 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002369 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002370 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002371
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002372 case DT_NEEDED:
2373 ++needed_count;
2374 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002375
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002376 case DT_FLAGS:
2377 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002378#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002379 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2380 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002381#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002382 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002383#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002384 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002385 if (d->d_un.d_val & DF_SYMBOLIC) {
2386 has_DT_SYMBOLIC = true;
2387 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002388 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002389
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002390 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002391 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002392
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002393 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002394 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2395 }
2396 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002397#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002398 case DT_MIPS_RLD_MAP:
2399 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2400 {
2401 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2402 *dp = &_r_debug;
2403 }
2404 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002405 case DT_MIPS_RLD_MAP2:
2406 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2407 {
2408 r_debug** dp = reinterpret_cast<r_debug**>(reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
2409 *dp = &_r_debug;
2410 }
2411 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002412
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002413 case DT_MIPS_RLD_VERSION:
2414 case DT_MIPS_FLAGS:
2415 case DT_MIPS_BASE_ADDRESS:
2416 case DT_MIPS_UNREFEXTNO:
2417 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002418
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002419 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002420 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002421 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002422
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002423 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002424 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002425 break;
2426
2427 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002428 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002429 break;
2430#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002431 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2432 case DT_BIND_NOW:
2433 break;
2434
2435 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002436 case DT_VERSYM:
2437 case DT_VERDEF:
2438 case DT_VERDEFNUM:
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002439 case DT_VERNEED:
2440 case DT_VERNEEDNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002441 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002442
2443 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002444 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002445 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002446 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2447 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002448 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002449 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002450 }
2451
2452 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002453 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002454
2455 // Sanity checks.
2456 if (relocating_linker && needed_count != 0) {
2457 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2458 return false;
2459 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002460 if (nbucket_ == 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002461 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 -07002462 return false;
2463 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002464 if (strtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002465 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2466 return false;
2467 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002468 if (symtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002469 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2470 return false;
2471 }
2472 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002473}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002474
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002475bool 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 -08002476
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002477 local_group_root_ = local_group.front();
2478 if (local_group_root_ == nullptr) {
2479 local_group_root_ = this;
2480 }
2481
Elliott Hughese4d792a2013-10-28 14:19:05 -07002482#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002483 if (has_text_relocations) {
2484 // Make segments writable to allow text relocations to work properly. We will later call
2485 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2486 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2487 "security hardening. Please fix.", name);
2488 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2489 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2490 name, strerror(errno));
2491 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002492 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002493 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002494#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002495
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002496#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002497 if (rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002498 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002499 if (relocate(rela_, rela_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002500 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002501 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002502 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002503 if (plt_rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002504 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002505 if (relocate(plt_rela_, plt_rela_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002506 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002507 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002508 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002509#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002510 if (rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002511 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002512 if (relocate(rel_, rel_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002513 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002514 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002515 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002516 if (plt_rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002517 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002518 if (relocate(plt_rel_, plt_rel_count_, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002519 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002520 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002521 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002522#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002523
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002524#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08002525 if (!mips_relocate_got(global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002526 return false;
2527 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002528#endif
2529
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002530 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002531
Elliott Hughese4d792a2013-10-28 14:19:05 -07002532#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002533 if (has_text_relocations) {
2534 // All relocations are done, we can protect our segments back to read-only.
2535 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2536 DL_ERR("can't protect segments for \"%s\": %s",
2537 name, strerror(errno));
2538 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002539 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002540 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002541#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002542
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002543 /* We can also turn on GNU RELRO protection */
2544 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2545 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2546 name, strerror(errno));
2547 return false;
2548 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002549
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002550 /* Handle serializing/sharing the RELRO segment */
2551 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2552 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2553 extinfo->relro_fd) < 0) {
2554 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2555 name, strerror(errno));
2556 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002557 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002558 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2559 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2560 extinfo->relro_fd) < 0) {
2561 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2562 name, strerror(errno));
2563 return false;
2564 }
2565 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002566
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002567 notify_gdb_of_load(this);
2568 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002569}
2570
Nick Kralevich468319c2011-11-11 15:53:17 -08002571/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002572 * This function add vdso to internal dso list.
2573 * It helps to stack unwinding through signal handlers.
2574 * Also, it makes bionic more like glibc.
2575 */
Kito Cheng812fd422014-03-25 22:53:56 +08002576static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002577#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002578 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002579 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002580 return;
2581 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002582
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002583 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002584
Elliott Hughes0266ae52014-02-10 17:46:57 -08002585 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2586 si->phnum = ehdr_vdso->e_phnum;
2587 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2588 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002589 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002590
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002591 si->prelink_image();
2592 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002593#endif
2594}
2595
2596/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002597 * This is linker soinfo for GDB. See details below.
2598 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002599#if defined(__LP64__)
2600#define LINKER_PATH "/system/bin/linker64"
2601#else
2602#define LINKER_PATH "/system/bin/linker"
2603#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002604static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002605
2606/* gdb expects the linker to be in the debug shared object list.
2607 * Without this, gdb has trouble locating the linker's ".text"
2608 * and ".plt" sections. Gdb could also potentially use this to
2609 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2610 * Don't use soinfo_alloc(), because the linker shouldn't
2611 * be on the soinfo list.
2612 */
2613static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002614 linker_soinfo_for_gdb.base = linker_base;
2615
2616 /*
2617 * Set the dynamic field in the link map otherwise gdb will complain with
2618 * the following:
2619 * warning: .dynamic section for "/system/bin/linker" is not at the
2620 * expected address (wrong library or version mismatch?)
2621 */
2622 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2623 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2624 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002625 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002626 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2627}
2628
2629/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002630 * This code is called after the linker has linked itself and
2631 * fixed it's own GOT. It is safe to make references to externs
2632 * and other non-local data at this point.
2633 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002634static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002635#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002636 struct timeval t0, t1;
2637 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002638#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002639
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002640 // Initialize environment functions, and get to the ELF aux vectors table.
2641 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002642
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002643 // If this is a setuid/setgid program, close the security hole described in
2644 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2645 if (get_AT_SECURE()) {
2646 nullify_closed_stdio();
2647 }
2648
2649 debuggerd_init();
2650
2651 // Get a few environment variables.
2652 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2653 if (LD_DEBUG != nullptr) {
2654 g_ld_debug_verbosity = atoi(LD_DEBUG);
2655 }
2656
2657 // Normally, these are cleaned by linker_env_init, but the test
2658 // doesn't cost us anything.
2659 const char* ldpath_env = nullptr;
2660 const char* ldpreload_env = nullptr;
2661 if (!get_AT_SECURE()) {
2662 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2663 ldpreload_env = linker_env_get("LD_PRELOAD");
2664 }
2665
2666 INFO("[ android linker & debugger ]");
2667
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002668 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002669 if (si == nullptr) {
2670 exit(EXIT_FAILURE);
2671 }
2672
2673 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002674 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002675 link_map* map = &(si->link_map_head);
2676
2677 map->l_addr = 0;
2678 map->l_name = args.argv[0];
2679 map->l_prev = nullptr;
2680 map->l_next = nullptr;
2681
2682 _r_debug.r_map = map;
2683 r_debug_tail = map;
2684
2685 init_linker_info_for_gdb(linker_base);
2686
2687 // Extract information passed from the kernel.
2688 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2689 si->phnum = args.getauxval(AT_PHNUM);
2690 si->entry = args.getauxval(AT_ENTRY);
2691
2692 /* Compute the value of si->base. We can't rely on the fact that
2693 * the first entry is the PHDR because this will not be true
2694 * for certain executables (e.g. some in the NDK unit test suite)
2695 */
2696 si->base = 0;
2697 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2698 si->load_bias = 0;
2699 for (size_t i = 0; i < si->phnum; ++i) {
2700 if (si->phdr[i].p_type == PT_PHDR) {
2701 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2702 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2703 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002704 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002705 }
2706 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002707
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002708 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2709 if (elf_hdr->e_type != ET_DYN) {
2710 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2711 exit(EXIT_FAILURE);
2712 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002713
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002714 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2715 parse_LD_LIBRARY_PATH(ldpath_env);
2716 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002717
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002718 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002719
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002720 si->prelink_image();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002721
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002722 // add somain to global group
2723 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2724
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002725 // Load ld_preloads and dependencies.
2726 StringLinkedList needed_library_name_list;
2727 size_t needed_libraries_count = 0;
2728 size_t ld_preloads_count = 0;
2729 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2730 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2731 ++needed_libraries_count;
2732 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002733
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002734 for_each_dt_needed(si, [&](const char* name) {
2735 needed_library_name_list.push_back(name);
2736 ++needed_libraries_count;
2737 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002738
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002739 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002740
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002741 memset(needed_library_names, 0, sizeof(needed_library_names));
2742 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002743
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002744 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 -07002745 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2746 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002747 } else if (needed_libraries_count == 0) {
2748 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
2749 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2750 exit(EXIT_FAILURE);
2751 }
2752 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002753 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002754
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002755 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002756
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002757 si->call_pre_init_constructors();
Matt Fischer4fd42c12009-12-31 12:09:10 -06002758
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002759 /* After the prelink_image, the si->load_bias is initialized.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002760 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2761 * We need to update this value for so exe here. So Unwind_Backtrace
2762 * for some arch like x86 could work correctly within so exe.
2763 */
2764 map->l_addr = si->load_bias;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002765 si->call_constructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002766
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002767#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002768 gettimeofday(&t1, nullptr);
2769 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2770 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2771 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002772#endif
2773#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002774 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2775 linker_stats.count[kRelocAbsolute],
2776 linker_stats.count[kRelocRelative],
2777 linker_stats.count[kRelocCopy],
2778 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002779#endif
2780#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002781 {
2782 unsigned n;
2783 unsigned i;
2784 unsigned count = 0;
2785 for (n = 0; n < 4096; n++) {
2786 if (bitmask[n]) {
2787 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002788#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002789 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002790#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002791 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002792#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002793 if (x & 1) {
2794 count++;
2795 }
2796 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002797 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002798 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002799 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002800 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2801 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002802#endif
2803
2804#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002805 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002806#endif
2807
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002808 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2809 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002810}
Nick Kralevich468319c2011-11-11 15:53:17 -08002811
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002812/* Compute the load-bias of an existing executable. This shall only
2813 * be used to compute the load bias of an executable or shared library
2814 * that was loaded by the kernel itself.
2815 *
2816 * Input:
2817 * elf -> address of ELF header, assumed to be at the start of the file.
2818 * Return:
2819 * load bias, i.e. add the value of any p_vaddr in the file to get
2820 * the corresponding address in memory.
2821 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002822static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2823 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002824 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002825 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002826
Elliott Hughes0266ae52014-02-10 17:46:57 -08002827 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002828 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002829 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002830 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002831 }
2832 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002833}
2834
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002835extern "C" void _start();
2836
Nick Kralevich468319c2011-11-11 15:53:17 -08002837/*
2838 * This is the entry point for the linker, called from begin.S. This
2839 * method is responsible for fixing the linker's own relocations, and
2840 * then calling __linker_init_post_relocation().
2841 *
2842 * Because this method is called before the linker has fixed it's own
2843 * relocations, any attempt to reference an extern variable, extern
2844 * function, or other GOT reference will generate a segfault.
2845 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002846extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002847 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002848
Elliott Hughes0266ae52014-02-10 17:46:57 -08002849 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002850 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002851 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002852 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002853
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002854 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002855
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002856 // If the linker is not acting as PT_INTERP entry_point is equal to
2857 // _start. Which means that the linker is running as an executable and
2858 // already linked by PT_INTERP.
2859 //
2860 // This happens when user tries to run 'adb shell /system/bin/linker'
2861 // see also https://code.google.com/p/android/issues/detail?id=63174
2862 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2863 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2864 }
2865
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002866 linker_so.base = linker_addr;
2867 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2868 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002869 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002870 linker_so.phdr = phdr;
2871 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002872 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07002873
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002874 // This might not be obvious... The reasons why we pass g_empty_list
2875 // in place of local_group here are (1) we do not really need it, because
2876 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2877 // itself without having to look into local_group and (2) allocators
2878 // are not yet initialized, and therefore we cannot use linked_list.push_*
2879 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002880 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002881 // It would be nice to print an error message, but if the linker
2882 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002883 // call write() (because it involves a GOT reference). We may as
2884 // well try though...
2885 const char* msg = "CANNOT LINK EXECUTABLE: ";
2886 write(2, msg, strlen(msg));
2887 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2888 write(2, "\n", 1);
2889 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002890 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002891
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002892 __libc_init_tls(args);
2893
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002894 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002895 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002896
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002897 // Initialize static variables. Note that in order to
2898 // get correct libdl_info we need to call constructors
2899 // before get_libdl_info().
2900 solist = get_libdl_info();
2901 sonext = get_libdl_info();
2902
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002903 // We have successfully fixed our own relocations. It's safe to run
2904 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002905 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002906 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002907
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002908 protect_data(PROT_READ);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002909
2910 // Return the address that the calling assembly stub should jump to.
2911 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002912}