blob: a9c2bc1e52c2ac37ea1e35435c4bc8e4ed4cfd00 [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 Ivanovc9d16582014-10-24 14:46:12 -0700156 map->l_name = info->name;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700157 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800158
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700159 // Stick the new library at the end of the list.
160 // gdb tends to care more about libc than it does
161 // about leaf libraries, and ordering it this way
162 // reduces the back-and-forth over the wire.
163 if (r_debug_tail) {
164 r_debug_tail->l_next = map;
165 map->l_prev = r_debug_tail;
166 map->l_next = 0;
167 } else {
168 _r_debug.r_map = map;
169 map->l_prev = 0;
170 map->l_next = 0;
171 }
172 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800173}
174
Elliott Hughesbedfe382012-08-14 14:07:59 -0700175static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700176 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700177
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700178 if (r_debug_tail == map) {
179 r_debug_tail = map->l_prev;
180 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700181
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700182 if (map->l_prev) {
183 map->l_prev->l_next = map->l_next;
184 }
185 if (map->l_next) {
186 map->l_next->l_prev = map->l_prev;
187 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700188}
189
Elliott Hughesbedfe382012-08-14 14:07:59 -0700190static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800191 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700192 // GDB already knows about the main executable
193 return;
194 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700196 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700198 _r_debug.r_state = r_debug::RT_ADD;
199 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700201 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700203 _r_debug.r_state = r_debug::RT_CONSISTENT;
204 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700205}
206
Elliott Hughesbedfe382012-08-14 14:07:59 -0700207static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800208 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700209 // GDB already knows about the main executable
210 return;
211 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700212
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700213 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700214
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700215 _r_debug.r_state = r_debug::RT_DELETE;
216 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700217
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700218 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700219
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700220 _r_debug.r_state = r_debug::RT_CONSISTENT;
221 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222}
223
Elliott Hughes18a206c2012-10-29 17:37:13 -0700224void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800225 _r_debug.r_state = r_debug::RT_ADD;
226 rtld_db_dlactivity();
227 _r_debug.r_state = r_debug::RT_CONSISTENT;
228 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229}
230
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700231LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
232 return g_soinfo_links_allocator.alloc();
233}
234
235void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
236 g_soinfo_links_allocator.free(entry);
237}
238
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700239static soinfo* soinfo_alloc(const char* name, struct stat* file_stat,
240 off64_t file_offset, uint32_t rtld_flags) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200241 if (strlen(name) >= SOINFO_NAME_LEN) {
242 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700243 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200244 }
245
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700246 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700247
Magnus Malmbornba98d922012-09-12 13:00:55 +0200248 sonext->next = si;
249 sonext = si;
250
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700251 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200252 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800253}
254
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800255static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700256 if (si == nullptr) {
257 return;
258 }
259
260 if (si->base != 0 && si->size != 0) {
261 munmap(reinterpret_cast<void*>(si->base), si->size);
262 }
263
264 soinfo *prev = nullptr, *trav;
265
266 TRACE("name %s: freeing soinfo @ %p", si->name, si);
267
268 for (trav = solist; trav != nullptr; trav = trav->next) {
269 if (trav == si) {
270 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700271 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700272 prev = trav;
273 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800274
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700275 if (trav == nullptr) {
276 // si was not in solist
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -0800277 DL_ERR("name \"%s\"@%p is not in solist!", si->name, si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700278 return;
279 }
Elliott Hughes46882792012-08-03 16:49:39 -0700280
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700281 // clear links to/from si
282 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700283
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700284 // prev will never be null, because the first entry in solist is
285 // always the static libdl_info.
286 prev->next = si->next;
287 if (si == sonext) {
288 sonext = prev;
289 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800290
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700291 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292}
293
Elliott Hughescade4c32012-12-20 14:42:14 -0800294static void parse_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700295 std::vector<std::string>* paths) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700296 if (path == nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800297 return;
298 }
299
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700300 paths->clear();
Elliott Hughescade4c32012-12-20 14:42:14 -0800301
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700302 for (const char *p = path; ; ++p) {
303 size_t len = strcspn(p, delimiters);
304 // skip empty tokens
305 if (len == 0) {
306 continue;
Elliott Hughescade4c32012-12-20 14:42:14 -0800307 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800308
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700309 paths->push_back(std::string(p, len));
310 p += len;
311
312 if (*p == '\0') {
313 break;
314 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800315 }
316}
317
318static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700319 parse_path(path, ":", &g_ld_library_paths);
Elliott Hughescade4c32012-12-20 14:42:14 -0800320}
321
322static void parse_LD_PRELOAD(const char* path) {
323 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700324 parse_path(path, " :", &g_ld_preload_names);
Elliott Hughescade4c32012-12-20 14:42:14 -0800325}
326
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700327#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700328
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700329// For a given PC, find the .so that it belongs to.
330// Returns the base address of the .ARM.exidx section
331// for that .so, and the number of 8-byte entries
332// in that section (via *pcount).
333//
334// Intended to be called by libc's __gnu_Unwind_Find_exidx().
335//
336// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800337_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800338 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700340 for (soinfo* si = solist; si != 0; si = si->next) {
341 if ((addr >= si->base) && (addr < (si->base + si->size))) {
342 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800343 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800344 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700345 }
346 *pcount = 0;
347 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800348}
Elliott Hughes46882792012-08-03 16:49:39 -0700349
Christopher Ferris24053a42013-08-19 17:45:09 -0700350#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700351
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700352// Here, we only have to provide a callback to iterate across all the
353// loaded libraries. gcc_eh does the rest.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800354int dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700355 int rv = 0;
356 for (soinfo* si = solist; si != nullptr; si = si->next) {
357 dl_phdr_info dl_info;
358 dl_info.dlpi_addr = si->link_map_head.l_addr;
359 dl_info.dlpi_name = si->link_map_head.l_name;
360 dl_info.dlpi_phdr = si->phdr;
361 dl_info.dlpi_phnum = si->phnum;
362 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
363 if (rv != 0) {
364 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800365 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700366 }
367 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800368}
Elliott Hughes46882792012-08-03 16:49:39 -0700369
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800370ElfW(Sym)* soinfo::find_symbol_by_name(SymbolName& symbol_name) {
371 return is_gnu_hash() ? gnu_lookup(symbol_name) : elf_lookup(symbol_name);
372}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800373
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800374static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
375 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
376 ELF_ST_BIND(s->st_info) == STB_WEAK) {
377 return s->st_shndx != SHN_UNDEF;
378 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
379 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
380 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->name);
381 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800382
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800383 return false;
384}
385
386ElfW(Sym)* soinfo::gnu_lookup(SymbolName& symbol_name) {
387 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800388 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800389
390 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800391 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
392 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800393
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700394 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
395 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
396
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800397 // test against bloom filter
398 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700399 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
400 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
401
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800402 return nullptr;
403 }
404
405 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700406 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800407
408 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700409 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
410 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
411
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800412 return nullptr;
413 }
414
415 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800416 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700417 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800418 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
419 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700420 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
421 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
422 static_cast<size_t>(s->st_size));
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800423 return s;
424 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700425 } while ((gnu_chain_[n++] & 1) == 0);
426
427 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
428 symbol_name.get_name(), name, reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800429
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800430 return nullptr;
431}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800432
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800433ElfW(Sym)* soinfo::elf_lookup(SymbolName& symbol_name) {
434 uint32_t hash = symbol_name.elf_hash();
435
436 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800437 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800438
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800439 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
440 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700441 if (strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
442 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800443 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
444 symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
445 static_cast<size_t>(s->st_size));
446 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800447 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800448 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800449
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700450 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800451 symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700452
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700453 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800454}
455
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700456soinfo::soinfo(const char* name, const struct stat* file_stat,
457 off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700458 memset(this, 0, sizeof(*this));
459
460 strlcpy(this->name, name, sizeof(this->name));
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800461 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800462 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700463
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700464 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800465 this->st_dev_ = file_stat->st_dev;
466 this->st_ino_ = file_stat->st_ino;
467 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700468 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700469
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800470 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700471}
472
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800473
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800474uint32_t SymbolName::elf_hash() {
475 if (!has_elf_hash_) {
476 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
477 uint32_t h = 0, g;
478
479 while (*name) {
480 h = (h << 4) + *name++;
481 g = h & 0xf0000000;
482 h ^= g;
483 h ^= g >> 24;
484 }
485
486 elf_hash_ = h;
487 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700488 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800489
490 return elf_hash_;
491}
492
493uint32_t SymbolName::gnu_hash() {
494 if (!has_gnu_hash_) {
495 uint32_t h = 5381;
496 const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
497 while (*name != 0) {
498 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
499 }
500
501 gnu_hash_ = h;
502 has_gnu_hash_ = true;
503 }
504
505 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800506}
507
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800508ElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700509 const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800510 SymbolName symbol_name(name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700511 ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700512
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700513 /* "This element's presence in a shared object library alters the dynamic linker's
514 * symbol resolution algorithm for references within the library. Instead of starting
515 * a symbol search with the executable file, the dynamic linker starts from the shared
516 * object itself. If the shared object fails to supply the referenced symbol, the
517 * dynamic linker then searches the executable file and other shared objects as usual."
518 *
519 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
520 *
521 * Note that this is unlikely since static linker avoids generating
522 * relocations for -Bsymbolic linked dynamic executables.
523 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700524 if (si_from->has_DT_SYMBOLIC) {
525 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->name, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800526 s = si_from->find_symbol_by_name(symbol_name);
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700527 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700528 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700529 }
530 }
531
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700532 // 1. Look for it in global_group
533 if (s == nullptr) {
534 global_group.visit([&](soinfo* global_si) {
535 DEBUG("%s: looking up %s in %s (from global group)", si_from->name, name, global_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800536 s = global_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700537 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700538 *si_found_in = global_si;
539 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700540 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700541
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700542 return true;
543 });
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700544 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700545
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700546 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700547 if (s == nullptr) {
548 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700549 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700550 // we already did this - skip
551 return true;
552 }
553
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700554 DEBUG("%s: looking up %s in %s (from local group)", si_from->name, name, local_si->name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800555 s = local_si->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700556 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700557 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700558 return false;
559 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700560
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700561 return true;
562 });
563 }
564
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700565 if (s != nullptr) {
566 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
567 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700568 si_from->name, name, reinterpret_cast<void*>(s->st_value),
569 (*si_found_in)->name, reinterpret_cast<void*>((*si_found_in)->base),
570 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700571 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700572
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700573 return s;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700574}
575
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800576class ProtectedDataGuard {
577 public:
578 ProtectedDataGuard() {
579 if (ref_count_++ == 0) {
580 protect_data(PROT_READ | PROT_WRITE);
581 }
582 }
583
584 ~ProtectedDataGuard() {
585 if (ref_count_ == 0) { // overflow
586 __libc_fatal("Too many nested calls to dlopen()");
587 }
588
589 if (--ref_count_ == 0) {
590 protect_data(PROT_READ);
591 }
592 }
593 private:
594 void protect_data(int protection) {
595 g_soinfo_allocator.protect_all(protection);
596 g_soinfo_links_allocator.protect_all(protection);
597 }
598
599 static size_t ref_count_;
600};
601
602size_t ProtectedDataGuard::ref_count_ = 0;
603
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700604// Each size has it's own allocator.
605template<size_t size>
606class SizeBasedAllocator {
607 public:
608 static void* alloc() {
609 return allocator_.alloc();
610 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700611
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700612 static void free(void* ptr) {
613 allocator_.free(ptr);
614 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700615
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700616 private:
617 static LinkerBlockAllocator allocator_;
618};
619
620template<size_t size>
621LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
622
623template<typename T>
624class TypeBasedAllocator {
625 public:
626 static T* alloc() {
627 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
628 }
629
630 static void free(T* ptr) {
631 SizeBasedAllocator<sizeof(T)>::free(ptr);
632 }
633};
634
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700635class LoadTask {
636 public:
637 struct deleter_t {
638 void operator()(LoadTask* t) {
639 TypeBasedAllocator<LoadTask>::free(t);
640 }
641 };
642
643 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
644
645 static deleter_t deleter;
646
647 static LoadTask* create(const char* name, soinfo* needed_by) {
648 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
649 return new (ptr) LoadTask(name, needed_by);
650 }
651
652 const char* get_name() const {
653 return name_;
654 }
655
656 soinfo* get_needed_by() const {
657 return needed_by_;
658 }
659 private:
660 LoadTask(const char* name, soinfo* needed_by)
661 : name_(name), needed_by_(needed_by) {}
662
663 const char* name_;
664 soinfo* needed_by_;
665
666 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
667};
668
Ningsheng Jiane93be992014-09-16 15:22:10 +0800669LoadTask::deleter_t LoadTask::deleter;
670
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700671template <typename T>
672using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
673
674typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700675typedef linked_list_t<const char> StringLinkedList;
676typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700677
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700678
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700679// This function walks down the tree of soinfo dependencies
680// in breadth-first order and
681// * calls action(soinfo* si) for each node, and
682// * terminates walk if action returns false.
683//
684// walk_dependencies_tree returns false if walk was terminated
685// by the action and true otherwise.
686template<typename F>
687static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700688 SoinfoLinkedList visit_list;
689 SoinfoLinkedList visited;
690
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700691 for (size_t i = 0; i < root_soinfos_size; ++i) {
692 visit_list.push_back(root_soinfos[i]);
693 }
694
695 soinfo* si;
696 while ((si = visit_list.pop_front()) != nullptr) {
697 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700698 continue;
699 }
700
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700701 if (!action(si)) {
702 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700703 }
704
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700705 visited.push_back(si);
706
707 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700708 visit_list.push_back(child);
709 });
710 }
711
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700712 return true;
713}
714
715
716// This is used by dlsym(3). It performs symbol lookup only within the
717// specified soinfo object and its dependencies in breadth first order.
718ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
719 ElfW(Sym)* result = nullptr;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800720 SymbolName symbol_name(name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700721
722
723 walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800724 result = current_soinfo->find_symbol_by_name(symbol_name);
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700725 if (result != nullptr) {
726 *found = current_soinfo;
727 return false;
728 }
729
730 return true;
731 });
732
733 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800734}
735
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800736/* This is used by dlsym(3) to performs a global symbol lookup. If the
737 start value is null (for RTLD_DEFAULT), the search starts at the
738 beginning of the global solist. Otherwise the search starts at the
739 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700740 */
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700741ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* caller, void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800742 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800743
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700744 soinfo* start = solist;
745
746 if (handle == RTLD_NEXT) {
747 if (caller == nullptr || caller->next == nullptr) {
748 return nullptr;
749 } else {
750 start = caller->next;
751 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800752 }
753
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700754 ElfW(Sym)* s = nullptr;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700755 for (soinfo* si = start; si != nullptr; si = si->next) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700756 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) {
757 continue;
758 }
759
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800760 s = si->find_symbol_by_name(symbol_name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700761 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800762 *found = si;
763 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600764 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800765 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600766
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700767 // If not found - look into local_group unless
768 // caller is part of the global group in which
769 // case we already did it.
770 if (s == nullptr && caller != nullptr &&
771 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
772 soinfo* local_group_root = caller->get_local_group_root();
773
774 if (handle == RTLD_DEFAULT) {
775 start = local_group_root;
776 }
777
778 for (soinfo* si = start; si != nullptr; si = si->next) {
779 if (si->get_local_group_root() != local_group_root) {
780 break;
781 }
782
783 s = si->find_symbol_by_name(symbol_name);
784 if (s != nullptr) {
785 *found = si;
786 break;
787 }
788 }
789 }
790
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700791 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700792 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
793 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800794 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800795
Elliott Hughescade4c32012-12-20 14:42:14 -0800796 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800797}
798
Kito Chengfa8c05d2013-03-12 14:58:06 +0800799soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800800 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700801 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800802 if (address >= si->base && address - si->base < si->size) {
803 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600804 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800805 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700806 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600807}
808
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800809ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
810 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
811}
812
813static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
814 return sym->st_shndx != SHN_UNDEF &&
815 soaddr >= sym->st_value &&
816 soaddr < sym->st_value + sym->st_size;
817}
818
819ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800820 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800821
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700822 for (size_t i = 0; i < gnu_nbucket_; ++i) {
823 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800824
825 if (n == 0) {
826 continue;
827 }
828
829 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800830 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800831 if (symbol_matches_soaddr(sym, soaddr)) {
832 return sym;
833 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700834 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800835 }
836
837 return nullptr;
838}
839
840ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -0800841 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600842
Kito Chengfa8c05d2013-03-12 14:58:06 +0800843 // Search the library's symbol table for any defined symbol which
844 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800845 for (size_t i = 0; i < nchain_; ++i) {
846 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800847 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800848 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600849 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800850 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600851
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700852 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600853}
854
Simon Baldwinaef71952015-01-16 13:22:54 +0000855static int open_library_in_zipfile(const char* const path,
856 off64_t* file_offset) {
857 TRACE("Trying zip file open from path '%s'", path);
858
859 // Treat an '!' character inside a path as the separator between the name
860 // of the zip file on disk and the subdirectory to search within it.
861 // For example, if path is "foo.zip!bar/bas/x.so", then we search for
862 // "bar/bas/x.so" within "foo.zip".
863 const char* separator = strchr(path, '!');
864 if (separator == nullptr) {
865 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -0700866 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000867
868 char buf[512];
869 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
870 PRINT("Warning: ignoring very long library path: %s", path);
871 return -1;
872 }
873
874 buf[separator - path] = '\0';
875
876 const char* zip_path = buf;
877 const char* file_path = &buf[separator - path + 1];
878 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
879 if (fd == -1) {
880 return -1;
881 }
882
883 ZipArchiveHandle handle;
884 if (OpenArchiveFd(fd, "", &handle, false) != 0) {
885 // invalid zip-file (?)
886 close(fd);
887 return -1;
888 }
889
890 auto archive_guard = make_scope_guard([&]() {
891 CloseArchive(handle);
892 });
893
894 ZipEntry entry;
895
896 if (FindEntry(handle, ZipEntryName(file_path), &entry) != 0) {
897 // Entry was not found.
898 close(fd);
899 return -1;
900 }
901
902 // Check if it is properly stored
903 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
904 close(fd);
905 return -1;
906 }
907
908 *file_offset = entry.offset;
909 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800910}
911
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700912static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
913 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
914 if (n < 0 || n >= static_cast<int>(buf_size)) {
915 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
916 return false;
917 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000918
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700919 return true;
920}
921
922static int open_library_on_default_path(const char* name, off64_t* file_offset) {
923 for (size_t i = 0; kDefaultLdPaths[i] != nullptr; ++i) {
924 char buf[512];
925 if(!format_path(buf, sizeof(buf), kDefaultLdPaths[i], name)) {
926 continue;
Simon Baldwinaef71952015-01-16 13:22:54 +0000927 }
928
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700929 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
930 if (fd != -1) {
931 *file_offset = 0;
932 return fd;
933 }
934 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000935
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700936 return -1;
937}
938
939static int open_library_on_ld_library_path(const char* name, off64_t* file_offset) {
940 for (const auto& path_str : g_ld_library_paths) {
941 char buf[512];
942 const char* const path = path_str.c_str();
943 if (!format_path(buf, sizeof(buf), path, name)) {
944 continue;
945 }
946
947 int fd = -1;
948 if (strchr(buf, '!') != nullptr) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000949 fd = open_library_in_zipfile(buf, file_offset);
950 }
951
952 if (fd == -1) {
953 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
954 if (fd != -1) {
955 *file_offset = 0;
956 }
957 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700958
959 if (fd != -1) {
960 return fd;
961 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000962 }
963
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700964 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +0000965}
966
967static int open_library(const char* name, off64_t* file_offset) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700968 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800969
Elliott Hughes124fae92012-10-31 14:20:03 -0700970 // 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 -0700971 if (strchr(name, '/') != nullptr) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000972 if (strchr(name, '!') != nullptr) {
973 int fd = open_library_in_zipfile(name, file_offset);
974 if (fd != -1) {
975 return fd;
976 }
977 }
978
Elliott Hughes6971fe42012-11-01 22:59:19 -0700979 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
980 if (fd != -1) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000981 *file_offset = 0;
Elliott Hughes6971fe42012-11-01 22:59:19 -0700982 }
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -0700983 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -0700984 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800985
Elliott Hughes124fae92012-10-31 14:20:03 -0700986 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700987 int fd = open_library_on_ld_library_path(name, file_offset);
Elliott Hughes124fae92012-10-31 14:20:03 -0700988 if (fd == -1) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700989 fd = open_library_on_default_path(name, file_offset);
Elliott Hughes124fae92012-10-31 14:20:03 -0700990 }
991 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800992}
993
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700994template<typename F>
995static void for_each_dt_needed(const soinfo* si, F action) {
996 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
997 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700998 action(si->get_string(d->d_un.d_val));
Dima Zavin2e855792009-05-20 18:28:09 -0700999 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001000 }
1001}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001002
Simon Baldwinaef71952015-01-16 13:22:54 +00001003static soinfo* load_library(LoadTaskList& load_tasks,
1004 const char* name, int rtld_flags,
1005 const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001006 int fd = -1;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001007 off64_t file_offset = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001008 ScopedFd file_guard(-1);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001009
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001010 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
1011 fd = extinfo->library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001012 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1013 file_offset = extinfo->library_fd_offset;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001014 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001015 } else {
1016 // Open the file.
Simon Baldwinaef71952015-01-16 13:22:54 +00001017 fd = open_library(name, &file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001018 if (fd == -1) {
1019 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001020 return nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001021 }
1022
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001023 file_guard.reset(fd);
1024 }
1025
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001026 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001027 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001028 return nullptr;
1029 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001030 if (file_offset < 0) {
1031 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
1032 return nullptr;
1033 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001034
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001035 struct stat file_stat;
1036 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001037 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001038 return nullptr;
1039 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001040 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001041 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1042 name, file_offset, file_stat.st_size);
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001043 return nullptr;
1044 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001045
1046 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001047 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1048 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
1049 for (soinfo* si = solist; si != nullptr; si = si->next) {
1050 if (si->get_st_dev() != 0 &&
1051 si->get_st_ino() != 0 &&
1052 si->get_st_dev() == file_stat.st_dev &&
1053 si->get_st_ino() == file_stat.st_ino &&
1054 si->get_file_offset() == file_offset) {
1055 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1056 "will return existing soinfo", name, si->name);
1057 return si;
1058 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001059 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001060 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001061
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001062 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001063 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001064 return nullptr;
1065 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001066
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001067 // Read the ELF header and load the segments.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001068 ElfReader elf_reader(name, fd, file_offset);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001069 if (!elf_reader.Load(extinfo)) {
1070 return nullptr;
1071 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001072
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001073 soinfo* si = soinfo_alloc(name, &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001074 if (si == nullptr) {
1075 return nullptr;
1076 }
1077 si->base = elf_reader.load_start();
1078 si->size = elf_reader.load_size();
1079 si->load_bias = elf_reader.load_bias();
1080 si->phnum = elf_reader.phdr_count();
1081 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001082
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001083 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001084 soinfo_free(si);
1085 return nullptr;
1086 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001087
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001088 for_each_dt_needed(si, [&] (const char* name) {
1089 load_tasks.push_back(LoadTask::create(name, si));
1090 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001091
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001092 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001093}
1094
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001095static soinfo *find_loaded_library_by_soname(const char* name) {
1096 // Ignore filename with path.
1097 if (strchr(name, '/') != nullptr) {
1098 return nullptr;
1099 }
1100
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001101 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001102 const char* soname = si->get_soname();
1103 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanov489e4982014-05-19 15:19:52 -07001104 return si;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001105 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -07001106 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001107 return nullptr;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001108}
1109
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001110static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name,
1111 int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001112 soinfo* si = find_loaded_library_by_soname(name);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001113
1114 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001115 // of this fact is done by load_library.
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001116 if (si == nullptr) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001117 TRACE("[ '%s' has not been found by soname. Trying harder...]", name);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001118 si = load_library(load_tasks, name, rtld_flags, extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001119 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001120
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001121 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001122}
1123
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001124static void soinfo_unload(soinfo* si);
1125
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001126// TODO: this is slightly unusual way to construct
1127// the global group for relocation. Not every RTLD_GLOBAL
1128// library is included in this group for backwards-compatibility
1129// reasons.
1130//
1131// This group consists of the main executable, LD_PRELOADs
1132// and libraries with the DF_1_GLOBAL flag set.
1133static soinfo::soinfo_list_t make_global_group() {
1134 soinfo::soinfo_list_t global_group;
1135 for (soinfo* si = somain; si != nullptr; si = si->next) {
1136 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1137 global_group.push_back(si);
1138 }
1139 }
1140
1141 return global_group;
1142}
1143
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001144static bool find_libraries(soinfo* start_with, const char* const library_names[],
1145 size_t library_names_count, soinfo* soinfos[], std::vector<soinfo*>* ld_preloads,
1146 size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001147 // Step 0: prepare.
1148 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001149 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001150 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001151 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001152 }
1153
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001154 // Construct global_group.
1155 soinfo::soinfo_list_t global_group = make_global_group();
1156
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001157 // If soinfos array is null allocate one on stack.
1158 // The array is needed in case of failure; for example
1159 // when library_names[] = {libone.so, libtwo.so} and libone.so
1160 // is loaded correctly but libtwo.so failed for some reason.
1161 // In this case libone.so should be unloaded on return.
1162 // See also implementation of failure_guard below.
1163
1164 if (soinfos == nullptr) {
1165 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1166 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1167 memset(soinfos, 0, soinfos_size);
1168 }
1169
1170 // list of libraries to link - see step 2.
1171 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001172
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001173 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001174 // Housekeeping
1175 load_tasks.for_each([] (LoadTask* t) {
1176 LoadTask::deleter(t);
1177 });
1178
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001179 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001180 soinfo_unload(soinfos[i]);
1181 }
1182 });
1183
1184 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001185 for (LoadTask::unique_ptr task(load_tasks.pop_front());
1186 task.get() != nullptr; task.reset(load_tasks.pop_front())) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001187 soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001188 if (si == nullptr) {
1189 return false;
1190 }
1191
1192 soinfo* needed_by = task->get_needed_by();
1193
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001194 if (needed_by != nullptr) {
1195 needed_by->add_child(si);
1196 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001197
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001198 if (si->is_linked()) {
1199 si->increment_ref_count();
1200 }
1201
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001202 // When ld_preloads is not null, the first
1203 // ld_preloads_count libs are in fact ld_preloads.
1204 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001205 // Add LD_PRELOADed libraries to the global group for future runs.
1206 // There is no need to explicitly add them to the global group
1207 // for this run because they are going to appear in the local
1208 // group in the correct order.
1209 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001210 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001211 }
1212
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001213 if (soinfos_count < library_names_count) {
1214 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001215 }
1216 }
1217
1218 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001219 soinfo::soinfo_list_t local_group;
1220 walk_dependencies_tree(
1221 start_with == nullptr ? soinfos : &start_with,
1222 start_with == nullptr ? soinfos_count : 1,
1223 [&] (soinfo* si) {
1224 local_group.push_back(si);
1225 return true;
1226 });
1227
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001228 // We need to increment ref_count in case
1229 // the root of the local group was not linked.
1230 bool was_local_group_root_linked = local_group.front()->is_linked();
1231
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001232 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001233 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001234 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001235 return false;
1236 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001237 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001238 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001239
1240 return true;
1241 });
1242
1243 if (linked) {
1244 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001245 }
1246
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001247 if (!was_local_group_root_linked) {
1248 local_group.front()->increment_ref_count();
1249 }
1250
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001251 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001252}
1253
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001254static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001255 soinfo* si;
1256
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001257 if (name == nullptr) {
1258 si = somain;
1259 } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001260 return nullptr;
1261 }
1262
Elliott Hughesd23736e2012-11-01 15:16:56 -07001263 return si;
1264}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001265
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001266static void soinfo_unload(soinfo* root) {
1267 // Note that the library can be loaded but not linked;
1268 // in which case there is no root but we still need
1269 // to walk the tree and unload soinfos involved.
1270 //
1271 // This happens on unsuccessful dlopen, when one of
1272 // the DT_NEEDED libraries could not be linked/found.
1273 if (root->is_linked()) {
1274 root = root->get_local_group_root();
1275 }
1276
1277 if (!root->can_unload()) {
1278 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->name);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001279 return;
1280 }
1281
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001282 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001283
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001284 if (ref_count == 0) {
1285 soinfo::soinfo_list_t local_unload_list;
1286 soinfo::soinfo_list_t external_unload_list;
1287 soinfo::soinfo_list_t depth_first_list;
1288 depth_first_list.push_back(root);
1289 soinfo* si = nullptr;
1290
1291 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001292 if (local_unload_list.contains(si)) {
1293 continue;
1294 }
1295
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001296 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001297
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001298 if (si->has_min_version(0)) {
1299 soinfo* child = nullptr;
1300 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001301 TRACE("%s@%p needs to unload %s@%p", si->name, si, child->name, child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001302 if (local_unload_list.contains(child)) {
1303 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001304 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001305 external_unload_list.push_back(child);
1306 } else {
1307 depth_first_list.push_front(child);
1308 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001309 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001310 } else {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001311#ifdef __LP64__
1312 __libc_fatal("soinfo for \"%s\"@%p has no version", si->name, si);
1313#else
1314 PRINT("warning: soinfo for \"%s\"@%p has no version", si->name, si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001315 for_each_dt_needed(si, [&] (const char* library_name) {
1316 TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
1317 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
1318 if (needed != nullptr) {
1319 // Not found: for example if symlink was deleted between dlopen and dlclose
1320 // Since we cannot really handle errors at this point - print and continue.
1321 PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
1322 return;
1323 } else if (local_unload_list.contains(needed)) {
1324 // already visited
1325 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001326 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001327 // external group
1328 external_unload_list.push_back(needed);
1329 } else {
1330 // local group
1331 depth_first_list.push_front(needed);
1332 }
1333 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001334#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001335 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001336 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001337
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001338 local_unload_list.for_each([](soinfo* si) {
1339 si->call_destructors();
1340 });
1341
1342 while ((si = local_unload_list.pop_front()) != nullptr) {
1343 notify_gdb_of_unload(si);
1344 soinfo_free(si);
1345 }
1346
1347 while ((si = external_unload_list.pop_front()) != nullptr) {
1348 soinfo_unload(si);
1349 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001350 } else {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001351 TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->name, ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001352 }
1353}
1354
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001355void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001356 // Use basic string manipulation calls to avoid snprintf.
1357 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1358 // When debug malloc is enabled, this call returns 0. This in turn causes
1359 // snprintf to do nothing, which causes libraries to fail to load.
1360 // See b/17302493 for further details.
1361 // Once the above bug is fixed, this code can be modified to use
1362 // snprintf again.
1363 size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
1364 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001365 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
1366 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001367 }
1368 char* end = stpcpy(buffer, kDefaultLdPaths[0]);
1369 *end = ':';
1370 strcpy(end + 1, kDefaultLdPaths[1]);
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001371}
1372
Elliott Hughescade4c32012-12-20 14:42:14 -08001373void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001374 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001375}
1376
Elliott Hughes1a586292014-06-03 16:23:08 -07001377soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001378 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001379 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001380 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001381 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001382 if (extinfo != nullptr) {
1383 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1384 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1385 return nullptr;
1386 }
1387 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001388 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001389 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
1390 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001391 return nullptr;
1392 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001393 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001394
1395 ProtectedDataGuard guard;
Dmitriy Ivanov9fb216f2014-11-01 02:30:38 +00001396 soinfo* si = find_library(name, flags, extinfo);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001397 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001398 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001399 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001400 return si;
1401}
1402
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001403void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001404 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001405 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001406}
1407
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001408static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1409 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1410 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1411 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001412 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
1413 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001414
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001415 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001416}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001417
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001418#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001419#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001420static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
1421 return rela->r_addend;
1422}
1423#else
1424static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001425 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
1426 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001427 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
1428 }
1429 return 0;
1430}
1431#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001432
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001433template<typename ElfRelIteratorT>
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001434bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group,
1435 const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001436 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
1437 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001438 if (rel == nullptr) {
1439 return false;
1440 }
1441
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001442 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
1443 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
1444
1445 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001446 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001447 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001448 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001449
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001450 DEBUG("Processing '%s' relocation at index %zd", this->name, idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001451 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001452 continue;
1453 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001454
1455 ElfW(Sym)* s = nullptr;
1456 soinfo* lsi = nullptr;
1457
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001458 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001459 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001460 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001461 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001462 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001463 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001464 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001465 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001466 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001467 }
1468
1469 /* IHI0044C AAELF 4.5.1.1:
1470
1471 Libraries are not searched to resolve weak references.
1472 It is not an error for a weak reference to remain unsatisfied.
1473
1474 During linking, the value of an undefined weak reference is:
1475 - Zero if the relocation type is absolute
1476 - The address of the place if the relocation is pc-relative
1477 - The address of nominal base address if the relocation
1478 type is base-relative.
1479 */
1480
1481 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001482 case R_GENERIC_JUMP_SLOT:
1483 case R_GENERIC_GLOB_DAT:
1484 case R_GENERIC_RELATIVE:
1485 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001486#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001487 case R_AARCH64_ABS64:
1488 case R_AARCH64_ABS32:
1489 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001490#elif defined(__x86_64__)
1491 case R_X86_64_32:
1492 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001493#elif defined(__arm__)
1494 case R_ARM_ABS32:
1495#elif defined(__i386__)
1496 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001497#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001498 /*
1499 * The sym_addr was initialized to be zero above, or the relocation
1500 * code below does not care about value of sym_addr.
1501 * No need to do anything.
1502 */
1503 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08001504#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00001505 case R_X86_64_PC32:
1506 sym_addr = reloc;
1507 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001508#elif defined(__i386__)
1509 case R_386_PC32:
1510 sym_addr = reloc;
1511 break;
1512#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001513 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001514 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001515 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001516 }
1517 } else {
1518 // We got a definition.
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001519 sym_addr = lsi->resolve_symbol_address(s);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001520 }
1521 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001522 }
1523
1524 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001525 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001526 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001527 MARK(rel->r_offset);
1528 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
1529 reinterpret_cast<void*>(reloc),
1530 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1531
1532 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001533 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001534 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001535 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001536 MARK(rel->r_offset);
1537 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
1538 reinterpret_cast<void*>(reloc),
1539 reinterpret_cast<void*>(sym_addr + addend), sym_name);
1540 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001541 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001542 case R_GENERIC_RELATIVE:
1543 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001544 MARK(rel->r_offset);
1545 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
1546 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001547 reinterpret_cast<void*>(load_bias + addend));
1548 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001549 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001550 case R_GENERIC_IRELATIVE:
1551 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001552 MARK(rel->r_offset);
1553 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
1554 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001555 reinterpret_cast<void*>(load_bias + addend));
1556 *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001557 break;
1558
1559#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001560 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001561 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001562 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001563 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001564 reloc, (sym_addr + addend), sym_name);
1565 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001566 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001567 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001568 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001569 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001570 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001571 reloc, (sym_addr + addend), sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001572 {
1573 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
1574 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
1575 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
1576 if ((min_value <= (reloc_value + (sym_addr + addend))) &&
1577 ((reloc_value + (sym_addr + addend)) <= max_value)) {
1578 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
1579 } else {
1580 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1581 (reloc_value + (sym_addr + addend)), min_value, max_value);
1582 return false;
1583 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001584 }
1585 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001586 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001587 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001588 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001589 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001590 reloc, (sym_addr + addend), sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001591 {
1592 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
1593 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
1594 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
1595 if ((min_value <= (reloc_value + (sym_addr + addend))) &&
1596 ((reloc_value + (sym_addr + addend)) <= max_value)) {
1597 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
1598 } else {
1599 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1600 reloc_value + (sym_addr + addend), min_value, max_value);
1601 return false;
1602 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001603 }
1604 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001605 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001606 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001607 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001608 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001609 reloc, (sym_addr + addend), rel->r_offset, sym_name);
1610 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend) - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001611 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001612 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001613 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001614 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001615 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001616 reloc, (sym_addr + addend), rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001617 {
1618 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
1619 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
1620 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
1621 if ((min_value <= (reloc_value + ((sym_addr + addend) - rel->r_offset))) &&
1622 ((reloc_value + ((sym_addr + addend) - rel->r_offset)) <= max_value)) {
1623 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
1624 } else {
1625 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1626 reloc_value + ((sym_addr + addend) - rel->r_offset), min_value, max_value);
1627 return false;
1628 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001629 }
1630 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001631 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001632 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001633 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001634 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001635 reloc, (sym_addr + addend), rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001636 {
1637 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
1638 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
1639 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
1640 if ((min_value <= (reloc_value + ((sym_addr + addend) - rel->r_offset))) &&
1641 ((reloc_value + ((sym_addr + addend) - rel->r_offset)) <= max_value)) {
1642 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
1643 } else {
1644 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
1645 reloc_value + ((sym_addr + addend) - rel->r_offset), min_value, max_value);
1646 return false;
1647 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001648 }
1649 break;
1650
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001651 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07001652 /*
1653 * ET_EXEC is not supported so this should not happen.
1654 *
1655 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1656 *
1657 * Section 4.7.1.10 "Dynamic relocations"
1658 * R_AARCH64_COPY may only appear in executable objects where e_type is
1659 * set to ET_EXEC.
1660 */
Dmitriy Ivanov29bbc9d2014-09-02 11:47:23 -07001661 DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001662 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001663 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001664 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001665 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001666 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001667 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08001668 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001669 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01001670 break;
1671#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001672 case R_X86_64_32:
1673 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001674 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001675 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1676 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001677 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001678 break;
1679 case R_X86_64_64:
1680 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001681 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001682 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1683 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001684 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001685 break;
1686 case R_X86_64_PC32:
1687 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001688 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001689 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1690 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1691 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001692 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001693 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001694#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001695 case R_ARM_ABS32:
1696 count_relocation(kRelocAbsolute);
1697 MARK(rel->r_offset);
1698 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1699 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1700 break;
1701 case R_ARM_REL32:
1702 count_relocation(kRelocRelative);
1703 MARK(rel->r_offset);
1704 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
1705 reloc, sym_addr, rel->r_offset, sym_name);
1706 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
1707 break;
1708 case R_ARM_COPY:
1709 /*
1710 * ET_EXEC is not supported so this should not happen.
1711 *
1712 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1713 *
1714 * Section 4.7.1.10 "Dynamic relocations"
1715 * R_ARM_COPY may only appear in executable objects where e_type is
1716 * set to ET_EXEC.
1717 */
1718 DL_ERR("%s R_ARM_COPY relocations are not supported", name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001719 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001720#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001721 case R_386_32:
1722 count_relocation(kRelocRelative);
1723 MARK(rel->r_offset);
1724 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1725 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
1726 break;
1727 case R_386_PC32:
1728 count_relocation(kRelocRelative);
1729 MARK(rel->r_offset);
1730 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1731 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1732 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
1733 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001734#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001735 default:
1736 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001737 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001738 }
1739 }
1740 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001741}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001742#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07001743
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001744void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
1745 size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001746 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001747 return;
1748 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001749
Elliott Hughesc6200592013-09-30 18:43:46 -07001750 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001751
1752 int begin = reverse ? (count - 1) : 0;
1753 int end = reverse ? -1 : count;
1754 int step = reverse ? -1 : 1;
1755
1756 for (int i = begin; i != end; i += step) {
1757 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001758 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001759 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02001760
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001761 TRACE("[ Done calling %s for '%s' ]", array_name, name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001762}
1763
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001764void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001765 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001766 return;
1767 }
1768
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001769 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001770 function();
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001771 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04001772}
1773
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001774void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001775 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1776 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001777 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001778}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001779
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001780void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07001781 if (constructors_called) {
1782 return;
1783 }
Jesse Hallf5d16932012-01-30 15:39:57 -08001784
Elliott Hughesd23736e2012-11-01 15:16:56 -07001785 // We set constructors_called before actually calling the constructors, otherwise it doesn't
1786 // protect against recursive constructor calls. One simple example of constructor recursion
1787 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1788 // 1. The program depends on libc, so libc's constructor is called here.
1789 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1790 // 3. dlopen() calls the constructors on the newly created
1791 // soinfo for libc_malloc_debug_leak.so.
1792 // 4. The debug .so depends on libc, so CallConstructors is
1793 // called again with the libc soinfo. If it doesn't trigger the early-
1794 // out above, the libc constructor will be called again (recursively!).
1795 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001796
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001797 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001798 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07001799 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001800 name, preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07001801 }
1802
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001803 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001804 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001805 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001806
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001807 TRACE("\"%s\": calling constructors", name);
1808
1809 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001810 call_function("DT_INIT", init_func_);
1811 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04001812}
David 'Digit' Turner82156792009-05-18 14:37:41 +02001813
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001814void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001815 if (!constructors_called) {
1816 return;
1817 }
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001818 TRACE("\"%s\": calling destructors", name);
1819
1820 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001821 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07001822
1823 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001824 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001825
1826 // This is needed on second call to dlopen
1827 // after library has been unloaded with RTLD_NODELETE
1828 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001829}
1830
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001831void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001832 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001833 child->parents_.push_back(this);
1834 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001835 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001836}
1837
1838void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001839 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001840 return;
1841 }
1842
1843 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001844 children_.for_each([&] (soinfo* child) {
1845 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001846 return parent == this;
1847 });
1848 });
1849
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001850 parents_.for_each([&] (soinfo* parent) {
1851 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001852 return child == this;
1853 });
1854 });
1855
1856 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001857 parents_.clear();
1858 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001859}
1860
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001861dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001862 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001863 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001864 }
1865
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001866 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001867};
1868
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001869ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001870 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001871 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001872 }
1873
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001874 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001875}
1876
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001877off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001878 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001879 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001880 }
1881
1882 return 0;
1883}
1884
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001885uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001886 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001887 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001888 }
1889
1890 return 0;
1891}
1892
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001893uint32_t soinfo::get_dt_flags_1() const {
1894 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001895 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001896 }
1897
1898 return 0;
1899}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001900
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001901void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
1902 if (has_min_version(1)) {
1903 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001904 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001905 }
1906
1907 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001908 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001909 }
1910
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001911 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001912 }
1913}
1914
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001915const char* soinfo::get_soname() {
1916 if (has_min_version(2)) {
1917 return soname_;
1918 } else {
1919 return name;
1920 }
1921}
1922
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001923// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001924// 'this->flags' does not have FLAG_NEW_SOINFO set.
1925static soinfo::soinfo_list_t g_empty_list;
1926
1927soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001928 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001929 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001930 }
1931
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07001932 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001933}
1934
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001935soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001936 if (has_min_version(0)) {
1937 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001938 }
1939
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001940 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001941}
1942
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001943ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
1944 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
1945 return call_ifunc_resolver(s->st_value + load_bias);
1946 }
1947
1948 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
1949}
1950
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001951const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001952 if (has_min_version(1) && (index >= strtab_size_)) {
1953 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001954 }
1955
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001956 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07001957}
1958
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001959bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001960 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001961}
1962
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001963bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001964 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001965}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001966
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001967bool soinfo::is_linked() const {
1968 return (flags_ & FLAG_LINKED) != 0;
1969}
1970
1971bool soinfo::is_main_executable() const {
1972 return (flags_ & FLAG_EXE) != 0;
1973}
1974
1975void soinfo::set_linked() {
1976 flags_ |= FLAG_LINKED;
1977}
1978
1979void soinfo::set_linker_flag() {
1980 flags_ |= FLAG_LINKER;
1981}
1982
1983void soinfo::set_main_executable() {
1984 flags_ |= FLAG_EXE;
1985}
1986
1987void soinfo::increment_ref_count() {
1988 local_group_root_->ref_count_++;
1989}
1990
1991size_t soinfo::decrement_ref_count() {
1992 return --local_group_root_->ref_count_;
1993}
1994
1995soinfo* soinfo::get_local_group_root() const {
1996 return local_group_root_;
1997}
1998
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001999/* Force any of the closed stdin, stdout and stderr to be associated with
2000 /dev/null. */
Elliott Hughes5419b942012-10-16 15:54:46 -07002001static int nullify_closed_stdio() {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002002 int dev_null, i, status;
2003 int return_value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002004
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002005 dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
2006 if (dev_null < 0) {
2007 DL_ERR("cannot open /dev/null: %s", strerror(errno));
2008 return -1;
2009 }
2010 TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002011
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002012 /* If any of the stdio file descriptors is valid and not associated
2013 with /dev/null, dup /dev/null to it. */
2014 for (i = 0; i < 3; i++) {
2015 /* If it is /dev/null already, we are done. */
2016 if (i == dev_null) {
2017 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002018 }
2019
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002020 TRACE("[ Nullifying stdio file descriptor %d]", i);
2021 status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
2022
2023 /* If file is opened, we are good. */
2024 if (status != -1) {
2025 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002026 }
2027
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002028 /* The only error we allow is that the file descriptor does not
2029 exist, in which case we dup /dev/null to it. */
2030 if (errno != EBADF) {
2031 DL_ERR("fcntl failed: %s", strerror(errno));
2032 return_value = -1;
2033 continue;
2034 }
2035
2036 /* Try dupping /dev/null to this stdio file descriptor and
2037 repeat if there is a signal. Note that any errors in closing
2038 the stdio descriptor are lost. */
2039 status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
2040 if (status < 0) {
2041 DL_ERR("dup2 failed: %s", strerror(errno));
2042 return_value = -1;
2043 continue;
2044 }
2045 }
2046
2047 /* If /dev/null is not one of the stdio file descriptors, close it. */
2048 if (dev_null > 2) {
2049 TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
2050 status = TEMP_FAILURE_RETRY(close(dev_null));
2051 if (status == -1) {
2052 DL_ERR("close failed: %s", strerror(errno));
2053 return_value = -1;
2054 }
2055 }
2056
2057 return return_value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002058}
2059
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002060bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002061 /* Extract dynamic section */
2062 ElfW(Word) dynamic_flags = 0;
2063 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002064
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002065 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002066 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002067 if (!relocating_linker) {
2068 INFO("[ linking %s ]", name);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002069 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002070 }
2071
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002072 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002073 if (!relocating_linker) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002074 DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002075 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002076 return false;
2077 } else {
2078 if (!relocating_linker) {
2079 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002080 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002081 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002082
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002083#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002084 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2085 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002086#endif
2087
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002088 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002089 // Note that: "Except for the DT_NULL element at the end of the array,
2090 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2091 //
2092 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002093 uint32_t needed_count = 0;
2094 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2095 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2096 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2097 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002098 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002099 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002100 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002101
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002102 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002103 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2104 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2105 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2106 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002107 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002108
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002109 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002110 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002111 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002112 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2113 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002114
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002115 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002116 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002117 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002118 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2119 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002120
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002121 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002122 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
2123 gnu_maskwords_, name);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002124 return false;
2125 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002126 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002127
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002128 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002129 break;
2130
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002131 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002132 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002133 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002134
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002135 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002136 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002137 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002138
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002139 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002140 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002141 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002142
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002143 case DT_SYMENT:
2144 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002145 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"", static_cast<size_t>(d->d_un.d_val), name);
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002146 return false;
2147 }
2148 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002149
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002150 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002151#if defined(USE_RELA)
2152 if (d->d_un.d_val != DT_RELA) {
2153 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002154 return false;
2155 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002156#else
2157 if (d->d_un.d_val != DT_REL) {
2158 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
2159 return false;
2160 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002161#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002162 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002163
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002164 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002165#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002166 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002167#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002168 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002169#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002170 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002171
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002172 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002173#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002174 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002175#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002176 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002177#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002178 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002179
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002180 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002181#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002182 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002183 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002184#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002185 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2186 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002187
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002188 case DT_DEBUG:
2189 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2190 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002191// FIXME: not working currently for N64
2192// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002193// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002194// read-only, but the DYNAMIC header claims it is writable.
2195#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002196 if ((dynamic_flags & PF_W) != 0) {
2197 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2198 }
Chris Dearman99186652014-02-06 20:36:51 -08002199#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002200 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002201#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002202 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002203 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002204 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002205
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002206 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002207 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002208 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002209
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002210 case DT_ANDROID_RELA:
2211 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2212 break;
2213
2214 case DT_ANDROID_RELASZ:
2215 android_relocs_size_ = d->d_un.d_val;
2216 break;
2217
2218 case DT_ANDROID_REL:
2219 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", name);
2220 return false;
2221
2222 case DT_ANDROID_RELSZ:
2223 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", name);
2224 return false;
2225
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002226 case DT_RELAENT:
2227 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002228 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002229 return false;
2230 }
2231 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002232
2233 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002234 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002235 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002236
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002237 case DT_REL:
2238 DL_ERR("unsupported DT_REL in \"%s\"", name);
2239 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002240
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002241 case DT_RELSZ:
2242 DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
2243 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002244
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002245#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002246 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002247 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002248 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002249
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002250 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002251 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002252 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002253
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002254 case DT_RELENT:
2255 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002256 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002257 return false;
2258 }
2259 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002260
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002261 case DT_ANDROID_REL:
2262 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2263 break;
2264
2265 case DT_ANDROID_RELSZ:
2266 android_relocs_size_ = d->d_un.d_val;
2267 break;
2268
2269 case DT_ANDROID_RELA:
2270 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", name);
2271 return false;
2272
2273 case DT_ANDROID_RELASZ:
2274 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", name);
2275 return false;
2276
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002277 // "Indicates that all RELATIVE relocations have been concatenated together,
2278 // and specifies the RELATIVE relocation count."
2279 //
2280 // TODO: Spec also mentions that this can be used to optimize relocation process;
2281 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002282 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002283 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002284
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002285 case DT_RELA:
2286 DL_ERR("unsupported DT_RELA in \"%s\"", name);
2287 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002288
2289 case DT_RELASZ:
2290 DL_ERR("unsupported DT_RELASZ in \"%s\"", name);
2291 return false;
2292
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002293#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002294 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002295 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2296 DEBUG("%s constructors (DT_INIT) found at %p", name, init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002297 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002298
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002299 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002300 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
2301 DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002302 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002303
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002304 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002305 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2306 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002307 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002308
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002309 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002310 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002311 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002312
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002313 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002314 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2315 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002316 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002317
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002318 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002319 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002320 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002321
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002322 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002323 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
2324 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002325 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002326
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002327 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002328 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002329 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002330
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002331 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002332#if defined(__LP64__)
2333 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002334 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002335#else
2336 has_text_relocations = true;
2337 break;
2338#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002339
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002340 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002341 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002342 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002343
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002344 case DT_NEEDED:
2345 ++needed_count;
2346 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002347
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002348 case DT_FLAGS:
2349 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002350#if defined(__LP64__)
2351 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002352 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002353#else
2354 has_text_relocations = true;
2355#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002356 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002357 if (d->d_un.d_val & DF_SYMBOLIC) {
2358 has_DT_SYMBOLIC = true;
2359 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002360 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002361
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002362 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002363 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002364
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002365 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002366 DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val));
2367 }
2368 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002369#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002370 case DT_MIPS_RLD_MAP:
2371 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2372 {
2373 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2374 *dp = &_r_debug;
2375 }
2376 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002377 case DT_MIPS_RLD_MAP2:
2378 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2379 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002380 r_debug** dp = reinterpret_cast<r_debug**>(
2381 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08002382 *dp = &_r_debug;
2383 }
2384 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002385
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002386 case DT_MIPS_RLD_VERSION:
2387 case DT_MIPS_FLAGS:
2388 case DT_MIPS_BASE_ADDRESS:
2389 case DT_MIPS_UNREFEXTNO:
2390 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002391
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002392 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002393 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002394 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002395
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002396 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002397 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002398 break;
2399
2400 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002401 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002402 break;
2403#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002404 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2405 case DT_BIND_NOW:
2406 break;
2407
2408 // Ignore: bionic does not support symbol versioning...
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002409 case DT_VERSYM:
2410 case DT_VERDEF:
2411 case DT_VERDEFNUM:
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002412 case DT_VERNEED:
2413 case DT_VERNEEDNUM:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002414 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002415
2416 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002417 if (!relocating_linker) {
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002418 DL_WARN("%s: unused DT entry: type %p arg %p", name,
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002419 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2420 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002421 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002422 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002423 }
2424
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002425 // second pass - parse entries relying on strtab
2426 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2427 if (d->d_tag == DT_SONAME) {
2428 soname_ = get_string(d->d_un.d_val);
2429 break;
2430 }
2431 }
2432
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002433 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002434 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002435
2436 // Sanity checks.
2437 if (relocating_linker && needed_count != 0) {
2438 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2439 return false;
2440 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002441 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002442 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" (new hash type from the future?)", name);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002443 return false;
2444 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002445 if (strtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002446 DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2447 return false;
2448 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002449 if (symtab_ == 0) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002450 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2451 return false;
2452 }
2453 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002454}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002455
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002456bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
2457 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002458
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002459 local_group_root_ = local_group.front();
2460 if (local_group_root_ == nullptr) {
2461 local_group_root_ = this;
2462 }
2463
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002464#if !defined(__LP64__)
2465 if (has_text_relocations) {
2466 // Make segments writable to allow text relocations to work properly. We will later call
2467 // phdr_table_protect_segments() after all of them are applied and all constructors are run.
2468 DL_WARN("%s has text relocations. This is wasting memory and prevents "
2469 "security hardening. Please fix.", name);
2470 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2471 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2472 name, strerror(errno));
2473 return false;
2474 }
2475 }
2476#endif
2477
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002478 if (android_relocs_ != nullptr) {
2479 // check signature
2480 if (android_relocs_size_ > 3 &&
2481 android_relocs_[0] == 'A' &&
2482 android_relocs_[1] == 'P' &&
2483 (android_relocs_[2] == 'U' || android_relocs_[2] == 'S') &&
2484 android_relocs_[3] == '2') {
2485 DEBUG("[ android relocating %s ]", name);
2486
2487 bool relocated = false;
2488 const uint8_t* packed_relocs = android_relocs_ + 4;
2489 const size_t packed_relocs_size = android_relocs_size_ - 4;
2490
2491 if (android_relocs_[2] == 'U') {
2492 relocated = relocate(
2493 packed_reloc_iterator<leb128_decoder>(
2494 leb128_decoder(packed_relocs, packed_relocs_size)),
2495 global_group, local_group);
2496 } else { // android_relocs_[2] == 'S'
2497 relocated = relocate(
2498 packed_reloc_iterator<sleb128_decoder>(
2499 sleb128_decoder(packed_relocs, packed_relocs_size)),
2500 global_group, local_group);
2501 }
2502
2503 if (!relocated) {
2504 return false;
2505 }
2506 } else {
2507 DL_ERR("bad android relocation header.");
2508 return false;
2509 }
2510 }
2511
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002512#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002513 if (rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002514 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002515 if (!relocate(plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002516 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002517 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002518 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002519 if (plt_rela_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002520 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002521 if (!relocate(plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002522 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002523 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002524 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002525#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002526 if (rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002527 DEBUG("[ relocating %s ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002528 if (!relocate(plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002529 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002530 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002531 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002532 if (plt_rel_ != nullptr) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002533 DEBUG("[ relocating %s plt ]", name);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002534 if (!relocate(plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002535 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002536 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002537 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002538#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002539
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002540#if defined(__mips__)
Dmitriy Ivanov88940912014-11-12 18:20:39 -08002541 if (!mips_relocate_got(global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002542 return false;
2543 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002544#endif
2545
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002546 DEBUG("[ finished linking %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002547
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002548#if !defined(__LP64__)
2549 if (has_text_relocations) {
2550 // All relocations are done, we can protect our segments back to read-only.
2551 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2552 DL_ERR("can't protect segments for \"%s\": %s",
2553 name, strerror(errno));
2554 return false;
2555 }
2556 }
2557#endif
2558
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002559 /* We can also turn on GNU RELRO protection */
2560 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
2561 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
2562 name, strerror(errno));
2563 return false;
2564 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08002565
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002566 /* Handle serializing/sharing the RELRO segment */
2567 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
2568 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2569 extinfo->relro_fd) < 0) {
2570 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
2571 name, strerror(errno));
2572 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002573 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002574 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
2575 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2576 extinfo->relro_fd) < 0) {
2577 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
2578 name, strerror(errno));
2579 return false;
2580 }
2581 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00002582
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002583 notify_gdb_of_load(this);
2584 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002585}
2586
Nick Kralevich468319c2011-11-11 15:53:17 -08002587/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002588 * This function add vdso to internal dso list.
2589 * It helps to stack unwinding through signal handlers.
2590 * Also, it makes bionic more like glibc.
2591 */
Kito Cheng812fd422014-03-25 22:53:56 +08002592static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002593#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08002594 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002595 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002596 return;
2597 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002598
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002599 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002600
Elliott Hughes0266ae52014-02-10 17:46:57 -08002601 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
2602 si->phnum = ehdr_vdso->e_phnum;
2603 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
2604 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002605 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04002606
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002607 si->prelink_image();
2608 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04002609#endif
2610}
2611
2612/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002613 * This is linker soinfo for GDB. See details below.
2614 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002615#if defined(__LP64__)
2616#define LINKER_PATH "/system/bin/linker64"
2617#else
2618#define LINKER_PATH "/system/bin/linker"
2619#endif
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002620static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002621
2622/* gdb expects the linker to be in the debug shared object list.
2623 * Without this, gdb has trouble locating the linker's ".text"
2624 * and ".plt" sections. Gdb could also potentially use this to
2625 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2626 * Don't use soinfo_alloc(), because the linker shouldn't
2627 * be on the soinfo list.
2628 */
2629static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002630 linker_soinfo_for_gdb.base = linker_base;
2631
2632 /*
2633 * Set the dynamic field in the link map otherwise gdb will complain with
2634 * the following:
2635 * warning: .dynamic section for "/system/bin/linker" is not at the
2636 * expected address (wrong library or version mismatch?)
2637 */
2638 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2639 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2640 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Ningsheng Jiane93be992014-09-16 15:22:10 +08002641 &linker_soinfo_for_gdb.dynamic, nullptr);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002642 insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2643}
2644
2645/*
Nick Kralevich468319c2011-11-11 15:53:17 -08002646 * This code is called after the linker has linked itself and
2647 * fixed it's own GOT. It is safe to make references to externs
2648 * and other non-local data at this point.
2649 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002650static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002651#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002652 struct timeval t0, t1;
2653 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04002654#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002655
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002656 // Initialize environment functions, and get to the ELF aux vectors table.
2657 linker_env_init(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002658
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002659 // If this is a setuid/setgid program, close the security hole described in
2660 // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
2661 if (get_AT_SECURE()) {
2662 nullify_closed_stdio();
2663 }
2664
2665 debuggerd_init();
2666
2667 // Get a few environment variables.
2668 const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2669 if (LD_DEBUG != nullptr) {
2670 g_ld_debug_verbosity = atoi(LD_DEBUG);
2671 }
2672
2673 // Normally, these are cleaned by linker_env_init, but the test
2674 // doesn't cost us anything.
2675 const char* ldpath_env = nullptr;
2676 const char* ldpreload_env = nullptr;
2677 if (!get_AT_SECURE()) {
2678 ldpath_env = linker_env_get("LD_LIBRARY_PATH");
2679 ldpreload_env = linker_env_get("LD_PRELOAD");
2680 }
2681
Dmitriy Ivanovbfa15e42015-01-07 15:05:49 -08002682#if !defined(__LP64__)
2683 if (personality(PER_LINUX32) == -1) {
2684 __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno));
2685 }
2686#endif
2687
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002688 INFO("[ android linker & debugger ]");
2689
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002690 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002691 if (si == nullptr) {
2692 exit(EXIT_FAILURE);
2693 }
2694
2695 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002696 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002697 link_map* map = &(si->link_map_head);
2698
2699 map->l_addr = 0;
2700 map->l_name = args.argv[0];
2701 map->l_prev = nullptr;
2702 map->l_next = nullptr;
2703
2704 _r_debug.r_map = map;
2705 r_debug_tail = map;
2706
2707 init_linker_info_for_gdb(linker_base);
2708
2709 // Extract information passed from the kernel.
2710 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
2711 si->phnum = args.getauxval(AT_PHNUM);
2712 si->entry = args.getauxval(AT_ENTRY);
2713
2714 /* Compute the value of si->base. We can't rely on the fact that
2715 * the first entry is the PHDR because this will not be true
2716 * for certain executables (e.g. some in the NDK unit test suite)
2717 */
2718 si->base = 0;
2719 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2720 si->load_bias = 0;
2721 for (size_t i = 0; i < si->phnum; ++i) {
2722 if (si->phdr[i].p_type == PT_PHDR) {
2723 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
2724 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
2725 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002726 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002727 }
2728 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07002729
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002730 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
2731 if (elf_hdr->e_type != ET_DYN) {
2732 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
2733 exit(EXIT_FAILURE);
2734 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002735
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002736 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
2737 parse_LD_LIBRARY_PATH(ldpath_env);
2738 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01002739
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002740 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002741
Dmitriy Ivanov67181252015-01-07 15:48:25 -08002742 if (!si->prelink_image()) {
2743 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2744 exit(EXIT_FAILURE);
2745 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002746
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002747 // add somain to global group
2748 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2749
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002750 // Load ld_preloads and dependencies.
2751 StringLinkedList needed_library_name_list;
2752 size_t needed_libraries_count = 0;
2753 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07002754
2755 for (const auto& ld_preload_name : g_ld_preload_names) {
2756 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002757 ++needed_libraries_count;
2758 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002759
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002760 for_each_dt_needed(si, [&](const char* name) {
2761 needed_library_name_list.push_back(name);
2762 ++needed_libraries_count;
2763 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002764
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002765 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002766
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002767 memset(needed_library_names, 0, sizeof(needed_library_names));
2768 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002769
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07002770 if (needed_libraries_count > 0 &&
2771 !find_libraries(si, needed_library_names, needed_libraries_count, nullptr,
2772 &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002773 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2774 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002775 } else if (needed_libraries_count == 0) {
2776 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
2777 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
2778 exit(EXIT_FAILURE);
2779 }
2780 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002781 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002782
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002783 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07002784
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002785 {
2786 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06002787
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002788 si->call_pre_init_constructors();
2789
2790 /* After the prelink_image, the si->load_bias is initialized.
2791 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2792 * We need to update this value for so exe here. So Unwind_Backtrace
2793 * for some arch like x86 could work correctly within so exe.
2794 */
2795 map->l_addr = si->load_bias;
2796 si->call_constructors();
2797 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002798
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002799#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002800 gettimeofday(&t1, nullptr);
2801 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
2802 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2803 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002804#endif
2805#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002806 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2807 linker_stats.count[kRelocAbsolute],
2808 linker_stats.count[kRelocRelative],
2809 linker_stats.count[kRelocCopy],
2810 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002811#endif
2812#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002813 {
2814 unsigned n;
2815 unsigned i;
2816 unsigned count = 0;
2817 for (n = 0; n < 4096; n++) {
2818 if (bitmask[n]) {
2819 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002820#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002821 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002822#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002823 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002824#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002825 if (x & 1) {
2826 count++;
2827 }
2828 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002829 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002830 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002831 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002832 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
2833 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002834#endif
2835
2836#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002837 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002838#endif
2839
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002840 TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
2841 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002842}
Nick Kralevich468319c2011-11-11 15:53:17 -08002843
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002844/* Compute the load-bias of an existing executable. This shall only
2845 * be used to compute the load bias of an executable or shared library
2846 * that was loaded by the kernel itself.
2847 *
2848 * Input:
2849 * elf -> address of ELF header, assumed to be at the start of the file.
2850 * Return:
2851 * load bias, i.e. add the value of any p_vaddr in the file to get
2852 * the corresponding address in memory.
2853 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002854static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
2855 ElfW(Addr) offset = elf->e_phoff;
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002856 const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002857 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002858
Elliott Hughes0266ae52014-02-10 17:46:57 -08002859 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08002860 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08002861 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002862 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08002863 }
2864 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02002865}
2866
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002867extern "C" void _start();
2868
Nick Kralevich468319c2011-11-11 15:53:17 -08002869/*
2870 * This is the entry point for the linker, called from begin.S. This
2871 * method is responsible for fixing the linker's own relocations, and
2872 * then calling __linker_init_post_relocation().
2873 *
2874 * Because this method is called before the linker has fixed it's own
2875 * relocations, any attempt to reference an extern variable, extern
2876 * function, or other GOT reference will generate a segfault.
2877 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08002878extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002879 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08002880
Elliott Hughes0266ae52014-02-10 17:46:57 -08002881 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002882 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002883 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08002884 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08002885
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002886 soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08002887
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002888 // If the linker is not acting as PT_INTERP entry_point is equal to
2889 // _start. Which means that the linker is running as an executable and
2890 // already linked by PT_INTERP.
2891 //
2892 // This happens when user tries to run 'adb shell /system/bin/linker'
2893 // see also https://code.google.com/p/android/issues/detail?id=63174
2894 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2895 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2896 }
2897
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002898 linker_so.base = linker_addr;
2899 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2900 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002901 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002902 linker_so.phdr = phdr;
2903 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002904 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07002905
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002906 // This might not be obvious... The reasons why we pass g_empty_list
2907 // in place of local_group here are (1) we do not really need it, because
2908 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
2909 // itself without having to look into local_group and (2) allocators
2910 // are not yet initialized, and therefore we cannot use linked_list.push_*
2911 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002912 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002913 // It would be nice to print an error message, but if the linker
2914 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08002915 // call write() (because it involves a GOT reference). We may as
2916 // well try though...
2917 const char* msg = "CANNOT LINK EXECUTABLE: ";
2918 write(2, msg, strlen(msg));
2919 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2920 write(2, "\n", 1);
2921 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002922 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002923
Dmitriy Ivanov14241402014-08-26 14:16:52 -07002924 __libc_init_tls(args);
2925
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07002926 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002927 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07002928
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002929 // Initialize static variables. Note that in order to
2930 // get correct libdl_info we need to call constructors
2931 // before get_libdl_info().
2932 solist = get_libdl_info();
2933 sonext = get_libdl_info();
2934
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002935 // We have successfully fixed our own relocations. It's safe to run
2936 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07002937 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08002938 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002939
Elliott Hughes611f9562015-01-23 10:43:58 -08002940 INFO("[ jumping to _start ]");
2941
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08002942 // Return the address that the calling assembly stub should jump to.
2943 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08002944}