blob: df6a4e2e16e71e60253180e695a3f1209aaaeaf6 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Doug Kwan94304352009-10-23 18:11:40 -07002 * Copyright (C) 2008, 2009 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughes46882792012-08-03 16:49:39 -070029#include <dlfcn.h>
30#include <errno.h>
31#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080032#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070037#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080038#include <sys/param.h>
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -080039#include <sys/personality.h>
Elliott Hughes46882792012-08-03 16:49:39 -070040#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070042#include <new>
43
Elliott Hughes46882792012-08-03 16:49:39 -070044// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070045#include "private/bionic_tls.h"
46#include "private/KernelArgumentBlock.h"
47#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070048#include "private/ScopedFd.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070049#include "private/ScopeGuard.h"
50#include "private/UniquePtr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051
52#include "linker.h"
53#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010054#include "linker_environ.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020055#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080056#include "linker_relocs.h"
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070057#include "linker_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080058
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
60 *
61 * Do NOT use malloc() and friends or pthread_*() code here.
62 * Don't use printf() either; it's caused mysterious memory
63 * corruption in the past.
64 * The linker runs before we bring up libc and it's easiest
65 * to make sure it does not depend on any complex libc features
66 *
67 * open issues / todo:
68 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070 * - after linking, set as much stuff as possible to READONLY
71 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070072 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080074// Override macros to use C++ style casts
75#undef ELF_ST_TYPE
76#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
77
Dmitriy Ivanov489e4982014-05-19 15:19:52 -070078#if defined(__LP64__)
79#define SEARCH_NAME(x) x
80#else
81// Nvidia drivers are relying on the bug:
82// http://code.google.com/p/android/issues/detail?id=6670
83// so we continue to use base-name lookup for lp32
84static const char* get_base_name(const char* name) {
85 const char* bname = strrchr(name, '/');
86 return bname ? bname + 1 : name;
87}
88#define SEARCH_NAME(x) get_base_name(x)
89#endif
90
Elliott Hughes0266ae52014-02-10 17:46:57 -080091static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092
Elliott Hughes1728b232014-05-14 10:02:03 -070093static LinkerAllocator<soinfo> g_soinfo_allocator;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070094static LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020095
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070096static soinfo* solist;
97static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -070098static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099
Elliott Hughes1728b232014-05-14 10:02:03 -0700100static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700101#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700102 "/vendor/lib64",
103 "/system/lib64",
104#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700105 "/vendor/lib",
106 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700107#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700108 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700109};
David Bartleybc3a5c22009-06-02 18:27:28 -0700110
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800111#define LDPATH_BUFSIZE (LDPATH_MAX*64)
112#define LDPATH_MAX 8
113
114#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
115#define LDPRELOAD_MAX 8
116
Elliott Hughes1728b232014-05-14 10:02:03 -0700117static char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
118static const char* g_ld_library_paths[LDPATH_MAX + 1];
Elliott Hughes124fae92012-10-31 14:20:03 -0700119
Elliott Hughes1728b232014-05-14 10:02:03 -0700120static char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
121static const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600122
Elliott Hughes1728b232014-05-14 10:02:03 -0700123static soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600124
Elliott Hughes1728b232014-05-14 10:02:03 -0700125__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700127__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700128
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700130struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700131 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700132};
133
134static linker_stats_t linker_stats;
135
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800136void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700137 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700138}
139#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800140void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700141}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142#endif
143
144#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800145uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146#endif
147
Elliott Hughes46882792012-08-03 16:49:39 -0700148// You shouldn't try to call memory-allocating functions in the dynamic linker.
149// Guard against the most obvious ones.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700150#define DISALLOW_ALLOCATION(return_type, name, ...) \
151 return_type name __VA_ARGS__ \
152 { \
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700153 __libc_fatal("ERROR: " #name " called from the dynamic linker!\n"); \
Dima Zavin2e855792009-05-20 18:28:09 -0700154 }
Kito Cheng812fd422014-03-25 22:53:56 +0800155DISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
156DISALLOW_ALLOCATION(void, free, (void* u __unused));
157DISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
158DISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
Dima Zavin2e855792009-05-20 18:28:09 -0700159
160static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700161
Elliott Hughes650be4e2013-03-05 18:47:58 -0800162char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700163 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700164}
165
Elliott Hughes650be4e2013-03-05 18:47:58 -0800166size_t linker_get_error_buffer_size() {
167 return sizeof(__linker_dl_err_buf);
168}
169
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700170// This function is an empty stub where GDB locates a breakpoint to get notified
171// about linker activity.
Elliott Hughes5419b942012-10-16 15:54:46 -0700172extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800173
Elliott Hughes1728b232014-05-14 10:02:03 -0700174static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700175static 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 -0800176static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800177
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800178static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700179 // Copy the necessary fields into the debug structure.
180 link_map* map = &(info->link_map_head);
181 map->l_addr = info->load_bias;
Dmitriy Ivanovc9d16582014-10-24 14:46:12 -0700182 map->l_name = info->name;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700183 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700185 // Stick the new library at the end of the list.
186 // gdb tends to care more about libc than it does
187 // about leaf libraries, and ordering it this way
188 // reduces the back-and-forth over the wire.
189 if (r_debug_tail) {
190 r_debug_tail->l_next = map;
191 map->l_prev = r_debug_tail;
192 map->l_next = 0;
193 } else {
194 _r_debug.r_map = map;
195 map->l_prev = 0;
196 map->l_next = 0;
197 }
198 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199}
200
Elliott Hughesbedfe382012-08-14 14:07:59 -0700201static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700202 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700203
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700204 if (r_debug_tail == map) {
205 r_debug_tail = map->l_prev;
206 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700207
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700208 if (map->l_prev) {
209 map->l_prev->l_next = map->l_next;
210 }
211 if (map->l_next) {
212 map->l_next->l_prev = map->l_prev;
213 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700214}
215
Elliott Hughesbedfe382012-08-14 14:07:59 -0700216static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800217 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700218 // GDB already knows about the main executable
219 return;
220 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700222 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700224 _r_debug.r_state = r_debug::RT_ADD;
225 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700227 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700229 _r_debug.r_state = r_debug::RT_CONSISTENT;
230 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700231}
232
Elliott Hughesbedfe382012-08-14 14:07:59 -0700233static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800234 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700235 // GDB already knows about the main executable
236 return;
237 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700238
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700239 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700240
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700241 _r_debug.r_state = r_debug::RT_DELETE;
242 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700243
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700244 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700245
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700246 _r_debug.r_state = r_debug::RT_CONSISTENT;
247 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248}
249
Elliott Hughes18a206c2012-10-29 17:37:13 -0700250void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800251 _r_debug.r_state = r_debug::RT_ADD;
252 rtld_db_dlactivity();
253 _r_debug.r_state = r_debug::RT_CONSISTENT;
254 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255}
256
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700257LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
258 return g_soinfo_links_allocator.alloc();
259}
260
261void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
262 g_soinfo_links_allocator.free(entry);
263}
264
265static void protect_data(int protection) {
266 g_soinfo_allocator.protect_all(protection);
267 g_soinfo_links_allocator.protect_all(protection);
268}
269
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700270static soinfo* soinfo_alloc(const char* name, struct stat* file_stat, off64_t file_offset, uint32_t rtld_flags) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200271 if (strlen(name) >= SOINFO_NAME_LEN) {
272 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700273 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200274 }
275
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700276 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700277
Magnus Malmbornba98d922012-09-12 13:00:55 +0200278 sonext->next = si;
279 sonext = si;
280
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700281 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200282 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283}
284
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800285static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700286 if (si == nullptr) {
287 return;
288 }
289
290 if (si->base != 0 && si->size != 0) {
291 munmap(reinterpret_cast<void*>(si->base), si->size);
292 }
293
294 soinfo *prev = nullptr, *trav;
295
296 TRACE("name %s: freeing soinfo @ %p", si->name, si);
297
298 for (trav = solist; trav != nullptr; trav = trav->next) {
299 if (trav == si) {
300 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700301 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700302 prev = trav;
303 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800304
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700305 if (trav == nullptr) {
306 // si was not in solist
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -0800307 DL_ERR("name \"%s\"@%p is not in solist!", si->name, si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700308 return;
309 }
Elliott Hughes46882792012-08-03 16:49:39 -0700310
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700311 // clear links to/from si
312 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700313
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700314 // prev will never be null, because the first entry in solist is
315 // always the static libdl_info.
316 prev->next = si->next;
317 if (si == sonext) {
318 sonext = prev;
319 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800320
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700321 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800322}
323
Elliott Hughescade4c32012-12-20 14:42:14 -0800324static void parse_path(const char* path, const char* delimiters,
325 const char** array, char* buf, size_t buf_size, size_t max_count) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700326 if (path == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800327 return;
328 }
329
330 size_t len = strlcpy(buf, path, buf_size);
331
332 size_t i = 0;
333 char* buf_p = buf;
334 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
335 if (*array[i] != '\0') {
336 ++i;
337 }
338 }
339
340 // Forget the last path if we had to truncate; this occurs if the 2nd to
341 // last char isn't '\0' (i.e. wasn't originally a delimiter).
342 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700343 array[i - 1] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800344 } else {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700345 array[i] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800346 }
347}
348
349static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700350 parse_path(path, ":", g_ld_library_paths,
351 g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800352}
353
354static void parse_LD_PRELOAD(const char* path) {
355 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes1728b232014-05-14 10:02:03 -0700356 parse_path(path, " :", g_ld_preload_names,
357 g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800358}
359
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700360#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700361
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700362// For a given PC, find the .so that it belongs to.
363// Returns the base address of the .ARM.exidx section
364// for that .so, and the number of 8-byte entries
365// in that section (via *pcount).
366//
367// Intended to be called by libc's __gnu_Unwind_Find_exidx().
368//
369// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800370_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800371 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800372
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700373 for (soinfo* si = solist; si != 0; si = si->next) {
374 if ((addr >= si->base) && (addr < (si->base + si->size))) {
375 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800376 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800377 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700378 }
379 *pcount = 0;
380 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800381}
Elliott Hughes46882792012-08-03 16:49:39 -0700382
Christopher Ferris24053a42013-08-19 17:45:09 -0700383#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700384
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700385// Here, we only have to provide a callback to iterate across all the
386// loaded libraries. gcc_eh does the rest.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800387int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700388 int rv = 0;
389 for (soinfo* si = solist; si != nullptr; si = si->next) {
390 dl_phdr_info dl_info;
391 dl_info.dlpi_addr = si->link_map_head.l_addr;
392 dl_info.dlpi_name = si->link_map_head.l_name;
393 dl_info.dlpi_phdr = si->phdr;
394 dl_info.dlpi_phnum = si->phnum;
395 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
396 if (rv != 0) {
397 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700399 }
400 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800401}
Elliott Hughes46882792012-08-03 16:49:39 -0700402
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800403ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name) {
404 return is_gnu_hash() ? gnu_lookup(symbol_name) : elf_lookup(symbol_name);
405}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800407static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
408 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
409 ELF_ST_BIND(s->st_info) == STB_WEAK) {
410 return s->st_shndx != SHN_UNDEF;
411 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
412 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
413 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->name);
414 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800415
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800416 return false;
417}
418
419ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name) {
420 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800421 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800422
423 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800424 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
425 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800426
427 // test against bloom filter
428 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
429 return nullptr;
430 }
431
432 // bloom test says "probably yes"...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800433 uint32_t n = bucket_[hash % nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800434
435 if (n == 0) {
436 return nullptr;
437 }
438
439 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800440 ElfW(Sym)* s = symtab_ + n;
441 if (((chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800442 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
443 is_symbol_global_and_defined(this, s)) {
444 return s;
445 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800446 } while ((chain_[n++] & 1) == 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800447
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800448 return nullptr;
449}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800450
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800451ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name) {
452 uint32_t hash = symbol_name.elf_hash();
453
454 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800455 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800456
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800457 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
458 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800459 if (strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && is_symbol_global_and_defined(this, s)) {
460 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
461 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
462 static_cast<size_t>(s->st_size));
463 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800464 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800465 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800466
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700467 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800468 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700469
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700470 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800471}
472
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700473soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700474 memset(this, 0, sizeof(*this));
475
476 strlcpy(this->name, name, sizeof(this->name));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800477 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800478 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700479
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700480 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800481 this->st_dev_ = file_stat->st_dev;
482 this->st_ino_ = file_stat->st_ino;
483 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700484 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700485
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800486 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700487}
488
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800489
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800490uint32_t SymbolName::elf_hash() {
491 if (!has_elf_hash_) {
492 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
493 uint32_t h = 0, g;
494
495 while (*name) {
496 h = (h << 4) + *name++;
497 g = h & 0xf0000000;
498 h ^= g;
499 h ^= g >> 24;
500 }
501
502 elf_hash_ = h;
503 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700504 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800505
506 return elf_hash_;
507}
508
509uint32_t SymbolName::gnu_hash() {
510 if (!has_gnu_hash_) {
511 uint32_t h = 5381;
512 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
513 while (*name != 0) {
514 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
515 }
516
517 gnu_hash_ = h;
518 has_gnu_hash_ = true;
519 }
520
521 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800522}
523
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800524ElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700525 const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800526 SymbolName symbol_name(name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700527 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700528
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700529 /* "This element's presence in a shared object library alters the dynamic linker's
530 * symbol resolution algorithm for references within the library. Instead of starting
531 * a symbol search with the executable file, the dynamic linker starts from the shared
532 * object itself. If the shared object fails to supply the referenced symbol, the
533 * dynamic linker then searches the executable file and other shared objects as usual."
534 *
535 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
536 *
537 * Note that this is unlikely since static linker avoids generating
538 * relocations for -Bsymbolic linked dynamic executables.
539 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700540 if (si_from->has_DT_SYMBOLIC) {
541 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->name, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800542 s = si_from->find_symbol_by_name(symbol_name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700543 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700544 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700545 }
546 }
547
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700548 // 1. Look for it in global_group
549 if (s == nullptr) {
550 global_group.visit([&](soinfo* global_si) {
551 DEBUG("%s: looking up %s in %s (from global group)", si_from->name, name, global_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800552 s = global_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700553 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700554 *si_found_in = global_si;
555 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700556 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700557
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700558 return true;
559 });
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700560 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700561
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700562 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700563 if (s == nullptr) {
564 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700565 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700566 // we already did this - skip
567 return true;
568 }
569
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700570 DEBUG("%s: looking up %s in %s (from local group)", si_from->name, name, local_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800571 s = local_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700572 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700573 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700574 return false;
575 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700576
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700577 return true;
578 });
579 }
580
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700581 if (s != nullptr) {
582 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
583 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700584 si_from->name, name, reinterpret_cast<void*>(s->st_value),
585 (*si_found_in)->name, reinterpret_cast<void*>((*si_found_in)->base),
586 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700587 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700588
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700589 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700590}
591
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700592// Each size has it's own allocator.
593template<size_t size>
594class SizeBasedAllocator {
595 public:
596 static void* alloc() {
597 return allocator_.alloc();
598 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700599
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700600 static void free(void* ptr) {
601 allocator_.free(ptr);
602 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700603
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700604 private:
605 static LinkerBlockAllocator allocator_;
606};
607
608template<size_t size>
609LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
610
611template<typename T>
612class TypeBasedAllocator {
613 public:
614 static T* alloc() {
615 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
616 }
617
618 static void free(T* ptr) {
619 SizeBasedAllocator<sizeof(T)>::free(ptr);
620 }
621};
622
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700623class LoadTask {
624 public:
625 struct deleter_t {
626 void operator()(LoadTask* t) {
627 TypeBasedAllocator<LoadTask>::free(t);
628 }
629 };
630
631 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
632
633 static deleter_t deleter;
634
635 static LoadTask* create(const char* name, soinfo* needed_by) {
636 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
637 return new (ptr) LoadTask(name, needed_by);
638 }
639
640 const char* get_name() const {
641 return name_;
642 }
643
644 soinfo* get_needed_by() const {
645 return needed_by_;
646 }
647 private:
648 LoadTask(const char* name, soinfo* needed_by)
649 : name_(name), needed_by_(needed_by) {}
650
651 const char* name_;
652 soinfo* needed_by_;
653
654 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
655};
656
Ningsheng Jiane93be992014-09-16 15:22:10 +0800657LoadTask::deleter_t LoadTask::deleter;
658
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700659template <typename T>
660using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
661
662typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700663typedef linked_list_t<const char> StringLinkedList;
664typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700665
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700666
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700667// This function walks down the tree of soinfo dependencies
668// in breadth-first order and
669// * calls action(soinfo* si) for each node, and
670// * terminates walk if action returns false.
671//
672// walk_dependencies_tree returns false if walk was terminated
673// by the action and true otherwise.
674template<typename F>
675static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700676 SoinfoLinkedList visit_list;
677 SoinfoLinkedList visited;
678
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700679 for (size_t i = 0; i < root_soinfos_size; ++i) {
680 visit_list.push_back(root_soinfos[i]);
681 }
682
683 soinfo* si;
684 while ((si = visit_list.pop_front()) != nullptr) {
685 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700686 continue;
687 }
688
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700689 if (!action(si)) {
690 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700691 }
692
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700693 visited.push_back(si);
694
695 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700696 visit_list.push_back(child);
697 });
698 }
699
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700700 return true;
701}
702
703
704// This is used by dlsym(3). It performs symbol lookup only within the
705// specified soinfo object and its dependencies in breadth first order.
706ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
707 ElfW(Sym)* result = nullptr;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800708 SymbolName symbol_name(name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700709
710
711 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800712 result = current_soinfo->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700713 if (result != nullptr) {
714 *found = current_soinfo;
715 return false;
716 }
717
718 return true;
719 });
720
721 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800722}
723
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800724/* This is used by dlsym(3) to performs a global symbol lookup. If the
725 start value is null (for RTLD_DEFAULT), the search starts at the
726 beginning of the global solist. Otherwise the search starts at the
727 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700728 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700729ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800730 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800731
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700732 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800733 start = solist;
734 }
735
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700736 ElfW(Sym)* s = nullptr;
737 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700738 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
739 continue;
740 }
741
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800742 s = si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700743 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800744 *found = si;
745 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600746 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800747 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600748
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700749 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700750 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
751 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800752 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800753
Elliott Hughescade4c32012-12-20 14:42:14 -0800754 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800755}
756
Kito Chengfa8c05d2013-03-12 14:58:06 +0800757soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800758 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700759 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800760 if (address >= si->base && address - si->base < si->size) {
761 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600762 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800763 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700764 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600765}
766
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800767ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
768 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
769}
770
771static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
772 return sym->st_shndx != SHN_UNDEF &&
773 soaddr >= sym->st_value &&
774 soaddr < sym->st_value + sym->st_size;
775}
776
777ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
778 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - base;
779
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800780 for (size_t i = 0; i < nbucket_; ++i) {
781 uint32_t n = bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800782
783 if (n == 0) {
784 continue;
785 }
786
787 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800788 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800789 if (symbol_matches_soaddr(sym, soaddr)) {
790 return sym;
791 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800792 } while ((chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800793 }
794
795 return nullptr;
796}
797
798ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
799 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - base;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600800
Kito Chengfa8c05d2013-03-12 14:58:06 +0800801 // Search the library's symbol table for any defined symbol which
802 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800803 for (size_t i = 0; i < nchain_; ++i) {
804 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800805 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800806 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600807 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800808 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600809
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700810 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600811}
812
Elliott Hughes124fae92012-10-31 14:20:03 -0700813static int open_library_on_path(const char* name, const char* const paths[]) {
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +0000814 char buf[512];
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700815 for (size_t i = 0; paths[i] != nullptr; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800816 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700817 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700818 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700819 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800820 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700821 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
822 if (fd != -1) {
823 return fd;
824 }
825 }
826 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800827}
828
Elliott Hughes124fae92012-10-31 14:20:03 -0700829static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700830 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800831
Elliott Hughes124fae92012-10-31 14:20:03 -0700832 // 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 -0700833 if (strchr(name, '/') != nullptr) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700834 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
835 if (fd != -1) {
836 return fd;
837 }
838 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700839#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700840 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700841#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700842 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800843
Elliott Hughes124fae92012-10-31 14:20:03 -0700844 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Elliott Hughes1728b232014-05-14 10:02:03 -0700845 int fd = open_library_on_path(name, g_ld_library_paths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700846 if (fd == -1) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700847 fd = open_library_on_path(name, kDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700848 }
849 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800850}
851
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700852template<typename F>
853static void for_each_dt_needed(const soinfo* si, F action) {
854 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
855 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700856 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700857 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700858 }
859}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800860
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700861static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700862 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700863 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700864 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700865
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700866 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
867 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700868 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
869 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700870 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700871 } else {
872 // Open the file.
873 fd = open_library(name);
874 if (fd == -1) {
875 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700876 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700877 }
878
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700879 file_guard.reset(fd);
880 }
881
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700882 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700883 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700884 return nullptr;
885 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800886 if (file_offset < 0) {
887 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
888 return nullptr;
889 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700890
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700891 struct stat file_stat;
892 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700893 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700894 return nullptr;
895 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800896 if (file_offset >= file_stat.st_size) {
897 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64, name, file_offset, file_stat.st_size);
898 return nullptr;
899 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700900
901 // Check for symlink and other situations where
902 // file can have different names.
903 for (soinfo* si = solist; si != nullptr; si = si->next) {
904 if (si->get_st_dev() != 0 &&
905 si->get_st_ino() != 0 &&
906 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700907 si->get_st_ino() == file_stat.st_ino &&
908 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700909 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
910 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700911 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700912 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700913
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700914 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700915 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700916 return nullptr;
917 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700918
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700919 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700920 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700921 if (!elf_reader.Load(extinfo)) {
922 return nullptr;
923 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800924
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700925 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700926 if (si == nullptr) {
927 return nullptr;
928 }
929 si->base = elf_reader.load_start();
930 si->size = elf_reader.load_size();
931 si->load_bias = elf_reader.load_bias();
932 si->phnum = elf_reader.phdr_count();
933 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700934
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800935 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700936 soinfo_free(si);
937 return nullptr;
938 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700939
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700940 for_each_dt_needed(si, [&] (const char* name) {
941 load_tasks.push_back(LoadTask::create(name, si));
942 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700943
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700944 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800945}
946
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700947static soinfo *find_loaded_library_by_name(const char* name) {
948 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700949 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700950 if (!strcmp(search_name, si->name)) {
951 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200952 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700953 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700954 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200955}
956
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700957static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200958
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700959 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700960
961 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700962 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700963 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700964 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700965 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700966 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800967
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700968 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800969}
970
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700971static void soinfo_unload(soinfo* si);
972
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700973// TODO: this is slightly unusual way to construct
974// the global group for relocation. Not every RTLD_GLOBAL
975// library is included in this group for backwards-compatibility
976// reasons.
977//
978// This group consists of the main executable, LD_PRELOADs
979// and libraries with the DF_1_GLOBAL flag set.
980static soinfo::soinfo_list_t make_global_group() {
981 soinfo::soinfo_list_t global_group;
982 for (soinfo* si = somain; si != nullptr; si = si->next) {
983 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
984 global_group.push_back(si);
985 }
986 }
987
988 return global_group;
989}
990
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700991static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
992 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700993 // Step 0: prepare.
994 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700995 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700996 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700997 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700998 }
999
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001000 // Construct global_group.
1001 soinfo::soinfo_list_t global_group = make_global_group();
1002
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001003 // If soinfos array is null allocate one on stack.
1004 // The array is needed in case of failure; for example
1005 // when library_names[] = {libone.so, libtwo.so} and libone.so
1006 // is loaded correctly but libtwo.so failed for some reason.
1007 // In this case libone.so should be unloaded on return.
1008 // See also implementation of failure_guard below.
1009
1010 if (soinfos == nullptr) {
1011 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1012 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1013 memset(soinfos, 0, soinfos_size);
1014 }
1015
1016 // list of libraries to link - see step 2.
1017 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001018
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001019 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001020 // Housekeeping
1021 load_tasks.for_each([] (LoadTask* t) {
1022 LoadTask::deleter(t);
1023 });
1024
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001025 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001026 soinfo_unload(soinfos[i]);
1027 }
1028 });
1029
1030 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
1031 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 -07001032 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001033 if (si == nullptr) {
1034 return false;
1035 }
1036
1037 soinfo* needed_by = task->get_needed_by();
1038
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001039 if (needed_by != nullptr) {
1040 needed_by->add_child(si);
1041 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001042
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001043 if (si->is_linked()) {
1044 si->increment_ref_count();
1045 }
1046
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001047 // When ld_preloads is not null, the first
1048 // ld_preloads_count libs are in fact ld_preloads.
1049 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001050 // Add LD_PRELOADed libraries to the global group for future runs.
1051 // There is no need to explicitly add them to the global group
1052 // for this run because they are going to appear in the local
1053 // group in the correct order.
1054 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001055 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001056 }
1057
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001058 if (soinfos_count < library_names_count) {
1059 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001060 }
1061 }
1062
1063 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001064 soinfo::soinfo_list_t local_group;
1065 walk_dependencies_tree(
1066 start_with == nullptr ? soinfos : &start_with,
1067 start_with == nullptr ? soinfos_count : 1,
1068 [&] (soinfo* si) {
1069 local_group.push_back(si);
1070 return true;
1071 });
1072
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001073 // We need to increment ref_count in case
1074 // the root of the local group was not linked.
1075 bool was_local_group_root_linked = local_group.front()->is_linked();
1076
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001077 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001078 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001079 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001080 return false;
1081 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001082 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001083 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001084
1085 return true;
1086 });
1087
1088 if (linked) {
1089 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001090 }
1091
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001092 if (!was_local_group_root_linked) {
1093 local_group.front()->increment_ref_count();
1094 }
1095
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001096 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001097}
1098
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001099static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001100 soinfo* si;
1101
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001102 if (name == nullptr) {
1103 si = somain;
1104 } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001105 return nullptr;
1106 }
1107
Elliott Hughesd23736e2012-11-01 15:16:56 -07001108 return si;
1109}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001110
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001111static void soinfo_unload(soinfo* root) {
1112 // Note that the library can be loaded but not linked;
1113 // in which case there is no root but we still need
1114 // to walk the tree and unload soinfos involved.
1115 //
1116 // This happens on unsuccessful dlopen, when one of
1117 // the DT_NEEDED libraries could not be linked/found.
1118 if (root->is_linked()) {
1119 root = root->get_local_group_root();
1120 }
1121
1122 if (!root->can_unload()) {
1123 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->name);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001124 return;
1125 }
1126
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001127 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001128
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001129 if (ref_count == 0) {
1130 soinfo::soinfo_list_t local_unload_list;
1131 soinfo::soinfo_list_t external_unload_list;
1132 soinfo::soinfo_list_t depth_first_list;
1133 depth_first_list.push_back(root);
1134 soinfo* si = nullptr;
1135
1136 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001137 if (local_unload_list.contains(si)) {
1138 continue;
1139 }
1140
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001141 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001142
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001143 if (si->has_min_version(0)) {
1144 soinfo* child = nullptr;
1145 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001146 TRACE("%s@%p needs to unload %s@%p", si->name, si, child->name, child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001147 if (local_unload_list.contains(child)) {
1148 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001149 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001150 external_unload_list.push_back(child);
1151 } else {
1152 depth_first_list.push_front(child);
1153 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001154 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001155 } else {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001156#ifdef __LP64__
1157 __libc_fatal("soinfo for \"%s\"@%p has no version", si->name, si);
1158#else
1159 PRINT("warning: soinfo for \"%s\"@%p has no version", si->name, si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001160 for_each_dt_needed(si, [&] (const char* library_name) {
1161 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1162 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1163 if (needed != nullptr) {
1164 // Not found: for example if symlink was deleted between dlopen and dlclose
1165 // Since we cannot really handle errors at this point - print and continue.
1166 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
1167 return;
1168 } else if (local_unload_list.contains(needed)) {
1169 // already visited
1170 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001171 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001172 // external group
1173 external_unload_list.push_back(needed);
1174 } else {
1175 // local group
1176 depth_first_list.push_front(needed);
1177 }
1178 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001179#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001180 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001181 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001182
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001183 local_unload_list.for_each([](soinfo* si) {
1184 si->call_destructors();
1185 });
1186
1187 while ((si = local_unload_list.pop_front()) != nullptr) {
1188 notify_gdb_of_unload(si);
1189 soinfo_free(si);
1190 }
1191
1192 while ((si = external_unload_list.pop_front()) != nullptr) {
1193 soinfo_unload(si);
1194 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001195 } else {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001196 TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->name, ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001197 }
1198}
1199
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001200void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001201 // Use basic string manipulation calls to avoid snprintf.
1202 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1203 // When debug malloc is enabled, this call returns 0. This in turn causes
1204 // snprintf to do nothing, which causes libraries to fail to load.
1205 // See b/17302493 for further details.
1206 // Once the above bug is fixed, this code can be modified to use
1207 // snprintf again.
1208 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1209 if (buffer_size < required_len) {
1210 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1211 buffer_size, required_len);
1212 }
1213 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1214 *end = ':';
1215 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001216}
1217
Elliott Hughescade4c32012-12-20 14:42:14 -08001218void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
1219 if (!get_AT_SECURE()) {
1220 parse_LD_LIBRARY_PATH(ld_library_path);
1221 }
1222}
1223
Elliott Hughes1a586292014-06-03 16:23:08 -07001224soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001225 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001226 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001227 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001228 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001229 if (extinfo != nullptr) {
1230 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1231 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1232 return nullptr;
1233 }
1234 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001235 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1236 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 -07001237 return nullptr;
1238 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001239 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001240 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001241 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001242 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001243 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001244 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001245 protect_data(PROT_READ);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001246 return si;
1247}
1248
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001249void do_dlclose(soinfo* si) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001250 protect_data(PROT_READ | PROT_WRITE);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001251 soinfo_unload(si);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001252 protect_data(PROT_READ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001253}
1254
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001255static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1256 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1257 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1258 ElfW(Addr) ifunc_addr = ifunc_resolver();
1259 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 -07001260
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001261 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001262}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001263
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001264#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001265#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001266static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
1267 return rela->r_addend;
1268}
1269#else
1270static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
1271 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE || ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
1272 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
1273 }
1274 return 0;
1275}
1276#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001277
1278template<typename ElfRelT>
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001279bool soinfo::relocate(ElfRelT* rel, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001280 for (size_t idx = 0; idx < count; ++idx, ++rel) {
1281 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
1282 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
1283
1284 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001285 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001286 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001287 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001288
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001289 DEBUG("Processing '%s' relocation at index %zd", this->name, idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001290 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001291 continue;
1292 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001293
1294 ElfW(Sym)* s = nullptr;
1295 soinfo* lsi = nullptr;
1296
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001297 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001298 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001299 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001300 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001301 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001302 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001303 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001304 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001305 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001306 }
1307
1308 /* IHI0044C AAELF 4.5.1.1:
1309
1310 Libraries are not searched to resolve weak references.
1311 It is not an error for a weak reference to remain unsatisfied.
1312
1313 During linking, the value of an undefined weak reference is:
1314 - Zero if the relocation type is absolute
1315 - The address of the place if the relocation is pc-relative
1316 - The address of nominal base address if the relocation
1317 type is base-relative.
1318 */
1319
1320 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001321 case R_GENERIC_JUMP_SLOT:
1322 case R_GENERIC_GLOB_DAT:
1323 case R_GENERIC_RELATIVE:
1324 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001325#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001326 case R_AARCH64_ABS64:
1327 case R_AARCH64_ABS32:
1328 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001329#elif defined(__x86_64__)
1330 case R_X86_64_32:
1331 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001332#elif defined(__arm__)
1333 case R_ARM_ABS32:
1334#elif defined(__i386__)
1335 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001336#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001337 /*
1338 * The sym_addr was initialized to be zero above, or the relocation
1339 * code below does not care about value of sym_addr.
1340 * No need to do anything.
1341 */
1342 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001343#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00001344 case R_X86_64_PC32:
1345 sym_addr = reloc;
1346 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001347#elif defined(__i386__)
1348 case R_386_PC32:
1349 sym_addr = reloc;
1350 break;
1351#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001352 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001353 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001354 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001355 }
1356 } else {
1357 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001358 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001359 }
1360 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001361 }
1362
1363 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001364 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001365 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001366 MARK(rel->r_offset);
1367 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
1368 reinterpret_cast<void*>(reloc),
1369 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1370
1371 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001372 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001373 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001374 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001375 MARK(rel->r_offset);
1376 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
1377 reinterpret_cast<void*>(reloc),
1378 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1379 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001380 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001381 case R_GENERIC_RELATIVE:
1382 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001383 MARK(rel->r_offset);
1384 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
1385 reinterpret_cast<void*>(reloc),
1386 reinterpret_cast<void*>(base + addend));
1387 *reinterpret_cast<ElfW(Addr)*>(reloc) = (base + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001388 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001389 case R_GENERIC_IRELATIVE:
1390 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001391 MARK(rel->r_offset);
1392 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
1393 reinterpret_cast<void*>(reloc),
1394 reinterpret_cast<void*>(base + addend));
1395 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001396 break;
1397
1398#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001399 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001400 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001401 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001402 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001403 reloc, (sym_addr + addend), sym_name);
1404 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001405 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001406 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001407 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001408 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001409 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001410 reloc, (sym_addr + addend), sym_name);
1411 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1412 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1413 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001414 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001415 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001416 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001417 static_cast<ElfW(Addr)>(INT32_MIN),
1418 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001419 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001420 }
1421 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001422 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001423 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001424 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001425 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001426 reloc, (sym_addr + addend), sym_name);
1427 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1428 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1429 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001430 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001431 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001432 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001433 static_cast<ElfW(Addr)>(INT16_MIN),
1434 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001435 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001436 }
1437 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001438 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001439 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001440 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001441 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001442 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1443 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend) - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001444 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001445 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001446 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001447 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001448 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001449 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1450 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1451 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1452 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001453 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001454 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001455 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001456 static_cast<ElfW(Addr)>(INT32_MIN),
1457 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001458 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001459 }
1460 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001461 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001462 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001463 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001464 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001465 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1466 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1467 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1468 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001469 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001470 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001471 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001472 static_cast<ElfW(Addr)>(INT16_MIN),
1473 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001474 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001475 }
1476 break;
1477
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001478 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001479 /*
1480 * ET_EXEC is not supported so this should not happen.
1481 *
1482 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1483 *
1484 * Section 4.7.1.10 "Dynamic relocations"
1485 * R_AARCH64_COPY may only appear in executable objects where e_type is
1486 * set to ET_EXEC.
1487 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001488 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001489 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001490 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001491 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001492 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001493 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001494 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001495 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001496 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001497 break;
1498#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001499 case R_X86_64_32:
1500 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001501 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001502 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1503 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001504 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001505 break;
1506 case R_X86_64_64:
1507 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001508 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001509 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1510 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001511 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001512 break;
1513 case R_X86_64_PC32:
1514 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001515 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001516 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1517 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1518 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001519 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001520 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001521#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001522 case R_ARM_ABS32:
1523 count_relocation(kRelocAbsolute);
1524 MARK(rel->r_offset);
1525 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1526 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1527 break;
1528 case R_ARM_REL32:
1529 count_relocation(kRelocRelative);
1530 MARK(rel->r_offset);
1531 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1532 reloc, sym_addr, rel->r_offset, sym_name);
1533 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1534 break;
1535 case R_ARM_COPY:
1536 /*
1537 * ET_EXEC is not supported so this should not happen.
1538 *
1539 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1540 *
1541 * Section 4.7.1.10 "Dynamic relocations"
1542 * R_ARM_COPY may only appear in executable objects where e_type is
1543 * set to ET_EXEC.
1544 */
1545 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001546 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001547#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001548 case R_386_32:
1549 count_relocation(kRelocRelative);
1550 MARK(rel->r_offset);
1551 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1552 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1553 break;
1554 case R_386_PC32:
1555 count_relocation(kRelocRelative);
1556 MARK(rel->r_offset);
1557 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1558 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1559 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1560 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001561#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001562 default:
1563 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001564 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001565 }
1566 }
1567 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001568}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001569#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001570
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001571void soinfo::call_array(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001572 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001573 return;
1574 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001575
Elliott Hughesc6200592013-09-30 18:43:46 -07001576 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001577
1578 int begin = reverse ? (count - 1) : 0;
1579 int end = reverse ? -1 : count;
1580 int step = reverse ? -1 : 1;
1581
1582 for (int i = begin; i != end; i += step) {
1583 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001584 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001585 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001586
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001587 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001588}
1589
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001590void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001591 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001592 return;
1593 }
1594
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001595 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001596 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001597 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesdb492b32013-01-03 15:44:03 -08001598
1599 // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1600 // are still writable. This happens with our debug malloc (see http://b/7941716).
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001601 protect_data(PROT_READ | PROT_WRITE);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001602}
1603
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001604void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001605 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1606 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001607 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001608}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001609
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001610void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001611 if (constructors_called) {
1612 return;
1613 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001614
Elliott Hughesd23736e2012-11-01 15:16:56 -07001615 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1616 // protect against recursive constructor calls. One simple example of constructor recursion
1617 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1618 // 1. The program depends on libc, so libc's constructor is called here.
1619 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1620 // 3. dlopen() calls the constructors on the newly created
1621 // soinfo for libc_malloc_debug_leak.so.
1622 // 4. The debug .so depends on libc, so CallConstructors is
1623 // called again with the libc soinfo. If it doesn't trigger the early-
1624 // out above, the libc constructor will be called again (recursively!).
1625 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001626
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001627 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001628 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001629 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001630 name, preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001631 }
1632
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001633 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001634 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001635 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001636
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001637 TRACE("\"%s\": calling constructors", name);
1638
1639 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001640 call_function("DT_INIT", init_func_);
1641 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001642}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001643
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001644void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001645 if (!constructors_called) {
1646 return;
1647 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001648 TRACE("\"%s\": calling destructors", name);
1649
1650 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001651 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001652
1653 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001654 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001655
1656 // This is needed on second call to dlopen
1657 // after library has been unloaded with RTLD_NODELETE
1658 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001659}
1660
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001661void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001662 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001663 child->parents_.push_back(this);
1664 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001665 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001666}
1667
1668void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001669 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001670 return;
1671 }
1672
1673 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001674 children_.for_each([&] (soinfo* child) {
1675 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001676 return parent == this;
1677 });
1678 });
1679
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001680 parents_.for_each([&] (soinfo* parent) {
1681 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001682 return child == this;
1683 });
1684 });
1685
1686 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001687 parents_.clear();
1688 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001689}
1690
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001691dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001692 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001693 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001694 }
1695
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001696 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001697};
1698
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001699ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001700 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001701 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001702 }
1703
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001704 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001705}
1706
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001707off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001708 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001709 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001710 }
1711
1712 return 0;
1713}
1714
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001715uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001716 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001717 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001718 }
1719
1720 return 0;
1721}
1722
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001723uint32_t soinfo::get_dt_flags_1() const {
1724 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001725 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001726 }
1727
1728 return 0;
1729}
1730void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1731 if (has_min_version(1)) {
1732 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001733 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001734 }
1735
1736 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001737 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001738 }
1739
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001740 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001741 }
1742}
1743
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001744// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001745// 'this->flags' does not have FLAG_NEW_SOINFO set.
1746static soinfo::soinfo_list_t g_empty_list;
1747
1748soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001749 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001750 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001751 }
1752
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001753 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001754}
1755
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001756soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001757 if (has_min_version(0)) {
1758 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001759 }
1760
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001761 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001762}
1763
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001764ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1765 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1766 return call_ifunc_resolver(s->st_value + load_bias);
1767 }
1768
1769 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1770}
1771
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001772const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001773 if (has_min_version(1) && (index >= strtab_size_)) {
1774 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001775 }
1776
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001777 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001778}
1779
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001780bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001781 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001782}
1783
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001784bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001785 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001786}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001787
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001788bool soinfo::is_linked() const {
1789 return (flags_ & FLAG_LINKED) != 0;
1790}
1791
1792bool soinfo::is_main_executable() const {
1793 return (flags_ & FLAG_EXE) != 0;
1794}
1795
1796void soinfo::set_linked() {
1797 flags_ |= FLAG_LINKED;
1798}
1799
1800void soinfo::set_linker_flag() {
1801 flags_ |= FLAG_LINKER;
1802}
1803
1804void soinfo::set_main_executable() {
1805 flags_ |= FLAG_EXE;
1806}
1807
1808void soinfo::increment_ref_count() {
1809 local_group_root_->ref_count_++;
1810}
1811
1812size_t soinfo::decrement_ref_count() {
1813 return --local_group_root_->ref_count_;
1814}
1815
1816soinfo* soinfo::get_local_group_root() const {
1817 return local_group_root_;
1818}
1819
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001820/* Force any of the closed stdin, stdout and stderr to be associated with
1821 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001822static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001823 int dev_null, i, status;
1824 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001825
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001826 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
1827 if (dev_null < 0) {
1828 DL_ERR("cannot open /dev/null: %s", strerror(errno));
1829 return -1;
1830 }
1831 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001832
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001833 /* If any of the stdio file descriptors is valid and not associated
1834 with /dev/null, dup /dev/null to it. */
1835 for (i = 0; i < 3; i++) {
1836 /* If it is /dev/null already, we are done. */
1837 if (i == dev_null) {
1838 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001839 }
1840
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001841 TRACE("[ Nullifying stdio file descriptor %d]", i);
1842 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
1843
1844 /* If file is opened, we are good. */
1845 if (status != -1) {
1846 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001847 }
1848
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001849 /* The only error we allow is that the file descriptor does not
1850 exist, in which case we dup /dev/null to it. */
1851 if (errno != EBADF) {
1852 DL_ERR("fcntl failed: %s", strerror(errno));
1853 return_value = -1;
1854 continue;
1855 }
1856
1857 /* Try dupping /dev/null to this stdio file descriptor and
1858 repeat if there is a signal. Note that any errors in closing
1859 the stdio descriptor are lost. */
1860 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
1861 if (status < 0) {
1862 DL_ERR("dup2 failed: %s", strerror(errno));
1863 return_value = -1;
1864 continue;
1865 }
1866 }
1867
1868 /* If /dev/null is not one of the stdio file descriptors, close it. */
1869 if (dev_null > 2) {
1870 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
1871 status = TEMP_FAILURE_RETRY(close(dev_null));
1872 if (status == -1) {
1873 DL_ERR("close failed: %s", strerror(errno));
1874 return_value = -1;
1875 }
1876 }
1877
1878 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001879}
1880
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001881bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08001882 /* Extract dynamic section */
1883 ElfW(Word) dynamic_flags = 0;
1884 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07001885
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001886 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001887 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001888 if (!relocating_linker) {
1889 INFO("[ linking %s ]", name);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001890 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001891 }
1892
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001893 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001894 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001895 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001896 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001897 return false;
1898 } else {
1899 if (!relocating_linker) {
1900 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001901 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001902 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001903
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001904#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001905 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
1906 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001907#endif
1908
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001909 // Extract useful information from dynamic section.
1910 uint32_t needed_count = 0;
1911 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
1912 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1913 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1914 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001915 case DT_SONAME:
1916 // TODO: glibc dynamic linker uses this name for
1917 // initial library lookup; consider doing the same here.
1918 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001919
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001920 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001921 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001922 // in case of --hash-style=both, we prefer gnu
1923 break;
1924 }
1925
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001926 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
1927 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
1928 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
1929 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001930 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001931
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001932 case DT_GNU_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001933 if (nbucket_ != 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001934 // in case of --hash-style=both, we prefer gnu
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001935 nchain_ = 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001936 }
1937
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001938 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001939 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001940 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
1941 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001942
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001943 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
1944 bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001945 // amend chain for symndx = header[1]
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001946 chain_ = bucket_ + nbucket_ - reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001947
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001948 if (!powerof2(gnu_maskwords_)) {
1949 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two", gnu_maskwords_, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001950 return false;
1951 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001952 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001953
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001954 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001955 break;
1956
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001957 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001958 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001959 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001960
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001961 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001962 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001963 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001964
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001965 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001966 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001967 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001968
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001969 case DT_SYMENT:
1970 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001971 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"", static_cast<size_t>(d->d_un.d_val), name);
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001972 return false;
1973 }
1974 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001975
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001976 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07001977#if defined(USE_RELA)
1978 if (d->d_un.d_val != DT_RELA) {
1979 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001980 return false;
1981 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07001982#else
1983 if (d->d_un.d_val != DT_REL) {
1984 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
1985 return false;
1986 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001987#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07001988 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001989
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001990 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001991#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001992 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001993#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001994 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001995#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001996 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001997
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001998 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001999#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002000 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002001#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002002 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002003#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002004 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002005
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002006 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002007#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002008 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002009 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002010#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002011 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2012 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002013
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002014 case DT_DEBUG:
2015 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2016 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002017// FIXME: not working currently for N64
2018// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002019// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002020// read-only, but the DYNAMIC header claims it is writable.
2021#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002022 if ((dynamic_flags & PF_W) != 0) {
2023 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2024 }
2025 break;
Chris Dearman99186652014-02-06 20:36:51 -08002026#endif
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002027#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002028 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002029 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002030 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002031
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002032 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002033 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002034 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002035
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002036 case DT_RELAENT:
2037 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002038 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002039 return false;
2040 }
2041 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002042
2043 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002044 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002045 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002046
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002047 case DT_REL:
2048 DL_ERR("unsupported DT_REL in \"%s\"", name);
2049 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002050
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002051 case DT_RELSZ:
2052 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2053 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002054#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002055 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002056 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002057 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002058
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002059 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002060 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002061 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002062
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002063 case DT_RELENT:
2064 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002065 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002066 return false;
2067 }
2068 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002069
2070 // "Indicates that all RELATIVE relocations have been concatenated together,
2071 // and specifies the RELATIVE relocation count."
2072 //
2073 // TODO: Spec also mentions that this can be used to optimize relocation process;
2074 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002075 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002076 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002077 case DT_RELA:
2078 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2079 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002080#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002081 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002082 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2083 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002084 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002085
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002086 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002087 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2088 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002089 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002090
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002091 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002092 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2093 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002094 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002095
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002096 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002097 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002098 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002099
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002100 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002101 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2102 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002103 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002104
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002105 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002106 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002107 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002108
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002109 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002110 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2111 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002112 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002113
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002114 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002115 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002116 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002117
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002118 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002119#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002120 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2121 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002122#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002123 has_text_relocations = true;
2124 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002125#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002126
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002127 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002128 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002129 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002130
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002131 case DT_NEEDED:
2132 ++needed_count;
2133 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002134
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002135 case DT_FLAGS:
2136 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002137#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002138 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2139 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002140#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002141 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002142#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002143 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002144 if (d->d_un.d_val & DF_SYMBOLIC) {
2145 has_DT_SYMBOLIC = true;
2146 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002147 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002148
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002149 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002150 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002151
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002152 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002153 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2154 }
2155 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002156#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002157 case DT_MIPS_RLD_MAP:
2158 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2159 {
2160 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2161 *dp = &_r_debug;
2162 }
2163 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002164 case DT_MIPS_RLD_MAP2:
2165 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2166 {
2167 r_debug** dp = reinterpret_cast<r_debug**>(reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
2168 *dp = &_r_debug;
2169 }
2170 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002171
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002172 case DT_MIPS_RLD_VERSION:
2173 case DT_MIPS_FLAGS:
2174 case DT_MIPS_BASE_ADDRESS:
2175 case DT_MIPS_UNREFEXTNO:
2176 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002177
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002178 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002179 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002180 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002181
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002182 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002183 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002184 break;
2185
2186 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002187 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002188 break;
2189#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002190 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2191 case DT_BIND_NOW:
2192 break;
2193
2194 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002195 case DT_VERSYM:
2196 case DT_VERDEF:
2197 case DT_VERDEFNUM:
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002198 case DT_VERNEED:
2199 case DT_VERNEEDNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002200 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002201
2202 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002203 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002204 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002205 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2206 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002207 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002208 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002209 }
2210
2211 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002212 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002213
2214 // Sanity checks.
2215 if (relocating_linker && needed_count != 0) {
2216 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2217 return false;
2218 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002219 if (nbucket_ == 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002220 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" (new hash type from the future?)", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002221 return false;
2222 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002223 if (strtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002224 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2225 return false;
2226 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002227 if (symtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002228 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2229 return false;
2230 }
2231 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002232}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002233
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002234bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group, const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002235
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002236 local_group_root_ = local_group.front();
2237 if (local_group_root_ == nullptr) {
2238 local_group_root_ = this;
2239 }
2240
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 Ivanov047b5932014-11-13 09:39:20 -08002256 if (rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002257 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002258 if (!relocate(rela_, rela_count_, global_group, 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 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002262 if (plt_rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002263 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002264 if (!relocate(plt_rela_, plt_rela_count_, global_group, 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 Ivanov047b5932014-11-13 09:39:20 -08002269 if (rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002270 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002271 if (!relocate(rel_, rel_count_, global_group, 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 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002275 if (plt_rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002276 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002277 if (!relocate(plt_rel_, plt_rel_count_, global_group, 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 Ivanov88940912014-11-12 18:20:39 -08002284 if (!mips_relocate_got(global_group, 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 Ivanov047b5932014-11-13 09:39:20 -08002350 si->prelink_image();
2351 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), 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
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -08002425#if !defined(__LP64__)
2426 if (personality(PER_LINUX32) == -1) {
2427 __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
2428 }
2429#endif
2430
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002431 INFO("[ android linker & debugger ]");
2432
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002433 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002434 if (si == nullptr) {
2435 exit(EXIT_FAILURE);
2436 }
2437
2438 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002439 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002440 link_map* map = &(si->link_map_head);
2441
2442 map->l_addr = 0;
2443 map->l_name = args.argv[0];
2444 map->l_prev = nullptr;
2445 map->l_next = nullptr;
2446
2447 _r_debug.r_map = map;
2448 r_debug_tail = map;
2449
2450 init_linker_info_for_gdb(linker_base);
2451
2452 // Extract information passed from the kernel.
2453 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2454 si->phnum = args.getauxval(AT_PHNUM);
2455 si->entry = args.getauxval(AT_ENTRY);
2456
2457 /* Compute the value of si->base. We can't rely on the fact that
2458 * the first entry is the PHDR because this will not be true
2459 * for certain executables (e.g. some in the NDK unit test suite)
2460 */
2461 si->base = 0;
2462 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2463 si->load_bias = 0;
2464 for (size_t i = 0; i < si->phnum; ++i) {
2465 if (si->phdr[i].p_type == PT_PHDR) {
2466 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2467 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2468 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002469 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002470 }
2471 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002472
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002473 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2474 if (elf_hdr->e_type != ET_DYN) {
2475 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2476 exit(EXIT_FAILURE);
2477 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002478
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002479 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2480 parse_LD_LIBRARY_PATH(ldpath_env);
2481 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002482
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002483 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002484
Dmitriy Ivanov67181252015-01-07 15:48:25 -08002485 if (!si->prelink_image()) {
2486 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2487 exit(EXIT_FAILURE);
2488 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002489
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002490 // add somain to global group
2491 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2492
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002493 // Load ld_preloads and dependencies.
2494 StringLinkedList needed_library_name_list;
2495 size_t needed_libraries_count = 0;
2496 size_t ld_preloads_count = 0;
2497 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2498 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2499 ++needed_libraries_count;
2500 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002501
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002502 for_each_dt_needed(si, [&](const char* name) {
2503 needed_library_name_list.push_back(name);
2504 ++needed_libraries_count;
2505 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002506
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002507 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002508
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002509 memset(needed_library_names, 0, sizeof(needed_library_names));
2510 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002511
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002512 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 -07002513 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2514 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002515 } else if (needed_libraries_count == 0) {
2516 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
2517 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2518 exit(EXIT_FAILURE);
2519 }
2520 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002521 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002522
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002523 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002524
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002525 si->call_pre_init_constructors();
Matt Fischer4fd42c12009-12-31 12:09:10 -06002526
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002527 /* After the prelink_image, the si->load_bias is initialized.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002528 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2529 * We need to update this value for so exe here. So Unwind_Backtrace
2530 * for some arch like x86 could work correctly within so exe.
2531 */
2532 map->l_addr = si->load_bias;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002533 si->call_constructors();
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002534
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002535#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002536 gettimeofday(&t1, nullptr);
2537 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2538 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2539 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002540#endif
2541#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002542 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2543 linker_stats.count[kRelocAbsolute],
2544 linker_stats.count[kRelocRelative],
2545 linker_stats.count[kRelocCopy],
2546 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002547#endif
2548#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002549 {
2550 unsigned n;
2551 unsigned i;
2552 unsigned count = 0;
2553 for (n = 0; n < 4096; n++) {
2554 if (bitmask[n]) {
2555 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002556#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002557 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002558#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002559 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002560#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002561 if (x & 1) {
2562 count++;
2563 }
2564 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002565 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002566 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002567 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002568 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2569 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002570#endif
2571
2572#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002573 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002574#endif
2575
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002576 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2577 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002578}
Nick Kralevich468319c2011-11-11 15:53:17 -08002579
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002580/* Compute the load-bias of an existing executable. This shall only
2581 * be used to compute the load bias of an executable or shared library
2582 * that was loaded by the kernel itself.
2583 *
2584 * Input:
2585 * elf -> address of ELF header, assumed to be at the start of the file.
2586 * Return:
2587 * load bias, i.e. add the value of any p_vaddr in the file to get
2588 * the corresponding address in memory.
2589 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002590static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2591 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002592 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002593 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002594
Elliott Hughes0266ae52014-02-10 17:46:57 -08002595 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002596 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002597 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002598 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002599 }
2600 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002601}
2602
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002603extern "C" void _start();
2604
Nick Kralevich468319c2011-11-11 15:53:17 -08002605/*
2606 * This is the entry point for the linker, called from begin.S. This
2607 * method is responsible for fixing the linker's own relocations, and
2608 * then calling __linker_init_post_relocation().
2609 *
2610 * Because this method is called before the linker has fixed it's own
2611 * relocations, any attempt to reference an extern variable, extern
2612 * function, or other GOT reference will generate a segfault.
2613 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002614extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002615 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002616
Elliott Hughes0266ae52014-02-10 17:46:57 -08002617 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002618 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002619 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002620 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002621
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002622 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002623
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002624 // If the linker is not acting as PT_INTERP entry_point is equal to
2625 // _start. Which means that the linker is running as an executable and
2626 // already linked by PT_INTERP.
2627 //
2628 // This happens when user tries to run 'adb shell /system/bin/linker'
2629 // see also https://code.google.com/p/android/issues/detail?id=63174
2630 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2631 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2632 }
2633
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002634 linker_so.base = linker_addr;
2635 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2636 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002637 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002638 linker_so.phdr = phdr;
2639 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002640 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07002641
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002642 // This might not be obvious... The reasons why we pass g_empty_list
2643 // in place of local_group here are (1) we do not really need it, because
2644 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2645 // itself without having to look into local_group and (2) allocators
2646 // are not yet initialized, and therefore we cannot use linked_list.push_*
2647 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002648 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002649 // It would be nice to print an error message, but if the linker
2650 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002651 // call write() (because it involves a GOT reference). We may as
2652 // well try though...
2653 const char* msg = "CANNOT LINK EXECUTABLE: ";
2654 write(2, msg, strlen(msg));
2655 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2656 write(2, "\n", 1);
2657 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002658 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002659
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002660 __libc_init_tls(args);
2661
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002662 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002663 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002664
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002665 // Initialize static variables. Note that in order to
2666 // get correct libdl_info we need to call constructors
2667 // before get_libdl_info().
2668 solist = get_libdl_info();
2669 sonext = get_libdl_info();
2670
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002671 // We have successfully fixed our own relocations. It's safe to run
2672 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002673 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002674 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002675
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002676 protect_data(PROT_READ);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002677
2678 // Return the address that the calling assembly stub should jump to.
2679 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002680}