blob: 15ea423f9cdc96a53de288f45c61f330cff02abc [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"
Simon Baldwinaef71952015-01-16 13:22:54 +000060#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
63 *
64 * Do NOT use malloc() and friends or pthread_*() code here.
65 * Don't use printf() either; it's caused mysterious memory
66 * corruption in the past.
67 * The linker runs before we bring up libc and it's easiest
68 * to make sure it does not depend on any complex libc features
69 *
70 * open issues / todo:
71 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073 * - after linking, set as much stuff as possible to READONLY
74 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070075 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080077// Override macros to use C++ style casts
78#undef ELF_ST_TYPE
79#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
80
Dmitriy Ivanov489e4982014-05-19 15:19:52 -070081#if defined(__LP64__)
82#define SEARCH_NAME(x) x
83#else
84// Nvidia drivers are relying on the bug:
85// http://code.google.com/p/android/issues/detail?id=6670
86// so we continue to use base-name lookup for lp32
87static const char* get_base_name(const char* name) {
88 const char* bname = strrchr(name, '/');
89 return bname ? bname + 1 : name;
90}
91#define SEARCH_NAME(x) get_base_name(x)
92#endif
93
Elliott Hughes0266ae52014-02-10 17:46:57 -080094static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070096static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
97static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020098
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070099static soinfo* solist;
100static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700101static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800102
Elliott Hughes1728b232014-05-14 10:02:03 -0700103static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700104#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700105 "/vendor/lib64",
106 "/system/lib64",
107#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700108 "/vendor/lib",
109 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700110#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700111 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700112};
David Bartleybc3a5c22009-06-02 18:27:28 -0700113
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800114#define LDPATH_BUFSIZE (LDPATH_MAX*64)
115#define LDPATH_MAX 8
116
117#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
118#define LDPRELOAD_MAX 8
119
Elliott Hughes1728b232014-05-14 10:02:03 -0700120static char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
121static const char* g_ld_library_paths[LDPATH_MAX + 1];
Elliott Hughes124fae92012-10-31 14:20:03 -0700122
Elliott Hughes1728b232014-05-14 10:02:03 -0700123static char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
124static const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600125
Elliott Hughes1728b232014-05-14 10:02:03 -0700126static soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
Matt Fischer4fd42c12009-12-31 12:09:10 -0600127
Elliott Hughes1728b232014-05-14 10:02:03 -0700128__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700130__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700131
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700133struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700134 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700135};
136
137static linker_stats_t linker_stats;
138
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800139void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700140 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700141}
142#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800143void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700144}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145#endif
146
147#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800148uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149#endif
150
Dima Zavin2e855792009-05-20 18:28:09 -0700151static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700152
Elliott Hughes650be4e2013-03-05 18:47:58 -0800153char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700154 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700155}
156
Elliott Hughes650be4e2013-03-05 18:47:58 -0800157size_t linker_get_error_buffer_size() {
158 return sizeof(__linker_dl_err_buf);
159}
160
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700161// This function is an empty stub where GDB locates a breakpoint to get notified
162// about linker activity.
Elliott Hughes5419b942012-10-16 15:54:46 -0700163extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164
Elliott Hughes1728b232014-05-14 10:02:03 -0700165static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700166static 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 -0800167static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800169static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700170 // Copy the necessary fields into the debug structure.
171 link_map* map = &(info->link_map_head);
172 map->l_addr = info->load_bias;
Dmitriy Ivanovc9d16582014-10-24 14:46:12 -0700173 map->l_name = info->name;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700174 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700176 // Stick the new library at the end of the list.
177 // gdb tends to care more about libc than it does
178 // about leaf libraries, and ordering it this way
179 // reduces the back-and-forth over the wire.
180 if (r_debug_tail) {
181 r_debug_tail->l_next = map;
182 map->l_prev = r_debug_tail;
183 map->l_next = 0;
184 } else {
185 _r_debug.r_map = map;
186 map->l_prev = 0;
187 map->l_next = 0;
188 }
189 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190}
191
Elliott Hughesbedfe382012-08-14 14:07:59 -0700192static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700193 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700194
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700195 if (r_debug_tail == map) {
196 r_debug_tail = map->l_prev;
197 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700198
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700199 if (map->l_prev) {
200 map->l_prev->l_next = map->l_next;
201 }
202 if (map->l_next) {
203 map->l_next->l_prev = map->l_prev;
204 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700205}
206
Elliott Hughesbedfe382012-08-14 14:07:59 -0700207static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800208 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700209 // GDB already knows about the main executable
210 return;
211 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700213 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700215 _r_debug.r_state = r_debug::RT_ADD;
216 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700218 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700220 _r_debug.r_state = r_debug::RT_CONSISTENT;
221 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700222}
223
Elliott Hughesbedfe382012-08-14 14:07:59 -0700224static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800225 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700226 // GDB already knows about the main executable
227 return;
228 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700229
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700230 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700231
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700232 _r_debug.r_state = r_debug::RT_DELETE;
233 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700234
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700235 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700236
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700237 _r_debug.r_state = r_debug::RT_CONSISTENT;
238 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239}
240
Elliott Hughes18a206c2012-10-29 17:37:13 -0700241void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800242 _r_debug.r_state = r_debug::RT_ADD;
243 rtld_db_dlactivity();
244 _r_debug.r_state = r_debug::RT_CONSISTENT;
245 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246}
247
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700248LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
249 return g_soinfo_links_allocator.alloc();
250}
251
252void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
253 g_soinfo_links_allocator.free(entry);
254}
255
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700256static 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 +0200257 if (strlen(name) >= SOINFO_NAME_LEN) {
258 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700259 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200260 }
261
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700262 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700263
Magnus Malmbornba98d922012-09-12 13:00:55 +0200264 sonext->next = si;
265 sonext = si;
266
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700267 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200268 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269}
270
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800271static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700272 if (si == nullptr) {
273 return;
274 }
275
276 if (si->base != 0 && si->size != 0) {
277 munmap(reinterpret_cast<void*>(si->base), si->size);
278 }
279
280 soinfo *prev = nullptr, *trav;
281
282 TRACE("name %s: freeing soinfo @ %p", si->name, si);
283
284 for (trav = solist; trav != nullptr; trav = trav->next) {
285 if (trav == si) {
286 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700287 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700288 prev = trav;
289 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800290
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700291 if (trav == nullptr) {
292 // si was not in solist
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -0800293 DL_ERR("name \"%s\"@%p is not in solist!", si->name, si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700294 return;
295 }
Elliott Hughes46882792012-08-03 16:49:39 -0700296
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700297 // clear links to/from si
298 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700299
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700300 // prev will never be null, because the first entry in solist is
301 // always the static libdl_info.
302 prev->next = si->next;
303 if (si == sonext) {
304 sonext = prev;
305 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800306
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700307 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308}
309
Elliott Hughescade4c32012-12-20 14:42:14 -0800310static void parse_path(const char* path, const char* delimiters,
311 const char** array, char* buf, size_t buf_size, size_t max_count) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700312 if (path == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800313 return;
314 }
315
316 size_t len = strlcpy(buf, path, buf_size);
317
318 size_t i = 0;
319 char* buf_p = buf;
320 while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
321 if (*array[i] != '\0') {
322 ++i;
323 }
324 }
325
326 // Forget the last path if we had to truncate; this occurs if the 2nd to
327 // last char isn't '\0' (i.e. wasn't originally a delimiter).
328 if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700329 array[i - 1] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800330 } else {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700331 array[i] = nullptr;
Elliott Hughescade4c32012-12-20 14:42:14 -0800332 }
333}
334
335static void parse_LD_LIBRARY_PATH(const char* path) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700336 parse_path(path, ":", g_ld_library_paths,
337 g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800338}
339
340static void parse_LD_PRELOAD(const char* path) {
341 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Elliott Hughes1728b232014-05-14 10:02:03 -0700342 parse_path(path, " :", g_ld_preload_names,
343 g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
Elliott Hughescade4c32012-12-20 14:42:14 -0800344}
345
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700346#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700347
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700348// For a given PC, find the .so that it belongs to.
349// Returns the base address of the .ARM.exidx section
350// for that .so, and the number of 8-byte entries
351// in that section (via *pcount).
352//
353// Intended to be called by libc's __gnu_Unwind_Find_exidx().
354//
355// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800356_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800357 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800358
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700359 for (soinfo* si = solist; si != 0; si = si->next) {
360 if ((addr >= si->base) && (addr < (si->base + si->size))) {
361 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800362 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800363 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700364 }
365 *pcount = 0;
366 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800367}
Elliott Hughes46882792012-08-03 16:49:39 -0700368
Christopher Ferris24053a42013-08-19 17:45:09 -0700369#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700370
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700371// Here, we only have to provide a callback to iterate across all the
372// loaded libraries. gcc_eh does the rest.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800373int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700374 int rv = 0;
375 for (soinfo* si = solist; si != nullptr; si = si->next) {
376 dl_phdr_info dl_info;
377 dl_info.dlpi_addr = si->link_map_head.l_addr;
378 dl_info.dlpi_name = si->link_map_head.l_name;
379 dl_info.dlpi_phdr = si->phdr;
380 dl_info.dlpi_phnum = si->phnum;
381 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
382 if (rv != 0) {
383 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800384 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700385 }
386 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800387}
Elliott Hughes46882792012-08-03 16:49:39 -0700388
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800389ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name) {
390 return is_gnu_hash() ? gnu_lookup(symbol_name) : elf_lookup(symbol_name);
391}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800392
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800393static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
394 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
395 ELF_ST_BIND(s->st_info) == STB_WEAK) {
396 return s->st_shndx != SHN_UNDEF;
397 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
398 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
399 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->name);
400 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800401
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800402 return false;
403}
404
405ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name) {
406 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800407 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800408
409 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800410 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
411 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800412
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700413 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
414 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
415
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800416 // test against bloom filter
417 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700418 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
419 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
420
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800421 return nullptr;
422 }
423
424 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700425 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800426
427 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700428 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
429 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
430
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800431 return nullptr;
432 }
433
434 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800435 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700436 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800437 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
438 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700439 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
440 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
441 static_cast<size_t>(s->st_size));
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800442 return s;
443 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700444 } while ((gnu_chain_[n++] & 1) == 0);
445
446 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
447 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800448
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800449 return nullptr;
450}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800451
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800452ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name) {
453 uint32_t hash = symbol_name.elf_hash();
454
455 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800456 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800457
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800458 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
459 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800460 if (strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && is_symbol_global_and_defined(this, s)) {
461 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
462 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
463 static_cast<size_t>(s->st_size));
464 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800465 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800466 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800467
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700468 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800469 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700470
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700471 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800472}
473
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700474soinfo::soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700475 memset(this, 0, sizeof(*this));
476
477 strlcpy(this->name, name, sizeof(this->name));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800478 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800479 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700480
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700481 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800482 this->st_dev_ = file_stat->st_dev;
483 this->st_ino_ = file_stat->st_ino;
484 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700485 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700486
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800487 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700488}
489
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800490
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800491uint32_t SymbolName::elf_hash() {
492 if (!has_elf_hash_) {
493 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
494 uint32_t h = 0, g;
495
496 while (*name) {
497 h = (h << 4) + *name++;
498 g = h & 0xf0000000;
499 h ^= g;
500 h ^= g >> 24;
501 }
502
503 elf_hash_ = h;
504 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700505 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800506
507 return elf_hash_;
508}
509
510uint32_t SymbolName::gnu_hash() {
511 if (!has_gnu_hash_) {
512 uint32_t h = 5381;
513 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
514 while (*name != 0) {
515 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
516 }
517
518 gnu_hash_ = h;
519 has_gnu_hash_ = true;
520 }
521
522 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800523}
524
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800525ElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700526 const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800527 SymbolName symbol_name(name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700528 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700529
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700530 /* "This element's presence in a shared object library alters the dynamic linker's
531 * symbol resolution algorithm for references within the library. Instead of starting
532 * a symbol search with the executable file, the dynamic linker starts from the shared
533 * object itself. If the shared object fails to supply the referenced symbol, the
534 * dynamic linker then searches the executable file and other shared objects as usual."
535 *
536 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
537 *
538 * Note that this is unlikely since static linker avoids generating
539 * relocations for -Bsymbolic linked dynamic executables.
540 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700541 if (si_from->has_DT_SYMBOLIC) {
542 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->name, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800543 s = si_from->find_symbol_by_name(symbol_name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700544 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700545 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700546 }
547 }
548
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700549 // 1. Look for it in global_group
550 if (s == nullptr) {
551 global_group.visit([&](soinfo* global_si) {
552 DEBUG("%s: looking up %s in %s (from global group)", si_from->name, name, global_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800553 s = global_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700554 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700555 *si_found_in = global_si;
556 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700557 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700558
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700559 return true;
560 });
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700561 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700562
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700563 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700564 if (s == nullptr) {
565 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700566 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700567 // we already did this - skip
568 return true;
569 }
570
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700571 DEBUG("%s: looking up %s in %s (from local group)", si_from->name, name, local_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800572 s = local_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700573 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700574 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700575 return false;
576 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700577
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700578 return true;
579 });
580 }
581
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700582 if (s != nullptr) {
583 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
584 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700585 si_from->name, name, reinterpret_cast<void*>(s->st_value),
586 (*si_found_in)->name, reinterpret_cast<void*>((*si_found_in)->base),
587 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700588 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700589
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700590 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700591}
592
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800593class ProtectedDataGuard {
594 public:
595 ProtectedDataGuard() {
596 if (ref_count_++ == 0) {
597 protect_data(PROT_READ | PROT_WRITE);
598 }
599 }
600
601 ~ProtectedDataGuard() {
602 if (ref_count_ == 0) { // overflow
603 __libc_fatal("Too many nested calls to dlopen()");
604 }
605
606 if (--ref_count_ == 0) {
607 protect_data(PROT_READ);
608 }
609 }
610 private:
611 void protect_data(int protection) {
612 g_soinfo_allocator.protect_all(protection);
613 g_soinfo_links_allocator.protect_all(protection);
614 }
615
616 static size_t ref_count_;
617};
618
619size_t ProtectedDataGuard::ref_count_ = 0;
620
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700621// Each size has it's own allocator.
622template<size_t size>
623class SizeBasedAllocator {
624 public:
625 static void* alloc() {
626 return allocator_.alloc();
627 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700628
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700629 static void free(void* ptr) {
630 allocator_.free(ptr);
631 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700632
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700633 private:
634 static LinkerBlockAllocator allocator_;
635};
636
637template<size_t size>
638LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
639
640template<typename T>
641class TypeBasedAllocator {
642 public:
643 static T* alloc() {
644 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
645 }
646
647 static void free(T* ptr) {
648 SizeBasedAllocator<sizeof(T)>::free(ptr);
649 }
650};
651
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700652class LoadTask {
653 public:
654 struct deleter_t {
655 void operator()(LoadTask* t) {
656 TypeBasedAllocator<LoadTask>::free(t);
657 }
658 };
659
660 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
661
662 static deleter_t deleter;
663
664 static LoadTask* create(const char* name, soinfo* needed_by) {
665 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
666 return new (ptr) LoadTask(name, needed_by);
667 }
668
669 const char* get_name() const {
670 return name_;
671 }
672
673 soinfo* get_needed_by() const {
674 return needed_by_;
675 }
676 private:
677 LoadTask(const char* name, soinfo* needed_by)
678 : name_(name), needed_by_(needed_by) {}
679
680 const char* name_;
681 soinfo* needed_by_;
682
683 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
684};
685
Ningsheng Jiane93be992014-09-16 15:22:10 +0800686LoadTask::deleter_t LoadTask::deleter;
687
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700688template <typename T>
689using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
690
691typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700692typedef linked_list_t<const char> StringLinkedList;
693typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700694
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700695
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700696// This function walks down the tree of soinfo dependencies
697// in breadth-first order and
698// * calls action(soinfo* si) for each node, and
699// * terminates walk if action returns false.
700//
701// walk_dependencies_tree returns false if walk was terminated
702// by the action and true otherwise.
703template<typename F>
704static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700705 SoinfoLinkedList visit_list;
706 SoinfoLinkedList visited;
707
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700708 for (size_t i = 0; i < root_soinfos_size; ++i) {
709 visit_list.push_back(root_soinfos[i]);
710 }
711
712 soinfo* si;
713 while ((si = visit_list.pop_front()) != nullptr) {
714 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700715 continue;
716 }
717
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700718 if (!action(si)) {
719 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700720 }
721
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700722 visited.push_back(si);
723
724 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700725 visit_list.push_back(child);
726 });
727 }
728
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700729 return true;
730}
731
732
733// This is used by dlsym(3). It performs symbol lookup only within the
734// specified soinfo object and its dependencies in breadth first order.
735ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
736 ElfW(Sym)* result = nullptr;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800737 SymbolName symbol_name(name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700738
739
740 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800741 result = current_soinfo->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700742 if (result != nullptr) {
743 *found = current_soinfo;
744 return false;
745 }
746
747 return true;
748 });
749
750 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800751}
752
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800753/* This is used by dlsym(3) to performs a global symbol lookup. If the
754 start value is null (for RTLD_DEFAULT), the search starts at the
755 beginning of the global solist. Otherwise the search starts at the
756 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700757 */
Dmitriy Ivanov02aa7052014-08-18 15:08:51 -0700758ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800759 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800760
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700761 if (start == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800762 start = solist;
763 }
764
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700765 ElfW(Sym)* s = nullptr;
766 for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700767 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
768 continue;
769 }
770
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800771 s = si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700772 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800773 *found = si;
774 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600775 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800776 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600777
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700778 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700779 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
780 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800781 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800782
Elliott Hughescade4c32012-12-20 14:42:14 -0800783 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800784}
785
Kito Chengfa8c05d2013-03-12 14:58:06 +0800786soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800787 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700788 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800789 if (address >= si->base && address - si->base < si->size) {
790 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600791 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800792 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700793 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600794}
795
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800796ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
797 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
798}
799
800static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
801 return sym->st_shndx != SHN_UNDEF &&
802 soaddr >= sym->st_value &&
803 soaddr < sym->st_value + sym->st_size;
804}
805
806ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800807 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800808
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700809 for (size_t i = 0; i < gnu_nbucket_; ++i) {
810 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800811
812 if (n == 0) {
813 continue;
814 }
815
816 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800817 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800818 if (symbol_matches_soaddr(sym, soaddr)) {
819 return sym;
820 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700821 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800822 }
823
824 return nullptr;
825}
826
827ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800828 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600829
Kito Chengfa8c05d2013-03-12 14:58:06 +0800830 // Search the library's symbol table for any defined symbol which
831 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800832 for (size_t i = 0; i < nchain_; ++i) {
833 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800834 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800835 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600836 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800837 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600838
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700839 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600840}
841
Simon Baldwinaef71952015-01-16 13:22:54 +0000842static int open_library_in_zipfile(const char* const path,
843 off64_t* file_offset) {
844 TRACE("Trying zip file open from path '%s'", path);
845
846 // Treat an '!' character inside a path as the separator between the name
847 // of the zip file on disk and the subdirectory to search within it.
848 // For example, if path is "foo.zip!bar/bas/x.so", then we search for
849 // "bar/bas/x.so" within "foo.zip".
850 const char* separator = strchr(path, '!');
851 if (separator == nullptr) {
852 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -0700853 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000854
855 char buf[512];
856 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
857 PRINT("Warning: ignoring very long library path: %s", path);
858 return -1;
859 }
860
861 buf[separator - path] = '\0';
862
863 const char* zip_path = buf;
864 const char* file_path = &buf[separator - path + 1];
865 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
866 if (fd == -1) {
867 return -1;
868 }
869
870 ZipArchiveHandle handle;
871 if (OpenArchiveFd(fd, "", &handle, false) != 0) {
872 // invalid zip-file (?)
873 close(fd);
874 return -1;
875 }
876
877 auto archive_guard = make_scope_guard([&]() {
878 CloseArchive(handle);
879 });
880
881 ZipEntry entry;
882
883 if (FindEntry(handle, ZipEntryName(file_path), &entry) != 0) {
884 // Entry was not found.
885 close(fd);
886 return -1;
887 }
888
889 // Check if it is properly stored
890 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
891 close(fd);
892 return -1;
893 }
894
895 *file_offset = entry.offset;
896 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800897}
898
Simon Baldwinaef71952015-01-16 13:22:54 +0000899static int open_library_on_path(const char* name,
900 const char* const paths[],
901 off64_t* file_offset) {
902 char buf[512];
903 int fd = -1;
904
905 for (size_t i = 0; paths[i] != nullptr && fd == -1; ++i) {
906 const char* const path = paths[i];
907 int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", path, name);
908 if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
909 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
910 return -1;
911 }
912
913 const char* separator = strchr(path, '!');
914
915 if (separator != nullptr) {
916 fd = open_library_in_zipfile(buf, file_offset);
917 }
918
919 if (fd == -1) {
920 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
921 if (fd != -1) {
922 *file_offset = 0;
923 }
924 }
925 }
926
927 return fd;
928}
929
930static int open_library(const char* name, off64_t* file_offset) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700931 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800932
Elliott Hughes124fae92012-10-31 14:20:03 -0700933 // 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 -0700934 if (strchr(name, '/') != nullptr) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000935 if (strchr(name, '!') != nullptr) {
936 int fd = open_library_in_zipfile(name, file_offset);
937 if (fd != -1) {
938 return fd;
939 }
940 }
941
Elliott Hughes6971fe42012-11-01 22:59:19 -0700942 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
943 if (fd != -1) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000944 *file_offset = 0;
Elliott Hughes6971fe42012-11-01 22:59:19 -0700945 return fd;
946 }
947 // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700948#if defined(__LP64__)
Dmitriy Ivanove43c4a72014-06-29 13:00:23 -0700949 return -1;
Dmitriy Ivanov5ca7ed92014-05-02 18:18:50 -0700950#endif
Elliott Hughes124fae92012-10-31 14:20:03 -0700951 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800952
Elliott Hughes124fae92012-10-31 14:20:03 -0700953 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Simon Baldwinaef71952015-01-16 13:22:54 +0000954 int fd = open_library_on_path(name, g_ld_library_paths, file_offset);
Elliott Hughes124fae92012-10-31 14:20:03 -0700955 if (fd == -1) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000956 fd = open_library_on_path(name, kDefaultLdPaths, file_offset);
Elliott Hughes124fae92012-10-31 14:20:03 -0700957 }
958 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800959}
960
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700961template<typename F>
962static void for_each_dt_needed(const soinfo* si, F action) {
963 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
964 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700965 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700966 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700967 }
968}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800969
Simon Baldwinaef71952015-01-16 13:22:54 +0000970static soinfo* load_library(LoadTaskList& load_tasks,
971 const char* name, int rtld_flags,
972 const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700973 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700974 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700975 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700976
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700977 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
978 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700979 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
980 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700981 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700982 } else {
983 // Open the file.
Simon Baldwinaef71952015-01-16 13:22:54 +0000984 fd = open_library(name, &file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700985 if (fd == -1) {
986 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700987 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700988 }
989
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700990 file_guard.reset(fd);
991 }
992
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700993 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700994 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700995 return nullptr;
996 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -0800997 if (file_offset < 0) {
998 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
999 return nullptr;
1000 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001001
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001002 struct stat file_stat;
1003 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001004 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001005 return nullptr;
1006 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001007 if (file_offset >= file_stat.st_size) {
1008 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64, name, file_offset, file_stat.st_size);
1009 return nullptr;
1010 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001011
1012 // Check for symlink and other situations where
1013 // file can have different names.
1014 for (soinfo* si = solist; si != nullptr; si = si->next) {
1015 if (si->get_st_dev() != 0 &&
1016 si->get_st_ino() != 0 &&
1017 si->get_st_dev() == file_stat.st_dev &&
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001018 si->get_st_ino() == file_stat.st_ino &&
1019 si->get_file_offset() == file_offset) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001020 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
1021 return si;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001022 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001023 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001024
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001025 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001026 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001027 return nullptr;
1028 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001029
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001030 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001031 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001032 if (!elf_reader.Load(extinfo)) {
1033 return nullptr;
1034 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001035
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001036 soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001037 if (si == nullptr) {
1038 return nullptr;
1039 }
1040 si->base = elf_reader.load_start();
1041 si->size = elf_reader.load_size();
1042 si->load_bias = elf_reader.load_bias();
1043 si->phnum = elf_reader.phdr_count();
1044 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001045
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001046 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001047 soinfo_free(si);
1048 return nullptr;
1049 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001050
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001051 for_each_dt_needed(si, [&] (const char* name) {
1052 load_tasks.push_back(LoadTask::create(name, si));
1053 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001054
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001055 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001056}
1057
Dmitriy Ivanov489e4982014-05-19 15:19:52 -07001058static soinfo *find_loaded_library_by_name(const char* name) {
1059 const char* search_name = SEARCH_NAME(name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001060 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -07001061 if (!strcmp(search_name, si->name)) {
1062 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001063 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -07001064 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001065 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001066}
1067
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001068static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001069
Dmitriy Ivanov489e4982014-05-19 15:19:52 -07001070 soinfo* si = find_loaded_library_by_name(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001071
1072 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001073 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001074 if (si == nullptr) {
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001075 TRACE("[ '%s' has not been found by name. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001076 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001077 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001078
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001079 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001080}
1081
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001082static void soinfo_unload(soinfo* si);
1083
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001084// TODO: this is slightly unusual way to construct
1085// the global group for relocation. Not every RTLD_GLOBAL
1086// library is included in this group for backwards-compatibility
1087// reasons.
1088//
1089// This group consists of the main executable, LD_PRELOADs
1090// and libraries with the DF_1_GLOBAL flag set.
1091static soinfo::soinfo_list_t make_global_group() {
1092 soinfo::soinfo_list_t global_group;
1093 for (soinfo* si = somain; si != nullptr; si = si->next) {
1094 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1095 global_group.push_back(si);
1096 }
1097 }
1098
1099 return global_group;
1100}
1101
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001102static bool find_libraries(soinfo* start_with, const char* const library_names[], size_t library_names_count, soinfo* soinfos[],
1103 soinfo* ld_preloads[], size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001104 // Step 0: prepare.
1105 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001106 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001107 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001108 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001109 }
1110
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001111 // Construct global_group.
1112 soinfo::soinfo_list_t global_group = make_global_group();
1113
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001114 // If soinfos array is null allocate one on stack.
1115 // The array is needed in case of failure; for example
1116 // when library_names[] = {libone.so, libtwo.so} and libone.so
1117 // is loaded correctly but libtwo.so failed for some reason.
1118 // In this case libone.so should be unloaded on return.
1119 // See also implementation of failure_guard below.
1120
1121 if (soinfos == nullptr) {
1122 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1123 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1124 memset(soinfos, 0, soinfos_size);
1125 }
1126
1127 // list of libraries to link - see step 2.
1128 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001129
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001130 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001131 // Housekeeping
1132 load_tasks.for_each([] (LoadTask* t) {
1133 LoadTask::deleter(t);
1134 });
1135
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001136 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001137 soinfo_unload(soinfos[i]);
1138 }
1139 });
1140
1141 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
1142 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 -07001143 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001144 if (si == nullptr) {
1145 return false;
1146 }
1147
1148 soinfo* needed_by = task->get_needed_by();
1149
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001150 if (needed_by != nullptr) {
1151 needed_by->add_child(si);
1152 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001153
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001154 if (si->is_linked()) {
1155 si->increment_ref_count();
1156 }
1157
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001158 // When ld_preloads is not null, the first
1159 // ld_preloads_count libs are in fact ld_preloads.
1160 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001161 // Add LD_PRELOADed libraries to the global group for future runs.
1162 // There is no need to explicitly add them to the global group
1163 // for this run because they are going to appear in the local
1164 // group in the correct order.
1165 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001166 ld_preloads[soinfos_count] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001167 }
1168
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001169 if (soinfos_count < library_names_count) {
1170 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001171 }
1172 }
1173
1174 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001175 soinfo::soinfo_list_t local_group;
1176 walk_dependencies_tree(
1177 start_with == nullptr ? soinfos : &start_with,
1178 start_with == nullptr ? soinfos_count : 1,
1179 [&] (soinfo* si) {
1180 local_group.push_back(si);
1181 return true;
1182 });
1183
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001184 // We need to increment ref_count in case
1185 // the root of the local group was not linked.
1186 bool was_local_group_root_linked = local_group.front()->is_linked();
1187
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001188 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001189 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001190 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001191 return false;
1192 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001193 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001194 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001195
1196 return true;
1197 });
1198
1199 if (linked) {
1200 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001201 }
1202
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001203 if (!was_local_group_root_linked) {
1204 local_group.front()->increment_ref_count();
1205 }
1206
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001207 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001208}
1209
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001210static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001211 soinfo* si;
1212
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001213 if (name == nullptr) {
1214 si = somain;
1215 } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001216 return nullptr;
1217 }
1218
Elliott Hughesd23736e2012-11-01 15:16:56 -07001219 return si;
1220}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001221
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001222static void soinfo_unload(soinfo* root) {
1223 // Note that the library can be loaded but not linked;
1224 // in which case there is no root but we still need
1225 // to walk the tree and unload soinfos involved.
1226 //
1227 // This happens on unsuccessful dlopen, when one of
1228 // the DT_NEEDED libraries could not be linked/found.
1229 if (root->is_linked()) {
1230 root = root->get_local_group_root();
1231 }
1232
1233 if (!root->can_unload()) {
1234 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->name);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001235 return;
1236 }
1237
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001238 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001239
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001240 if (ref_count == 0) {
1241 soinfo::soinfo_list_t local_unload_list;
1242 soinfo::soinfo_list_t external_unload_list;
1243 soinfo::soinfo_list_t depth_first_list;
1244 depth_first_list.push_back(root);
1245 soinfo* si = nullptr;
1246
1247 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001248 if (local_unload_list.contains(si)) {
1249 continue;
1250 }
1251
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001252 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001253
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001254 if (si->has_min_version(0)) {
1255 soinfo* child = nullptr;
1256 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001257 TRACE("%s@%p needs to unload %s@%p", si->name, si, child->name, child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001258 if (local_unload_list.contains(child)) {
1259 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001260 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001261 external_unload_list.push_back(child);
1262 } else {
1263 depth_first_list.push_front(child);
1264 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001265 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001266 } else {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001267#ifdef __LP64__
1268 __libc_fatal("soinfo for \"%s\"@%p has no version", si->name, si);
1269#else
1270 PRINT("warning: soinfo for \"%s\"@%p has no version", si->name, si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001271 for_each_dt_needed(si, [&] (const char* library_name) {
1272 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1273 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1274 if (needed != nullptr) {
1275 // Not found: for example if symlink was deleted between dlopen and dlclose
1276 // Since we cannot really handle errors at this point - print and continue.
1277 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
1278 return;
1279 } else if (local_unload_list.contains(needed)) {
1280 // already visited
1281 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001282 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001283 // external group
1284 external_unload_list.push_back(needed);
1285 } else {
1286 // local group
1287 depth_first_list.push_front(needed);
1288 }
1289 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001290#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001291 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001292 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001293
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001294 local_unload_list.for_each([](soinfo* si) {
1295 si->call_destructors();
1296 });
1297
1298 while ((si = local_unload_list.pop_front()) != nullptr) {
1299 notify_gdb_of_unload(si);
1300 soinfo_free(si);
1301 }
1302
1303 while ((si = external_unload_list.pop_front()) != nullptr) {
1304 soinfo_unload(si);
1305 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001306 } else {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001307 TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->name, ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001308 }
1309}
1310
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001311void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001312 // Use basic string manipulation calls to avoid snprintf.
1313 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1314 // When debug malloc is enabled, this call returns 0. This in turn causes
1315 // snprintf to do nothing, which causes libraries to fail to load.
1316 // See b/17302493 for further details.
1317 // Once the above bug is fixed, this code can be modified to use
1318 // snprintf again.
1319 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1320 if (buffer_size < required_len) {
1321 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
1322 buffer_size, required_len);
1323 }
1324 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1325 *end = ':';
1326 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001327}
1328
Elliott Hughescade4c32012-12-20 14:42:14 -08001329void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001330 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001331}
1332
Elliott Hughes1a586292014-06-03 16:23:08 -07001333soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001334 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001335 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001336 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001337 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001338 if (extinfo != nullptr) {
1339 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1340 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1341 return nullptr;
1342 }
1343 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001344 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1345 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 -07001346 return nullptr;
1347 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001348 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001349
1350 ProtectedDataGuard guard;
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001351 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001352 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001353 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001354 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001355 return si;
1356}
1357
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001358void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001359 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001360 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001361}
1362
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001363static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1364 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1365 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1366 ElfW(Addr) ifunc_addr = ifunc_resolver();
1367 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 -07001368
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001369 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001370}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001371
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001372#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001373#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001374static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
1375 return rela->r_addend;
1376}
1377#else
1378static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
1379 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE || ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
1380 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
1381 }
1382 return 0;
1383}
1384#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001385
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001386template<typename ElfRelIteratorT>
1387bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
1388 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
1389 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001390 if (rel == nullptr) {
1391 return false;
1392 }
1393
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001394 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
1395 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
1396
1397 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001398 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001399 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001400 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001401
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001402 DEBUG("Processing '%s' relocation at index %zd", this->name, idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001403 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001404 continue;
1405 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001406
1407 ElfW(Sym)* s = nullptr;
1408 soinfo* lsi = nullptr;
1409
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001410 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001411 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001412 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001413 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001414 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001415 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001416 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001417 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001418 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001419 }
1420
1421 /* IHI0044C AAELF 4.5.1.1:
1422
1423 Libraries are not searched to resolve weak references.
1424 It is not an error for a weak reference to remain unsatisfied.
1425
1426 During linking, the value of an undefined weak reference is:
1427 - Zero if the relocation type is absolute
1428 - The address of the place if the relocation is pc-relative
1429 - The address of nominal base address if the relocation
1430 type is base-relative.
1431 */
1432
1433 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001434 case R_GENERIC_JUMP_SLOT:
1435 case R_GENERIC_GLOB_DAT:
1436 case R_GENERIC_RELATIVE:
1437 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001438#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001439 case R_AARCH64_ABS64:
1440 case R_AARCH64_ABS32:
1441 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001442#elif defined(__x86_64__)
1443 case R_X86_64_32:
1444 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001445#elif defined(__arm__)
1446 case R_ARM_ABS32:
1447#elif defined(__i386__)
1448 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001449#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001450 /*
1451 * The sym_addr was initialized to be zero above, or the relocation
1452 * code below does not care about value of sym_addr.
1453 * No need to do anything.
1454 */
1455 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001456#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00001457 case R_X86_64_PC32:
1458 sym_addr = reloc;
1459 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001460#elif defined(__i386__)
1461 case R_386_PC32:
1462 sym_addr = reloc;
1463 break;
1464#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001465 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001466 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001467 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001468 }
1469 } else {
1470 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001471 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001472 }
1473 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001474 }
1475
1476 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001477 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001478 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001479 MARK(rel->r_offset);
1480 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
1481 reinterpret_cast<void*>(reloc),
1482 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1483
1484 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001485 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001486 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001487 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001488 MARK(rel->r_offset);
1489 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
1490 reinterpret_cast<void*>(reloc),
1491 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1492 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001493 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001494 case R_GENERIC_RELATIVE:
1495 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001496 MARK(rel->r_offset);
1497 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
1498 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001499 reinterpret_cast<void*>(load_bias + addend));
1500 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001501 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001502 case R_GENERIC_IRELATIVE:
1503 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001504 MARK(rel->r_offset);
1505 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
1506 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001507 reinterpret_cast<void*>(load_bias + addend));
1508 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001509 break;
1510
1511#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001512 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001513 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001514 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001515 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001516 reloc, (sym_addr + addend), sym_name);
1517 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001518 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001519 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001520 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001521 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001522 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001523 reloc, (sym_addr + addend), sym_name);
1524 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1525 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1526 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001527 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001528 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001529 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001530 static_cast<ElfW(Addr)>(INT32_MIN),
1531 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001532 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001533 }
1534 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001535 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001536 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001537 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001538 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001539 reloc, (sym_addr + addend), sym_name);
1540 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend))) &&
1541 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1542 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001543 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001544 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001545 (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + addend)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001546 static_cast<ElfW(Addr)>(INT16_MIN),
1547 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001548 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001549 }
1550 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001551 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001552 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001553 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001554 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001555 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1556 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend) - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001557 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001558 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001559 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001560 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001561 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001562 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1563 if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1564 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
1565 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001566 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001567 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001568 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001569 static_cast<ElfW(Addr)>(INT32_MIN),
1570 static_cast<ElfW(Addr)>(UINT32_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001571 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001572 }
1573 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001574 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001575 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001576 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001577 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001578 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1579 if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset))) &&
1580 ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
1581 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001582 } else {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001583 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001584 (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + addend) - rel->r_offset)),
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001585 static_cast<ElfW(Addr)>(INT16_MIN),
1586 static_cast<ElfW(Addr)>(UINT16_MAX));
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001587 return false;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001588 }
1589 break;
1590
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001591 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001592 /*
1593 * ET_EXEC is not supported so this should not happen.
1594 *
1595 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1596 *
1597 * Section 4.7.1.10 "Dynamic relocations"
1598 * R_AARCH64_COPY may only appear in executable objects where e_type is
1599 * set to ET_EXEC.
1600 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001601 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001602 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001603 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001604 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001605 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001606 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001607 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001608 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001609 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001610 break;
1611#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001612 case R_X86_64_32:
1613 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001614 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001615 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1616 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001617 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001618 break;
1619 case R_X86_64_64:
1620 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001621 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001622 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1623 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001624 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001625 break;
1626 case R_X86_64_PC32:
1627 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001628 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001629 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1630 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1631 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001632 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001633 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001634#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001635 case R_ARM_ABS32:
1636 count_relocation(kRelocAbsolute);
1637 MARK(rel->r_offset);
1638 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1639 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1640 break;
1641 case R_ARM_REL32:
1642 count_relocation(kRelocRelative);
1643 MARK(rel->r_offset);
1644 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1645 reloc, sym_addr, rel->r_offset, sym_name);
1646 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1647 break;
1648 case R_ARM_COPY:
1649 /*
1650 * ET_EXEC is not supported so this should not happen.
1651 *
1652 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1653 *
1654 * Section 4.7.1.10 "Dynamic relocations"
1655 * R_ARM_COPY may only appear in executable objects where e_type is
1656 * set to ET_EXEC.
1657 */
1658 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001659 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001660#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001661 case R_386_32:
1662 count_relocation(kRelocRelative);
1663 MARK(rel->r_offset);
1664 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1665 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1666 break;
1667 case R_386_PC32:
1668 count_relocation(kRelocRelative);
1669 MARK(rel->r_offset);
1670 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1671 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1672 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1673 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001674#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001675 default:
1676 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001677 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001678 }
1679 }
1680 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001681}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001682#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001683
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001684void soinfo::call_array(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001685 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001686 return;
1687 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001688
Elliott Hughesc6200592013-09-30 18:43:46 -07001689 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001690
1691 int begin = reverse ? (count - 1) : 0;
1692 int end = reverse ? -1 : count;
1693 int step = reverse ? -1 : 1;
1694
1695 for (int i = begin; i != end; i += step) {
1696 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001697 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001698 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001699
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001700 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001701}
1702
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001703void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001704 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001705 return;
1706 }
1707
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001708 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001709 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001710 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001711}
1712
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001713void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001714 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1715 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001716 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001717}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001718
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001719void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001720 if (constructors_called) {
1721 return;
1722 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001723
Elliott Hughesd23736e2012-11-01 15:16:56 -07001724 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1725 // protect against recursive constructor calls. One simple example of constructor recursion
1726 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1727 // 1. The program depends on libc, so libc's constructor is called here.
1728 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1729 // 3. dlopen() calls the constructors on the newly created
1730 // soinfo for libc_malloc_debug_leak.so.
1731 // 4. The debug .so depends on libc, so CallConstructors is
1732 // called again with the libc soinfo. If it doesn't trigger the early-
1733 // out above, the libc constructor will be called again (recursively!).
1734 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001735
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001736 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001737 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001738 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001739 name, preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001740 }
1741
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001742 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001743 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001744 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001745
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001746 TRACE("\"%s\": calling constructors", name);
1747
1748 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001749 call_function("DT_INIT", init_func_);
1750 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001751}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001752
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001753void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001754 if (!constructors_called) {
1755 return;
1756 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001757 TRACE("\"%s\": calling destructors", name);
1758
1759 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001760 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001761
1762 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001763 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001764
1765 // This is needed on second call to dlopen
1766 // after library has been unloaded with RTLD_NODELETE
1767 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001768}
1769
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001770void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001771 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001772 child->parents_.push_back(this);
1773 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001774 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001775}
1776
1777void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001778 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001779 return;
1780 }
1781
1782 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001783 children_.for_each([&] (soinfo* child) {
1784 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001785 return parent == this;
1786 });
1787 });
1788
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001789 parents_.for_each([&] (soinfo* parent) {
1790 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001791 return child == this;
1792 });
1793 });
1794
1795 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001796 parents_.clear();
1797 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001798}
1799
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001800dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001801 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001802 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001803 }
1804
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001805 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001806};
1807
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001808ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001809 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001810 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001811 }
1812
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001813 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001814}
1815
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001816off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001817 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001818 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001819 }
1820
1821 return 0;
1822}
1823
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001824uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001825 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001826 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001827 }
1828
1829 return 0;
1830}
1831
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001832uint32_t soinfo::get_dt_flags_1() const {
1833 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001834 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001835 }
1836
1837 return 0;
1838}
1839void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1840 if (has_min_version(1)) {
1841 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001842 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001843 }
1844
1845 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001846 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001847 }
1848
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001849 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001850 }
1851}
1852
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001853// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001854// 'this->flags' does not have FLAG_NEW_SOINFO set.
1855static soinfo::soinfo_list_t g_empty_list;
1856
1857soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001858 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001859 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001860 }
1861
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001862 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001863}
1864
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001865soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001866 if (has_min_version(0)) {
1867 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001868 }
1869
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001870 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001871}
1872
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001873ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1874 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1875 return call_ifunc_resolver(s->st_value + load_bias);
1876 }
1877
1878 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1879}
1880
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001881const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001882 if (has_min_version(1) && (index >= strtab_size_)) {
1883 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001884 }
1885
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001886 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001887}
1888
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001889bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001890 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001891}
1892
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001893bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001894 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001895}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001896
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001897bool soinfo::is_linked() const {
1898 return (flags_ & FLAG_LINKED) != 0;
1899}
1900
1901bool soinfo::is_main_executable() const {
1902 return (flags_ & FLAG_EXE) != 0;
1903}
1904
1905void soinfo::set_linked() {
1906 flags_ |= FLAG_LINKED;
1907}
1908
1909void soinfo::set_linker_flag() {
1910 flags_ |= FLAG_LINKER;
1911}
1912
1913void soinfo::set_main_executable() {
1914 flags_ |= FLAG_EXE;
1915}
1916
1917void soinfo::increment_ref_count() {
1918 local_group_root_->ref_count_++;
1919}
1920
1921size_t soinfo::decrement_ref_count() {
1922 return --local_group_root_->ref_count_;
1923}
1924
1925soinfo* soinfo::get_local_group_root() const {
1926 return local_group_root_;
1927}
1928
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001929/* Force any of the closed stdin, stdout and stderr to be associated with
1930 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07001931static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001932 int dev_null, i, status;
1933 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001934
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001935 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
1936 if (dev_null < 0) {
1937 DL_ERR("cannot open /dev/null: %s", strerror(errno));
1938 return -1;
1939 }
1940 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001941
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001942 /* If any of the stdio file descriptors is valid and not associated
1943 with /dev/null, dup /dev/null to it. */
1944 for (i = 0; i < 3; i++) {
1945 /* If it is /dev/null already, we are done. */
1946 if (i == dev_null) {
1947 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001948 }
1949
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001950 TRACE("[ Nullifying stdio file descriptor %d]", i);
1951 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
1952
1953 /* If file is opened, we are good. */
1954 if (status != -1) {
1955 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001956 }
1957
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001958 /* The only error we allow is that the file descriptor does not
1959 exist, in which case we dup /dev/null to it. */
1960 if (errno != EBADF) {
1961 DL_ERR("fcntl failed: %s", strerror(errno));
1962 return_value = -1;
1963 continue;
1964 }
1965
1966 /* Try dupping /dev/null to this stdio file descriptor and
1967 repeat if there is a signal. Note that any errors in closing
1968 the stdio descriptor are lost. */
1969 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
1970 if (status < 0) {
1971 DL_ERR("dup2 failed: %s", strerror(errno));
1972 return_value = -1;
1973 continue;
1974 }
1975 }
1976
1977 /* If /dev/null is not one of the stdio file descriptors, close it. */
1978 if (dev_null > 2) {
1979 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
1980 status = TEMP_FAILURE_RETRY(close(dev_null));
1981 if (status == -1) {
1982 DL_ERR("close failed: %s", strerror(errno));
1983 return_value = -1;
1984 }
1985 }
1986
1987 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001988}
1989
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001990bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08001991 /* Extract dynamic section */
1992 ElfW(Word) dynamic_flags = 0;
1993 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07001994
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001995 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001996 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001997 if (!relocating_linker) {
1998 INFO("[ linking %s ]", name);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001999 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002000 }
2001
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002002 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002003 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002004 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002005 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002006 return false;
2007 } else {
2008 if (!relocating_linker) {
2009 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002010 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002011 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002012
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002013#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002014 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2015 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002016#endif
2017
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002018 // Extract useful information from dynamic section.
2019 uint32_t needed_count = 0;
2020 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2021 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2022 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2023 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002024 case DT_SONAME:
2025 // TODO: glibc dynamic linker uses this name for
2026 // initial library lookup; consider doing the same here.
2027 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002028
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002029 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002030 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2031 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2032 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2033 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002034 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002035
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002036 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002037 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002038 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002039 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2040 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002041
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002042 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002043 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002044 // amend chain for symndx = header[1]
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002045 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 -08002046
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002047 if (!powerof2(gnu_maskwords_)) {
2048 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 -08002049 return false;
2050 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002051 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002052
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002053 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002054 break;
2055
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002056 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002057 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002058 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002059
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002060 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002061 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002062 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002063
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002064 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002065 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002066 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002067
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002068 case DT_SYMENT:
2069 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002070 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 -07002071 return false;
2072 }
2073 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002074
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002075 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002076#if defined(USE_RELA)
2077 if (d->d_un.d_val != DT_RELA) {
2078 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002079 return false;
2080 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002081#else
2082 if (d->d_un.d_val != DT_REL) {
2083 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
2084 return false;
2085 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002086#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002087 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002088
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002089 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002090#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002091 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002092#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002093 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002094#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002095 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002096
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002097 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002098#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002099 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002100#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002101 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002102#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002103 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002104
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002105 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002106#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002107 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002108 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002109#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002110 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2111 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002112
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002113 case DT_DEBUG:
2114 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2115 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002116// FIXME: not working currently for N64
2117// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002118// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002119// read-only, but the DYNAMIC header claims it is writable.
2120#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002121 if ((dynamic_flags & PF_W) != 0) {
2122 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2123 }
Chris Dearman99186652014-02-06 20:36:51 -08002124#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002125 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002126#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002127 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002128 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002129 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002130
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002131 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002132 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002133 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002134
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002135 case DT_ANDROID_RELA:
2136 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2137 break;
2138
2139 case DT_ANDROID_RELASZ:
2140 android_relocs_size_ = d->d_un.d_val;
2141 break;
2142
2143 case DT_ANDROID_REL:
2144 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", name);
2145 return false;
2146
2147 case DT_ANDROID_RELSZ:
2148 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", name);
2149 return false;
2150
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002151 case DT_RELAENT:
2152 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002153 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002154 return false;
2155 }
2156 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002157
2158 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002159 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002160 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002161
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002162 case DT_REL:
2163 DL_ERR("unsupported DT_REL in \"%s\"", name);
2164 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002165
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002166 case DT_RELSZ:
2167 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2168 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002169
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002170#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002171 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002172 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002173 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002174
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002175 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002176 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002177 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002178
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002179 case DT_RELENT:
2180 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002181 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002182 return false;
2183 }
2184 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002185
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002186 case DT_ANDROID_REL:
2187 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2188 break;
2189
2190 case DT_ANDROID_RELSZ:
2191 android_relocs_size_ = d->d_un.d_val;
2192 break;
2193
2194 case DT_ANDROID_RELA:
2195 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", name);
2196 return false;
2197
2198 case DT_ANDROID_RELASZ:
2199 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", name);
2200 return false;
2201
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002202 // "Indicates that all RELATIVE relocations have been concatenated together,
2203 // and specifies the RELATIVE relocation count."
2204 //
2205 // TODO: Spec also mentions that this can be used to optimize relocation process;
2206 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002207 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002208 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002209
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002210 case DT_RELA:
2211 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2212 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002213
2214 case DT_RELASZ:
2215 DL_ERR("unsupported DT_RELASZ in \"%s\"", name);
2216 return false;
2217
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002218#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002219 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002220 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2221 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002222 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002223
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002224 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002225 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2226 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002227 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002228
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002229 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002230 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2231 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002232 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002233
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002234 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002235 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002236 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002237
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002238 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002239 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2240 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002241 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002242
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002243 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002244 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002245 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002246
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002247 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002248 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2249 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002250 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002251
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002252 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002253 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002254 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002255
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002256 case DT_TEXTREL:
Elliott Hughese4d792a2013-10-28 14:19:05 -07002257#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002258 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2259 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002260#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002261 has_text_relocations = true;
2262 break;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002263#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002264
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002265 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002266 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002267 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002268
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002269 case DT_NEEDED:
2270 ++needed_count;
2271 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002272
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002273 case DT_FLAGS:
2274 if (d->d_un.d_val & DF_TEXTREL) {
Elliott Hughese4d792a2013-10-28 14:19:05 -07002275#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002276 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
2277 return false;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002278#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002279 has_text_relocations = true;
Elliott Hughese4d792a2013-10-28 14:19:05 -07002280#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002281 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002282 if (d->d_un.d_val & DF_SYMBOLIC) {
2283 has_DT_SYMBOLIC = true;
2284 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002285 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002286
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002287 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002288 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002289
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002290 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002291 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2292 }
2293 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002294#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002295 case DT_MIPS_RLD_MAP:
2296 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2297 {
2298 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2299 *dp = &_r_debug;
2300 }
2301 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002302 case DT_MIPS_RLD_MAP2:
2303 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2304 {
2305 r_debug** dp = reinterpret_cast<r_debug**>(reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
2306 *dp = &_r_debug;
2307 }
2308 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002309
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002310 case DT_MIPS_RLD_VERSION:
2311 case DT_MIPS_FLAGS:
2312 case DT_MIPS_BASE_ADDRESS:
2313 case DT_MIPS_UNREFEXTNO:
2314 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002315
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002316 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002317 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002318 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002319
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002320 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002321 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002322 break;
2323
2324 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002325 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002326 break;
2327#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002328 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2329 case DT_BIND_NOW:
2330 break;
2331
2332 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002333 case DT_VERSYM:
2334 case DT_VERDEF:
2335 case DT_VERDEFNUM:
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002336 case DT_VERNEED:
2337 case DT_VERNEEDNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002338 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002339
2340 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002341 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002342 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002343 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2344 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002345 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002346 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002347 }
2348
2349 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002350 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002351
2352 // Sanity checks.
2353 if (relocating_linker && needed_count != 0) {
2354 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2355 return false;
2356 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002357 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002358 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 -07002359 return false;
2360 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002361 if (strtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002362 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2363 return false;
2364 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002365 if (symtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002366 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2367 return false;
2368 }
2369 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002370}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002371
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002372bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
2373 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002374
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002375 local_group_root_ = local_group.front();
2376 if (local_group_root_ == nullptr) {
2377 local_group_root_ = this;
2378 }
2379
Elliott Hughese4d792a2013-10-28 14:19:05 -07002380#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002381 if (has_text_relocations) {
2382 // Make segments writable to allow text relocations to work properly. We will later call
2383 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2384 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2385 "security hardening. Please fix.", name);
2386 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2387 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2388 name, strerror(errno));
2389 return false;
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002390 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002391 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002392#endif
Nick Kralevich5135b3a2012-08-10 21:08:42 -07002393
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002394 if (android_relocs_ != nullptr) {
2395 // check signature
2396 if (android_relocs_size_ > 3 &&
2397 android_relocs_[0] == 'A' &&
2398 android_relocs_[1] == 'P' &&
2399 (android_relocs_[2] == 'U' || android_relocs_[2] == 'S') &&
2400 android_relocs_[3] == '2') {
2401 DEBUG("[ android relocating %s ]", name);
2402
2403 bool relocated = false;
2404 const uint8_t* packed_relocs = android_relocs_ + 4;
2405 const size_t packed_relocs_size = android_relocs_size_ - 4;
2406
2407 if (android_relocs_[2] == 'U') {
2408 relocated = relocate(
2409 packed_reloc_iterator<leb128_decoder>(
2410 leb128_decoder(packed_relocs, packed_relocs_size)),
2411 global_group, local_group);
2412 } else { // android_relocs_[2] == 'S'
2413 relocated = relocate(
2414 packed_reloc_iterator<sleb128_decoder>(
2415 sleb128_decoder(packed_relocs, packed_relocs_size)),
2416 global_group, local_group);
2417 }
2418
2419 if (!relocated) {
2420 return false;
2421 }
2422 } else {
2423 DL_ERR("bad android relocation header.");
2424 return false;
2425 }
2426 }
2427
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002428#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002429 if (rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002430 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002431 if (!relocate(plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002432 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002433 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002434 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002435 if (plt_rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002436 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002437 if (!relocate(plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002438 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002439 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002440 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002441#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002442 if (rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002443 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002444 if (!relocate(plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002445 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002446 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002447 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002448 if (plt_rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002449 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002450 if (!relocate(plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002451 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002452 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002453 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002454#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002455
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002456#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08002457 if (!mips_relocate_got(global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002458 return false;
2459 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002460#endif
2461
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002462 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002463
Elliott Hughese4d792a2013-10-28 14:19:05 -07002464#if !defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002465 if (has_text_relocations) {
2466 // All relocations are done, we can protect our segments back to read-only.
2467 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2468 DL_ERR("can't protect segments for \"%s\": %s",
2469 name, strerror(errno));
2470 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002471 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002472 }
Elliott Hughese4d792a2013-10-28 14:19:05 -07002473#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002474
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002475 /* We can also turn on GNU RELRO protection */
2476 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2477 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2478 name, strerror(errno));
2479 return false;
2480 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002481
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002482 /* Handle serializing/sharing the RELRO segment */
2483 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2484 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2485 extinfo->relro_fd) < 0) {
2486 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2487 name, strerror(errno));
2488 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002489 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002490 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2491 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2492 extinfo->relro_fd) < 0) {
2493 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2494 name, strerror(errno));
2495 return false;
2496 }
2497 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002498
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002499 notify_gdb_of_load(this);
2500 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002501}
2502
Nick Kralevich468319c2011-11-11 15:53:17 -08002503/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002504 * This function add vdso to internal dso list.
2505 * It helps to stack unwinding through signal handlers.
2506 * Also, it makes bionic more like glibc.
2507 */
Kito Cheng812fd422014-03-25 22:53:56 +08002508static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002509#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002510 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002511 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002512 return;
2513 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002514
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002515 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002516
Elliott Hughes0266ae52014-02-10 17:46:57 -08002517 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2518 si->phnum = ehdr_vdso->e_phnum;
2519 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2520 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002521 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002522
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002523 si->prelink_image();
2524 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002525#endif
2526}
2527
2528/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002529 * This is linker soinfo for GDB. See details below.
2530 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002531#if defined(__LP64__)
2532#define LINKER_PATH "/system/bin/linker64"
2533#else
2534#define LINKER_PATH "/system/bin/linker"
2535#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002536static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002537
2538/* gdb expects the linker to be in the debug shared object list.
2539 * Without this, gdb has trouble locating the linker's ".text"
2540 * and ".plt" sections. Gdb could also potentially use this to
2541 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2542 * Don't use soinfo_alloc(), because the linker shouldn't
2543 * be on the soinfo list.
2544 */
2545static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002546 linker_soinfo_for_gdb.base = linker_base;
2547
2548 /*
2549 * Set the dynamic field in the link map otherwise gdb will complain with
2550 * the following:
2551 * warning: .dynamic section for "/system/bin/linker" is not at the
2552 * expected address (wrong library or version mismatch?)
2553 */
2554 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2555 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2556 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002557 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002558 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2559}
2560
2561/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002562 * This code is called after the linker has linked itself and
2563 * fixed it's own GOT. It is safe to make references to externs
2564 * and other non-local data at this point.
2565 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002566static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002567#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002568 struct timeval t0, t1;
2569 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002570#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002571
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002572 // Initialize environment functions, and get to the ELF aux vectors table.
2573 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002574
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002575 // If this is a setuid/setgid program, close the security hole described in
2576 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2577 if (get_AT_SECURE()) {
2578 nullify_closed_stdio();
2579 }
2580
2581 debuggerd_init();
2582
2583 // Get a few environment variables.
2584 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2585 if (LD_DEBUG != nullptr) {
2586 g_ld_debug_verbosity = atoi(LD_DEBUG);
2587 }
2588
2589 // Normally, these are cleaned by linker_env_init, but the test
2590 // doesn't cost us anything.
2591 const char* ldpath_env = nullptr;
2592 const char* ldpreload_env = nullptr;
2593 if (!get_AT_SECURE()) {
2594 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2595 ldpreload_env = linker_env_get("LD_PRELOAD");
2596 }
2597
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -08002598#if !defined(__LP64__)
2599 if (personality(PER_LINUX32) == -1) {
2600 __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
2601 }
2602#endif
2603
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002604 INFO("[ android linker & debugger ]");
2605
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002606 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002607 if (si == nullptr) {
2608 exit(EXIT_FAILURE);
2609 }
2610
2611 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002612 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002613 link_map* map = &(si->link_map_head);
2614
2615 map->l_addr = 0;
2616 map->l_name = args.argv[0];
2617 map->l_prev = nullptr;
2618 map->l_next = nullptr;
2619
2620 _r_debug.r_map = map;
2621 r_debug_tail = map;
2622
2623 init_linker_info_for_gdb(linker_base);
2624
2625 // Extract information passed from the kernel.
2626 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2627 si->phnum = args.getauxval(AT_PHNUM);
2628 si->entry = args.getauxval(AT_ENTRY);
2629
2630 /* Compute the value of si->base. We can't rely on the fact that
2631 * the first entry is the PHDR because this will not be true
2632 * for certain executables (e.g. some in the NDK unit test suite)
2633 */
2634 si->base = 0;
2635 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2636 si->load_bias = 0;
2637 for (size_t i = 0; i < si->phnum; ++i) {
2638 if (si->phdr[i].p_type == PT_PHDR) {
2639 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2640 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2641 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002642 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002643 }
2644 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002645
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002646 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2647 if (elf_hdr->e_type != ET_DYN) {
2648 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2649 exit(EXIT_FAILURE);
2650 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002651
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002652 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2653 parse_LD_LIBRARY_PATH(ldpath_env);
2654 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002655
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002656 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002657
Dmitriy Ivanov67181252015-01-07 15:48:25 -08002658 if (!si->prelink_image()) {
2659 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2660 exit(EXIT_FAILURE);
2661 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002662
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002663 // add somain to global group
2664 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2665
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002666 // Load ld_preloads and dependencies.
2667 StringLinkedList needed_library_name_list;
2668 size_t needed_libraries_count = 0;
2669 size_t ld_preloads_count = 0;
2670 while (g_ld_preload_names[ld_preloads_count] != nullptr) {
2671 needed_library_name_list.push_back(g_ld_preload_names[ld_preloads_count++]);
2672 ++needed_libraries_count;
2673 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002674
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002675 for_each_dt_needed(si, [&](const char* name) {
2676 needed_library_name_list.push_back(name);
2677 ++needed_libraries_count;
2678 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002679
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002680 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002681
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002682 memset(needed_library_names, 0, sizeof(needed_library_names));
2683 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002684
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002685 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 -07002686 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2687 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002688 } else if (needed_libraries_count == 0) {
2689 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
2690 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2691 exit(EXIT_FAILURE);
2692 }
2693 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002694 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002695
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002696 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002697
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002698 {
2699 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06002700
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002701 si->call_pre_init_constructors();
2702
2703 /* After the prelink_image, the si->load_bias is initialized.
2704 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2705 * We need to update this value for so exe here. So Unwind_Backtrace
2706 * for some arch like x86 could work correctly within so exe.
2707 */
2708 map->l_addr = si->load_bias;
2709 si->call_constructors();
2710 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002711
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002712#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002713 gettimeofday(&t1, nullptr);
2714 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2715 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2716 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002717#endif
2718#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002719 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2720 linker_stats.count[kRelocAbsolute],
2721 linker_stats.count[kRelocRelative],
2722 linker_stats.count[kRelocCopy],
2723 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002724#endif
2725#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002726 {
2727 unsigned n;
2728 unsigned i;
2729 unsigned count = 0;
2730 for (n = 0; n < 4096; n++) {
2731 if (bitmask[n]) {
2732 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002733#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002734 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002735#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002736 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002737#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002738 if (x & 1) {
2739 count++;
2740 }
2741 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002742 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002743 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002744 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002745 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2746 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002747#endif
2748
2749#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002750 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002751#endif
2752
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002753 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2754 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002755}
Nick Kralevich468319c2011-11-11 15:53:17 -08002756
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002757/* Compute the load-bias of an existing executable. This shall only
2758 * be used to compute the load bias of an executable or shared library
2759 * that was loaded by the kernel itself.
2760 *
2761 * Input:
2762 * elf -> address of ELF header, assumed to be at the start of the file.
2763 * Return:
2764 * load bias, i.e. add the value of any p_vaddr in the file to get
2765 * the corresponding address in memory.
2766 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002767static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2768 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002769 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002770 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002771
Elliott Hughes0266ae52014-02-10 17:46:57 -08002772 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002773 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002774 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002775 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002776 }
2777 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002778}
2779
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002780extern "C" void _start();
2781
Nick Kralevich468319c2011-11-11 15:53:17 -08002782/*
2783 * This is the entry point for the linker, called from begin.S. This
2784 * method is responsible for fixing the linker's own relocations, and
2785 * then calling __linker_init_post_relocation().
2786 *
2787 * Because this method is called before the linker has fixed it's own
2788 * relocations, any attempt to reference an extern variable, extern
2789 * function, or other GOT reference will generate a segfault.
2790 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002791extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002792 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002793
Elliott Hughes0266ae52014-02-10 17:46:57 -08002794 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002795 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002796 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002797 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002798
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002799 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002800
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002801 // If the linker is not acting as PT_INTERP entry_point is equal to
2802 // _start. Which means that the linker is running as an executable and
2803 // already linked by PT_INTERP.
2804 //
2805 // This happens when user tries to run 'adb shell /system/bin/linker'
2806 // see also https://code.google.com/p/android/issues/detail?id=63174
2807 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2808 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2809 }
2810
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002811 linker_so.base = linker_addr;
2812 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2813 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002814 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002815 linker_so.phdr = phdr;
2816 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002817 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07002818
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002819 // This might not be obvious... The reasons why we pass g_empty_list
2820 // in place of local_group here are (1) we do not really need it, because
2821 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2822 // itself without having to look into local_group and (2) allocators
2823 // are not yet initialized, and therefore we cannot use linked_list.push_*
2824 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002825 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002826 // It would be nice to print an error message, but if the linker
2827 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002828 // call write() (because it involves a GOT reference). We may as
2829 // well try though...
2830 const char* msg = "CANNOT LINK EXECUTABLE: ";
2831 write(2, msg, strlen(msg));
2832 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2833 write(2, "\n", 1);
2834 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002835 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002836
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002837 __libc_init_tls(args);
2838
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002839 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002840 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002841
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002842 // Initialize static variables. Note that in order to
2843 // get correct libdl_info we need to call constructors
2844 // before get_libdl_info().
2845 solist = get_libdl_info();
2846 sonext = get_libdl_info();
2847
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002848 // We have successfully fixed our own relocations. It's safe to run
2849 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002850 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002851 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002852
Elliott Hughes611f9562015-01-23 10:43:58 -08002853 INFO("[ jumping to _start ]");
2854
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002855 // Return the address that the calling assembly stub should jump to.
2856 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002857}