blob: 3c8ba76e6fe1c453fc95e3fbb9c11896d161fed8 [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>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070043#include <string>
44#include <vector>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070045
Elliott Hughes46882792012-08-03 16:49:39 -070046// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070047#include "private/bionic_tls.h"
48#include "private/KernelArgumentBlock.h"
49#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070050#include "private/ScopedFd.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070051#include "private/ScopeGuard.h"
52#include "private/UniquePtr.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
54#include "linker.h"
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070055#include "linker_block_allocator.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056#include "linker_debug.h"
David 'Digit' Turnerbe575592010-12-16 19:52:02 +010057#include "linker_environ.h"
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080058#include "linker_leb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020059#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080060#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080061#include "linker_reloc_iterators.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000062#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
65 *
66 * Do NOT use malloc() and friends or pthread_*() code here.
67 * Don't use printf() either; it's caused mysterious memory
68 * corruption in the past.
69 * The linker runs before we bring up libc and it's easiest
70 * to make sure it does not depend on any complex libc features
71 *
72 * open issues / todo:
73 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074 * - cleaner error reporting
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075 * - after linking, set as much stuff as possible to READONLY
76 * and NOEXEC
Elliott Hughes46882792012-08-03 16:49:39 -070077 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080079// Override macros to use C++ style casts
80#undef ELF_ST_TYPE
81#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
82
Elliott Hughes0266ae52014-02-10 17:46:57 -080083static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070085static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
86static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020087
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070088static soinfo* solist;
89static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -070090static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091
Elliott Hughes1728b232014-05-14 10:02:03 -070092static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070093#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070094 "/vendor/lib64",
95 "/system/lib64",
96#else
Elliott Hughes124fae92012-10-31 14:20:03 -070097 "/vendor/lib",
98 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -070099#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700100 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700101};
David Bartleybc3a5c22009-06-02 18:27:28 -0700102
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700103static std::vector<std::string> g_ld_library_paths;
104static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800105
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700106static std::vector<soinfo*> g_ld_preloads;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600107
Elliott Hughes1728b232014-05-14 10:02:03 -0700108__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700110__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700111
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700113struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700114 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700115};
116
117static linker_stats_t linker_stats;
118
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800119void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700120 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700121}
122#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800123void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700124}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125#endif
126
127#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800128uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129#endif
130
Dima Zavin2e855792009-05-20 18:28:09 -0700131static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700132
Elliott Hughes650be4e2013-03-05 18:47:58 -0800133char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700134 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700135}
136
Elliott Hughes650be4e2013-03-05 18:47:58 -0800137size_t linker_get_error_buffer_size() {
138 return sizeof(__linker_dl_err_buf);
139}
140
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700141// This function is an empty stub where GDB locates a breakpoint to get notified
142// about linker activity.
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700143extern "C"
144void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145
Elliott Hughes1728b232014-05-14 10:02:03 -0700146static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700147static r_debug _r_debug =
148 {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
149
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800150static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800151
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800152static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700153 // Copy the necessary fields into the debug structure.
154 link_map* map = &(info->link_map_head);
155 map->l_addr = info->load_bias;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700156 // link_map l_name field is not const.
157 map->l_name = const_cast<char*>(info->get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700158 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700160 // Stick the new library at the end of the list.
161 // gdb tends to care more about libc than it does
162 // about leaf libraries, and ordering it this way
163 // reduces the back-and-forth over the wire.
164 if (r_debug_tail) {
165 r_debug_tail->l_next = map;
166 map->l_prev = r_debug_tail;
167 map->l_next = 0;
168 } else {
169 _r_debug.r_map = map;
170 map->l_prev = 0;
171 map->l_next = 0;
172 }
173 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174}
175
Elliott Hughesbedfe382012-08-14 14:07:59 -0700176static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700177 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700178
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700179 if (r_debug_tail == map) {
180 r_debug_tail = map->l_prev;
181 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700182
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700183 if (map->l_prev) {
184 map->l_prev->l_next = map->l_next;
185 }
186 if (map->l_next) {
187 map->l_next->l_prev = map->l_prev;
188 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700189}
190
Elliott Hughesbedfe382012-08-14 14:07:59 -0700191static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800192 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700193 // GDB already knows about the main executable
194 return;
195 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700197 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800198
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700199 _r_debug.r_state = r_debug::RT_ADD;
200 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700202 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700204 _r_debug.r_state = r_debug::RT_CONSISTENT;
205 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700206}
207
Elliott Hughesbedfe382012-08-14 14:07:59 -0700208static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800209 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700210 // GDB already knows about the main executable
211 return;
212 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700213
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700214 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700215
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700216 _r_debug.r_state = r_debug::RT_DELETE;
217 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700218
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700219 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700220
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700221 _r_debug.r_state = r_debug::RT_CONSISTENT;
222 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223}
224
Elliott Hughes18a206c2012-10-29 17:37:13 -0700225void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800226 _r_debug.r_state = r_debug::RT_ADD;
227 rtld_db_dlactivity();
228 _r_debug.r_state = r_debug::RT_CONSISTENT;
229 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230}
231
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700232LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
233 return g_soinfo_links_allocator.alloc();
234}
235
236void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
237 g_soinfo_links_allocator.free(entry);
238}
239
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700240static soinfo* soinfo_alloc(const char* name, struct stat* file_stat,
241 off64_t file_offset, uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700242 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200243 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700244 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200245 }
246
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700247 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700248
Magnus Malmbornba98d922012-09-12 13:00:55 +0200249 sonext->next = si;
250 sonext = si;
251
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700252 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200253 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800254}
255
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800256static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700257 if (si == nullptr) {
258 return;
259 }
260
261 if (si->base != 0 && si->size != 0) {
262 munmap(reinterpret_cast<void*>(si->base), si->size);
263 }
264
265 soinfo *prev = nullptr, *trav;
266
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700267 TRACE("name %s: freeing soinfo @ %p", si->get_soname(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700268
269 for (trav = solist; trav != nullptr; trav = trav->next) {
270 if (trav == si) {
271 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700272 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700273 prev = trav;
274 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800275
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700276 if (trav == nullptr) {
277 // si was not in solist
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700278 DL_ERR("name \"%s\"@%p is not in solist!", si->get_soname(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700279 return;
280 }
Elliott Hughes46882792012-08-03 16:49:39 -0700281
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700282 // clear links to/from si
283 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700284
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700285 // prev will never be null, because the first entry in solist is
286 // always the static libdl_info.
287 prev->next = si->next;
288 if (si == sonext) {
289 sonext = prev;
290 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700292 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800293}
294
Elliott Hughescade4c32012-12-20 14:42:14 -0800295static void parse_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700296 std::vector<std::string>* paths) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700297 if (path == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800298 return;
299 }
300
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700301 paths->clear();
Elliott Hughescade4c32012-12-20 14:42:14 -0800302
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700303 for (const char *p = path; ; ++p) {
304 size_t len = strcspn(p, delimiters);
305 // skip empty tokens
306 if (len == 0) {
307 continue;
Elliott Hughescade4c32012-12-20 14:42:14 -0800308 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800309
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700310 paths->push_back(std::string(p, len));
311 p += len;
312
313 if (*p == '\0') {
314 break;
315 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800316 }
317}
318
319static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700320 parse_path(path, ":", &g_ld_library_paths);
Elliott Hughescade4c32012-12-20 14:42:14 -0800321}
322
323static void parse_LD_PRELOAD(const char* path) {
324 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700325 parse_path(path, " :", &g_ld_preload_names);
Elliott Hughescade4c32012-12-20 14:42:14 -0800326}
327
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700328static bool realpath_fd(int fd, std::string* realpath) {
329 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
330 snprintf(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
331 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
332 return false;
333 }
334
335 *realpath = std::string(&buf[0]);
336 return true;
337}
338
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700339#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700340
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700341// For a given PC, find the .so that it belongs to.
342// Returns the base address of the .ARM.exidx section
343// for that .so, and the number of 8-byte entries
344// in that section (via *pcount).
345//
346// Intended to be called by libc's __gnu_Unwind_Find_exidx().
347//
348// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800349_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800350 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800351
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700352 for (soinfo* si = solist; si != 0; si = si->next) {
353 if ((addr >= si->base) && (addr < (si->base + si->size))) {
354 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800355 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800356 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700357 }
358 *pcount = 0;
359 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800360}
Elliott Hughes46882792012-08-03 16:49:39 -0700361
Christopher Ferris24053a42013-08-19 17:45:09 -0700362#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700363
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700364// Here, we only have to provide a callback to iterate across all the
365// loaded libraries. gcc_eh does the rest.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800366int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700367 int rv = 0;
368 for (soinfo* si = solist; si != nullptr; si = si->next) {
369 dl_phdr_info dl_info;
370 dl_info.dlpi_addr = si->link_map_head.l_addr;
371 dl_info.dlpi_name = si->link_map_head.l_name;
372 dl_info.dlpi_phdr = si->phdr;
373 dl_info.dlpi_phnum = si->phnum;
374 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
375 if (rv != 0) {
376 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800377 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700378 }
379 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800380}
Elliott Hughes46882792012-08-03 16:49:39 -0700381
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800382ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name) {
383 return is_gnu_hash() ? gnu_lookup(symbol_name) : elf_lookup(symbol_name);
384}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800386static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
387 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
388 ELF_ST_BIND(s->st_info) == STB_WEAK) {
389 return s->st_shndx != SHN_UNDEF;
390 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
391 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700392 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_soname());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800393 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800394
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800395 return false;
396}
397
398ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name) {
399 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800400 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800401
402 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800403 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
404 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800405
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700406 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700407 symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700408
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800409 // test against bloom filter
410 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700411 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700412 symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700413
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800414 return nullptr;
415 }
416
417 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700418 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800419
420 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700421 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700422 symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700423
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800424 return nullptr;
425 }
426
427 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800428 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700429 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800430 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
431 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700432 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700433 symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(s->st_value),
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700434 static_cast<size_t>(s->st_size));
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800435 return s;
436 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700437 } while ((gnu_chain_[n++] & 1) == 0);
438
439 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700440 symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800441
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800442 return nullptr;
443}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800444
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800445ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name) {
446 uint32_t hash = symbol_name.elf_hash();
447
448 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700449 symbol_name.get_name(), get_soname(),
450 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800451
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800452 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
453 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700454 if (strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
455 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800456 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700457 symbol_name.get_name(), get_soname(),
458 reinterpret_cast<void*>(s->st_value),
459 static_cast<size_t>(s->st_size));
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800460 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800461 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800462 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800463
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700464 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700465 symbol_name.get_name(), get_soname(),
466 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700467
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700468 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800469}
470
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700471soinfo::soinfo(const char* realpath, const struct stat* file_stat,
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700472 off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700473 memset(this, 0, sizeof(*this));
474
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700475 if (realpath != nullptr) {
476 realpath_ = realpath;
477 }
478
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800479 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800480 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700481
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700482 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800483 this->st_dev_ = file_stat->st_dev;
484 this->st_ino_ = file_stat->st_ino;
485 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700486 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700487
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800488 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700489}
490
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800491
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800492uint32_t SymbolName::elf_hash() {
493 if (!has_elf_hash_) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700494 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800495 uint32_t h = 0, g;
496
497 while (*name) {
498 h = (h << 4) + *name++;
499 g = h & 0xf0000000;
500 h ^= g;
501 h ^= g >> 24;
502 }
503
504 elf_hash_ = h;
505 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700506 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800507
508 return elf_hash_;
509}
510
511uint32_t SymbolName::gnu_hash() {
512 if (!has_gnu_hash_) {
513 uint32_t h = 5381;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700514 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800515 while (*name != 0) {
516 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
517 }
518
519 gnu_hash_ = h;
520 has_gnu_hash_ = true;
521 }
522
523 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800524}
525
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800526ElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700527 const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800528 SymbolName symbol_name(name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700529 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700530
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700531 /* "This element's presence in a shared object library alters the dynamic linker's
532 * symbol resolution algorithm for references within the library. Instead of starting
533 * a symbol search with the executable file, the dynamic linker starts from the shared
534 * object itself. If the shared object fails to supply the referenced symbol, the
535 * dynamic linker then searches the executable file and other shared objects as usual."
536 *
537 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
538 *
539 * Note that this is unlikely since static linker avoids generating
540 * relocations for -Bsymbolic linked dynamic executables.
541 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700542 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700543 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_soname(), name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800544 s = si_from->find_symbol_by_name(symbol_name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700545 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700546 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700547 }
548 }
549
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700550 // 1. Look for it in global_group
551 if (s == nullptr) {
552 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700553 DEBUG("%s: looking up %s in %s (from global group)",
554 si_from->get_soname(), name, global_si->get_soname());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800555 s = global_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700556 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700557 *si_found_in = global_si;
558 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700559 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700560
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700561 return true;
562 });
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700563 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700564
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700565 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700566 if (s == nullptr) {
567 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700568 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700569 // we already did this - skip
570 return true;
571 }
572
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700573 DEBUG("%s: looking up %s in %s (from local group)",
574 si_from->get_soname(), name, local_si->get_soname());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800575 s = local_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700576 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700577 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700578 return false;
579 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700580
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700581 return true;
582 });
583 }
584
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700585 if (s != nullptr) {
586 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
587 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700588 si_from->get_soname(), name, reinterpret_cast<void*>(s->st_value),
589 (*si_found_in)->get_soname(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700590 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700591 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700592
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700593 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700594}
595
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800596class ProtectedDataGuard {
597 public:
598 ProtectedDataGuard() {
599 if (ref_count_++ == 0) {
600 protect_data(PROT_READ | PROT_WRITE);
601 }
602 }
603
604 ~ProtectedDataGuard() {
605 if (ref_count_ == 0) { // overflow
606 __libc_fatal("Too many nested calls to dlopen()");
607 }
608
609 if (--ref_count_ == 0) {
610 protect_data(PROT_READ);
611 }
612 }
613 private:
614 void protect_data(int protection) {
615 g_soinfo_allocator.protect_all(protection);
616 g_soinfo_links_allocator.protect_all(protection);
617 }
618
619 static size_t ref_count_;
620};
621
622size_t ProtectedDataGuard::ref_count_ = 0;
623
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700624// Each size has it's own allocator.
625template<size_t size>
626class SizeBasedAllocator {
627 public:
628 static void* alloc() {
629 return allocator_.alloc();
630 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700631
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700632 static void free(void* ptr) {
633 allocator_.free(ptr);
634 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700635
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700636 private:
637 static LinkerBlockAllocator allocator_;
638};
639
640template<size_t size>
641LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
642
643template<typename T>
644class TypeBasedAllocator {
645 public:
646 static T* alloc() {
647 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
648 }
649
650 static void free(T* ptr) {
651 SizeBasedAllocator<sizeof(T)>::free(ptr);
652 }
653};
654
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700655class LoadTask {
656 public:
657 struct deleter_t {
658 void operator()(LoadTask* t) {
659 TypeBasedAllocator<LoadTask>::free(t);
660 }
661 };
662
663 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
664
665 static deleter_t deleter;
666
667 static LoadTask* create(const char* name, soinfo* needed_by) {
668 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
669 return new (ptr) LoadTask(name, needed_by);
670 }
671
672 const char* get_name() const {
673 return name_;
674 }
675
676 soinfo* get_needed_by() const {
677 return needed_by_;
678 }
679 private:
680 LoadTask(const char* name, soinfo* needed_by)
681 : name_(name), needed_by_(needed_by) {}
682
683 const char* name_;
684 soinfo* needed_by_;
685
686 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
687};
688
Ningsheng Jiane93be992014-09-16 15:22:10 +0800689LoadTask::deleter_t LoadTask::deleter;
690
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700691template <typename T>
692using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
693
694typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700695typedef linked_list_t<const char> StringLinkedList;
696typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700697
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700698
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700699// This function walks down the tree of soinfo dependencies
700// in breadth-first order and
701// * calls action(soinfo* si) for each node, and
702// * terminates walk if action returns false.
703//
704// walk_dependencies_tree returns false if walk was terminated
705// by the action and true otherwise.
706template<typename F>
707static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700708 SoinfoLinkedList visit_list;
709 SoinfoLinkedList visited;
710
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700711 for (size_t i = 0; i < root_soinfos_size; ++i) {
712 visit_list.push_back(root_soinfos[i]);
713 }
714
715 soinfo* si;
716 while ((si = visit_list.pop_front()) != nullptr) {
717 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700718 continue;
719 }
720
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700721 if (!action(si)) {
722 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700723 }
724
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700725 visited.push_back(si);
726
727 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700728 visit_list.push_back(child);
729 });
730 }
731
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700732 return true;
733}
734
735
736// This is used by dlsym(3). It performs symbol lookup only within the
737// specified soinfo object and its dependencies in breadth first order.
738ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
739 ElfW(Sym)* result = nullptr;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800740 SymbolName symbol_name(name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700741
742
743 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800744 result = current_soinfo->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700745 if (result != nullptr) {
746 *found = current_soinfo;
747 return false;
748 }
749
750 return true;
751 });
752
753 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800754}
755
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800756/* This is used by dlsym(3) to performs a global symbol lookup. If the
757 start value is null (for RTLD_DEFAULT), the search starts at the
758 beginning of the global solist. Otherwise the search starts at the
759 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700760 */
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700761ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* caller, void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800762 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800763
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700764 soinfo* start = solist;
765
766 if (handle == RTLD_NEXT) {
767 if (caller == nullptr || caller->next == nullptr) {
768 return nullptr;
769 } else {
770 start = caller->next;
771 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800772 }
773
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700774 ElfW(Sym)* s = nullptr;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700775 for (soinfo* si = start; si != nullptr; si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700776 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
777 continue;
778 }
779
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800780 s = si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700781 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800782 *found = si;
783 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600784 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800785 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600786
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700787 // If not found - look into local_group unless
788 // caller is part of the global group in which
789 // case we already did it.
790 if (s == nullptr && caller != nullptr &&
791 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
792 soinfo* local_group_root = caller->get_local_group_root();
793
794 if (handle == RTLD_DEFAULT) {
795 start = local_group_root;
796 }
797
798 for (soinfo* si = start; si != nullptr; si = si->next) {
799 if (si->get_local_group_root() != local_group_root) {
800 break;
801 }
802
803 s = si->find_symbol_by_name(symbol_name);
804 if (s != nullptr) {
805 *found = si;
806 break;
807 }
808 }
809 }
810
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700811 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700812 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
813 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800814 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800815
Elliott Hughescade4c32012-12-20 14:42:14 -0800816 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800817}
818
Kito Chengfa8c05d2013-03-12 14:58:06 +0800819soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800820 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700821 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800822 if (address >= si->base && address - si->base < si->size) {
823 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600824 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800825 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700826 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600827}
828
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800829ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
830 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
831}
832
833static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
834 return sym->st_shndx != SHN_UNDEF &&
835 soaddr >= sym->st_value &&
836 soaddr < sym->st_value + sym->st_size;
837}
838
839ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800840 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800841
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700842 for (size_t i = 0; i < gnu_nbucket_; ++i) {
843 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800844
845 if (n == 0) {
846 continue;
847 }
848
849 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800850 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800851 if (symbol_matches_soaddr(sym, soaddr)) {
852 return sym;
853 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700854 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800855 }
856
857 return nullptr;
858}
859
860ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800861 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600862
Kito Chengfa8c05d2013-03-12 14:58:06 +0800863 // Search the library's symbol table for any defined symbol which
864 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800865 for (size_t i = 0; i < nchain_; ++i) {
866 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800867 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800868 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600869 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800870 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600871
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700872 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600873}
874
Simon Baldwinaef71952015-01-16 13:22:54 +0000875static int open_library_in_zipfile(const char* const path,
876 off64_t* file_offset) {
877 TRACE("Trying zip file open from path '%s'", path);
878
879 // Treat an '!' character inside a path as the separator between the name
880 // of the zip file on disk and the subdirectory to search within it.
881 // For example, if path is "foo.zip!bar/bas/x.so", then we search for
882 // "bar/bas/x.so" within "foo.zip".
883 const char* separator = strchr(path, '!');
884 if (separator == nullptr) {
885 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -0700886 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000887
888 char buf[512];
889 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
890 PRINT("Warning: ignoring very long library path: %s", path);
891 return -1;
892 }
893
894 buf[separator - path] = '\0';
895
896 const char* zip_path = buf;
897 const char* file_path = &buf[separator - path + 1];
898 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
899 if (fd == -1) {
900 return -1;
901 }
902
903 ZipArchiveHandle handle;
904 if (OpenArchiveFd(fd, "", &handle, false) != 0) {
905 // invalid zip-file (?)
906 close(fd);
907 return -1;
908 }
909
910 auto archive_guard = make_scope_guard([&]() {
911 CloseArchive(handle);
912 });
913
914 ZipEntry entry;
915
916 if (FindEntry(handle, ZipEntryName(file_path), &entry) != 0) {
917 // Entry was not found.
918 close(fd);
919 return -1;
920 }
921
922 // Check if it is properly stored
923 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
924 close(fd);
925 return -1;
926 }
927
928 *file_offset = entry.offset;
929 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800930}
931
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700932static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
933 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
934 if (n < 0 || n >= static_cast<int>(buf_size)) {
935 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
936 return false;
937 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000938
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700939 return true;
940}
941
942static int open_library_on_default_path(const char* name, off64_t* file_offset) {
943 for (size_t i = 0; kDefaultLdPaths[i] != nullptr; ++i) {
944 char buf[512];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700945 if (!format_path(buf, sizeof(buf), kDefaultLdPaths[i], name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700946 continue;
Simon Baldwinaef71952015-01-16 13:22:54 +0000947 }
948
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700949 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
950 if (fd != -1) {
951 *file_offset = 0;
952 return fd;
953 }
954 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000955
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700956 return -1;
957}
958
959static int open_library_on_ld_library_path(const char* name, off64_t* file_offset) {
960 for (const auto& path_str : g_ld_library_paths) {
961 char buf[512];
962 const char* const path = path_str.c_str();
963 if (!format_path(buf, sizeof(buf), path, name)) {
964 continue;
965 }
966
967 int fd = -1;
968 if (strchr(buf, '!') != nullptr) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000969 fd = open_library_in_zipfile(buf, file_offset);
970 }
971
972 if (fd == -1) {
973 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
974 if (fd != -1) {
975 *file_offset = 0;
976 }
977 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700978
979 if (fd != -1) {
980 return fd;
981 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000982 }
983
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700984 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +0000985}
986
987static int open_library(const char* name, off64_t* file_offset) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700988 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800989
Elliott Hughes124fae92012-10-31 14:20:03 -0700990 // 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 -0700991 if (strchr(name, '/') != nullptr) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000992 if (strchr(name, '!') != nullptr) {
993 int fd = open_library_in_zipfile(name, file_offset);
994 if (fd != -1) {
995 return fd;
996 }
997 }
998
Elliott Hughes6971fe42012-11-01 22:59:19 -0700999 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
1000 if (fd != -1) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001001 *file_offset = 0;
Elliott Hughes6971fe42012-11-01 22:59:19 -07001002 }
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001003 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001004 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001005
Elliott Hughes124fae92012-10-31 14:20:03 -07001006 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001007 int fd = open_library_on_ld_library_path(name, file_offset);
Elliott Hughes124fae92012-10-31 14:20:03 -07001008 if (fd == -1) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001009 fd = open_library_on_default_path(name, file_offset);
Elliott Hughes124fae92012-10-31 14:20:03 -07001010 }
1011 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001012}
1013
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001014template<typename F>
1015static void for_each_dt_needed(const soinfo* si, F action) {
1016 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1017 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001018 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -07001019 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001020 }
1021}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001022
Simon Baldwinaef71952015-01-16 13:22:54 +00001023static soinfo* load_library(LoadTaskList& load_tasks,
1024 const char* name, int rtld_flags,
1025 const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001026 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001027 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001028 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001029
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001030 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
1031 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001032 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1033 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001034 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001035 } else {
1036 // Open the file.
Simon Baldwinaef71952015-01-16 13:22:54 +00001037 fd = open_library(name, &file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001038 if (fd == -1) {
1039 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001040 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001041 }
1042
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001043 file_guard.reset(fd);
1044 }
1045
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001046 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001047 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001048 return nullptr;
1049 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001050 if (file_offset < 0) {
1051 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
1052 return nullptr;
1053 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001054
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001055 struct stat file_stat;
1056 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001057 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001058 return nullptr;
1059 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001060 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001061 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1062 name, file_offset, file_stat.st_size);
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001063 return nullptr;
1064 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001065
1066 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001067 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1068 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
1069 for (soinfo* si = solist; si != nullptr; si = si->next) {
1070 if (si->get_st_dev() != 0 &&
1071 si->get_st_ino() != 0 &&
1072 si->get_st_dev() == file_stat.st_dev &&
1073 si->get_st_ino() == file_stat.st_ino &&
1074 si->get_file_offset() == file_offset) {
1075 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001076 "will return existing soinfo", name, si->get_realpath());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001077 return si;
1078 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001079 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001080 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001081
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001082 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001083 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001084 return nullptr;
1085 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001086
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001087 std::string realpath = name;
1088 if (!realpath_fd(fd, &realpath)) {
1089 PRINT("cannot resolve realpath for the library \"%s\": %s", name, strerror(errno));
1090 realpath = name;
1091 }
1092
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001093 // Read the ELF header and load the segments.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001094 ElfReader elf_reader(realpath.c_str(), fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001095 if (!elf_reader.Load(extinfo)) {
1096 return nullptr;
1097 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001098
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001099 soinfo* si = soinfo_alloc(realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001100 if (si == nullptr) {
1101 return nullptr;
1102 }
1103 si->base = elf_reader.load_start();
1104 si->size = elf_reader.load_size();
1105 si->load_bias = elf_reader.load_bias();
1106 si->phnum = elf_reader.phdr_count();
1107 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001108
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001109 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001110 soinfo_free(si);
1111 return nullptr;
1112 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001113
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001114 for_each_dt_needed(si, [&] (const char* name) {
1115 load_tasks.push_back(LoadTask::create(name, si));
1116 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001117
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001118 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001119}
1120
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001121static soinfo *find_loaded_library_by_soname(const char* name) {
1122 // Ignore filename with path.
1123 if (strchr(name, '/') != nullptr) {
1124 return nullptr;
1125 }
1126
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001127 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001128 const char* soname = si->get_soname();
1129 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -07001130 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001131 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -07001132 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001133 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001134}
1135
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001136static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name,
1137 int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001138 soinfo* si = find_loaded_library_by_soname(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001139
1140 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001141 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001142 if (si == nullptr) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001143 TRACE("[ '%s' has not been found by soname. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001144 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001145 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001146
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001147 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001148}
1149
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001150static void soinfo_unload(soinfo* si);
1151
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001152// TODO: this is slightly unusual way to construct
1153// the global group for relocation. Not every RTLD_GLOBAL
1154// library is included in this group for backwards-compatibility
1155// reasons.
1156//
1157// This group consists of the main executable, LD_PRELOADs
1158// and libraries with the DF_1_GLOBAL flag set.
1159static soinfo::soinfo_list_t make_global_group() {
1160 soinfo::soinfo_list_t global_group;
1161 for (soinfo* si = somain; si != nullptr; si = si->next) {
1162 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1163 global_group.push_back(si);
1164 }
1165 }
1166
1167 return global_group;
1168}
1169
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001170static bool find_libraries(soinfo* start_with, const char* const library_names[],
1171 size_t library_names_count, soinfo* soinfos[], std::vector<soinfo*>* ld_preloads,
1172 size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001173 // Step 0: prepare.
1174 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001175 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001176 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001177 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001178 }
1179
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001180 // Construct global_group.
1181 soinfo::soinfo_list_t global_group = make_global_group();
1182
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001183 // If soinfos array is null allocate one on stack.
1184 // The array is needed in case of failure; for example
1185 // when library_names[] = {libone.so, libtwo.so} and libone.so
1186 // is loaded correctly but libtwo.so failed for some reason.
1187 // In this case libone.so should be unloaded on return.
1188 // See also implementation of failure_guard below.
1189
1190 if (soinfos == nullptr) {
1191 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1192 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1193 memset(soinfos, 0, soinfos_size);
1194 }
1195
1196 // list of libraries to link - see step 2.
1197 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001198
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001199 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001200 // Housekeeping
1201 load_tasks.for_each([] (LoadTask* t) {
1202 LoadTask::deleter(t);
1203 });
1204
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001205 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001206 soinfo_unload(soinfos[i]);
1207 }
1208 });
1209
1210 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001211 for (LoadTask::unique_ptr task(load_tasks.pop_front());
1212 task.get() != nullptr; task.reset(load_tasks.pop_front())) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001213 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001214 if (si == nullptr) {
1215 return false;
1216 }
1217
1218 soinfo* needed_by = task->get_needed_by();
1219
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001220 if (needed_by != nullptr) {
1221 needed_by->add_child(si);
1222 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001223
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001224 if (si->is_linked()) {
1225 si->increment_ref_count();
1226 }
1227
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001228 // When ld_preloads is not null, the first
1229 // ld_preloads_count libs are in fact ld_preloads.
1230 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001231 // Add LD_PRELOADed libraries to the global group for future runs.
1232 // There is no need to explicitly add them to the global group
1233 // for this run because they are going to appear in the local
1234 // group in the correct order.
1235 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001236 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001237 }
1238
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001239 if (soinfos_count < library_names_count) {
1240 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001241 }
1242 }
1243
1244 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001245 soinfo::soinfo_list_t local_group;
1246 walk_dependencies_tree(
1247 start_with == nullptr ? soinfos : &start_with,
1248 start_with == nullptr ? soinfos_count : 1,
1249 [&] (soinfo* si) {
1250 local_group.push_back(si);
1251 return true;
1252 });
1253
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001254 // We need to increment ref_count in case
1255 // the root of the local group was not linked.
1256 bool was_local_group_root_linked = local_group.front()->is_linked();
1257
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001258 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001259 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001260 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001261 return false;
1262 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001263 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001264 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001265
1266 return true;
1267 });
1268
1269 if (linked) {
1270 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001271 }
1272
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001273 if (!was_local_group_root_linked) {
1274 local_group.front()->increment_ref_count();
1275 }
1276
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001277 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001278}
1279
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001280static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001281 soinfo* si;
1282
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001283 if (name == nullptr) {
1284 si = somain;
1285 } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001286 return nullptr;
1287 }
1288
Elliott Hughesd23736e2012-11-01 15:16:56 -07001289 return si;
1290}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001291
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001292static void soinfo_unload(soinfo* root) {
1293 // Note that the library can be loaded but not linked;
1294 // in which case there is no root but we still need
1295 // to walk the tree and unload soinfos involved.
1296 //
1297 // This happens on unsuccessful dlopen, when one of
1298 // the DT_NEEDED libraries could not be linked/found.
1299 if (root->is_linked()) {
1300 root = root->get_local_group_root();
1301 }
1302
1303 if (!root->can_unload()) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001304 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_soname());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001305 return;
1306 }
1307
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001308 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001309
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001310 if (ref_count == 0) {
1311 soinfo::soinfo_list_t local_unload_list;
1312 soinfo::soinfo_list_t external_unload_list;
1313 soinfo::soinfo_list_t depth_first_list;
1314 depth_first_list.push_back(root);
1315 soinfo* si = nullptr;
1316
1317 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001318 if (local_unload_list.contains(si)) {
1319 continue;
1320 }
1321
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001322 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001323
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001324 if (si->has_min_version(0)) {
1325 soinfo* child = nullptr;
1326 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001327 TRACE("%s@%p needs to unload %s@%p", si->get_soname(), si, child->get_soname(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001328 if (local_unload_list.contains(child)) {
1329 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001330 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001331 external_unload_list.push_back(child);
1332 } else {
1333 depth_first_list.push_front(child);
1334 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001335 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001336 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001337#if !defined(__arm__)
1338 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_soname(), si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001339#else
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001340 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_soname(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001341 for_each_dt_needed(si, [&] (const char* library_name) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001342 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
1343 si->get_soname(), library_name);
1344
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001345 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1346 if (needed != nullptr) {
1347 // Not found: for example if symlink was deleted between dlopen and dlclose
1348 // Since we cannot really handle errors at this point - print and continue.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001349 PRINT("warning: couldn't find %s needed by %s on unload.",
1350 library_name, si->get_soname());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001351 return;
1352 } else if (local_unload_list.contains(needed)) {
1353 // already visited
1354 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001355 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001356 // external group
1357 external_unload_list.push_back(needed);
1358 } else {
1359 // local group
1360 depth_first_list.push_front(needed);
1361 }
1362 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001363#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001364 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001365 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001366
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001367 local_unload_list.for_each([](soinfo* si) {
1368 si->call_destructors();
1369 });
1370
1371 while ((si = local_unload_list.pop_front()) != nullptr) {
1372 notify_gdb_of_unload(si);
1373 soinfo_free(si);
1374 }
1375
1376 while ((si = external_unload_list.pop_front()) != nullptr) {
1377 soinfo_unload(si);
1378 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001379 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001380 TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->get_soname(), ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001381 }
1382}
1383
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001384void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001385 // Use basic string manipulation calls to avoid snprintf.
1386 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1387 // When debug malloc is enabled, this call returns 0. This in turn causes
1388 // snprintf to do nothing, which causes libraries to fail to load.
1389 // See b/17302493 for further details.
1390 // Once the above bug is fixed, this code can be modified to use
1391 // snprintf again.
1392 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1393 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001394 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
1395 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001396 }
1397 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1398 *end = ':';
1399 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001400}
1401
Elliott Hughescade4c32012-12-20 14:42:14 -08001402void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001403 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001404}
1405
Elliott Hughes1a586292014-06-03 16:23:08 -07001406soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001407 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001408 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001409 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001410 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001411 if (extinfo != nullptr) {
1412 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1413 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1414 return nullptr;
1415 }
1416 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001417 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001418 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
1419 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001420 return nullptr;
1421 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001422 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001423
1424 ProtectedDataGuard guard;
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001425 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001426 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001427 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001428 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001429 return si;
1430}
1431
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001432void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001433 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001434 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001435}
1436
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001437static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1438 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1439 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1440 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001441 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
1442 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001443
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001444 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001445}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001446
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001447#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001448#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001449static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
1450 return rela->r_addend;
1451}
1452#else
1453static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001454 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
1455 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001456 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
1457 }
1458 return 0;
1459}
1460#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001461
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001462template<typename ElfRelIteratorT>
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001463bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group,
1464 const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001465 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
1466 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001467 if (rel == nullptr) {
1468 return false;
1469 }
1470
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001471 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
1472 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
1473
1474 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001475 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001476 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001477 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001478
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001479 DEBUG("Processing '%s' relocation at index %zd", get_soname(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001480 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001481 continue;
1482 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001483
1484 ElfW(Sym)* s = nullptr;
1485 soinfo* lsi = nullptr;
1486
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001487 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001488 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001489 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001490 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001491 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001492 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001493 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001494 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_soname());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001495 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001496 }
1497
1498 /* IHI0044C AAELF 4.5.1.1:
1499
1500 Libraries are not searched to resolve weak references.
1501 It is not an error for a weak reference to remain unsatisfied.
1502
1503 During linking, the value of an undefined weak reference is:
1504 - Zero if the relocation type is absolute
1505 - The address of the place if the relocation is pc-relative
1506 - The address of nominal base address if the relocation
1507 type is base-relative.
1508 */
1509
1510 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001511 case R_GENERIC_JUMP_SLOT:
1512 case R_GENERIC_GLOB_DAT:
1513 case R_GENERIC_RELATIVE:
1514 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001515#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001516 case R_AARCH64_ABS64:
1517 case R_AARCH64_ABS32:
1518 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001519#elif defined(__x86_64__)
1520 case R_X86_64_32:
1521 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001522#elif defined(__arm__)
1523 case R_ARM_ABS32:
1524#elif defined(__i386__)
1525 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001526#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001527 /*
1528 * The sym_addr was initialized to be zero above, or the relocation
1529 * code below does not care about value of sym_addr.
1530 * No need to do anything.
1531 */
1532 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001533#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00001534 case R_X86_64_PC32:
1535 sym_addr = reloc;
1536 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001537#elif defined(__i386__)
1538 case R_386_PC32:
1539 sym_addr = reloc;
1540 break;
1541#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001542 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001543 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001544 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001545 }
1546 } else {
1547 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001548 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001549 }
1550 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001551 }
1552
1553 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001554 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001555 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001556 MARK(rel->r_offset);
1557 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
1558 reinterpret_cast<void*>(reloc),
1559 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1560
1561 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001562 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001563 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001564 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001565 MARK(rel->r_offset);
1566 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
1567 reinterpret_cast<void*>(reloc),
1568 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1569 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001570 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001571 case R_GENERIC_RELATIVE:
1572 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001573 MARK(rel->r_offset);
1574 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
1575 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001576 reinterpret_cast<void*>(load_bias + addend));
1577 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001578 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001579 case R_GENERIC_IRELATIVE:
1580 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001581 MARK(rel->r_offset);
1582 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
1583 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001584 reinterpret_cast<void*>(load_bias + addend));
1585 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001586 break;
1587
1588#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001589 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001590 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001591 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001592 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001593 reloc, (sym_addr + addend), sym_name);
1594 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001595 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001596 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001597 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001598 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001599 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001600 reloc, (sym_addr + addend), sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001601 {
1602 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
1603 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
1604 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
1605 if ((min_value <= (reloc_value + (sym_addr + addend))) &&
1606 ((reloc_value + (sym_addr + addend)) <= max_value)) {
1607 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
1608 } else {
1609 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1610 (reloc_value + (sym_addr + addend)), min_value, max_value);
1611 return false;
1612 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001613 }
1614 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001615 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001616 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001617 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001618 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001619 reloc, (sym_addr + addend), sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001620 {
1621 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
1622 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
1623 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
1624 if ((min_value <= (reloc_value + (sym_addr + addend))) &&
1625 ((reloc_value + (sym_addr + addend)) <= max_value)) {
1626 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
1627 } else {
1628 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1629 reloc_value + (sym_addr + addend), min_value, max_value);
1630 return false;
1631 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001632 }
1633 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001634 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001635 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001636 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001637 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001638 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1639 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend) - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001640 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001641 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001642 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001643 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001644 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001645 reloc, (sym_addr + addend), rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001646 {
1647 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
1648 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
1649 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
1650 if ((min_value <= (reloc_value + ((sym_addr + addend) - rel->r_offset))) &&
1651 ((reloc_value + ((sym_addr + addend) - rel->r_offset)) <= max_value)) {
1652 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
1653 } else {
1654 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1655 reloc_value + ((sym_addr + addend) - rel->r_offset), min_value, max_value);
1656 return false;
1657 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001658 }
1659 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001660 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001661 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001662 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001663 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001664 reloc, (sym_addr + addend), rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001665 {
1666 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
1667 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
1668 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
1669 if ((min_value <= (reloc_value + ((sym_addr + addend) - rel->r_offset))) &&
1670 ((reloc_value + ((sym_addr + addend) - rel->r_offset)) <= max_value)) {
1671 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
1672 } else {
1673 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1674 reloc_value + ((sym_addr + addend) - rel->r_offset), min_value, max_value);
1675 return false;
1676 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001677 }
1678 break;
1679
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001680 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001681 /*
1682 * ET_EXEC is not supported so this should not happen.
1683 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001684 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07001685 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001686 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07001687 * R_AARCH64_COPY may only appear in executable objects where e_type is
1688 * set to ET_EXEC.
1689 */
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001690 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_soname());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001691 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001692 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001693 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001694 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001695 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001696 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001697 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001698 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001699 break;
1700#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001701 case R_X86_64_32:
1702 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001703 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001704 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1705 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001706 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001707 break;
1708 case R_X86_64_64:
1709 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001710 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001711 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1712 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001713 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001714 break;
1715 case R_X86_64_PC32:
1716 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001717 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001718 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1719 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1720 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001721 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001722 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001723#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001724 case R_ARM_ABS32:
1725 count_relocation(kRelocAbsolute);
1726 MARK(rel->r_offset);
1727 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1728 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1729 break;
1730 case R_ARM_REL32:
1731 count_relocation(kRelocRelative);
1732 MARK(rel->r_offset);
1733 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1734 reloc, sym_addr, rel->r_offset, sym_name);
1735 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1736 break;
1737 case R_ARM_COPY:
1738 /*
1739 * ET_EXEC is not supported so this should not happen.
1740 *
1741 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1742 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001743 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001744 * R_ARM_COPY may only appear in executable objects where e_type is
1745 * set to ET_EXEC.
1746 */
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001747 DL_ERR("%s R_ARM_COPY relocations are not supported", get_soname());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001748 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001749#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001750 case R_386_32:
1751 count_relocation(kRelocRelative);
1752 MARK(rel->r_offset);
1753 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1754 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1755 break;
1756 case R_386_PC32:
1757 count_relocation(kRelocRelative);
1758 MARK(rel->r_offset);
1759 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1760 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1761 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1762 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001763#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001764 default:
1765 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001766 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001767 }
1768 }
1769 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001770}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001771#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001772
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001773void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
1774 size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001775 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001776 return;
1777 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001778
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001779 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_soname());
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001780
1781 int begin = reverse ? (count - 1) : 0;
1782 int end = reverse ? -1 : count;
1783 int step = reverse ? -1 : 1;
1784
1785 for (int i = begin; i != end; i += step) {
1786 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001787 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001788 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001789
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001790 TRACE("[ Done calling %s for '%s' ]", array_name, get_soname());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001791}
1792
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001793void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001794 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001795 return;
1796 }
1797
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001798 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_soname());
Elliott Hughesd23736e2012-11-01 15:16:56 -07001799 function();
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001800 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_soname());
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001801}
1802
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001803void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001804 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1805 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001806 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001807}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001808
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001809void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001810 if (constructors_called) {
1811 return;
1812 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001813
Elliott Hughesd23736e2012-11-01 15:16:56 -07001814 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1815 // protect against recursive constructor calls. One simple example of constructor recursion
1816 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1817 // 1. The program depends on libc, so libc's constructor is called here.
1818 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1819 // 3. dlopen() calls the constructors on the newly created
1820 // soinfo for libc_malloc_debug_leak.so.
1821 // 4. The debug .so depends on libc, so CallConstructors is
1822 // called again with the libc soinfo. If it doesn't trigger the early-
1823 // out above, the libc constructor will be called again (recursively!).
1824 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001825
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001826 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001827 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001828 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001829 get_soname(), preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001830 }
1831
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001832 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001833 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001834 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001835
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001836 TRACE("\"%s\": calling constructors", get_soname());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001837
1838 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001839 call_function("DT_INIT", init_func_);
1840 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001841}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001842
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001843void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001844 if (!constructors_called) {
1845 return;
1846 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001847 TRACE("\"%s\": calling destructors", get_soname());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001848
1849 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001850 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001851
1852 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001853 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001854
1855 // This is needed on second call to dlopen
1856 // after library has been unloaded with RTLD_NODELETE
1857 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001858}
1859
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001860void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001861 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001862 child->parents_.push_back(this);
1863 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001864 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001865}
1866
1867void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001868 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001869 return;
1870 }
1871
1872 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001873 children_.for_each([&] (soinfo* child) {
1874 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001875 return parent == this;
1876 });
1877 });
1878
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001879 parents_.for_each([&] (soinfo* parent) {
1880 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001881 return child == this;
1882 });
1883 });
1884
1885 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001886 parents_.clear();
1887 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001888}
1889
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001890dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001891 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001892 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001893 }
1894
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001895 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001896};
1897
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001898ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001899 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001900 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001901 }
1902
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001903 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001904}
1905
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001906off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001907 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001908 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001909 }
1910
1911 return 0;
1912}
1913
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001914uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001915 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001916 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001917 }
1918
1919 return 0;
1920}
1921
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001922uint32_t soinfo::get_dt_flags_1() const {
1923 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001924 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001925 }
1926
1927 return 0;
1928}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001929
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001930void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1931 if (has_min_version(1)) {
1932 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001933 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001934 }
1935
1936 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001937 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001938 }
1939
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001940 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001941 }
1942}
1943
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001944const char* soinfo::get_realpath() const {
1945#if defined(__arm__)
1946 if (has_min_version(2)) {
1947 return realpath_.c_str();
1948 } else {
1949 return old_name_;
1950 }
1951#else
1952 return realpath_.c_str();
1953#endif
1954}
1955
1956const char* soinfo::get_soname() const {
1957#if defined(__arm__)
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001958 if (has_min_version(2)) {
1959 return soname_;
1960 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001961 return old_name_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001962 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001963#else
1964 return soname_;
1965#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001966}
1967
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001968// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001969// 'this->flags' does not have FLAG_NEW_SOINFO set.
1970static soinfo::soinfo_list_t g_empty_list;
1971
1972soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001973 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001974 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001975 }
1976
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001977 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001978}
1979
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001980soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001981 if (has_min_version(0)) {
1982 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001983 }
1984
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001985 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001986}
1987
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001988ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1989 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1990 return call_ifunc_resolver(s->st_value + load_bias);
1991 }
1992
1993 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1994}
1995
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001996const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001997 if (has_min_version(1) && (index >= strtab_size_)) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001998 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
1999 get_soname(), strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002000 }
2001
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002002 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002003}
2004
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002005bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002006 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002007}
2008
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002009bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002010 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002011}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002012
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002013bool soinfo::is_linked() const {
2014 return (flags_ & FLAG_LINKED) != 0;
2015}
2016
2017bool soinfo::is_main_executable() const {
2018 return (flags_ & FLAG_EXE) != 0;
2019}
2020
2021void soinfo::set_linked() {
2022 flags_ |= FLAG_LINKED;
2023}
2024
2025void soinfo::set_linker_flag() {
2026 flags_ |= FLAG_LINKER;
2027}
2028
2029void soinfo::set_main_executable() {
2030 flags_ |= FLAG_EXE;
2031}
2032
2033void soinfo::increment_ref_count() {
2034 local_group_root_->ref_count_++;
2035}
2036
2037size_t soinfo::decrement_ref_count() {
2038 return --local_group_root_->ref_count_;
2039}
2040
2041soinfo* soinfo::get_local_group_root() const {
2042 return local_group_root_;
2043}
2044
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002045/* Force any of the closed stdin, stdout and stderr to be associated with
2046 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07002047static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002048 int dev_null, i, status;
2049 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002050
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002051 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
2052 if (dev_null < 0) {
2053 DL_ERR("cannot open /dev/null: %s", strerror(errno));
2054 return -1;
2055 }
2056 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002057
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002058 /* If any of the stdio file descriptors is valid and not associated
2059 with /dev/null, dup /dev/null to it. */
2060 for (i = 0; i < 3; i++) {
2061 /* If it is /dev/null already, we are done. */
2062 if (i == dev_null) {
2063 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002064 }
2065
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002066 TRACE("[ Nullifying stdio file descriptor %d]", i);
2067 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
2068
2069 /* If file is opened, we are good. */
2070 if (status != -1) {
2071 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002072 }
2073
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002074 /* The only error we allow is that the file descriptor does not
2075 exist, in which case we dup /dev/null to it. */
2076 if (errno != EBADF) {
2077 DL_ERR("fcntl failed: %s", strerror(errno));
2078 return_value = -1;
2079 continue;
2080 }
2081
2082 /* Try dupping /dev/null to this stdio file descriptor and
2083 repeat if there is a signal. Note that any errors in closing
2084 the stdio descriptor are lost. */
2085 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
2086 if (status < 0) {
2087 DL_ERR("dup2 failed: %s", strerror(errno));
2088 return_value = -1;
2089 continue;
2090 }
2091 }
2092
2093 /* If /dev/null is not one of the stdio file descriptors, close it. */
2094 if (dev_null > 2) {
2095 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
2096 status = TEMP_FAILURE_RETRY(close(dev_null));
2097 if (status == -1) {
2098 DL_ERR("close failed: %s", strerror(errno));
2099 return_value = -1;
2100 }
2101 }
2102
2103 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002104}
2105
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002106bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002107 /* Extract dynamic section */
2108 ElfW(Word) dynamic_flags = 0;
2109 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002110
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002111 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002112 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002113 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002114 INFO("[ linking %s ]", get_soname());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002115 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002116 }
2117
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002118 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002119 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002120 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_soname());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002121 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002122 return false;
2123 } else {
2124 if (!relocating_linker) {
2125 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002126 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002127 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002128
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002129#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002130 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2131 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002132#endif
2133
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002134 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002135 // Note that: "Except for the DT_NULL element at the end of the array,
2136 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2137 //
2138 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002139 uint32_t needed_count = 0;
2140 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2141 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2142 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2143 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002144 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002145 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002146 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002147
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002148 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002149 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2150 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2151 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2152 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002153 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002154
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002155 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002156 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002157 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002158 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2159 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002160
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002161 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002162 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002163 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002164 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2165 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002166
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002167 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002168 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002169 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002170 return false;
2171 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002172 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002173
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002174 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002175 break;
2176
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002177 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002178 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002179 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002180
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002181 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002182 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002183 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002184
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002185 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002186 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002187 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002188
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002189 case DT_SYMENT:
2190 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002191 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2192 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002193 return false;
2194 }
2195 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002196
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002197 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002198#if defined(USE_RELA)
2199 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002200 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002201 return false;
2202 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002203#else
2204 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002205 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002206 return false;
2207 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002208#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002209 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002210
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002211 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002212#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002213 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002214#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002215 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002216#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002217 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002218
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002219 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002220#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002221 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002222#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002223 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002224#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002225 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002226
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002227 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002228#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002229 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002230 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002231#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002232 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2233 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002234
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002235 case DT_DEBUG:
2236 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2237 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002238// FIXME: not working currently for N64
2239// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002240// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002241// read-only, but the DYNAMIC header claims it is writable.
2242#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002243 if ((dynamic_flags & PF_W) != 0) {
2244 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2245 }
Chris Dearman99186652014-02-06 20:36:51 -08002246#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002247 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002248#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002249 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002250 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002251 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002252
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002253 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002254 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002255 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002256
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002257 case DT_ANDROID_RELA:
2258 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2259 break;
2260
2261 case DT_ANDROID_RELASZ:
2262 android_relocs_size_ = d->d_un.d_val;
2263 break;
2264
2265 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002266 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002267 return false;
2268
2269 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002270 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002271 return false;
2272
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002273 case DT_RELAENT:
2274 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002275 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002276 return false;
2277 }
2278 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002279
2280 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002281 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002282 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002283
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002284 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002285 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002286 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002287
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002288 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002289 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002290 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002291
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002292#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002293 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002294 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002295 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002296
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002297 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002298 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002299 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002300
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002301 case DT_RELENT:
2302 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002303 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002304 return false;
2305 }
2306 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002307
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002308 case DT_ANDROID_REL:
2309 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2310 break;
2311
2312 case DT_ANDROID_RELSZ:
2313 android_relocs_size_ = d->d_un.d_val;
2314 break;
2315
2316 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002317 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002318 return false;
2319
2320 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002321 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002322 return false;
2323
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002324 // "Indicates that all RELATIVE relocations have been concatenated together,
2325 // and specifies the RELATIVE relocation count."
2326 //
2327 // TODO: Spec also mentions that this can be used to optimize relocation process;
2328 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002329 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002330 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002331
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002332 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002333 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002334 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002335
2336 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002337 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002338 return false;
2339
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002340#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002341 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002342 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002343 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002344 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002345
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002346 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002347 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002348 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002349 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002350
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002351 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002352 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002353 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002354 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002355
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002356 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002357 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002358 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002359
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002360 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002361 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002362 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002363 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002364
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002365 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002366 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002367 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002368
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002369 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002370 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002371 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002372 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002373
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002374 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002375 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002376 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002377
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002378 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002379#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002380 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002381 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002382#else
2383 has_text_relocations = true;
2384 break;
2385#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002386
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002387 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002388 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002389 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002390
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002391 case DT_NEEDED:
2392 ++needed_count;
2393 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002394
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002395 case DT_FLAGS:
2396 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002397#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002398 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002399 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002400#else
2401 has_text_relocations = true;
2402#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002403 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002404 if (d->d_un.d_val & DF_SYMBOLIC) {
2405 has_DT_SYMBOLIC = true;
2406 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002407 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002408
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002409 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002410 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002411
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002412 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002413 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2414 }
2415 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002416#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002417 case DT_MIPS_RLD_MAP:
2418 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2419 {
2420 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2421 *dp = &_r_debug;
2422 }
2423 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002424 case DT_MIPS_RLD_MAP2:
2425 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2426 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002427 r_debug** dp = reinterpret_cast<r_debug**>(
2428 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08002429 *dp = &_r_debug;
2430 }
2431 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002432
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002433 case DT_MIPS_RLD_VERSION:
2434 case DT_MIPS_FLAGS:
2435 case DT_MIPS_BASE_ADDRESS:
2436 case DT_MIPS_UNREFEXTNO:
2437 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002438
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002439 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002440 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002441 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002442
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002443 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002444 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002445 break;
2446
2447 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002448 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002449 break;
2450#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002451 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2452 case DT_BIND_NOW:
2453 break;
2454
2455 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002456 case DT_VERSYM:
2457 case DT_VERDEF:
2458 case DT_VERDEFNUM:
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002459 case DT_VERNEED:
2460 case DT_VERNEEDNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002461 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002462
2463 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002464 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002465 DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002466 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2467 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002468 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002469 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002470 }
2471
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002472 // second pass - parse entries relying on strtab
2473 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2474 if (d->d_tag == DT_SONAME) {
2475 soname_ = get_string(d->d_un.d_val);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002476#if defined(__arm__)
2477 strlcpy(old_name_, soname_, sizeof(old_name_));
2478#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002479 break;
2480 }
2481 }
2482
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002483 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002484 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002485
2486 // Sanity checks.
2487 if (relocating_linker && needed_count != 0) {
2488 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2489 return false;
2490 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002491 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002492 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
2493 "(new hash type from the future?)", get_soname());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002494 return false;
2495 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002496 if (strtab_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002497 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_soname());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002498 return false;
2499 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002500 if (symtab_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002501 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_soname());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002502 return false;
2503 }
2504 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002505}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002506
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002507bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
2508 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002509
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002510 local_group_root_ = local_group.front();
2511 if (local_group_root_ == nullptr) {
2512 local_group_root_ = this;
2513 }
2514
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002515#if !defined(__LP64__)
2516 if (has_text_relocations) {
2517 // Make segments writable to allow text relocations to work properly. We will later call
2518 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2519 DL_WARN("%s has text relocations. This is wasting memory and prevents "
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002520 "security hardening. Please fix.", get_soname());
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002521 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2522 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002523 get_soname(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002524 return false;
2525 }
2526 }
2527#endif
2528
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002529 if (android_relocs_ != nullptr) {
2530 // check signature
2531 if (android_relocs_size_ > 3 &&
2532 android_relocs_[0] == 'A' &&
2533 android_relocs_[1] == 'P' &&
2534 (android_relocs_[2] == 'U' || android_relocs_[2] == 'S') &&
2535 android_relocs_[3] == '2') {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002536 DEBUG("[ android relocating %s ]", get_soname());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002537
2538 bool relocated = false;
2539 const uint8_t* packed_relocs = android_relocs_ + 4;
2540 const size_t packed_relocs_size = android_relocs_size_ - 4;
2541
2542 if (android_relocs_[2] == 'U') {
2543 relocated = relocate(
2544 packed_reloc_iterator<leb128_decoder>(
2545 leb128_decoder(packed_relocs, packed_relocs_size)),
2546 global_group, local_group);
2547 } else { // android_relocs_[2] == 'S'
2548 relocated = relocate(
2549 packed_reloc_iterator<sleb128_decoder>(
2550 sleb128_decoder(packed_relocs, packed_relocs_size)),
2551 global_group, local_group);
2552 }
2553
2554 if (!relocated) {
2555 return false;
2556 }
2557 } else {
2558 DL_ERR("bad android relocation header.");
2559 return false;
2560 }
2561 }
2562
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002563#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002564 if (rela_ != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002565 DEBUG("[ relocating %s ]", get_soname());
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002566 if (!relocate(plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002567 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002568 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002569 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002570 if (plt_rela_ != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002571 DEBUG("[ relocating %s plt ]", get_soname());
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002572 if (!relocate(plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002573 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002574 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002575 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002576#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002577 if (rel_ != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002578 DEBUG("[ relocating %s ]", get_soname());
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002579 if (!relocate(plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002580 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002581 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002582 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002583 if (plt_rel_ != nullptr) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002584 DEBUG("[ relocating %s plt ]", get_soname());
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002585 if (!relocate(plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002586 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002587 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002588 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002589#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002590
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002591#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08002592 if (!mips_relocate_got(global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002593 return false;
2594 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002595#endif
2596
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002597 DEBUG("[ finished linking %s ]", get_soname());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002598
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002599#if !defined(__LP64__)
2600 if (has_text_relocations) {
2601 // All relocations are done, we can protect our segments back to read-only.
2602 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2603 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002604 get_soname(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002605 return false;
2606 }
2607 }
2608#endif
2609
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002610 /* We can also turn on GNU RELRO protection */
2611 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2612 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002613 get_soname(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002614 return false;
2615 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002616
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002617 /* Handle serializing/sharing the RELRO segment */
2618 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2619 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2620 extinfo->relro_fd) < 0) {
2621 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002622 get_soname(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002623 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002624 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002625 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2626 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2627 extinfo->relro_fd) < 0) {
2628 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002629 get_soname(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002630 return false;
2631 }
2632 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002633
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002634 notify_gdb_of_load(this);
2635 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002636}
2637
Nick Kralevich468319c2011-11-11 15:53:17 -08002638/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002639 * This function add vdso to internal dso list.
2640 * It helps to stack unwinding through signal handlers.
2641 * Also, it makes bionic more like glibc.
2642 */
Kito Cheng812fd422014-03-25 22:53:56 +08002643static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002644#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002645 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002646 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002647 return;
2648 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002649
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002650 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002651
Elliott Hughes0266ae52014-02-10 17:46:57 -08002652 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2653 si->phnum = ehdr_vdso->e_phnum;
2654 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2655 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002656 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002657
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002658 si->prelink_image();
2659 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002660#endif
2661}
2662
2663/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002664 * This is linker soinfo for GDB. See details below.
2665 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002666#if defined(__LP64__)
2667#define LINKER_PATH "/system/bin/linker64"
2668#else
2669#define LINKER_PATH "/system/bin/linker"
2670#endif
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002671
2672// This is done to avoid calling c-tor prematurely
2673// because soinfo c-tor needs memory allocator
2674// which might be initialized after global variables.
2675static uint8_t linker_soinfo_for_gdb_buf[sizeof(soinfo)] __attribute__((aligned(8)));
2676static soinfo* linker_soinfo_for_gdb = nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002677
2678/* gdb expects the linker to be in the debug shared object list.
2679 * Without this, gdb has trouble locating the linker's ".text"
2680 * and ".plt" sections. Gdb could also potentially use this to
2681 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2682 * Don't use soinfo_alloc(), because the linker shouldn't
2683 * be on the soinfo list.
2684 */
2685static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002686 linker_soinfo_for_gdb = new (linker_soinfo_for_gdb_buf) soinfo(LINKER_PATH, nullptr, 0, 0);
2687
2688 linker_soinfo_for_gdb->base = linker_base;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002689
2690 /*
2691 * Set the dynamic field in the link map otherwise gdb will complain with
2692 * the following:
2693 * warning: .dynamic section for "/system/bin/linker" is not at the
2694 * expected address (wrong library or version mismatch?)
2695 */
2696 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2697 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2698 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002699 &linker_soinfo_for_gdb->dynamic, nullptr);
2700 insert_soinfo_into_debug_map(linker_soinfo_for_gdb);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002701}
2702
2703/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002704 * This code is called after the linker has linked itself and
2705 * fixed it's own GOT. It is safe to make references to externs
2706 * and other non-local data at this point.
2707 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002708static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002709#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002710 struct timeval t0, t1;
2711 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002712#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002713
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002714 // Initialize environment functions, and get to the ELF aux vectors table.
2715 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002716
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002717 // If this is a setuid/setgid program, close the security hole described in
2718 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2719 if (get_AT_SECURE()) {
2720 nullify_closed_stdio();
2721 }
2722
2723 debuggerd_init();
2724
2725 // Get a few environment variables.
2726 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2727 if (LD_DEBUG != nullptr) {
2728 g_ld_debug_verbosity = atoi(LD_DEBUG);
2729 }
2730
2731 // Normally, these are cleaned by linker_env_init, but the test
2732 // doesn't cost us anything.
2733 const char* ldpath_env = nullptr;
2734 const char* ldpreload_env = nullptr;
2735 if (!get_AT_SECURE()) {
2736 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2737 ldpreload_env = linker_env_get("LD_PRELOAD");
2738 }
2739
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -08002740#if !defined(__LP64__)
2741 if (personality(PER_LINUX32) == -1) {
2742 __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
2743 }
2744#endif
2745
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002746 INFO("[ android linker & debugger ]");
2747
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002748 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002749 if (si == nullptr) {
2750 exit(EXIT_FAILURE);
2751 }
2752
2753 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002754 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002755 link_map* map = &(si->link_map_head);
2756
2757 map->l_addr = 0;
2758 map->l_name = args.argv[0];
2759 map->l_prev = nullptr;
2760 map->l_next = nullptr;
2761
2762 _r_debug.r_map = map;
2763 r_debug_tail = map;
2764
2765 init_linker_info_for_gdb(linker_base);
2766
2767 // Extract information passed from the kernel.
2768 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2769 si->phnum = args.getauxval(AT_PHNUM);
2770 si->entry = args.getauxval(AT_ENTRY);
2771
2772 /* Compute the value of si->base. We can't rely on the fact that
2773 * the first entry is the PHDR because this will not be true
2774 * for certain executables (e.g. some in the NDK unit test suite)
2775 */
2776 si->base = 0;
2777 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2778 si->load_bias = 0;
2779 for (size_t i = 0; i < si->phnum; ++i) {
2780 if (si->phdr[i].p_type == PT_PHDR) {
2781 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2782 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2783 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002784 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002785 }
2786 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002787
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002788 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2789 if (elf_hdr->e_type != ET_DYN) {
2790 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2791 exit(EXIT_FAILURE);
2792 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002793
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002794 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2795 parse_LD_LIBRARY_PATH(ldpath_env);
2796 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002797
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002798 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002799
Dmitriy Ivanov67181252015-01-07 15:48:25 -08002800 if (!si->prelink_image()) {
2801 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2802 exit(EXIT_FAILURE);
2803 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002804
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002805 // add somain to global group
2806 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2807
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002808 // Load ld_preloads and dependencies.
2809 StringLinkedList needed_library_name_list;
2810 size_t needed_libraries_count = 0;
2811 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07002812
2813 for (const auto& ld_preload_name : g_ld_preload_names) {
2814 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002815 ++needed_libraries_count;
2816 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002817
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002818 for_each_dt_needed(si, [&](const char* name) {
2819 needed_library_name_list.push_back(name);
2820 ++needed_libraries_count;
2821 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002822
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002823 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002824
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002825 memset(needed_library_names, 0, sizeof(needed_library_names));
2826 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002827
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07002828 if (needed_libraries_count > 0 &&
2829 !find_libraries(si, needed_library_names, needed_libraries_count, nullptr,
2830 &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002831 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2832 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002833 } else if (needed_libraries_count == 0) {
2834 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
2835 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2836 exit(EXIT_FAILURE);
2837 }
2838 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002839 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002840
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002841 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002842
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002843 {
2844 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06002845
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002846 si->call_pre_init_constructors();
2847
2848 /* After the prelink_image, the si->load_bias is initialized.
2849 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2850 * We need to update this value for so exe here. So Unwind_Backtrace
2851 * for some arch like x86 could work correctly within so exe.
2852 */
2853 map->l_addr = si->load_bias;
2854 si->call_constructors();
2855 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002856
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002857#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002858 gettimeofday(&t1, nullptr);
2859 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2860 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2861 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002862#endif
2863#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002864 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2865 linker_stats.count[kRelocAbsolute],
2866 linker_stats.count[kRelocRelative],
2867 linker_stats.count[kRelocCopy],
2868 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002869#endif
2870#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002871 {
2872 unsigned n;
2873 unsigned i;
2874 unsigned count = 0;
2875 for (n = 0; n < 4096; n++) {
2876 if (bitmask[n]) {
2877 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002878#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002879 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002880#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002881 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002882#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002883 if (x & 1) {
2884 count++;
2885 }
2886 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002887 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002888 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002889 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002890 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2891 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002892#endif
2893
2894#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002895 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002896#endif
2897
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002898 TRACE("[ Ready to execute '%s' @ %p ]", si->get_soname(), reinterpret_cast<void*>(si->entry));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002899 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002900}
Nick Kralevich468319c2011-11-11 15:53:17 -08002901
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002902/* Compute the load-bias of an existing executable. This shall only
2903 * be used to compute the load bias of an executable or shared library
2904 * that was loaded by the kernel itself.
2905 *
2906 * Input:
2907 * elf -> address of ELF header, assumed to be at the start of the file.
2908 * Return:
2909 * load bias, i.e. add the value of any p_vaddr in the file to get
2910 * the corresponding address in memory.
2911 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002912static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2913 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002914 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002915 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002916
Elliott Hughes0266ae52014-02-10 17:46:57 -08002917 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002918 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002919 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002920 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002921 }
2922 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002923}
2924
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002925extern "C" void _start();
2926
Nick Kralevich468319c2011-11-11 15:53:17 -08002927/*
2928 * This is the entry point for the linker, called from begin.S. This
2929 * method is responsible for fixing the linker's own relocations, and
2930 * then calling __linker_init_post_relocation().
2931 *
2932 * Because this method is called before the linker has fixed it's own
2933 * relocations, any attempt to reference an extern variable, extern
2934 * function, or other GOT reference will generate a segfault.
2935 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002936extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002937 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002938
Elliott Hughes0266ae52014-02-10 17:46:57 -08002939 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002940 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002941 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002942 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002943
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002944 soinfo linker_so(nullptr, nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002945
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002946 // If the linker is not acting as PT_INTERP entry_point is equal to
2947 // _start. Which means that the linker is running as an executable and
2948 // already linked by PT_INTERP.
2949 //
2950 // This happens when user tries to run 'adb shell /system/bin/linker'
2951 // see also https://code.google.com/p/android/issues/detail?id=63174
2952 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2953 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2954 }
2955
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002956 linker_so.base = linker_addr;
2957 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2958 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002959 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002960 linker_so.phdr = phdr;
2961 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002962 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07002963
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002964 // This might not be obvious... The reasons why we pass g_empty_list
2965 // in place of local_group here are (1) we do not really need it, because
2966 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2967 // itself without having to look into local_group and (2) allocators
2968 // are not yet initialized, and therefore we cannot use linked_list.push_*
2969 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002970 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002971 // It would be nice to print an error message, but if the linker
2972 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002973 // call write() (because it involves a GOT reference). We may as
2974 // well try though...
2975 const char* msg = "CANNOT LINK EXECUTABLE: ";
2976 write(2, msg, strlen(msg));
2977 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2978 write(2, "\n", 1);
2979 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002980 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002981
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002982 __libc_init_tls(args);
2983
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002984 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002985 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002986
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002987 // Initialize static variables. Note that in order to
2988 // get correct libdl_info we need to call constructors
2989 // before get_libdl_info().
2990 solist = get_libdl_info();
2991 sonext = get_libdl_info();
2992
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002993 // We have successfully fixed our own relocations. It's safe to run
2994 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002995 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002996 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002997
Elliott Hughes611f9562015-01-23 10:43:58 -08002998 INFO("[ jumping to _start ]");
2999
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003000 // Return the address that the calling assembly stub should jump to.
3001 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08003002}