blob: f14d8b48ddd955d4b2ce7d86fa7cd52231dbbaba [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Doug Kwan94304352009-10-23 18:11:40 -07002 * Copyright (C) 2008, 2009 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughes46882792012-08-03 16:49:39 -070029#include <dlfcn.h>
30#include <errno.h>
31#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080032#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070037#include <sys/mman.h>
Elliott Hughes46882792012-08-03 16:49:39 -070038#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070040#include <new>
41
Elliott Hughes46882792012-08-03 16:49:39 -070042// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070043#include "private/bionic_tls.h"
44#include "private/KernelArgumentBlock.h"
45#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070046#include "private/ScopedFd.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070047#include "private/ScopeGuard.h"
48#include "private/UniquePtr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049
50#include "linker.h"
51#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010052#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020053#include "linker_phdr.h"
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070054#include "linker_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
57 *
58 * Do NOT use malloc() and friends or pthread_*() code here.
59 * Don't use printf() either; it's caused mysterious memory
60 * corruption in the past.
61 * The linker runs before we bring up libc and it's easiest
62 * to make sure it does not depend on any complex libc features
63 *
64 * open issues / todo:
65 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067 * - after linking, set as much stuff as possible to READONLY
68 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070069 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070
Dmitriy Ivanov489e4982014-05-19 15:19:52 -070071#if defined(__LP64__)
72#define SEARCH_NAME(x) x
73#else
74// Nvidia drivers are relying on the bug:
75// http://code.google.com/p/android/issues/detail?id=6670
76// so we continue to use base-name lookup for lp32
77static const char* get_base_name(const char* name) {
78 const char* bname = strrchr(name, '/');
79 return bname ? bname + 1 : name;
80}
81#define SEARCH_NAME(x) get_base_name(x)
82#endif
83
Elliott Hughes0266ae52014-02-10 17:46:57 -080084static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085
Elliott Hughes1728b232014-05-14 10:02:03 -070086static LinkerAllocator<soinfo> g_soinfo_allocator;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070087static LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020088
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070089static soinfo* solist;
90static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -070091static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092
Elliott Hughes1728b232014-05-14 10:02:03 -070093static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070094#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070095 "/vendor/lib64",
96 "/system/lib64",
97#else
Elliott Hughes124fae92012-10-31 14:20:03 -070098 "/vendor/lib",
99 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700100#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700101 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700102};
David Bartleybc3a5c22009-06-02 18:27:28 -0700103
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800104#define LDPATH_BUFSIZE (LDPATH_MAX*64)
105#define LDPATH_MAX 8
106
107#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
108#define LDPRELOAD_MAX 8
109
Elliott Hughes1728b232014-05-14 10:02:03 -0700110static char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
111static const char* g_ld_library_paths[LDPATH_MAX + 1];
Elliott Hughes124fae92012-10-31 14:20:03 -0700112
Elliott Hughes1728b232014-05-14 10:02:03 -0700113static char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
114static const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600115
Elliott Hughes1728b232014-05-14 10:02:03 -0700116static soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600117
Elliott Hughes1728b232014-05-14 10:02:03 -0700118__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700120__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700121
Elliott Hughesbedfe382012-08-14 14:07:59 -0700122enum RelocationKind {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700123 kRelocAbsolute = 0,
124 kRelocRelative,
125 kRelocCopy,
126 kRelocSymbol,
127 kRelocMax
Elliott Hughesbedfe382012-08-14 14:07:59 -0700128};
David 'Digit' Turnerbe575592010-12-16 19:52:02 +0100129
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700131struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700132 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700133};
134
135static linker_stats_t linker_stats;
136
137static void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700138 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700139}
140#else
141static void count_relocation(RelocationKind) {
142}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143#endif
144
145#if COUNT_PAGES
Elliott Hughesbedfe382012-08-14 14:07:59 -0700146static unsigned bitmask[4096];
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100147#if defined(__LP64__)
148#define MARK(offset) \
149 do { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700150 if ((((offset) >> 12) >> 5) < 4096) \
151 bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800152 } while (0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100153#else
Elliott Hughesbedfe382012-08-14 14:07:59 -0700154#define MARK(offset) \
155 do { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700156 bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800157 } while (0)
Marcus Oaklande365f9d2013-10-10 15:19:31 +0100158#endif
Elliott Hughesbedfe382012-08-14 14:07:59 -0700159#else
160#define MARK(x) do {} while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161#endif
162
Elliott Hughes46882792012-08-03 16:49:39 -0700163// You shouldn't try to call memory-allocating functions in the dynamic linker.
164// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700165#define DISALLOW_ALLOCATION(return_type, name, ...) \
166 return_type name __VA_ARGS__ \
167 { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700168 __libc_fatal("ERROR: " #name " called from the dynamic linker!\n"); \
Dima Zavin2e855792009-05-20 18:28:09 -0700169 }
Kito Cheng812fd422014-03-25 22:53:56 +0800170DISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
171DISALLOW_ALLOCATION(void, free, (void* u __unused));
172DISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
173DISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
Dima Zavin2e855792009-05-20 18:28:09 -0700174
175static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700176
Elliott Hughes650be4e2013-03-05 18:47:58 -0800177char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700178 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700179}
180
Elliott Hughes650be4e2013-03-05 18:47:58 -0800181size_t linker_get_error_buffer_size() {
182 return sizeof(__linker_dl_err_buf);
183}
184
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700185// This function is an empty stub where GDB locates a breakpoint to get notified
186// about linker activity.
Elliott Hughes5419b942012-10-16 15:54:46 -0700187extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188
Elliott Hughes1728b232014-05-14 10:02:03 -0700189static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700190static 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 -0800191static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800193static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700194 // Copy the necessary fields into the debug structure.
195 link_map* map = &(info->link_map_head);
196 map->l_addr = info->load_bias;
Dmitriy Ivanovc9d16582014-10-24 14:46:12 -0700197 map->l_name = info->name;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700198 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700200 // Stick the new library at the end of the list.
201 // gdb tends to care more about libc than it does
202 // about leaf libraries, and ordering it this way
203 // reduces the back-and-forth over the wire.
204 if (r_debug_tail) {
205 r_debug_tail->l_next = map;
206 map->l_prev = r_debug_tail;
207 map->l_next = 0;
208 } else {
209 _r_debug.r_map = map;
210 map->l_prev = 0;
211 map->l_next = 0;
212 }
213 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214}
215
Elliott Hughesbedfe382012-08-14 14:07:59 -0700216static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700217 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700218
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700219 if (r_debug_tail == map) {
220 r_debug_tail = map->l_prev;
221 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700222
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700223 if (map->l_prev) {
224 map->l_prev->l_next = map->l_next;
225 }
226 if (map->l_next) {
227 map->l_next->l_prev = map->l_prev;
228 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700229}
230
Elliott Hughesbedfe382012-08-14 14:07:59 -0700231static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700232 if (info->flags & FLAG_EXE) {
233 // GDB already knows about the main executable
234 return;
235 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700237 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700239 _r_debug.r_state = r_debug::RT_ADD;
240 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700242 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700244 _r_debug.r_state = r_debug::RT_CONSISTENT;
245 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700246}
247
Elliott Hughesbedfe382012-08-14 14:07:59 -0700248static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700249 if (info->flags & FLAG_EXE) {
250 // GDB already knows about the main executable
251 return;
252 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700253
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700254 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700255
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700256 _r_debug.r_state = r_debug::RT_DELETE;
257 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700258
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700259 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700260
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700261 _r_debug.r_state = r_debug::RT_CONSISTENT;
262 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800263}
264
Elliott Hughes18a206c2012-10-29 17:37:13 -0700265void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800266 _r_debug.r_state = r_debug::RT_ADD;
267 rtld_db_dlactivity();
268 _r_debug.r_state = r_debug::RT_CONSISTENT;
269 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800270}
271
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700272LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
273 return g_soinfo_links_allocator.alloc();
274}
275
276void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
277 g_soinfo_links_allocator.free(entry);
278}
279
280static void protect_data(int protection) {
281 g_soinfo_allocator.protect_all(protection);
282 g_soinfo_links_allocator.protect_all(protection);
283}
284
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700285static soinfo* soinfo_alloc(const char* name, struct stat* file_stat, off64_t file_offset, int rtld_flags) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200286 if (strlen(name) >= SOINFO_NAME_LEN) {
287 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700288 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200289 }
290
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700291 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700292
Magnus Malmbornba98d922012-09-12 13:00:55 +0200293 sonext->next = si;
294 sonext = si;
295
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700296 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200297 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800298}
299
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800300static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700301 if (si == nullptr) {
302 return;
303 }
304
305 if (si->base != 0 && si->size != 0) {
306 munmap(reinterpret_cast<void*>(si->base), si->size);
307 }
308
309 soinfo *prev = nullptr, *trav;
310
311 TRACE("name %s: freeing soinfo @ %p", si->name, si);
312
313 for (trav = solist; trav != nullptr; trav = trav->next) {
314 if (trav == si) {
315 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700316 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700317 prev = trav;
318 }
319 if (trav == nullptr) {
320 // si was not in solist
321 DL_ERR("name \"%s\" is not in solist!", si->name);
322 return;
323 }
Elliott Hughes46882792012-08-03 16:49:39 -0700324
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700325 // clear links to/from si
326 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700327
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700328 // prev will never be null, because the first entry in solist is
329 // always the static libdl_info.
330 prev->next = si->next;
331 if (si == sonext) {
332 sonext = prev;
333 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700335 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336}
337
Elliott Hughescade4c32012-12-20 14:42:14 -0800338
339static void parse_path(const char* path, const char* delimiters,
340 const char** array, char* buf, size_t buf_size, size_t max_count) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700341 if (path == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800342 return;
343 }
344
345 size_t len = strlcpy(buf, path, buf_size);
346
347 size_t i = 0;
348 char* buf_p = buf;
349 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
350 if (*array[i] != '\0') {
351 ++i;
352 }
353 }
354
355 // Forget the last path if we had to truncate; this occurs if the 2nd to
356 // last char isn't '\0' (i.e. wasn't originally a delimiter).
357 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700358 array[i - 1] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800359 } else {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700360 array[i] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800361 }
362}
363
364static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700365 parse_path(path, ":", g_ld_library_paths,
366 g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800367}
368
369static void parse_LD_PRELOAD(const char* path) {
370 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes1728b232014-05-14 10:02:03 -0700371 parse_path(path, " :", g_ld_preload_names,
372 g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800373}
374
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700375#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700376
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700377// For a given PC, find the .so that it belongs to.
378// Returns the base address of the .ARM.exidx section
379// for that .so, and the number of 8-byte entries
380// in that section (via *pcount).
381//
382// Intended to be called by libc's __gnu_Unwind_Find_exidx().
383//
384// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800385_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700386 unsigned addr = (unsigned)pc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800387
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700388 for (soinfo* si = solist; si != 0; si = si->next) {
389 if ((addr >= si->base) && (addr < (si->base + si->size))) {
390 *pcount = si->ARM_exidx_count;
391 return (_Unwind_Ptr)si->ARM_exidx;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800392 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700393 }
394 *pcount = 0;
395 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396}
Elliott Hughes46882792012-08-03 16:49:39 -0700397
Christopher Ferris24053a42013-08-19 17:45:09 -0700398#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700399
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700400// Here, we only have to provide a callback to iterate across all the
401// loaded libraries. gcc_eh does the rest.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800402int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700403 int rv = 0;
404 for (soinfo* si = solist; si != nullptr; si = si->next) {
405 dl_phdr_info dl_info;
406 dl_info.dlpi_addr = si->link_map_head.l_addr;
407 dl_info.dlpi_name = si->link_map_head.l_name;
408 dl_info.dlpi_phdr = si->phdr;
409 dl_info.dlpi_phnum = si->phnum;
410 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
411 if (rv != 0) {
412 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800413 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700414 }
415 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416}
Elliott Hughes46882792012-08-03 16:49:39 -0700417
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700418static ElfW(Sym)* soinfo_elf_lookup(const soinfo* si, unsigned hash, const char* name) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800419 ElfW(Sym)* symtab = si->symtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800420
Elliott Hughes0266ae52014-02-10 17:46:57 -0800421 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
422 name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800423
Elliott Hughes0266ae52014-02-10 17:46:57 -0800424 for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
425 ElfW(Sym)* s = symtab + n;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700426 if (strcmp(si->get_string(s->st_name), name)) continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800427
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700428 // only concern ourselves with global and weak symbol definitions
Elliott Hughes0266ae52014-02-10 17:46:57 -0800429 switch (ELF_ST_BIND(s->st_info)) {
430 case STB_GLOBAL:
431 case STB_WEAK:
432 if (s->st_shndx == SHN_UNDEF) {
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -0700433 continue;
434 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800435
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -0700436 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Elliott Hughes0266ae52014-02-10 17:46:57 -0800437 name, si->name, reinterpret_cast<void*>(s->st_value),
438 static_cast<size_t>(s->st_size));
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -0700439 return s;
440 case STB_LOCAL:
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700441 continue;
Dmitriy Ivanovd97e9f52014-06-29 12:28:37 -0700442 default:
Dmitriy Ivanov12bf3bc2014-07-01 14:24:45 -0700443 __libc_fatal("ERROR: Unexpected ST_BIND value: %d for '%s' in '%s'",
444 ELF_ST_BIND(s->st_info), name, si->name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800445 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800446 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800447
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700448 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
449 name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
450
451
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700452 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800453}
454
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700455soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700456 memset(this, 0, sizeof(*this));
457
458 strlcpy(this->name, name, sizeof(this->name));
459 flags = FLAG_NEW_SOINFO;
460 version = SOINFO_VERSION;
461
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700462 if (file_stat != nullptr) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700463 this->st_dev = file_stat->st_dev;
464 this->st_ino = file_stat->st_ino;
465 this->file_offset = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700466 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700467
468 this->rtld_flags = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700469}
470
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800471static unsigned elfhash(const char* _name) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700472 const unsigned char* name = reinterpret_cast<const unsigned char*>(_name);
473 unsigned h = 0, g;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800474
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700475 while (*name) {
476 h = (h << 4) + *name++;
477 g = h & 0xf0000000;
478 h ^= g;
479 h ^= g >> 24;
480 }
481 return h;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800482}
483
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700484static ElfW(Sym)* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700485 unsigned elf_hash = elfhash(name);
486 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700487
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700488 /* "This element's presence in a shared object library alters the dynamic linker's
489 * symbol resolution algorithm for references within the library. Instead of starting
490 * a symbol search with the executable file, the dynamic linker starts from the shared
491 * object itself. If the shared object fails to supply the referenced symbol, the
492 * dynamic linker then searches the executable file and other shared objects as usual."
493 *
494 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
495 *
496 * Note that this is unlikely since static linker avoids generating
497 * relocations for -Bsymbolic linked dynamic executables.
498 */
499 if (si->has_DT_SYMBOLIC) {
500 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si->name, name);
501 s = soinfo_elf_lookup(si, elf_hash, name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700502 if (s != nullptr) {
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700503 *lsi = si;
504 }
505 }
506
507 if (s == nullptr && somain != nullptr) {
508 // 1. Look for it in the main executable unless we already did.
509 if (si != somain || !si->has_DT_SYMBOLIC) {
510 DEBUG("%s: looking up %s in executable %s",
511 si->name, name, somain->name);
512 s = soinfo_elf_lookup(somain, elf_hash, name);
513 if (s != nullptr) {
514 *lsi = somain;
515 }
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700516 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700517
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700518 // 2. Look for it in the ld_preloads
519 if (s == nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700520 for (int i = 0; g_ld_preloads[i] != NULL; i++) {
521 s = soinfo_elf_lookup(g_ld_preloads[i], elf_hash, name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700522 if (s != nullptr) {
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700523 *lsi = g_ld_preloads[i];
524 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700525 }
526 }
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700527 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700528 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700529
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700530 // 3. Look for it in the local group
531 if (s == nullptr) {
532 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700533 if (local_si == si && si->has_DT_SYMBOLIC) {
534 // we already did this - skip
535 return true;
536 }
537
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700538 DEBUG("%s: looking up %s in %s (from local group)", si->name, name, local_si->name);
539 s = soinfo_elf_lookup(local_si, elf_hash, name);
540 if (s != nullptr) {
541 *lsi = local_si;
542 return false;
543 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700544
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700545 return true;
546 });
547 }
548
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700549 if (s != nullptr) {
550 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
551 "found in %s, base = %p, load bias = %p",
552 si->name, name, reinterpret_cast<void*>(s->st_value),
553 (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
554 reinterpret_cast<void*>((*lsi)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700555 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700556
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700557 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700558}
559
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700560// Each size has it's own allocator.
561template<size_t size>
562class SizeBasedAllocator {
563 public:
564 static void* alloc() {
565 return allocator_.alloc();
566 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700567
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700568 static void free(void* ptr) {
569 allocator_.free(ptr);
570 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700571
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700572 private:
573 static LinkerBlockAllocator allocator_;
574};
575
576template<size_t size>
577LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
578
579template<typename T>
580class TypeBasedAllocator {
581 public:
582 static T* alloc() {
583 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
584 }
585
586 static void free(T* ptr) {
587 SizeBasedAllocator<sizeof(T)>::free(ptr);
588 }
589};
590
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700591class LoadTask {
592 public:
593 struct deleter_t {
594 void operator()(LoadTask* t) {
595 TypeBasedAllocator<LoadTask>::free(t);
596 }
597 };
598
599 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
600
601 static deleter_t deleter;
602
603 static LoadTask* create(const char* name, soinfo* needed_by) {
604 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
605 return new (ptr) LoadTask(name, needed_by);
606 }
607
608 const char* get_name() const {
609 return name_;
610 }
611
612 soinfo* get_needed_by() const {
613 return needed_by_;
614 }
615 private:
616 LoadTask(const char* name, soinfo* needed_by)
617 : name_(name), needed_by_(needed_by) {}
618
619 const char* name_;
620 soinfo* needed_by_;
621
622 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
623};
624
Ningsheng Jiane93be992014-09-16 15:22:10 +0800625LoadTask::deleter_t LoadTask::deleter;
626
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700627template <typename T>
628using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
629
630typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700631typedef linked_list_t<const char> StringLinkedList;
632typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700633
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700634
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700635// This function walks down the tree of soinfo dependencies
636// in breadth-first order and
637// * calls action(soinfo* si) for each node, and
638// * terminates walk if action returns false.
639//
640// walk_dependencies_tree returns false if walk was terminated
641// by the action and true otherwise.
642template<typename F>
643static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700644 SoinfoLinkedList visit_list;
645 SoinfoLinkedList visited;
646
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700647 for (size_t i = 0; i < root_soinfos_size; ++i) {
648 visit_list.push_back(root_soinfos[i]);
649 }
650
651 soinfo* si;
652 while ((si = visit_list.pop_front()) != nullptr) {
653 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700654 continue;
655 }
656
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700657 if (!action(si)) {
658 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700659 }
660
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700661 visited.push_back(si);
662
663 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700664 visit_list.push_back(child);
665 });
666 }
667
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700668 return true;
669}
670
671
672// This is used by dlsym(3). It performs symbol lookup only within the
673// specified soinfo object and its dependencies in breadth first order.
674ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
675 ElfW(Sym)* result = nullptr;
676 uint32_t elf_hash = elfhash(name);
677
678
679 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
680 result = soinfo_elf_lookup(current_soinfo, elf_hash, name);
681 if (result != nullptr) {
682 *found = current_soinfo;
683 return false;
684 }
685
686 return true;
687 });
688
689 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800690}
691
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800692/* This is used by dlsym(3) to performs a global symbol lookup. If the
693 start value is null (for RTLD_DEFAULT), the search starts at the
694 beginning of the global solist. Otherwise the search starts at the
695 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700696 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700697ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800698 unsigned elf_hash = elfhash(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800699
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700700 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800701 start = solist;
702 }
703
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700704 ElfW(Sym)* s = nullptr;
705 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700706 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
707 continue;
708 }
709
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700710 s = soinfo_elf_lookup(si, elf_hash, name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700711 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800712 *found = si;
713 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600714 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800715 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600716
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700717 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700718 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
719 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800720 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800721
Elliott Hughescade4c32012-12-20 14:42:14 -0800722 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800723}
724
Kito Chengfa8c05d2013-03-12 14:58:06 +0800725soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800726 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700727 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800728 if (address >= si->base && address - si->base < si->size) {
729 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600730 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800731 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700732 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600733}
734
Elliott Hughes0266ae52014-02-10 17:46:57 -0800735ElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr) {
736 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - si->base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600737
Kito Chengfa8c05d2013-03-12 14:58:06 +0800738 // Search the library's symbol table for any defined symbol which
739 // contains this address.
740 for (size_t i = 0; i < si->nchain; ++i) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800741 ElfW(Sym)* sym = &si->symtab[i];
Kito Chengfa8c05d2013-03-12 14:58:06 +0800742 if (sym->st_shndx != SHN_UNDEF &&
743 soaddr >= sym->st_value &&
744 soaddr < sym->st_value + sym->st_size) {
745 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600746 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800747 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600748
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700749 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600750}
751
Elliott Hughes124fae92012-10-31 14:20:03 -0700752static int open_library_on_path(const char* name, const char* const paths[]) {
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +0000753 char buf[512];
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700754 for (size_t i = 0; paths[i] != nullptr; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800755 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700756 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700757 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700758 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800759 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700760 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
761 if (fd != -1) {
762 return fd;
763 }
764 }
765 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800766}
767
Elliott Hughes124fae92012-10-31 14:20:03 -0700768static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700769 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800770
Elliott Hughes124fae92012-10-31 14:20:03 -0700771 // 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 -0700772 if (strchr(name, '/') != nullptr) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700773 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
774 if (fd != -1) {
775 return fd;
776 }
777 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700778#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700779 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700780#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700781 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800782
Elliott Hughes124fae92012-10-31 14:20:03 -0700783 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Elliott Hughes1728b232014-05-14 10:02:03 -0700784 int fd = open_library_on_path(name, g_ld_library_paths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700785 if (fd == -1) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700786 fd = open_library_on_path(name, kDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700787 }
788 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800789}
790
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700791template<typename F>
792static void for_each_dt_needed(const soinfo* si, F action) {
793 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
794 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700795 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700796 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700797 }
798}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800799
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700800static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700801 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700802 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700803 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700804
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700805 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
806 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700807 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
808 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700809 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700810 } else {
811 // Open the file.
812 fd = open_library(name);
813 if (fd == -1) {
814 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700815 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700816 }
817
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700818 file_guard.reset(fd);
819 }
820
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700821 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700822 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700823 return nullptr;
824 }
825
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700826 struct stat file_stat;
827 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700828 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700829 return nullptr;
830 }
831
832 // Check for symlink and other situations where
833 // file can have different names.
834 for (soinfo* si = solist; si != nullptr; si = si->next) {
835 if (si->get_st_dev() != 0 &&
836 si->get_st_ino() != 0 &&
837 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700838 si->get_st_ino() == file_stat.st_ino &&
839 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700840 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
841 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700842 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700843 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700844
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700845 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700846 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700847 return nullptr;
848 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700849
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700850 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700851 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700852 if (!elf_reader.Load(extinfo)) {
853 return nullptr;
854 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800855
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700856 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700857 if (si == nullptr) {
858 return nullptr;
859 }
860 si->base = elf_reader.load_start();
861 si->size = elf_reader.load_size();
862 si->load_bias = elf_reader.load_bias();
863 si->phnum = elf_reader.phdr_count();
864 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700865
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700866 if (!si->PrelinkImage()) {
867 soinfo_free(si);
868 return nullptr;
869 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700870
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700871 for_each_dt_needed(si, [&] (const char* name) {
872 load_tasks.push_back(LoadTask::create(name, si));
873 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700874
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700875 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800876}
877
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700878static soinfo *find_loaded_library_by_name(const char* name) {
879 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700880 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700881 if (!strcmp(search_name, si->name)) {
882 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200883 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700884 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700885 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200886}
887
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700888static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200889
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700890 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700891
892 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700893 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700894 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700895 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700896 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700897 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800898
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700899 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800900}
901
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700902static void soinfo_unload(soinfo* si);
903
904static bool is_recursive(soinfo* si, soinfo* parent) {
905 if (parent == nullptr) {
906 return false;
Dmitriy Ivanova3ad4502014-07-29 14:21:45 -0700907 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700908
909 if (si == parent) {
910 DL_ERR("recursive link to \"%s\"", si->name);
911 return true;
912 }
913
914 return !parent->get_parents().visit([&](soinfo* grandparent) {
915 return !is_recursive(si, grandparent);
916 });
917}
918
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700919static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
920 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700921 // Step 0: prepare.
922 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700923 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700924 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700925 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700926 }
927
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700928 // If soinfos array is null allocate one on stack.
929 // The array is needed in case of failure; for example
930 // when library_names[] = {libone.so, libtwo.so} and libone.so
931 // is loaded correctly but libtwo.so failed for some reason.
932 // In this case libone.so should be unloaded on return.
933 // See also implementation of failure_guard below.
934
935 if (soinfos == nullptr) {
936 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
937 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
938 memset(soinfos, 0, soinfos_size);
939 }
940
941 // list of libraries to link - see step 2.
942 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700943
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -0700944 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700945 // Housekeeping
946 load_tasks.for_each([] (LoadTask* t) {
947 LoadTask::deleter(t);
948 });
949
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700950 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700951 soinfo_unload(soinfos[i]);
952 }
953 });
954
955 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
956 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 -0700957 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700958 if (si == nullptr) {
959 return false;
960 }
961
962 soinfo* needed_by = task->get_needed_by();
963
964 if (is_recursive(si, needed_by)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700965 return false;
966 }
967
968 si->ref_count++;
969 if (needed_by != nullptr) {
970 needed_by->add_child(si);
971 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700972
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700973 // When ld_preloads is not null, the first
974 // ld_preloads_count libs are in fact ld_preloads.
975 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
976 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700977 }
978
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700979 if (soinfos_count < library_names_count) {
980 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700981 }
982 }
983
984 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700985 soinfo::soinfo_list_t local_group;
986 walk_dependencies_tree(
987 start_with == nullptr ? soinfos : &start_with,
988 start_with == nullptr ? soinfos_count : 1,
989 [&] (soinfo* si) {
990 local_group.push_back(si);
991 return true;
992 });
993
994 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700995 if ((si->flags & FLAG_LINKED) == 0) {
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700996 if (!si->LinkImage(local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700997 return false;
998 }
999 si->flags |= FLAG_LINKED;
1000 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001001
1002 return true;
1003 });
1004
1005 if (linked) {
1006 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001007 }
1008
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001009 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001010}
1011
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001012static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001013 if (name == nullptr) {
1014 somain->ref_count++;
1015 return somain;
1016 }
1017
1018 soinfo* si;
1019
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001020 if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001021 return nullptr;
1022 }
1023
Elliott Hughesd23736e2012-11-01 15:16:56 -07001024 return si;
1025}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001026
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001027static void soinfo_unload(soinfo* si) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001028 if (!si->can_unload()) {
1029 TRACE("not unloading '%s' - the binary is flagged with NODELETE", si->name);
1030 return;
1031 }
1032
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001033 if (si->ref_count == 1) {
1034 TRACE("unloading '%s'", si->name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001035 si->CallDestructors();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001036
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001037 if (si->has_min_version(0)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001038 soinfo* child = nullptr;
1039 while ((child = si->get_children().pop_front()) != nullptr) {
1040 TRACE("%s needs to unload %s", si->name, child->name);
1041 soinfo_unload(child);
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001042 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001043 } else {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001044 for_each_dt_needed(si, [&] (const char* library_name) {
1045 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1046 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1047 if (needed != nullptr) {
1048 soinfo_unload(needed);
1049 } else {
1050 // Not found: for example if symlink was deleted between dlopen and dlclose
1051 // Since we cannot really handle errors at this point - print and continue.
1052 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001053 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001054 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001055 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001056
Elliott Hughesd23736e2012-11-01 15:16:56 -07001057 notify_gdb_of_unload(si);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001058 si->ref_count = 0;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -07001059 soinfo_free(si);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001060 } else {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001061 si->ref_count--;
Elliott Hughesc6200592013-09-30 18:43:46 -07001062 TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001063 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001064}
1065
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001066void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001067 // Use basic string manipulation calls to avoid snprintf.
1068 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1069 // When debug malloc is enabled, this call returns 0. This in turn causes
1070 // snprintf to do nothing, which causes libraries to fail to load.
1071 // See b/17302493 for further details.
1072 // Once the above bug is fixed, this code can be modified to use
1073 // snprintf again.
1074 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1075 if (buffer_size < required_len) {
1076 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1077 buffer_size, required_len);
1078 }
1079 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1080 *end = ':';
1081 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001082}
1083
Elliott Hughescade4c32012-12-20 14:42:14 -08001084void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
1085 if (!get_AT_SECURE()) {
1086 parse_LD_LIBRARY_PATH(ld_library_path);
1087 }
1088}
1089
Elliott Hughes1a586292014-06-03 16:23:08 -07001090soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001091 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001092 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001093 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001094 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001095 if (extinfo != nullptr) {
1096 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1097 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1098 return nullptr;
1099 }
1100 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001101 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1102 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 -07001103 return nullptr;
1104 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001105 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001106 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001107 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001108 if (si != nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001109 si->CallConstructors();
1110 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001111 protect_data(PROT_READ);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001112 return si;
1113}
1114
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001115void do_dlclose(soinfo* si) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001116 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001117 soinfo_unload(si);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001118 protect_data(PROT_READ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001119}
1120
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001121static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1122 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1123 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1124 ElfW(Addr) ifunc_addr = ifunc_resolver();
1125 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 -07001126
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001127 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001128}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001129
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001130#if defined(USE_RELA)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001131int soinfo::Relocate(ElfW(Rela)* rela, unsigned count, const soinfo_list_t& local_group) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001132 for (size_t idx = 0; idx < count; ++idx, ++rela) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001133 unsigned type = ELFW(R_TYPE)(rela->r_info);
1134 unsigned sym = ELFW(R_SYM)(rela->r_info);
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001135 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001136 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001137 const char* sym_name = nullptr;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001138
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001139 DEBUG("Processing '%s' relocation at index %zd", name, idx);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001140 if (type == 0) { // R_*_NONE
1141 continue;
1142 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001143
1144 ElfW(Sym)* s = nullptr;
1145 soinfo* lsi = nullptr;
1146
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001147 if (sym != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001148 sym_name = get_string(symtab[sym].st_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001149 s = soinfo_do_lookup(this, sym_name, &lsi, local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001150 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001151 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001152 s = &symtab[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001153 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001154 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001155 return -1;
1156 }
1157
1158 /* IHI0044C AAELF 4.5.1.1:
1159
1160 Libraries are not searched to resolve weak references.
1161 It is not an error for a weak reference to remain unsatisfied.
1162
1163 During linking, the value of an undefined weak reference is:
1164 - Zero if the relocation type is absolute
1165 - The address of the place if the relocation is pc-relative
1166 - The address of nominal base address if the relocation
1167 type is base-relative.
1168 */
1169
1170 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001171#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001172 case R_AARCH64_JUMP_SLOT:
1173 case R_AARCH64_GLOB_DAT:
1174 case R_AARCH64_ABS64:
1175 case R_AARCH64_ABS32:
1176 case R_AARCH64_ABS16:
1177 case R_AARCH64_RELATIVE:
1178 case R_AARCH64_IRELATIVE:
1179 /*
1180 * The sym_addr was initialized to be zero above, or the relocation
1181 * code below does not care about value of sym_addr.
1182 * No need to do anything.
1183 */
1184 break;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001185#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001186 case R_X86_64_JUMP_SLOT:
1187 case R_X86_64_GLOB_DAT:
1188 case R_X86_64_32:
1189 case R_X86_64_64:
1190 case R_X86_64_RELATIVE:
1191 case R_X86_64_IRELATIVE:
1192 // No need to do anything.
1193 break;
1194 case R_X86_64_PC32:
1195 sym_addr = reloc;
1196 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001197#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001198 default:
1199 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
1200 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001201 }
1202 } else {
1203 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001204 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001205 }
1206 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001207 }
1208
1209 switch (type) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001210#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001211 case R_AARCH64_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001212 count_relocation(kRelocAbsolute);
1213 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001214 TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
1215 reloc, (sym_addr + rela->r_addend), sym_name);
1216 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001217 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001218 case R_AARCH64_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001219 count_relocation(kRelocAbsolute);
1220 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001221 TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
1222 reloc, (sym_addr + rela->r_addend), sym_name);
1223 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001224 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001225 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001226 count_relocation(kRelocAbsolute);
1227 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001228 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
1229 reloc, (sym_addr + rela->r_addend), sym_name);
1230 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001231 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001232 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001233 count_relocation(kRelocAbsolute);
1234 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001235 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
1236 reloc, (sym_addr + rela->r_addend), sym_name);
1237 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1238 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001239 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001240 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001241 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1242 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1243 static_cast<ElfW(Addr)>(INT32_MIN),
1244 static_cast<ElfW(Addr)>(UINT32_MAX));
1245 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001246 }
1247 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001248 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001249 count_relocation(kRelocAbsolute);
1250 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001251 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
1252 reloc, (sym_addr + rela->r_addend), sym_name);
1253 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
1254 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001255 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001256 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001257 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1258 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
1259 static_cast<ElfW(Addr)>(INT16_MIN),
1260 static_cast<ElfW(Addr)>(UINT16_MAX));
1261 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001262 }
1263 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001264 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001265 count_relocation(kRelocRelative);
1266 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001267 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
1268 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1269 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001270 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001271 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001272 count_relocation(kRelocRelative);
1273 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001274 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
1275 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1276 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1277 ((*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 -07001278 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001279 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001280 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1281 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1282 static_cast<ElfW(Addr)>(INT32_MIN),
1283 static_cast<ElfW(Addr)>(UINT32_MAX));
1284 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001285 }
1286 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001287 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001288 count_relocation(kRelocRelative);
1289 MARK(rela->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001290 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
1291 reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
1292 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
1293 ((*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 -07001294 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001295 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001296 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1297 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
1298 static_cast<ElfW(Addr)>(INT16_MIN),
1299 static_cast<ElfW(Addr)>(UINT16_MAX));
1300 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001301 }
1302 break;
1303
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001304 case R_AARCH64_RELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001305 count_relocation(kRelocRelative);
1306 MARK(rela->r_offset);
1307 if (sym) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001308 DL_ERR("odd RELATIVE form...");
1309 return -1;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001310 }
Elliott Hughes0266ae52014-02-10 17:46:57 -08001311 TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001312 reloc, (base + rela->r_addend));
1313 *reinterpret_cast<ElfW(Addr)*>(reloc) = (base + rela->r_addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001314 break;
1315
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001316 case R_AARCH64_IRELATIVE:
1317 count_relocation(kRelocRelative);
1318 MARK(rela->r_offset);
1319 TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
1320 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
1321 break;
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001322
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001323 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001324 /*
1325 * ET_EXEC is not supported so this should not happen.
1326 *
1327 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1328 *
1329 * Section 4.7.1.10 "Dynamic relocations"
1330 * R_AARCH64_COPY may only appear in executable objects where e_type is
1331 * set to ET_EXEC.
1332 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001333 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Nick Kralevich76e289c2014-07-03 12:04:31 -07001334 return -1;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001335 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001336 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
1337 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001338 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001339 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001340 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
1341 reloc, (sym_addr + rela->r_addend), rela->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001342 break;
1343#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001344 case R_X86_64_JUMP_SLOT:
1345 count_relocation(kRelocAbsolute);
1346 MARK(rela->r_offset);
1347 TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1348 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1349 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1350 break;
1351 case R_X86_64_GLOB_DAT:
1352 count_relocation(kRelocAbsolute);
1353 MARK(rela->r_offset);
1354 TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1355 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1356 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1357 break;
1358 case R_X86_64_RELATIVE:
1359 count_relocation(kRelocRelative);
1360 MARK(rela->r_offset);
1361 if (sym) {
1362 DL_ERR("odd RELATIVE form...");
1363 return -1;
1364 }
1365 TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1366 static_cast<size_t>(base));
1367 *reinterpret_cast<ElfW(Addr)*>(reloc) = base + rela->r_addend;
1368 break;
1369 case R_X86_64_IRELATIVE:
1370 count_relocation(kRelocRelative);
1371 MARK(rela->r_offset);
1372 TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
1373 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
1374 break;
1375 case R_X86_64_32:
1376 count_relocation(kRelocRelative);
1377 MARK(rela->r_offset);
1378 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1379 static_cast<size_t>(sym_addr), sym_name);
1380 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1381 break;
1382 case R_X86_64_64:
1383 count_relocation(kRelocRelative);
1384 MARK(rela->r_offset);
1385 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1386 static_cast<size_t>(sym_addr), sym_name);
1387 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1388 break;
1389 case R_X86_64_PC32:
1390 count_relocation(kRelocRelative);
1391 MARK(rela->r_offset);
1392 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1393 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1394 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
1395 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
1396 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001397#endif
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001398
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001399 default:
1400 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
1401 return -1;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001402 }
1403 }
1404 return 0;
1405}
Chris Dearman99186652014-02-06 20:36:51 -08001406
1407#else // REL, not RELA.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001408int soinfo::Relocate(ElfW(Rel)* rel, unsigned count, const soinfo_list_t& local_group) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001409 for (size_t idx = 0; idx < count; ++idx, ++rel) {
1410 unsigned type = ELFW(R_TYPE)(rel->r_info);
1411 // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
1412 unsigned sym = ELFW(R_SYM)(rel->r_info);
1413 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
1414 ElfW(Addr) sym_addr = 0;
1415 const char* sym_name = nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001416
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001417 DEBUG("Processing '%s' relocation at index %zd", name, idx);
1418 if (type == 0) { // R_*_NONE
1419 continue;
1420 }
1421
1422 ElfW(Sym)* s = nullptr;
1423 soinfo* lsi = nullptr;
1424
1425 if (sym != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001426 sym_name = get_string(symtab[sym].st_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001427 s = soinfo_do_lookup(this, sym_name, &lsi, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001428 if (s == nullptr) {
1429 // We only allow an undefined symbol if this is a weak reference...
1430 s = &symtab[sym];
1431 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1432 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
1433 return -1;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001434 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001435
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001436 /* IHI0044C AAELF 4.5.1.1:
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001437
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001438 Libraries are not searched to resolve weak references.
1439 It is not an error for a weak reference to remain
1440 unsatisfied.
Doug Kwane8238072009-10-26 12:05:23 -07001441
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001442 During linking, the value of an undefined weak reference is:
1443 - Zero if the relocation type is absolute
1444 - The address of the place if the relocation is pc-relative
1445 - The address of nominal base address if the relocation
1446 type is base-relative.
1447 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001448
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001449 switch (type) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001450#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001451 case R_ARM_JUMP_SLOT:
1452 case R_ARM_GLOB_DAT:
1453 case R_ARM_ABS32:
1454 case R_ARM_RELATIVE: /* Don't care. */
1455 // sym_addr was initialized to be zero above or relocation
1456 // code below does not care about value of sym_addr.
1457 // No need to do anything.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001458 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001459#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001460 case R_386_JMP_SLOT:
1461 case R_386_GLOB_DAT:
1462 case R_386_32:
1463 case R_386_RELATIVE: /* Don't care. */
1464 case R_386_IRELATIVE:
1465 // sym_addr was initialized to be zero above or relocation
1466 // code below does not care about value of sym_addr.
1467 // No need to do anything.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001468 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001469 case R_386_PC32:
1470 sym_addr = reloc;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001471 break;
1472#endif
1473
1474#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001475 case R_ARM_COPY:
1476 // Fall through. Can't really copy if weak symbol is not found at run-time.
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001477#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001478 default:
1479 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001480 return -1;
1481 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001482 } else {
1483 // We got a definition.
1484 sym_addr = lsi->resolve_symbol_address(s);
1485 }
1486 count_relocation(kRelocSymbol);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001487 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001488
1489 switch (type) {
1490#if defined(__arm__)
1491 case R_ARM_JUMP_SLOT:
1492 count_relocation(kRelocAbsolute);
1493 MARK(rel->r_offset);
1494 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1495 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1496 break;
1497 case R_ARM_GLOB_DAT:
1498 count_relocation(kRelocAbsolute);
1499 MARK(rel->r_offset);
1500 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1501 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1502 break;
1503 case R_ARM_ABS32:
1504 count_relocation(kRelocAbsolute);
1505 MARK(rel->r_offset);
1506 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1507 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1508 break;
1509 case R_ARM_REL32:
1510 count_relocation(kRelocRelative);
1511 MARK(rel->r_offset);
1512 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1513 reloc, sym_addr, rel->r_offset, sym_name);
1514 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1515 break;
1516 case R_ARM_COPY:
1517 /*
1518 * ET_EXEC is not supported so this should not happen.
1519 *
1520 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1521 *
1522 * Section 4.7.1.10 "Dynamic relocations"
1523 * R_ARM_COPY may only appear in executable objects where e_type is
1524 * set to ET_EXEC.
1525 */
1526 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
1527 return -1;
1528#elif defined(__i386__)
1529 case R_386_JMP_SLOT:
1530 count_relocation(kRelocAbsolute);
1531 MARK(rel->r_offset);
1532 TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1533 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1534 break;
1535 case R_386_GLOB_DAT:
1536 count_relocation(kRelocAbsolute);
1537 MARK(rel->r_offset);
1538 TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1539 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1540 break;
1541 case R_386_32:
1542 count_relocation(kRelocRelative);
1543 MARK(rel->r_offset);
1544 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1545 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1546 break;
1547 case R_386_PC32:
1548 count_relocation(kRelocRelative);
1549 MARK(rel->r_offset);
1550 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1551 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1552 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1553 break;
1554#elif defined(__mips__)
1555 case R_MIPS_REL32:
1556#if defined(__LP64__)
1557 // MIPS Elf64_Rel entries contain compound relocations
1558 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
1559 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
1560 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
1561 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
1562 type, (unsigned)ELF64_R_TYPE2(rel->r_info),
1563 (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
1564 return -1;
1565 }
1566#endif
1567 count_relocation(kRelocAbsolute);
1568 MARK(rel->r_offset);
1569 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
1570 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
1571 if (s) {
1572 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1573 } else {
1574 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1575 }
1576 break;
1577#endif
1578
1579#if defined(__arm__)
1580 case R_ARM_RELATIVE:
1581#elif defined(__i386__)
1582 case R_386_RELATIVE:
1583#endif
1584 count_relocation(kRelocRelative);
1585 MARK(rel->r_offset);
1586 if (sym) {
1587 DL_ERR("odd RELATIVE form...");
1588 return -1;
1589 }
1590 TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
1591 reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1592 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
1593 break;
1594#if defined(__i386__)
1595 case R_386_IRELATIVE:
1596 count_relocation(kRelocRelative);
1597 MARK(rel->r_offset);
1598 TRACE_TYPE(RELO, "RELO IRELATIVE %p <- %p", reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
1599 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + *reinterpret_cast<ElfW(Addr)*>(reloc));
1600 break;
1601#endif
1602
1603 default:
1604 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
1605 return -1;
1606 }
1607 }
1608 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001609}
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001610#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001611
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001612#if defined(__mips__)
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001613static bool mips_relocate_got(soinfo* si, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001614 ElfW(Addr)** got = si->plt_got;
1615 if (got == nullptr) {
Brian Carlstrom87c35852013-08-20 21:05:44 -07001616 return true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001617 }
1618 unsigned local_gotno = si->mips_local_gotno;
1619 unsigned gotsym = si->mips_gotsym;
1620 unsigned symtabno = si->mips_symtabno;
1621 ElfW(Sym)* symtab = si->symtab;
1622
1623 // got[0] is the address of the lazy resolver function.
1624 // got[1] may be used for a GNU extension.
1625 // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
1626 // FIXME: maybe this should be in a separate routine?
1627 if ((si->flags & FLAG_LINKER) == 0) {
1628 size_t g = 0;
1629 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
1630 if (reinterpret_cast<intptr_t>(got[g]) < 0) {
1631 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
1632 }
1633 // Relocate the local GOT entries.
1634 for (; g < local_gotno; g++) {
1635 got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias);
1636 }
1637 }
1638
1639 // Now for the global GOT entries...
1640 ElfW(Sym)* sym = symtab + gotsym;
1641 got = si->plt_got + local_gotno;
1642 for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
1643 // This is an undefined reference... try to locate it.
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001644 const char* sym_name = si->get_string(sym->st_name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001645 soinfo* lsi = nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001646 ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi, local_group);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001647 if (s == nullptr) {
1648 // We only allow an undefined symbol if this is a weak reference.
1649 s = &symtab[g];
1650 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1651 DL_ERR("cannot locate \"%s\"...", sym_name);
1652 return false;
1653 }
1654 *got = 0;
1655 } else {
1656 // FIXME: is this sufficient?
1657 // For reference see NetBSD link loader
1658 // 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
1659 *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s));
1660 }
1661 }
1662 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001663}
1664#endif
1665
Kito Cheng812fd422014-03-25 22:53:56 +08001666void soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001667 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001668 return;
1669 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001670
Elliott Hughesc6200592013-09-30 18:43:46 -07001671 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001672
1673 int begin = reverse ? (count - 1) : 0;
1674 int end = reverse ? -1 : count;
1675 int step = reverse ? -1 : 1;
1676
1677 for (int i = begin; i != end; i += step) {
1678 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1679 CallFunction("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001680 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001681
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001682 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001683}
1684
Kito Cheng812fd422014-03-25 22:53:56 +08001685void soinfo::CallFunction(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001686 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001687 return;
1688 }
1689
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001690 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001691 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001692 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001693
1694 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1695 // are still writable. This happens with our debug malloc (see http://b/7941716).
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001696 protect_data(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001697}
1698
Elliott Hughesd23736e2012-11-01 15:16:56 -07001699void soinfo::CallPreInitConstructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001700 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1701 // but ignored in a shared library.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001702 CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1703}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001704
Elliott Hughesd23736e2012-11-01 15:16:56 -07001705void soinfo::CallConstructors() {
1706 if (constructors_called) {
1707 return;
1708 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001709
Elliott Hughesd23736e2012-11-01 15:16:56 -07001710 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1711 // protect against recursive constructor calls. One simple example of constructor recursion
1712 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1713 // 1. The program depends on libc, so libc's constructor is called here.
1714 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1715 // 3. dlopen() calls the constructors on the newly created
1716 // soinfo for libc_malloc_debug_leak.so.
1717 // 4. The debug .so depends on libc, so CallConstructors is
1718 // called again with the libc soinfo. If it doesn't trigger the early-
1719 // out above, the libc constructor will be called again (recursively!).
1720 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001721
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001722 if ((flags & FLAG_EXE) == 0 && preinit_array != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001723 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001724 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001725 name, preinit_array_count);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001726 }
1727
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001728 get_children().for_each([] (soinfo* si) {
1729 si->CallConstructors();
1730 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001731
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001732 TRACE("\"%s\": calling constructors", name);
1733
1734 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001735 CallFunction("DT_INIT", init_func);
1736 CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001737}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001738
Elliott Hughesd23736e2012-11-01 15:16:56 -07001739void soinfo::CallDestructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001740 if (!constructors_called) {
1741 return;
1742 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001743 TRACE("\"%s\": calling destructors", name);
1744
1745 // DT_FINI_ARRAY must be parsed in reverse order.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001746 CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001747
1748 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Elliott Hughesd23736e2012-11-01 15:16:56 -07001749 CallFunction("DT_FINI", fini_func);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001750
1751 // This is needed on second call to dlopen
1752 // after library has been unloaded with RTLD_NODELETE
1753 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001754}
1755
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001756void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001757 if (has_min_version(0)) {
Dmitriy Ivanovb2a30ee2014-09-04 18:23:00 -07001758 child->parents.push_back(this);
1759 this->children.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001760 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001761}
1762
1763void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001764 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001765 return;
1766 }
1767
1768 // 1. Untie connected soinfos from 'this'.
1769 children.for_each([&] (soinfo* child) {
1770 child->parents.remove_if([&] (const soinfo* parent) {
1771 return parent == this;
1772 });
1773 });
1774
1775 parents.for_each([&] (soinfo* parent) {
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001776 parent->children.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001777 return child == this;
1778 });
1779 });
1780
1781 // 2. Once everything untied - clear local lists.
1782 parents.clear();
1783 children.clear();
1784}
1785
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001786dev_t soinfo::get_st_dev() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001787 if (has_min_version(0)) {
1788 return st_dev;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001789 }
1790
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001791 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001792};
1793
1794ino_t soinfo::get_st_ino() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001795 if (has_min_version(0)) {
1796 return st_ino;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001797 }
1798
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001799 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001800}
1801
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001802off64_t soinfo::get_file_offset() {
1803 if (has_min_version(1)) {
1804 return file_offset;
1805 }
1806
1807 return 0;
1808}
1809
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001810int soinfo::get_rtld_flags() {
1811 if (has_min_version(1)) {
1812 return rtld_flags;
1813 }
1814
1815 return 0;
1816}
1817
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001818// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001819// 'this->flags' does not have FLAG_NEW_SOINFO set.
1820static soinfo::soinfo_list_t g_empty_list;
1821
1822soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001823 if (has_min_version(0)) {
1824 return this->children;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001825 }
1826
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001827 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001828}
1829
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001830soinfo::soinfo_list_t& soinfo::get_parents() {
1831 if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1832 return g_empty_list;
1833 }
1834
1835 return this->parents;
1836}
1837
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001838ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1839 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1840 return call_ifunc_resolver(s->st_value + load_bias);
1841 }
1842
1843 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1844}
1845
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001846const char* soinfo::get_string(ElfW(Word) index) const {
1847 if (has_min_version(1) && (index >= strtab_size)) {
1848 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size, index);
1849 }
1850
1851 return strtab + index;
1852}
1853
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001854bool soinfo::can_unload() const {
1855 return (rtld_flags & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
1856}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001857/* Force any of the closed stdin, stdout and stderr to be associated with
1858 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001859static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001860 int dev_null, i, status;
1861 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001862
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001863 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
1864 if (dev_null < 0) {
1865 DL_ERR("cannot open /dev/null: %s", strerror(errno));
1866 return -1;
1867 }
1868 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001869
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001870 /* If any of the stdio file descriptors is valid and not associated
1871 with /dev/null, dup /dev/null to it. */
1872 for (i = 0; i < 3; i++) {
1873 /* If it is /dev/null already, we are done. */
1874 if (i == dev_null) {
1875 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001876 }
1877
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001878 TRACE("[ Nullifying stdio file descriptor %d]", i);
1879 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
1880
1881 /* If file is opened, we are good. */
1882 if (status != -1) {
1883 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001884 }
1885
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001886 /* The only error we allow is that the file descriptor does not
1887 exist, in which case we dup /dev/null to it. */
1888 if (errno != EBADF) {
1889 DL_ERR("fcntl failed: %s", strerror(errno));
1890 return_value = -1;
1891 continue;
1892 }
1893
1894 /* Try dupping /dev/null to this stdio file descriptor and
1895 repeat if there is a signal. Note that any errors in closing
1896 the stdio descriptor are lost. */
1897 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
1898 if (status < 0) {
1899 DL_ERR("dup2 failed: %s", strerror(errno));
1900 return_value = -1;
1901 continue;
1902 }
1903 }
1904
1905 /* If /dev/null is not one of the stdio file descriptors, close it. */
1906 if (dev_null > 2) {
1907 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
1908 status = TEMP_FAILURE_RETRY(close(dev_null));
1909 if (status == -1) {
1910 DL_ERR("close failed: %s", strerror(errno));
1911 return_value = -1;
1912 }
1913 }
1914
1915 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001916}
1917
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001918bool soinfo::PrelinkImage() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08001919 /* Extract dynamic section */
1920 ElfW(Word) dynamic_flags = 0;
1921 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07001922
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001923 /* We can't log anything until the linker is relocated */
1924 bool relocating_linker = (flags & FLAG_LINKER) != 0;
1925 if (!relocating_linker) {
1926 INFO("[ linking %s ]", name);
1927 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags);
1928 }
1929
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001930 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001931 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001932 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001933 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001934 return false;
1935 } else {
1936 if (!relocating_linker) {
1937 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001938 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001939 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001940
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001941#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001942 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
1943 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001944#endif
1945
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001946 // Extract useful information from dynamic section.
1947 uint32_t needed_count = 0;
1948 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
1949 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1950 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1951 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001952 case DT_SONAME:
1953 // TODO: glibc dynamic linker uses this name for
1954 // initial library lookup; consider doing the same here.
1955 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001956
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001957 case DT_HASH:
1958 nbucket = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
1959 nchain = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
1960 bucket = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
1961 chain = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket * 4);
1962 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001963
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001964 case DT_STRTAB:
1965 strtab = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
1966 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001967
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001968 case DT_STRSZ:
1969 strtab_size = d->d_un.d_val;
1970 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001971
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001972 case DT_SYMTAB:
1973 symtab = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
1974 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001975
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001976 case DT_SYMENT:
1977 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07001978 DL_ERR("invalid DT_SYMENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001979 return false;
1980 }
1981 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001982
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001983 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07001984#if defined(USE_RELA)
1985 if (d->d_un.d_val != DT_RELA) {
1986 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001987 return false;
1988 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07001989#else
1990 if (d->d_un.d_val != DT_REL) {
1991 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
1992 return false;
1993 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001994#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07001995 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001996
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001997 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001998#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001999 plt_rela = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002000#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002001 plt_rel = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002002#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002003 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002004
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002005 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002006#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002007 plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002008#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002009 plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002010#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002011 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002012
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002013 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002014#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002015 // Used by mips and mips64.
2016 plt_got = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002017#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002018 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2019 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002020
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002021 case DT_DEBUG:
2022 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2023 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002024// FIXME: not working currently for N64
2025// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002026// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002027// read-only, but the DYNAMIC header claims it is writable.
2028#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002029 if ((dynamic_flags & PF_W) != 0) {
2030 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2031 }
2032 break;
Chris Dearman99186652014-02-06 20:36:51 -08002033#endif
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002034#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002035 case DT_RELA:
2036 rela = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
2037 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002038
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002039 case DT_RELASZ:
2040 rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
2041 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002042
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002043 case DT_RELAENT:
2044 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002045 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002046 return false;
2047 }
2048 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002049
2050 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002051 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002052 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002053
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002054 case DT_REL:
2055 DL_ERR("unsupported DT_REL in \"%s\"", name);
2056 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002057
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002058 case DT_RELSZ:
2059 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2060 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002061#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002062 case DT_REL:
2063 rel = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
2064 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002065
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002066 case DT_RELSZ:
2067 rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
2068 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002069
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002070 case DT_RELENT:
2071 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002072 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002073 return false;
2074 }
2075 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002076
2077 // "Indicates that all RELATIVE relocations have been concatenated together,
2078 // and specifies the RELATIVE relocation count."
2079 //
2080 // TODO: Spec also mentions that this can be used to optimize relocation process;
2081 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002082 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002083 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002084 case DT_RELA:
2085 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2086 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002087#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002088 case DT_INIT:
2089 init_func = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2090 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func);
2091 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002092
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002093 case DT_FINI:
2094 fini_func = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2095 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func);
2096 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002097
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002098 case DT_INIT_ARRAY:
2099 init_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2100 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array);
2101 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002102
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002103 case DT_INIT_ARRAYSZ:
2104 init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
2105 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002106
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002107 case DT_FINI_ARRAY:
2108 fini_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2109 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array);
2110 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002111
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002112 case DT_FINI_ARRAYSZ:
2113 fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
2114 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002115
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002116 case DT_PREINIT_ARRAY:
2117 preinit_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2118 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array);
2119 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002120
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002121 case DT_PREINIT_ARRAYSZ:
2122 preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
2123 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002124
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002125 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002126#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002127 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2128 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002129#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002130 has_text_relocations = true;
2131 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002132#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002133
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002134 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002135 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002136 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002137
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002138 case DT_NEEDED:
2139 ++needed_count;
2140 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002141
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002142 case DT_FLAGS:
2143 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002144#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002145 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2146 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002147#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002148 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002149#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002150 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002151 if (d->d_un.d_val & DF_SYMBOLIC) {
2152 has_DT_SYMBOLIC = true;
2153 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002154 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002155
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002156 case DT_FLAGS_1:
2157 if ((d->d_un.d_val & DF_1_GLOBAL) != 0) {
2158 rtld_flags |= RTLD_GLOBAL;
2159 }
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002160
2161 if ((d->d_un.d_val & DF_1_NODELETE) != 0) {
2162 rtld_flags |= RTLD_NODELETE;
2163 }
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002164 // TODO: Implement other flags
2165
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002166 if ((d->d_un.d_val & ~(DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE)) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002167 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2168 }
2169 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002170#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002171 case DT_MIPS_RLD_MAP:
2172 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2173 {
2174 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2175 *dp = &_r_debug;
2176 }
2177 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002178
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002179 case DT_MIPS_RLD_VERSION:
2180 case DT_MIPS_FLAGS:
2181 case DT_MIPS_BASE_ADDRESS:
2182 case DT_MIPS_UNREFEXTNO:
2183 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002184
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002185 case DT_MIPS_SYMTABNO:
2186 mips_symtabno = d->d_un.d_val;
2187 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002188
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002189 case DT_MIPS_LOCAL_GOTNO:
2190 mips_local_gotno = d->d_un.d_val;
2191 break;
2192
2193 case DT_MIPS_GOTSYM:
2194 mips_gotsym = d->d_un.d_val;
2195 break;
2196#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002197 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2198 case DT_BIND_NOW:
2199 break;
2200
2201 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002202 case DT_VERSYM:
2203 case DT_VERDEF:
2204 case DT_VERDEFNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002205 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002206
2207 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002208 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002209 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002210 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2211 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002212 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002213 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002214 }
2215
2216 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
2217 reinterpret_cast<void*>(base), strtab, symtab);
2218
2219 // Sanity checks.
2220 if (relocating_linker && needed_count != 0) {
2221 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2222 return false;
2223 }
2224 if (nbucket == 0) {
2225 DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", name);
2226 return false;
2227 }
2228 if (strtab == 0) {
2229 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2230 return false;
2231 }
2232 if (symtab == 0) {
2233 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2234 return false;
2235 }
2236 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002237}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002238
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002239bool soinfo::LinkImage(const soinfo_list_t& local_group, const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002240
Elliott Hughese4d792a2013-10-28 14:19:05 -07002241#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002242 if (has_text_relocations) {
2243 // Make segments writable to allow text relocations to work properly. We will later call
2244 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2245 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2246 "security hardening. Please fix.", name);
2247 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2248 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2249 name, strerror(errno));
2250 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002251 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002252 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002253#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002254
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002255#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002256 if (rela != nullptr) {
2257 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002258 if (Relocate(rela, rela_count, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002259 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002260 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002261 }
2262 if (plt_rela != nullptr) {
2263 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002264 if (Relocate(plt_rela, plt_rela_count, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002265 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002266 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002267 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002268#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002269 if (rel != nullptr) {
2270 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002271 if (Relocate(rel, rel_count, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002272 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002273 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002274 }
2275 if (plt_rel != nullptr) {
2276 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002277 if (Relocate(plt_rel, plt_rel_count, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002278 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002279 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002280 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002281#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002282
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002283#if defined(__mips__)
Dmitriy Ivanov90b74fb2014-10-23 14:34:12 -07002284 if (!mips_relocate_got(this, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002285 return false;
2286 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002287#endif
2288
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002289 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002290
Elliott Hughese4d792a2013-10-28 14:19:05 -07002291#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002292 if (has_text_relocations) {
2293 // All relocations are done, we can protect our segments back to read-only.
2294 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2295 DL_ERR("can't protect segments for \"%s\": %s",
2296 name, strerror(errno));
2297 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002298 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002299 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002300#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002301
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002302 /* We can also turn on GNU RELRO protection */
2303 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2304 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2305 name, strerror(errno));
2306 return false;
2307 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002308
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002309 /* Handle serializing/sharing the RELRO segment */
2310 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2311 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2312 extinfo->relro_fd) < 0) {
2313 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2314 name, strerror(errno));
2315 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002316 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002317 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2318 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2319 extinfo->relro_fd) < 0) {
2320 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2321 name, strerror(errno));
2322 return false;
2323 }
2324 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002325
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002326 notify_gdb_of_load(this);
2327 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002328}
2329
Nick Kralevich468319c2011-11-11 15:53:17 -08002330/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002331 * This function add vdso to internal dso list.
2332 * It helps to stack unwinding through signal handlers.
2333 * Also, it makes bionic more like glibc.
2334 */
Kito Cheng812fd422014-03-25 22:53:56 +08002335static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002336#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002337 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002338 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002339 return;
2340 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002341
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002342 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002343
Elliott Hughes0266ae52014-02-10 17:46:57 -08002344 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2345 si->phnum = ehdr_vdso->e_phnum;
2346 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2347 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002348 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002349
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002350 si->PrelinkImage();
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002351 si->LinkImage(g_empty_list, nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002352#endif
2353}
2354
2355/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002356 * This is linker soinfo for GDB. See details below.
2357 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002358#if defined(__LP64__)
2359#define LINKER_PATH "/system/bin/linker64"
2360#else
2361#define LINKER_PATH "/system/bin/linker"
2362#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002363static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002364
2365/* gdb expects the linker to be in the debug shared object list.
2366 * Without this, gdb has trouble locating the linker's ".text"
2367 * and ".plt" sections. Gdb could also potentially use this to
2368 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2369 * Don't use soinfo_alloc(), because the linker shouldn't
2370 * be on the soinfo list.
2371 */
2372static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002373 linker_soinfo_for_gdb.base = linker_base;
2374
2375 /*
2376 * Set the dynamic field in the link map otherwise gdb will complain with
2377 * the following:
2378 * warning: .dynamic section for "/system/bin/linker" is not at the
2379 * expected address (wrong library or version mismatch?)
2380 */
2381 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2382 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2383 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002384 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002385 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2386}
2387
2388/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002389 * This code is called after the linker has linked itself and
2390 * fixed it's own GOT. It is safe to make references to externs
2391 * and other non-local data at this point.
2392 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002393static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002394#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002395 struct timeval t0, t1;
2396 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002397#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002398
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002399 // Initialize environment functions, and get to the ELF aux vectors table.
2400 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002401
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002402 // If this is a setuid/setgid program, close the security hole described in
2403 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2404 if (get_AT_SECURE()) {
2405 nullify_closed_stdio();
2406 }
2407
2408 debuggerd_init();
2409
2410 // Get a few environment variables.
2411 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2412 if (LD_DEBUG != nullptr) {
2413 g_ld_debug_verbosity = atoi(LD_DEBUG);
2414 }
2415
2416 // Normally, these are cleaned by linker_env_init, but the test
2417 // doesn't cost us anything.
2418 const char* ldpath_env = nullptr;
2419 const char* ldpreload_env = nullptr;
2420 if (!get_AT_SECURE()) {
2421 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2422 ldpreload_env = linker_env_get("LD_PRELOAD");
2423 }
2424
2425 INFO("[ android linker & debugger ]");
2426
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002427 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002428 if (si == nullptr) {
2429 exit(EXIT_FAILURE);
2430 }
2431
2432 /* bootstrap the link map, the main exe always needs to be first */
2433 si->flags |= FLAG_EXE;
2434 link_map* map = &(si->link_map_head);
2435
2436 map->l_addr = 0;
2437 map->l_name = args.argv[0];
2438 map->l_prev = nullptr;
2439 map->l_next = nullptr;
2440
2441 _r_debug.r_map = map;
2442 r_debug_tail = map;
2443
2444 init_linker_info_for_gdb(linker_base);
2445
2446 // Extract information passed from the kernel.
2447 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2448 si->phnum = args.getauxval(AT_PHNUM);
2449 si->entry = args.getauxval(AT_ENTRY);
2450
2451 /* Compute the value of si->base. We can't rely on the fact that
2452 * the first entry is the PHDR because this will not be true
2453 * for certain executables (e.g. some in the NDK unit test suite)
2454 */
2455 si->base = 0;
2456 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2457 si->load_bias = 0;
2458 for (size_t i = 0; i < si->phnum; ++i) {
2459 if (si->phdr[i].p_type == PT_PHDR) {
2460 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2461 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2462 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002463 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002464 }
2465 si->dynamic = nullptr;
2466 si->ref_count = 1;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002467
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002468 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2469 if (elf_hdr->e_type != ET_DYN) {
2470 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2471 exit(EXIT_FAILURE);
2472 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002473
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002474 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2475 parse_LD_LIBRARY_PATH(ldpath_env);
2476 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002477
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002478 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002479
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002480 si->PrelinkImage();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002481
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002482 // Load ld_preloads and dependencies.
2483 StringLinkedList needed_library_name_list;
2484 size_t needed_libraries_count = 0;
2485 size_t ld_preloads_count = 0;
2486 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2487 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2488 ++needed_libraries_count;
2489 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002490
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002491 for_each_dt_needed(si, [&](const char* name) {
2492 needed_library_name_list.push_back(name);
2493 ++needed_libraries_count;
2494 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002495
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002496 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002497
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002498 memset(needed_library_names, 0, sizeof(needed_library_names));
2499 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002500
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002501 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 -07002502 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2503 exit(EXIT_FAILURE);
2504 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002505
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002506 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002507
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002508 si->CallPreInitConstructors();
Matt Fischer4fd42c12009-12-31 12:09:10 -06002509
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002510 /* After the PrelinkImage, the si->load_bias is initialized.
2511 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2512 * We need to update this value for so exe here. So Unwind_Backtrace
2513 * for some arch like x86 could work correctly within so exe.
2514 */
2515 map->l_addr = si->load_bias;
2516 si->CallConstructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002517
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002518#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002519 gettimeofday(&t1, nullptr);
2520 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2521 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2522 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002523#endif
2524#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002525 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2526 linker_stats.count[kRelocAbsolute],
2527 linker_stats.count[kRelocRelative],
2528 linker_stats.count[kRelocCopy],
2529 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002530#endif
2531#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002532 {
2533 unsigned n;
2534 unsigned i;
2535 unsigned count = 0;
2536 for (n = 0; n < 4096; n++) {
2537 if (bitmask[n]) {
2538 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002539#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002540 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002541#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002542 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002543#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002544 if (x & 1) {
2545 count++;
2546 }
2547 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002548 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002549 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002550 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002551 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2552 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002553#endif
2554
2555#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002556 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002557#endif
2558
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002559 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2560 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002561}
Nick Kralevich468319c2011-11-11 15:53:17 -08002562
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002563/* Compute the load-bias of an existing executable. This shall only
2564 * be used to compute the load bias of an executable or shared library
2565 * that was loaded by the kernel itself.
2566 *
2567 * Input:
2568 * elf -> address of ELF header, assumed to be at the start of the file.
2569 * Return:
2570 * load bias, i.e. add the value of any p_vaddr in the file to get
2571 * the corresponding address in memory.
2572 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002573static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2574 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002575 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002576 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002577
Elliott Hughes0266ae52014-02-10 17:46:57 -08002578 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002579 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002580 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002581 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002582 }
2583 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002584}
2585
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002586extern "C" void _start();
2587
Nick Kralevich468319c2011-11-11 15:53:17 -08002588/*
2589 * This is the entry point for the linker, called from begin.S. This
2590 * method is responsible for fixing the linker's own relocations, and
2591 * then calling __linker_init_post_relocation().
2592 *
2593 * Because this method is called before the linker has fixed it's own
2594 * relocations, any attempt to reference an extern variable, extern
2595 * function, or other GOT reference will generate a segfault.
2596 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002597extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002598 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002599
Elliott Hughes0266ae52014-02-10 17:46:57 -08002600 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002601 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002602 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002603 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002604
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002605 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002606
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002607 // If the linker is not acting as PT_INTERP entry_point is equal to
2608 // _start. Which means that the linker is running as an executable and
2609 // already linked by PT_INTERP.
2610 //
2611 // This happens when user tries to run 'adb shell /system/bin/linker'
2612 // see also https://code.google.com/p/android/issues/detail?id=63174
2613 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2614 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2615 }
2616
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002617 linker_so.base = linker_addr;
2618 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2619 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002620 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002621 linker_so.phdr = phdr;
2622 linker_so.phnum = elf_hdr->e_phnum;
2623 linker_so.flags |= FLAG_LINKER;
Elliott Hughes5419b942012-10-16 15:54:46 -07002624
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002625 if (!(linker_so.PrelinkImage() && linker_so.LinkImage(g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002626 // It would be nice to print an error message, but if the linker
2627 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002628 // call write() (because it involves a GOT reference). We may as
2629 // well try though...
2630 const char* msg = "CANNOT LINK EXECUTABLE: ";
2631 write(2, msg, strlen(msg));
2632 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2633 write(2, "\n", 1);
2634 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002635 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002636
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002637 __libc_init_tls(args);
2638
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002639 // Initialize the linker's own global variables
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002640 linker_so.CallConstructors();
2641
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002642 // Initialize static variables. Note that in order to
2643 // get correct libdl_info we need to call constructors
2644 // before get_libdl_info().
2645 solist = get_libdl_info();
2646 sonext = get_libdl_info();
2647
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002648 // We have successfully fixed our own relocations. It's safe to run
2649 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002650 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002651 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002652
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002653 protect_data(PROT_READ);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002654
2655 // Return the address that the calling assembly stub should jump to.
2656 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002657}