blob: 18f8cdcb494722ccd21d6162cfb571c3fa16f04f [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"
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070053#include "linker_block_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010055#include "linker_environ.h"
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080056#include "linker_leb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020057#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080058#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080059#include "linker_reloc_iterators.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
62 *
63 * Do NOT use malloc() and friends or pthread_*() code here.
64 * Don't use printf() either; it's caused mysterious memory
65 * corruption in the past.
66 * The linker runs before we bring up libc and it's easiest
67 * to make sure it does not depend on any complex libc features
68 *
69 * open issues / todo:
70 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072 * - after linking, set as much stuff as possible to READONLY
73 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070074 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080076// Override macros to use C++ style casts
77#undef ELF_ST_TYPE
78#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
79
Dmitriy Ivanov489e4982014-05-19 15:19:52 -070080#if defined(__LP64__)
81#define SEARCH_NAME(x) x
82#else
83// Nvidia drivers are relying on the bug:
84// http://code.google.com/p/android/issues/detail?id=6670
85// so we continue to use base-name lookup for lp32
86static const char* get_base_name(const char* name) {
87 const char* bname = strrchr(name, '/');
88 return bname ? bname + 1 : name;
89}
90#define SEARCH_NAME(x) get_base_name(x)
91#endif
92
Elliott Hughes0266ae52014-02-10 17:46:57 -080093static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070095static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
96static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020097
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070098static soinfo* solist;
99static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700100static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101
Elliott Hughes1728b232014-05-14 10:02:03 -0700102static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700103#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700104 "/vendor/lib64",
105 "/system/lib64",
106#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700107 "/vendor/lib",
108 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700109#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700110 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700111};
David Bartleybc3a5c22009-06-02 18:27:28 -0700112
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800113#define LDPATH_BUFSIZE (LDPATH_MAX*64)
114#define LDPATH_MAX 8
115
116#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
117#define LDPRELOAD_MAX 8
118
Elliott Hughes1728b232014-05-14 10:02:03 -0700119static char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
120static const char* g_ld_library_paths[LDPATH_MAX + 1];
Elliott Hughes124fae92012-10-31 14:20:03 -0700121
Elliott Hughes1728b232014-05-14 10:02:03 -0700122static char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
123static const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600124
Elliott Hughes1728b232014-05-14 10:02:03 -0700125static soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600126
Elliott Hughes1728b232014-05-14 10:02:03 -0700127__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700129__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700130
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700132struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700133 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700134};
135
136static linker_stats_t linker_stats;
137
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800138void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700139 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700140}
141#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800142void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700143}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144#endif
145
146#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800147uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148#endif
149
Dima Zavin2e855792009-05-20 18:28:09 -0700150static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700151
Elliott Hughes650be4e2013-03-05 18:47:58 -0800152char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700153 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700154}
155
Elliott Hughes650be4e2013-03-05 18:47:58 -0800156size_t linker_get_error_buffer_size() {
157 return sizeof(__linker_dl_err_buf);
158}
159
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700160// This function is an empty stub where GDB locates a breakpoint to get notified
161// about linker activity.
Elliott Hughes5419b942012-10-16 15:54:46 -0700162extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163
Elliott Hughes1728b232014-05-14 10:02:03 -0700164static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700165static 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 -0800166static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800168static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700169 // Copy the necessary fields into the debug structure.
170 link_map* map = &(info->link_map_head);
171 map->l_addr = info->load_bias;
Dmitriy Ivanovc9d16582014-10-24 14:46:12 -0700172 map->l_name = info->name;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700173 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700175 // Stick the new library at the end of the list.
176 // gdb tends to care more about libc than it does
177 // about leaf libraries, and ordering it this way
178 // reduces the back-and-forth over the wire.
179 if (r_debug_tail) {
180 r_debug_tail->l_next = map;
181 map->l_prev = r_debug_tail;
182 map->l_next = 0;
183 } else {
184 _r_debug.r_map = map;
185 map->l_prev = 0;
186 map->l_next = 0;
187 }
188 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189}
190
Elliott Hughesbedfe382012-08-14 14:07:59 -0700191static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700192 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700193
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700194 if (r_debug_tail == map) {
195 r_debug_tail = map->l_prev;
196 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700197
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700198 if (map->l_prev) {
199 map->l_prev->l_next = map->l_next;
200 }
201 if (map->l_next) {
202 map->l_next->l_prev = map->l_prev;
203 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700204}
205
Elliott Hughesbedfe382012-08-14 14:07:59 -0700206static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800207 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700208 // GDB already knows about the main executable
209 return;
210 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800211
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700212 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800213
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700214 _r_debug.r_state = r_debug::RT_ADD;
215 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700217 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700219 _r_debug.r_state = r_debug::RT_CONSISTENT;
220 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700221}
222
Elliott Hughesbedfe382012-08-14 14:07:59 -0700223static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800224 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700225 // GDB already knows about the main executable
226 return;
227 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700228
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700229 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700230
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700231 _r_debug.r_state = r_debug::RT_DELETE;
232 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700233
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700234 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700235
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700236 _r_debug.r_state = r_debug::RT_CONSISTENT;
237 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238}
239
Elliott Hughes18a206c2012-10-29 17:37:13 -0700240void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800241 _r_debug.r_state = r_debug::RT_ADD;
242 rtld_db_dlactivity();
243 _r_debug.r_state = r_debug::RT_CONSISTENT;
244 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245}
246
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700247LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
248 return g_soinfo_links_allocator.alloc();
249}
250
251void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
252 g_soinfo_links_allocator.free(entry);
253}
254
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700255static 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 +0200256 if (strlen(name) >= SOINFO_NAME_LEN) {
257 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700258 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200259 }
260
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700261 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700262
Magnus Malmbornba98d922012-09-12 13:00:55 +0200263 sonext->next = si;
264 sonext = si;
265
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700266 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200267 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800268}
269
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800270static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700271 if (si == nullptr) {
272 return;
273 }
274
275 if (si->base != 0 && si->size != 0) {
276 munmap(reinterpret_cast<void*>(si->base), si->size);
277 }
278
279 soinfo *prev = nullptr, *trav;
280
281 TRACE("name %s: freeing soinfo @ %p", si->name, si);
282
283 for (trav = solist; trav != nullptr; trav = trav->next) {
284 if (trav == si) {
285 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700286 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700287 prev = trav;
288 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800289
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700290 if (trav == nullptr) {
291 // si was not in solist
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -0800292 DL_ERR("name \"%s\"@%p is not in solist!", si->name, si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700293 return;
294 }
Elliott Hughes46882792012-08-03 16:49:39 -0700295
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700296 // clear links to/from si
297 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700298
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700299 // prev will never be null, because the first entry in solist is
300 // always the static libdl_info.
301 prev->next = si->next;
302 if (si == sonext) {
303 sonext = prev;
304 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800305
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700306 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800307}
308
Elliott Hughescade4c32012-12-20 14:42:14 -0800309static void parse_path(const char* path, const char* delimiters,
310 const char** array, char* buf, size_t buf_size, size_t max_count) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700311 if (path == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800312 return;
313 }
314
315 size_t len = strlcpy(buf, path, buf_size);
316
317 size_t i = 0;
318 char* buf_p = buf;
319 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
320 if (*array[i] != '\0') {
321 ++i;
322 }
323 }
324
325 // Forget the last path if we had to truncate; this occurs if the 2nd to
326 // last char isn't '\0' (i.e. wasn't originally a delimiter).
327 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700328 array[i - 1] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800329 } else {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700330 array[i] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800331 }
332}
333
334static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700335 parse_path(path, ":", g_ld_library_paths,
336 g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800337}
338
339static void parse_LD_PRELOAD(const char* path) {
340 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes1728b232014-05-14 10:02:03 -0700341 parse_path(path, " :", g_ld_preload_names,
342 g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800343}
344
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700345#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700346
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700347// For a given PC, find the .so that it belongs to.
348// Returns the base address of the .ARM.exidx section
349// for that .so, and the number of 8-byte entries
350// in that section (via *pcount).
351//
352// Intended to be called by libc's __gnu_Unwind_Find_exidx().
353//
354// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800355_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800356 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800357
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700358 for (soinfo* si = solist; si != 0; si = si->next) {
359 if ((addr >= si->base) && (addr < (si->base + si->size))) {
360 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800361 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800362 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700363 }
364 *pcount = 0;
365 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800366}
Elliott Hughes46882792012-08-03 16:49:39 -0700367
Christopher Ferris24053a42013-08-19 17:45:09 -0700368#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700369
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700370// Here, we only have to provide a callback to iterate across all the
371// loaded libraries. gcc_eh does the rest.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800372int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700373 int rv = 0;
374 for (soinfo* si = solist; si != nullptr; si = si->next) {
375 dl_phdr_info dl_info;
376 dl_info.dlpi_addr = si->link_map_head.l_addr;
377 dl_info.dlpi_name = si->link_map_head.l_name;
378 dl_info.dlpi_phdr = si->phdr;
379 dl_info.dlpi_phnum = si->phnum;
380 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
381 if (rv != 0) {
382 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800383 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700384 }
385 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800386}
Elliott Hughes46882792012-08-03 16:49:39 -0700387
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800388ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name) {
389 return is_gnu_hash() ? gnu_lookup(symbol_name) : elf_lookup(symbol_name);
390}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800391
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800392static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
393 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
394 ELF_ST_BIND(s->st_info) == STB_WEAK) {
395 return s->st_shndx != SHN_UNDEF;
396 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
397 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
398 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->name);
399 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800400
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800401 return false;
402}
403
404ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name) {
405 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800406 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800407
408 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800409 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
410 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800411
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700412 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
413 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
414
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800415 // test against bloom filter
416 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700417 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
418 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
419
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800420 return nullptr;
421 }
422
423 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700424 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800425
426 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700427 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
428 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
429
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800430 return nullptr;
431 }
432
433 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800434 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700435 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800436 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
437 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700438 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
439 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
440 static_cast<size_t>(s->st_size));
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800441 return s;
442 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700443 } while ((gnu_chain_[n++] & 1) == 0);
444
445 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
446 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
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 Ivanov279a22f2015-01-23 12:03:53 -0800592class ProtectedDataGuard {
593 public:
594 ProtectedDataGuard() {
595 if (ref_count_++ == 0) {
596 protect_data(PROT_READ | PROT_WRITE);
597 }
598 }
599
600 ~ProtectedDataGuard() {
601 if (ref_count_ == 0) { // overflow
602 __libc_fatal("Too many nested calls to dlopen()");
603 }
604
605 if (--ref_count_ == 0) {
606 protect_data(PROT_READ);
607 }
608 }
609 private:
610 void protect_data(int protection) {
611 g_soinfo_allocator.protect_all(protection);
612 g_soinfo_links_allocator.protect_all(protection);
613 }
614
615 static size_t ref_count_;
616};
617
618size_t ProtectedDataGuard::ref_count_ = 0;
619
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700620// Each size has it's own allocator.
621template<size_t size>
622class SizeBasedAllocator {
623 public:
624 static void* alloc() {
625 return allocator_.alloc();
626 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700627
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700628 static void free(void* ptr) {
629 allocator_.free(ptr);
630 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700631
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700632 private:
633 static LinkerBlockAllocator allocator_;
634};
635
636template<size_t size>
637LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
638
639template<typename T>
640class TypeBasedAllocator {
641 public:
642 static T* alloc() {
643 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
644 }
645
646 static void free(T* ptr) {
647 SizeBasedAllocator<sizeof(T)>::free(ptr);
648 }
649};
650
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700651class LoadTask {
652 public:
653 struct deleter_t {
654 void operator()(LoadTask* t) {
655 TypeBasedAllocator<LoadTask>::free(t);
656 }
657 };
658
659 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
660
661 static deleter_t deleter;
662
663 static LoadTask* create(const char* name, soinfo* needed_by) {
664 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
665 return new (ptr) LoadTask(name, needed_by);
666 }
667
668 const char* get_name() const {
669 return name_;
670 }
671
672 soinfo* get_needed_by() const {
673 return needed_by_;
674 }
675 private:
676 LoadTask(const char* name, soinfo* needed_by)
677 : name_(name), needed_by_(needed_by) {}
678
679 const char* name_;
680 soinfo* needed_by_;
681
682 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
683};
684
Ningsheng Jiane93be992014-09-16 15:22:10 +0800685LoadTask::deleter_t LoadTask::deleter;
686
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700687template <typename T>
688using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
689
690typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700691typedef linked_list_t<const char> StringLinkedList;
692typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700693
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700694
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700695// This function walks down the tree of soinfo dependencies
696// in breadth-first order and
697// * calls action(soinfo* si) for each node, and
698// * terminates walk if action returns false.
699//
700// walk_dependencies_tree returns false if walk was terminated
701// by the action and true otherwise.
702template<typename F>
703static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700704 SoinfoLinkedList visit_list;
705 SoinfoLinkedList visited;
706
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700707 for (size_t i = 0; i < root_soinfos_size; ++i) {
708 visit_list.push_back(root_soinfos[i]);
709 }
710
711 soinfo* si;
712 while ((si = visit_list.pop_front()) != nullptr) {
713 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700714 continue;
715 }
716
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700717 if (!action(si)) {
718 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700719 }
720
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700721 visited.push_back(si);
722
723 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700724 visit_list.push_back(child);
725 });
726 }
727
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700728 return true;
729}
730
731
732// This is used by dlsym(3). It performs symbol lookup only within the
733// specified soinfo object and its dependencies in breadth first order.
734ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
735 ElfW(Sym)* result = nullptr;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800736 SymbolName symbol_name(name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700737
738
739 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800740 result = current_soinfo->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700741 if (result != nullptr) {
742 *found = current_soinfo;
743 return false;
744 }
745
746 return true;
747 });
748
749 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800750}
751
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800752/* This is used by dlsym(3) to performs a global symbol lookup. If the
753 start value is null (for RTLD_DEFAULT), the search starts at the
754 beginning of the global solist. Otherwise the search starts at the
755 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700756 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700757ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800758 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800759
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700760 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800761 start = solist;
762 }
763
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700764 ElfW(Sym)* s = nullptr;
765 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700766 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
767 continue;
768 }
769
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800770 s = si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700771 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800772 *found = si;
773 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600774 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800775 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600776
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700777 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700778 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
779 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800780 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800781
Elliott Hughescade4c32012-12-20 14:42:14 -0800782 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800783}
784
Kito Chengfa8c05d2013-03-12 14:58:06 +0800785soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800786 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700787 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800788 if (address >= si->base && address - si->base < si->size) {
789 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600790 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800791 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700792 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600793}
794
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800795ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
796 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
797}
798
799static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
800 return sym->st_shndx != SHN_UNDEF &&
801 soaddr >= sym->st_value &&
802 soaddr < sym->st_value + sym->st_size;
803}
804
805ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800806 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800807
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700808 for (size_t i = 0; i < gnu_nbucket_; ++i) {
809 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800810
811 if (n == 0) {
812 continue;
813 }
814
815 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800816 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800817 if (symbol_matches_soaddr(sym, soaddr)) {
818 return sym;
819 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700820 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800821 }
822
823 return nullptr;
824}
825
826ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800827 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600828
Kito Chengfa8c05d2013-03-12 14:58:06 +0800829 // Search the library's symbol table for any defined symbol which
830 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800831 for (size_t i = 0; i < nchain_; ++i) {
832 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800833 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800834 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600835 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800836 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600837
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700838 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600839}
840
Elliott Hughes124fae92012-10-31 14:20:03 -0700841static int open_library_on_path(const char* name, const char* const paths[]) {
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +0000842 char buf[512];
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700843 for (size_t i = 0; paths[i] != nullptr; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800844 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700845 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700846 PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
Elliott Hughes124fae92012-10-31 14:20:03 -0700847 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800848 }
Elliott Hughes124fae92012-10-31 14:20:03 -0700849 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
850 if (fd != -1) {
851 return fd;
852 }
853 }
854 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800855}
856
Elliott Hughes124fae92012-10-31 14:20:03 -0700857static int open_library(const char* name) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700858 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800859
Elliott Hughes124fae92012-10-31 14:20:03 -0700860 // 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 -0700861 if (strchr(name, '/') != nullptr) {
Elliott Hughes6971fe42012-11-01 22:59:19 -0700862 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
863 if (fd != -1) {
864 return fd;
865 }
866 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700867#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700868 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700869#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700870 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800871
Elliott Hughes124fae92012-10-31 14:20:03 -0700872 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Elliott Hughes1728b232014-05-14 10:02:03 -0700873 int fd = open_library_on_path(name, g_ld_library_paths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700874 if (fd == -1) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700875 fd = open_library_on_path(name, kDefaultLdPaths);
Elliott Hughes124fae92012-10-31 14:20:03 -0700876 }
877 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800878}
879
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700880template<typename F>
881static void for_each_dt_needed(const soinfo* si, F action) {
882 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
883 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700884 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700885 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700886 }
887}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800888
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700889static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700890 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700891 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700892 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700893
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700894 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
895 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700896 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
897 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700898 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700899 } else {
900 // Open the file.
901 fd = open_library(name);
902 if (fd == -1) {
903 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700904 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700905 }
906
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700907 file_guard.reset(fd);
908 }
909
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700910 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700911 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700912 return nullptr;
913 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800914 if (file_offset < 0) {
915 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
916 return nullptr;
917 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700918
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700919 struct stat file_stat;
920 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700921 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700922 return nullptr;
923 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800924 if (file_offset >= file_stat.st_size) {
925 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64, name, file_offset, file_stat.st_size);
926 return nullptr;
927 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700928
929 // Check for symlink and other situations where
930 // file can have different names.
931 for (soinfo* si = solist; si != nullptr; si = si->next) {
932 if (si->get_st_dev() != 0 &&
933 si->get_st_ino() != 0 &&
934 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700935 si->get_st_ino() == file_stat.st_ino &&
936 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700937 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
938 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700939 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700940 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700941
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700942 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -0700943 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700944 return nullptr;
945 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700946
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700947 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700948 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700949 if (!elf_reader.Load(extinfo)) {
950 return nullptr;
951 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800952
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700953 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700954 if (si == nullptr) {
955 return nullptr;
956 }
957 si->base = elf_reader.load_start();
958 si->size = elf_reader.load_size();
959 si->load_bias = elf_reader.load_bias();
960 si->phnum = elf_reader.phdr_count();
961 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700962
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800963 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700964 soinfo_free(si);
965 return nullptr;
966 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700967
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700968 for_each_dt_needed(si, [&] (const char* name) {
969 load_tasks.push_back(LoadTask::create(name, si));
970 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700971
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700972 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800973}
974
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700975static soinfo *find_loaded_library_by_name(const char* name) {
976 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700977 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700978 if (!strcmp(search_name, si->name)) {
979 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200980 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700981 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700982 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200983}
984
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700985static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200986
Dmitriy Ivanov489e4982014-05-19 15:19:52 -0700987 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700988
989 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700990 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700991 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700992 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700993 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700994 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800995
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700996 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800997}
998
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700999static void soinfo_unload(soinfo* si);
1000
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001001// TODO: this is slightly unusual way to construct
1002// the global group for relocation. Not every RTLD_GLOBAL
1003// library is included in this group for backwards-compatibility
1004// reasons.
1005//
1006// This group consists of the main executable, LD_PRELOADs
1007// and libraries with the DF_1_GLOBAL flag set.
1008static soinfo::soinfo_list_t make_global_group() {
1009 soinfo::soinfo_list_t global_group;
1010 for (soinfo* si = somain; si != nullptr; si = si->next) {
1011 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1012 global_group.push_back(si);
1013 }
1014 }
1015
1016 return global_group;
1017}
1018
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001019static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
1020 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001021 // Step 0: prepare.
1022 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001023 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001024 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001025 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001026 }
1027
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001028 // Construct global_group.
1029 soinfo::soinfo_list_t global_group = make_global_group();
1030
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001031 // If soinfos array is null allocate one on stack.
1032 // The array is needed in case of failure; for example
1033 // when library_names[] = {libone.so, libtwo.so} and libone.so
1034 // is loaded correctly but libtwo.so failed for some reason.
1035 // In this case libone.so should be unloaded on return.
1036 // See also implementation of failure_guard below.
1037
1038 if (soinfos == nullptr) {
1039 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1040 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1041 memset(soinfos, 0, soinfos_size);
1042 }
1043
1044 // list of libraries to link - see step 2.
1045 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001046
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001047 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001048 // Housekeeping
1049 load_tasks.for_each([] (LoadTask* t) {
1050 LoadTask::deleter(t);
1051 });
1052
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001053 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001054 soinfo_unload(soinfos[i]);
1055 }
1056 });
1057
1058 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
1059 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 -07001060 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001061 if (si == nullptr) {
1062 return false;
1063 }
1064
1065 soinfo* needed_by = task->get_needed_by();
1066
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001067 if (needed_by != nullptr) {
1068 needed_by->add_child(si);
1069 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001070
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001071 if (si->is_linked()) {
1072 si->increment_ref_count();
1073 }
1074
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001075 // When ld_preloads is not null, the first
1076 // ld_preloads_count libs are in fact ld_preloads.
1077 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001078 // Add LD_PRELOADed libraries to the global group for future runs.
1079 // There is no need to explicitly add them to the global group
1080 // for this run because they are going to appear in the local
1081 // group in the correct order.
1082 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001083 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001084 }
1085
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001086 if (soinfos_count < library_names_count) {
1087 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001088 }
1089 }
1090
1091 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001092 soinfo::soinfo_list_t local_group;
1093 walk_dependencies_tree(
1094 start_with == nullptr ? soinfos : &start_with,
1095 start_with == nullptr ? soinfos_count : 1,
1096 [&] (soinfo* si) {
1097 local_group.push_back(si);
1098 return true;
1099 });
1100
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001101 // We need to increment ref_count in case
1102 // the root of the local group was not linked.
1103 bool was_local_group_root_linked = local_group.front()->is_linked();
1104
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001105 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001106 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001107 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001108 return false;
1109 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001110 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001111 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001112
1113 return true;
1114 });
1115
1116 if (linked) {
1117 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001118 }
1119
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001120 if (!was_local_group_root_linked) {
1121 local_group.front()->increment_ref_count();
1122 }
1123
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001124 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001125}
1126
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001127static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001128 soinfo* si;
1129
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001130 if (name == nullptr) {
1131 si = somain;
1132 } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001133 return nullptr;
1134 }
1135
Elliott Hughesd23736e2012-11-01 15:16:56 -07001136 return si;
1137}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001138
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001139static void soinfo_unload(soinfo* root) {
1140 // Note that the library can be loaded but not linked;
1141 // in which case there is no root but we still need
1142 // to walk the tree and unload soinfos involved.
1143 //
1144 // This happens on unsuccessful dlopen, when one of
1145 // the DT_NEEDED libraries could not be linked/found.
1146 if (root->is_linked()) {
1147 root = root->get_local_group_root();
1148 }
1149
1150 if (!root->can_unload()) {
1151 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->name);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001152 return;
1153 }
1154
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001155 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001156
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001157 if (ref_count == 0) {
1158 soinfo::soinfo_list_t local_unload_list;
1159 soinfo::soinfo_list_t external_unload_list;
1160 soinfo::soinfo_list_t depth_first_list;
1161 depth_first_list.push_back(root);
1162 soinfo* si = nullptr;
1163
1164 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001165 if (local_unload_list.contains(si)) {
1166 continue;
1167 }
1168
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001169 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001170
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001171 if (si->has_min_version(0)) {
1172 soinfo* child = nullptr;
1173 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001174 TRACE("%s@%p needs to unload %s@%p", si->name, si, child->name, child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001175 if (local_unload_list.contains(child)) {
1176 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001177 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001178 external_unload_list.push_back(child);
1179 } else {
1180 depth_first_list.push_front(child);
1181 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001182 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001183 } else {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001184#ifdef __LP64__
1185 __libc_fatal("soinfo for \"%s\"@%p has no version", si->name, si);
1186#else
1187 PRINT("warning: soinfo for \"%s\"@%p has no version", si->name, si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001188 for_each_dt_needed(si, [&] (const char* library_name) {
1189 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1190 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1191 if (needed != nullptr) {
1192 // Not found: for example if symlink was deleted between dlopen and dlclose
1193 // Since we cannot really handle errors at this point - print and continue.
1194 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
1195 return;
1196 } else if (local_unload_list.contains(needed)) {
1197 // already visited
1198 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001199 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001200 // external group
1201 external_unload_list.push_back(needed);
1202 } else {
1203 // local group
1204 depth_first_list.push_front(needed);
1205 }
1206 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001207#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001208 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001209 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001210
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001211 local_unload_list.for_each([](soinfo* si) {
1212 si->call_destructors();
1213 });
1214
1215 while ((si = local_unload_list.pop_front()) != nullptr) {
1216 notify_gdb_of_unload(si);
1217 soinfo_free(si);
1218 }
1219
1220 while ((si = external_unload_list.pop_front()) != nullptr) {
1221 soinfo_unload(si);
1222 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001223 } else {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001224 TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->name, ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001225 }
1226}
1227
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001228void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001229 // Use basic string manipulation calls to avoid snprintf.
1230 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1231 // When debug malloc is enabled, this call returns 0. This in turn causes
1232 // snprintf to do nothing, which causes libraries to fail to load.
1233 // See b/17302493 for further details.
1234 // Once the above bug is fixed, this code can be modified to use
1235 // snprintf again.
1236 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1237 if (buffer_size < required_len) {
1238 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1239 buffer_size, required_len);
1240 }
1241 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1242 *end = ':';
1243 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001244}
1245
Elliott Hughescade4c32012-12-20 14:42:14 -08001246void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001247 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001248}
1249
Elliott Hughes1a586292014-06-03 16:23:08 -07001250soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001251 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001252 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001253 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001254 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001255 if (extinfo != nullptr) {
1256 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1257 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1258 return nullptr;
1259 }
1260 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001261 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1262 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 -07001263 return nullptr;
1264 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001265 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001266
1267 ProtectedDataGuard guard;
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001268 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001269 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001270 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001271 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001272 return si;
1273}
1274
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001275void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001276 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001277 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001278}
1279
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001280static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1281 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1282 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1283 ElfW(Addr) ifunc_addr = ifunc_resolver();
1284 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 -07001285
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001286 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001287}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001288
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001289#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001290#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001291static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
1292 return rela->r_addend;
1293}
1294#else
1295static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
1296 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE || ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
1297 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
1298 }
1299 return 0;
1300}
1301#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001302
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001303template<typename ElfRelIteratorT>
1304bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
1305 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
1306 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001307 if (rel == nullptr) {
1308 return false;
1309 }
1310
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001311 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
1312 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
1313
1314 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001315 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001316 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001317 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001318
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001319 DEBUG("Processing '%s' relocation at index %zd", this->name, idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001320 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001321 continue;
1322 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001323
1324 ElfW(Sym)* s = nullptr;
1325 soinfo* lsi = nullptr;
1326
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001327 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001328 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001329 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001330 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001331 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001332 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001333 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001334 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001335 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001336 }
1337
1338 /* IHI0044C AAELF 4.5.1.1:
1339
1340 Libraries are not searched to resolve weak references.
1341 It is not an error for a weak reference to remain unsatisfied.
1342
1343 During linking, the value of an undefined weak reference is:
1344 - Zero if the relocation type is absolute
1345 - The address of the place if the relocation is pc-relative
1346 - The address of nominal base address if the relocation
1347 type is base-relative.
1348 */
1349
1350 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001351 case R_GENERIC_JUMP_SLOT:
1352 case R_GENERIC_GLOB_DAT:
1353 case R_GENERIC_RELATIVE:
1354 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001355#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001356 case R_AARCH64_ABS64:
1357 case R_AARCH64_ABS32:
1358 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001359#elif defined(__x86_64__)
1360 case R_X86_64_32:
1361 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001362#elif defined(__arm__)
1363 case R_ARM_ABS32:
1364#elif defined(__i386__)
1365 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001366#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001367 /*
1368 * The sym_addr was initialized to be zero above, or the relocation
1369 * code below does not care about value of sym_addr.
1370 * No need to do anything.
1371 */
1372 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001373#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00001374 case R_X86_64_PC32:
1375 sym_addr = reloc;
1376 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001377#elif defined(__i386__)
1378 case R_386_PC32:
1379 sym_addr = reloc;
1380 break;
1381#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001382 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001383 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001384 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001385 }
1386 } else {
1387 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001388 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001389 }
1390 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001391 }
1392
1393 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001394 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001395 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001396 MARK(rel->r_offset);
1397 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
1398 reinterpret_cast<void*>(reloc),
1399 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1400
1401 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001402 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001403 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001404 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001405 MARK(rel->r_offset);
1406 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
1407 reinterpret_cast<void*>(reloc),
1408 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1409 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001410 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001411 case R_GENERIC_RELATIVE:
1412 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001413 MARK(rel->r_offset);
1414 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
1415 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001416 reinterpret_cast<void*>(load_bias + addend));
1417 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001418 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001419 case R_GENERIC_IRELATIVE:
1420 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001421 MARK(rel->r_offset);
1422 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
1423 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001424 reinterpret_cast<void*>(load_bias + addend));
1425 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001426 break;
1427
1428#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001429 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001430 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001431 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001432 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001433 reloc, (sym_addr + addend), sym_name);
1434 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001435 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001436 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001437 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001438 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001439 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001440 reloc, (sym_addr + addend), sym_name);
1441 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1442 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1443 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001444 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001445 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001446 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001447 static_cast<ElfW(Addr)>(INT32_MIN),
1448 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001449 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001450 }
1451 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001452 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001453 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001454 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001455 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001456 reloc, (sym_addr + addend), sym_name);
1457 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1458 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1459 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001460 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001461 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001462 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001463 static_cast<ElfW(Addr)>(INT16_MIN),
1464 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001465 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001466 }
1467 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001468 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001469 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001470 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001471 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001472 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1473 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend) - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001474 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001475 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001476 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001477 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001478 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001479 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1480 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1481 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1482 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001483 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001484 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001485 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001486 static_cast<ElfW(Addr)>(INT32_MIN),
1487 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001488 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001489 }
1490 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001491 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001492 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001493 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001494 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001495 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1496 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1497 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1498 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001499 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001500 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001501 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001502 static_cast<ElfW(Addr)>(INT16_MIN),
1503 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001504 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001505 }
1506 break;
1507
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001508 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001509 /*
1510 * ET_EXEC is not supported so this should not happen.
1511 *
1512 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1513 *
1514 * Section 4.7.1.10 "Dynamic relocations"
1515 * R_AARCH64_COPY may only appear in executable objects where e_type is
1516 * set to ET_EXEC.
1517 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001518 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001519 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001520 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001521 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001522 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001523 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001524 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001525 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001526 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001527 break;
1528#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001529 case R_X86_64_32:
1530 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001531 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001532 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1533 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001534 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001535 break;
1536 case R_X86_64_64:
1537 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001538 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001539 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1540 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001541 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001542 break;
1543 case R_X86_64_PC32:
1544 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001545 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001546 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1547 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1548 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001549 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001550 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001551#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001552 case R_ARM_ABS32:
1553 count_relocation(kRelocAbsolute);
1554 MARK(rel->r_offset);
1555 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1556 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1557 break;
1558 case R_ARM_REL32:
1559 count_relocation(kRelocRelative);
1560 MARK(rel->r_offset);
1561 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1562 reloc, sym_addr, rel->r_offset, sym_name);
1563 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1564 break;
1565 case R_ARM_COPY:
1566 /*
1567 * ET_EXEC is not supported so this should not happen.
1568 *
1569 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1570 *
1571 * Section 4.7.1.10 "Dynamic relocations"
1572 * R_ARM_COPY may only appear in executable objects where e_type is
1573 * set to ET_EXEC.
1574 */
1575 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001576 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001577#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001578 case R_386_32:
1579 count_relocation(kRelocRelative);
1580 MARK(rel->r_offset);
1581 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1582 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1583 break;
1584 case R_386_PC32:
1585 count_relocation(kRelocRelative);
1586 MARK(rel->r_offset);
1587 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1588 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1589 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1590 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001591#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001592 default:
1593 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001594 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001595 }
1596 }
1597 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001598}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001599#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001600
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001601void soinfo::call_array(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001602 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001603 return;
1604 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001605
Elliott Hughesc6200592013-09-30 18:43:46 -07001606 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001607
1608 int begin = reverse ? (count - 1) : 0;
1609 int end = reverse ? -1 : count;
1610 int step = reverse ? -1 : 1;
1611
1612 for (int i = begin; i != end; i += step) {
1613 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001614 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001615 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001616
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001617 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001618}
1619
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001620void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001621 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001622 return;
1623 }
1624
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001625 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001626 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001627 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001628}
1629
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001630void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001631 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1632 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001633 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001634}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001635
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001636void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001637 if (constructors_called) {
1638 return;
1639 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001640
Elliott Hughesd23736e2012-11-01 15:16:56 -07001641 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1642 // protect against recursive constructor calls. One simple example of constructor recursion
1643 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1644 // 1. The program depends on libc, so libc's constructor is called here.
1645 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1646 // 3. dlopen() calls the constructors on the newly created
1647 // soinfo for libc_malloc_debug_leak.so.
1648 // 4. The debug .so depends on libc, so CallConstructors is
1649 // called again with the libc soinfo. If it doesn't trigger the early-
1650 // out above, the libc constructor will be called again (recursively!).
1651 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001653 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001654 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001655 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001656 name, preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001657 }
1658
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001659 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001660 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001661 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001662
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001663 TRACE("\"%s\": calling constructors", name);
1664
1665 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001666 call_function("DT_INIT", init_func_);
1667 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001668}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001669
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001670void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001671 if (!constructors_called) {
1672 return;
1673 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001674 TRACE("\"%s\": calling destructors", name);
1675
1676 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001677 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001678
1679 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001680 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001681
1682 // This is needed on second call to dlopen
1683 // after library has been unloaded with RTLD_NODELETE
1684 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001685}
1686
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001687void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001688 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001689 child->parents_.push_back(this);
1690 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001691 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001692}
1693
1694void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001695 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001696 return;
1697 }
1698
1699 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001700 children_.for_each([&] (soinfo* child) {
1701 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001702 return parent == this;
1703 });
1704 });
1705
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001706 parents_.for_each([&] (soinfo* parent) {
1707 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001708 return child == this;
1709 });
1710 });
1711
1712 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001713 parents_.clear();
1714 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001715}
1716
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001717dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001718 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001719 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001720 }
1721
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001722 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001723};
1724
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001725ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001726 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001727 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001728 }
1729
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001730 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001731}
1732
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001733off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001734 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001735 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001736 }
1737
1738 return 0;
1739}
1740
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001741uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001742 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001743 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001744 }
1745
1746 return 0;
1747}
1748
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001749uint32_t soinfo::get_dt_flags_1() const {
1750 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001751 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001752 }
1753
1754 return 0;
1755}
1756void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1757 if (has_min_version(1)) {
1758 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001759 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001760 }
1761
1762 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001763 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001764 }
1765
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001766 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001767 }
1768}
1769
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001770// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001771// 'this->flags' does not have FLAG_NEW_SOINFO set.
1772static soinfo::soinfo_list_t g_empty_list;
1773
1774soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001775 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001776 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001777 }
1778
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001779 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001780}
1781
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001782soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001783 if (has_min_version(0)) {
1784 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001785 }
1786
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001787 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001788}
1789
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001790ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1791 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1792 return call_ifunc_resolver(s->st_value + load_bias);
1793 }
1794
1795 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1796}
1797
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001798const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001799 if (has_min_version(1) && (index >= strtab_size_)) {
1800 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001801 }
1802
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001803 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001804}
1805
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001806bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001807 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001808}
1809
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001810bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001811 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001812}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001813
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001814bool soinfo::is_linked() const {
1815 return (flags_ & FLAG_LINKED) != 0;
1816}
1817
1818bool soinfo::is_main_executable() const {
1819 return (flags_ & FLAG_EXE) != 0;
1820}
1821
1822void soinfo::set_linked() {
1823 flags_ |= FLAG_LINKED;
1824}
1825
1826void soinfo::set_linker_flag() {
1827 flags_ |= FLAG_LINKER;
1828}
1829
1830void soinfo::set_main_executable() {
1831 flags_ |= FLAG_EXE;
1832}
1833
1834void soinfo::increment_ref_count() {
1835 local_group_root_->ref_count_++;
1836}
1837
1838size_t soinfo::decrement_ref_count() {
1839 return --local_group_root_->ref_count_;
1840}
1841
1842soinfo* soinfo::get_local_group_root() const {
1843 return local_group_root_;
1844}
1845
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001846/* Force any of the closed stdin, stdout and stderr to be associated with
1847 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001848static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001849 int dev_null, i, status;
1850 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001851
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001852 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
1853 if (dev_null < 0) {
1854 DL_ERR("cannot open /dev/null: %s", strerror(errno));
1855 return -1;
1856 }
1857 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001858
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001859 /* If any of the stdio file descriptors is valid and not associated
1860 with /dev/null, dup /dev/null to it. */
1861 for (i = 0; i < 3; i++) {
1862 /* If it is /dev/null already, we are done. */
1863 if (i == dev_null) {
1864 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001865 }
1866
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001867 TRACE("[ Nullifying stdio file descriptor %d]", i);
1868 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
1869
1870 /* If file is opened, we are good. */
1871 if (status != -1) {
1872 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001873 }
1874
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001875 /* The only error we allow is that the file descriptor does not
1876 exist, in which case we dup /dev/null to it. */
1877 if (errno != EBADF) {
1878 DL_ERR("fcntl failed: %s", strerror(errno));
1879 return_value = -1;
1880 continue;
1881 }
1882
1883 /* Try dupping /dev/null to this stdio file descriptor and
1884 repeat if there is a signal. Note that any errors in closing
1885 the stdio descriptor are lost. */
1886 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
1887 if (status < 0) {
1888 DL_ERR("dup2 failed: %s", strerror(errno));
1889 return_value = -1;
1890 continue;
1891 }
1892 }
1893
1894 /* If /dev/null is not one of the stdio file descriptors, close it. */
1895 if (dev_null > 2) {
1896 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
1897 status = TEMP_FAILURE_RETRY(close(dev_null));
1898 if (status == -1) {
1899 DL_ERR("close failed: %s", strerror(errno));
1900 return_value = -1;
1901 }
1902 }
1903
1904 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001905}
1906
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001907bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08001908 /* Extract dynamic section */
1909 ElfW(Word) dynamic_flags = 0;
1910 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07001911
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001912 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001913 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001914 if (!relocating_linker) {
1915 INFO("[ linking %s ]", name);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001916 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001917 }
1918
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001919 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001920 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001921 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02001922 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001923 return false;
1924 } else {
1925 if (!relocating_linker) {
1926 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001927 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001928 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001929
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001930#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001931 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
1932 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02001933#endif
1934
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001935 // Extract useful information from dynamic section.
1936 uint32_t needed_count = 0;
1937 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
1938 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1939 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1940 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001941 case DT_SONAME:
1942 // TODO: glibc dynamic linker uses this name for
1943 // initial library lookup; consider doing the same here.
1944 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001945
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001946 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001947 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
1948 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
1949 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
1950 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001951 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001952
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001953 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001954 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001955 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001956 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
1957 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001958
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001959 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001960 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001961 // amend chain for symndx = header[1]
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001962 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ - reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001963
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001964 if (!powerof2(gnu_maskwords_)) {
1965 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 -08001966 return false;
1967 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001968 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001969
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001970 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001971 break;
1972
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001973 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001974 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001975 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001976
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001977 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001978 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001979 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001980
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001981 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001982 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001983 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001984
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07001985 case DT_SYMENT:
1986 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001987 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 -07001988 return false;
1989 }
1990 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07001991
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001992 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07001993#if defined(USE_RELA)
1994 if (d->d_un.d_val != DT_RELA) {
1995 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001996 return false;
1997 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07001998#else
1999 if (d->d_un.d_val != DT_REL) {
2000 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
2001 return false;
2002 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002003#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002004 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002005
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002006 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002007#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002008 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002009#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002010 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002011#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002012 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002013
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002014 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002015#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002016 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002017#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002018 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002019#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002020 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002021
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002022 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002023#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002024 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002025 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002026#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002027 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2028 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002029
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002030 case DT_DEBUG:
2031 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2032 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002033// FIXME: not working currently for N64
2034// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002035// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002036// read-only, but the DYNAMIC header claims it is writable.
2037#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002038 if ((dynamic_flags & PF_W) != 0) {
2039 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2040 }
Chris Dearman99186652014-02-06 20:36:51 -08002041#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002042 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002043#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002044 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002045 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002046 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002047
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002048 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002049 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002050 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002051
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002052 case DT_ANDROID_RELA:
2053 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2054 break;
2055
2056 case DT_ANDROID_RELASZ:
2057 android_relocs_size_ = d->d_un.d_val;
2058 break;
2059
2060 case DT_ANDROID_REL:
2061 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", name);
2062 return false;
2063
2064 case DT_ANDROID_RELSZ:
2065 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", name);
2066 return false;
2067
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002068 case DT_RELAENT:
2069 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002070 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002071 return false;
2072 }
2073 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002074
2075 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002076 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002077 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002078
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002079 case DT_REL:
2080 DL_ERR("unsupported DT_REL in \"%s\"", name);
2081 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002082
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002083 case DT_RELSZ:
2084 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2085 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002086
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002087#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002088 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002089 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002090 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002091
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002092 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002093 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002094 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002095
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002096 case DT_RELENT:
2097 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002098 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002099 return false;
2100 }
2101 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002102
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002103 case DT_ANDROID_REL:
2104 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2105 break;
2106
2107 case DT_ANDROID_RELSZ:
2108 android_relocs_size_ = d->d_un.d_val;
2109 break;
2110
2111 case DT_ANDROID_RELA:
2112 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", name);
2113 return false;
2114
2115 case DT_ANDROID_RELASZ:
2116 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", name);
2117 return false;
2118
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002119 // "Indicates that all RELATIVE relocations have been concatenated together,
2120 // and specifies the RELATIVE relocation count."
2121 //
2122 // TODO: Spec also mentions that this can be used to optimize relocation process;
2123 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002124 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002125 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002126
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002127 case DT_RELA:
2128 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2129 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002130
2131 case DT_RELASZ:
2132 DL_ERR("unsupported DT_RELASZ in \"%s\"", name);
2133 return false;
2134
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002135#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002136 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002137 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2138 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002139 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002140
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002141 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002142 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2143 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002144 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002145
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002146 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002147 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2148 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002149 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002150
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002151 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002152 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002153 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002154
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002155 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002156 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2157 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002158 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002159
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002160 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002161 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002162 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002163
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002164 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002165 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2166 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002167 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002168
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002169 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002170 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002171 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002172
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002173 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002174#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002175 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2176 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002177#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002178 has_text_relocations = true;
2179 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002180#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002181
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002182 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002183 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002184 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002185
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002186 case DT_NEEDED:
2187 ++needed_count;
2188 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002189
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002190 case DT_FLAGS:
2191 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002192#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002193 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2194 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002195#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002196 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002197#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002198 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002199 if (d->d_un.d_val & DF_SYMBOLIC) {
2200 has_DT_SYMBOLIC = true;
2201 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002202 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002203
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002204 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002205 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002206
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002207 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002208 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2209 }
2210 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002211#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002212 case DT_MIPS_RLD_MAP:
2213 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2214 {
2215 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2216 *dp = &_r_debug;
2217 }
2218 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002219 case DT_MIPS_RLD_MAP2:
2220 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2221 {
2222 r_debug** dp = reinterpret_cast<r_debug**>(reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
2223 *dp = &_r_debug;
2224 }
2225 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002226
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002227 case DT_MIPS_RLD_VERSION:
2228 case DT_MIPS_FLAGS:
2229 case DT_MIPS_BASE_ADDRESS:
2230 case DT_MIPS_UNREFEXTNO:
2231 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002232
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002233 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002234 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002235 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002236
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002237 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002238 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002239 break;
2240
2241 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002242 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002243 break;
2244#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002245 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2246 case DT_BIND_NOW:
2247 break;
2248
2249 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002250 case DT_VERSYM:
2251 case DT_VERDEF:
2252 case DT_VERDEFNUM:
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002253 case DT_VERNEED:
2254 case DT_VERNEEDNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002255 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002256
2257 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002258 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002259 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002260 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2261 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002262 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002263 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002264 }
2265
2266 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002267 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002268
2269 // Sanity checks.
2270 if (relocating_linker && needed_count != 0) {
2271 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2272 return false;
2273 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002274 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002275 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 -07002276 return false;
2277 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002278 if (strtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002279 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2280 return false;
2281 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002282 if (symtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002283 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2284 return false;
2285 }
2286 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002287}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002288
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002289bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
2290 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002291
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002292 local_group_root_ = local_group.front();
2293 if (local_group_root_ == nullptr) {
2294 local_group_root_ = this;
2295 }
2296
Elliott Hughese4d792a2013-10-28 14:19:05 -07002297#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002298 if (has_text_relocations) {
2299 // Make segments writable to allow text relocations to work properly. We will later call
2300 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2301 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2302 "security hardening. Please fix.", name);
2303 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2304 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2305 name, strerror(errno));
2306 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002307 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002308 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002309#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002310
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002311 if (android_relocs_ != nullptr) {
2312 // check signature
2313 if (android_relocs_size_ > 3 &&
2314 android_relocs_[0] == 'A' &&
2315 android_relocs_[1] == 'P' &&
2316 (android_relocs_[2] == 'U' || android_relocs_[2] == 'S') &&
2317 android_relocs_[3] == '2') {
2318 DEBUG("[ android relocating %s ]", name);
2319
2320 bool relocated = false;
2321 const uint8_t* packed_relocs = android_relocs_ + 4;
2322 const size_t packed_relocs_size = android_relocs_size_ - 4;
2323
2324 if (android_relocs_[2] == 'U') {
2325 relocated = relocate(
2326 packed_reloc_iterator<leb128_decoder>(
2327 leb128_decoder(packed_relocs, packed_relocs_size)),
2328 global_group, local_group);
2329 } else { // android_relocs_[2] == 'S'
2330 relocated = relocate(
2331 packed_reloc_iterator<sleb128_decoder>(
2332 sleb128_decoder(packed_relocs, packed_relocs_size)),
2333 global_group, local_group);
2334 }
2335
2336 if (!relocated) {
2337 return false;
2338 }
2339 } else {
2340 DL_ERR("bad android relocation header.");
2341 return false;
2342 }
2343 }
2344
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002345#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002346 if (rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002347 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002348 if (!relocate(plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002349 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002350 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002351 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002352 if (plt_rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002353 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002354 if (!relocate(plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002355 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002356 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002357 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002358#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002359 if (rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002360 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002361 if (!relocate(plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002362 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002363 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002364 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002365 if (plt_rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002366 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002367 if (!relocate(plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002368 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002369 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002370 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002371#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002372
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002373#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08002374 if (!mips_relocate_got(global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002375 return false;
2376 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002377#endif
2378
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002379 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002380
Elliott Hughese4d792a2013-10-28 14:19:05 -07002381#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002382 if (has_text_relocations) {
2383 // All relocations are done, we can protect our segments back to read-only.
2384 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2385 DL_ERR("can't protect segments for \"%s\": %s",
2386 name, strerror(errno));
2387 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002388 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002389 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002390#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002391
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002392 /* We can also turn on GNU RELRO protection */
2393 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2394 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2395 name, strerror(errno));
2396 return false;
2397 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002398
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002399 /* Handle serializing/sharing the RELRO segment */
2400 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2401 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2402 extinfo->relro_fd) < 0) {
2403 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2404 name, strerror(errno));
2405 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002406 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002407 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2408 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2409 extinfo->relro_fd) < 0) {
2410 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2411 name, strerror(errno));
2412 return false;
2413 }
2414 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002415
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002416 notify_gdb_of_load(this);
2417 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002418}
2419
Nick Kralevich468319c2011-11-11 15:53:17 -08002420/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002421 * This function add vdso to internal dso list.
2422 * It helps to stack unwinding through signal handlers.
2423 * Also, it makes bionic more like glibc.
2424 */
Kito Cheng812fd422014-03-25 22:53:56 +08002425static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002426#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002427 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002428 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002429 return;
2430 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002431
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002432 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002433
Elliott Hughes0266ae52014-02-10 17:46:57 -08002434 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2435 si->phnum = ehdr_vdso->e_phnum;
2436 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2437 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002438 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002439
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002440 si->prelink_image();
2441 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002442#endif
2443}
2444
2445/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002446 * This is linker soinfo for GDB. See details below.
2447 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002448#if defined(__LP64__)
2449#define LINKER_PATH "/system/bin/linker64"
2450#else
2451#define LINKER_PATH "/system/bin/linker"
2452#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002453static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002454
2455/* gdb expects the linker to be in the debug shared object list.
2456 * Without this, gdb has trouble locating the linker's ".text"
2457 * and ".plt" sections. Gdb could also potentially use this to
2458 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2459 * Don't use soinfo_alloc(), because the linker shouldn't
2460 * be on the soinfo list.
2461 */
2462static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002463 linker_soinfo_for_gdb.base = linker_base;
2464
2465 /*
2466 * Set the dynamic field in the link map otherwise gdb will complain with
2467 * the following:
2468 * warning: .dynamic section for "/system/bin/linker" is not at the
2469 * expected address (wrong library or version mismatch?)
2470 */
2471 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2472 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2473 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002474 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002475 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2476}
2477
2478/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002479 * This code is called after the linker has linked itself and
2480 * fixed it's own GOT. It is safe to make references to externs
2481 * and other non-local data at this point.
2482 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002483static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002484#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002485 struct timeval t0, t1;
2486 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002487#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002488
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002489 // Initialize environment functions, and get to the ELF aux vectors table.
2490 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002491
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002492 // If this is a setuid/setgid program, close the security hole described in
2493 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2494 if (get_AT_SECURE()) {
2495 nullify_closed_stdio();
2496 }
2497
2498 debuggerd_init();
2499
2500 // Get a few environment variables.
2501 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2502 if (LD_DEBUG != nullptr) {
2503 g_ld_debug_verbosity = atoi(LD_DEBUG);
2504 }
2505
2506 // Normally, these are cleaned by linker_env_init, but the test
2507 // doesn't cost us anything.
2508 const char* ldpath_env = nullptr;
2509 const char* ldpreload_env = nullptr;
2510 if (!get_AT_SECURE()) {
2511 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2512 ldpreload_env = linker_env_get("LD_PRELOAD");
2513 }
2514
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -08002515#if !defined(__LP64__)
2516 if (personality(PER_LINUX32) == -1) {
2517 __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
2518 }
2519#endif
2520
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002521 INFO("[ android linker & debugger ]");
2522
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002523 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002524 if (si == nullptr) {
2525 exit(EXIT_FAILURE);
2526 }
2527
2528 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002529 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002530 link_map* map = &(si->link_map_head);
2531
2532 map->l_addr = 0;
2533 map->l_name = args.argv[0];
2534 map->l_prev = nullptr;
2535 map->l_next = nullptr;
2536
2537 _r_debug.r_map = map;
2538 r_debug_tail = map;
2539
2540 init_linker_info_for_gdb(linker_base);
2541
2542 // Extract information passed from the kernel.
2543 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2544 si->phnum = args.getauxval(AT_PHNUM);
2545 si->entry = args.getauxval(AT_ENTRY);
2546
2547 /* Compute the value of si->base. We can't rely on the fact that
2548 * the first entry is the PHDR because this will not be true
2549 * for certain executables (e.g. some in the NDK unit test suite)
2550 */
2551 si->base = 0;
2552 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2553 si->load_bias = 0;
2554 for (size_t i = 0; i < si->phnum; ++i) {
2555 if (si->phdr[i].p_type == PT_PHDR) {
2556 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2557 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2558 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002559 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002560 }
2561 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002562
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002563 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2564 if (elf_hdr->e_type != ET_DYN) {
2565 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2566 exit(EXIT_FAILURE);
2567 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002568
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002569 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2570 parse_LD_LIBRARY_PATH(ldpath_env);
2571 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002572
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002573 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002574
Dmitriy Ivanov67181252015-01-07 15:48:25 -08002575 if (!si->prelink_image()) {
2576 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2577 exit(EXIT_FAILURE);
2578 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002579
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002580 // add somain to global group
2581 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2582
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002583 // Load ld_preloads and dependencies.
2584 StringLinkedList needed_library_name_list;
2585 size_t needed_libraries_count = 0;
2586 size_t ld_preloads_count = 0;
2587 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2588 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2589 ++needed_libraries_count;
2590 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002591
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002592 for_each_dt_needed(si, [&](const char* name) {
2593 needed_library_name_list.push_back(name);
2594 ++needed_libraries_count;
2595 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002596
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002597 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002598
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002599 memset(needed_library_names, 0, sizeof(needed_library_names));
2600 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002601
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002602 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 -07002603 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2604 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002605 } else if (needed_libraries_count == 0) {
2606 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
2607 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2608 exit(EXIT_FAILURE);
2609 }
2610 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002611 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002612
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002613 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002614
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002615 {
2616 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06002617
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002618 si->call_pre_init_constructors();
2619
2620 /* After the prelink_image, the si->load_bias is initialized.
2621 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2622 * We need to update this value for so exe here. So Unwind_Backtrace
2623 * for some arch like x86 could work correctly within so exe.
2624 */
2625 map->l_addr = si->load_bias;
2626 si->call_constructors();
2627 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002628
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002629#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002630 gettimeofday(&t1, nullptr);
2631 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2632 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2633 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002634#endif
2635#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002636 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2637 linker_stats.count[kRelocAbsolute],
2638 linker_stats.count[kRelocRelative],
2639 linker_stats.count[kRelocCopy],
2640 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002641#endif
2642#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002643 {
2644 unsigned n;
2645 unsigned i;
2646 unsigned count = 0;
2647 for (n = 0; n < 4096; n++) {
2648 if (bitmask[n]) {
2649 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002650#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002651 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002652#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002653 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002654#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002655 if (x & 1) {
2656 count++;
2657 }
2658 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002659 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002660 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002661 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002662 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2663 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002664#endif
2665
2666#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002667 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002668#endif
2669
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002670 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2671 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002672}
Nick Kralevich468319c2011-11-11 15:53:17 -08002673
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002674/* Compute the load-bias of an existing executable. This shall only
2675 * be used to compute the load bias of an executable or shared library
2676 * that was loaded by the kernel itself.
2677 *
2678 * Input:
2679 * elf -> address of ELF header, assumed to be at the start of the file.
2680 * Return:
2681 * load bias, i.e. add the value of any p_vaddr in the file to get
2682 * the corresponding address in memory.
2683 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002684static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2685 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002686 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002687 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002688
Elliott Hughes0266ae52014-02-10 17:46:57 -08002689 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002690 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002691 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002692 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002693 }
2694 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002695}
2696
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002697extern "C" void _start();
2698
Nick Kralevich468319c2011-11-11 15:53:17 -08002699/*
2700 * This is the entry point for the linker, called from begin.S. This
2701 * method is responsible for fixing the linker's own relocations, and
2702 * then calling __linker_init_post_relocation().
2703 *
2704 * Because this method is called before the linker has fixed it's own
2705 * relocations, any attempt to reference an extern variable, extern
2706 * function, or other GOT reference will generate a segfault.
2707 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002708extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002709 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002710
Elliott Hughes0266ae52014-02-10 17:46:57 -08002711 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002712 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002713 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002714 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002715
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002716 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002717
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002718 // If the linker is not acting as PT_INTERP entry_point is equal to
2719 // _start. Which means that the linker is running as an executable and
2720 // already linked by PT_INTERP.
2721 //
2722 // This happens when user tries to run 'adb shell /system/bin/linker'
2723 // see also https://code.google.com/p/android/issues/detail?id=63174
2724 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2725 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2726 }
2727
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002728 linker_so.base = linker_addr;
2729 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2730 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002731 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002732 linker_so.phdr = phdr;
2733 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002734 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07002735
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002736 // This might not be obvious... The reasons why we pass g_empty_list
2737 // in place of local_group here are (1) we do not really need it, because
2738 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2739 // itself without having to look into local_group and (2) allocators
2740 // are not yet initialized, and therefore we cannot use linked_list.push_*
2741 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002742 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002743 // It would be nice to print an error message, but if the linker
2744 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002745 // call write() (because it involves a GOT reference). We may as
2746 // well try though...
2747 const char* msg = "CANNOT LINK EXECUTABLE: ";
2748 write(2, msg, strlen(msg));
2749 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2750 write(2, "\n", 1);
2751 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002752 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002753
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002754 __libc_init_tls(args);
2755
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002756 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002757 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002758
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002759 // Initialize static variables. Note that in order to
2760 // get correct libdl_info we need to call constructors
2761 // before get_libdl_info().
2762 solist = get_libdl_info();
2763 sonext = get_libdl_info();
2764
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002765 // We have successfully fixed our own relocations. It's safe to run
2766 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002767 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002768 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002769
Elliott Hughes611f9562015-01-23 10:43:58 -08002770 INFO("[ jumping to _start ]");
2771
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002772 // Return the address that the calling assembly stub should jump to.
2773 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002774}