blob: 5f4e35c66c41d75c41ff37806034947b038c0153 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002 * Copyright (C) 2008 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
Dmitriy Ivanov19133522015-06-02 17:36:54 -070029#include <android/api-level.h>
Elliott Hughes46882792012-08-03 16:49:39 -070030#include <dlfcn.h>
31#include <errno.h>
32#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080033#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070034#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070038#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080039#include <sys/param.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>
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070044#include <unordered_map>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070045#include <vector>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070046
Elliott Hughes46882792012-08-03 16:49:39 -070047// Private C library headers.
Elliott Hugheseb847bc2013-10-09 15:50:50 -070048#include "private/bionic_tls.h"
49#include "private/KernelArgumentBlock.h"
50#include "private/ScopedPthreadMutexLocker.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"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070057#include "linker_sleb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020058#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080059#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080060#include "linker_reloc_iterators.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070061#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080062
63#include "base/strings.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000064#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065
Elliott Hughes1801db32015-06-08 18:04:00 -070066extern void __libc_init_AT_SECURE(KernelArgumentBlock&);
67
68// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080069#undef ELF_ST_TYPE
70#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
71
Elliott Hughes0266ae52014-02-10 17:46:57 -080072static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070074static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
75static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020076
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070077static soinfo* solist;
78static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -070079static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080
Elliott Hughes1728b232014-05-14 10:02:03 -070081static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070082#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070083 "/vendor/lib64",
84 "/system/lib64",
85#else
Elliott Hughes124fae92012-10-31 14:20:03 -070086 "/vendor/lib",
87 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -070088#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -070089 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -070090};
David Bartleybc3a5c22009-06-02 18:27:28 -070091
Evgenii Stepanovd640b222015-07-10 17:54:01 -070092static const char* const kAsanDefaultLdPaths[] = {
93#if defined(__LP64__)
94 "/data/vendor/lib64",
95 "/vendor/lib64",
96 "/data/lib64",
97 "/system/lib64",
98#else
99 "/data/vendor/lib",
100 "/vendor/lib",
101 "/data/lib",
102 "/system/lib",
103#endif
104 nullptr
105};
106
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700107static const ElfW(Versym) kVersymNotNeeded = 0;
108static const ElfW(Versym) kVersymGlobal = 1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -0700109static const char* const kZipFileSeparator = "!/";
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700110
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700111static const char* const* g_default_ld_paths;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700112static std::vector<std::string> g_ld_library_paths;
113static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800114
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700115static std::vector<soinfo*> g_ld_preloads;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600116
Elliott Hughes1728b232014-05-14 10:02:03 -0700117__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700119__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700120
Evgenii Stepanov68650822015-06-10 13:38:39 -0700121static std::string dirname(const char *path) {
122 const char* last_slash = strrchr(path, '/');
123 if (last_slash == path) return "/";
124 else if (last_slash == nullptr) return ".";
125 else
126 return std::string(path, last_slash - path);
127}
128
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700130struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700131 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700132};
133
134static linker_stats_t linker_stats;
135
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800136void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700137 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700138}
139#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800140void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700141}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142#endif
143
144#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800145uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146#endif
147
Dima Zavin2e855792009-05-20 18:28:09 -0700148static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700149
Elliott Hughes650be4e2013-03-05 18:47:58 -0800150char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700151 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700152}
153
Elliott Hughes650be4e2013-03-05 18:47:58 -0800154size_t linker_get_error_buffer_size() {
155 return sizeof(__linker_dl_err_buf);
156}
157
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700158// This function is an empty stub where GDB locates a breakpoint to get notified
159// about linker activity.
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700160extern "C"
161void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162
Elliott Hughes1728b232014-05-14 10:02:03 -0700163static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700164static r_debug _r_debug =
165 {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
166
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800167static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800169static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700170 // Copy the necessary fields into the debug structure.
171 link_map* map = &(info->link_map_head);
172 map->l_addr = info->load_bias;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700173 // link_map l_name field is not const.
174 map->l_name = const_cast<char*>(info->get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700175 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700177 // Stick the new library at the end of the list.
178 // gdb tends to care more about libc than it does
179 // about leaf libraries, and ordering it this way
180 // reduces the back-and-forth over the wire.
181 if (r_debug_tail) {
182 r_debug_tail->l_next = map;
183 map->l_prev = r_debug_tail;
184 map->l_next = 0;
185 } else {
186 _r_debug.r_map = map;
187 map->l_prev = 0;
188 map->l_next = 0;
189 }
190 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191}
192
Elliott Hughesbedfe382012-08-14 14:07:59 -0700193static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700194 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700195
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700196 if (r_debug_tail == map) {
197 r_debug_tail = map->l_prev;
198 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700199
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700200 if (map->l_prev) {
201 map->l_prev->l_next = map->l_next;
202 }
203 if (map->l_next) {
204 map->l_next->l_prev = map->l_prev;
205 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700206}
207
Elliott Hughesbedfe382012-08-14 14:07:59 -0700208static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800209 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700210 // GDB already knows about the main executable
211 return;
212 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800213
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700214 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800215
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700216 _r_debug.r_state = r_debug::RT_ADD;
217 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700219 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700221 _r_debug.r_state = r_debug::RT_CONSISTENT;
222 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700223}
224
Elliott Hughesbedfe382012-08-14 14:07:59 -0700225static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800226 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700227 // GDB already knows about the main executable
228 return;
229 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700230
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700231 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700232
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700233 _r_debug.r_state = r_debug::RT_DELETE;
234 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700235
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700236 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700237
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700238 _r_debug.r_state = r_debug::RT_CONSISTENT;
239 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240}
241
Elliott Hughes18a206c2012-10-29 17:37:13 -0700242void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800243 _r_debug.r_state = r_debug::RT_ADD;
244 rtld_db_dlactivity();
245 _r_debug.r_state = r_debug::RT_CONSISTENT;
246 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800247}
248
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700249LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
250 return g_soinfo_links_allocator.alloc();
251}
252
253void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
254 g_soinfo_links_allocator.free(entry);
255}
256
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700257static soinfo* soinfo_alloc(const char* name, struct stat* file_stat,
258 off64_t file_offset, uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700259 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200260 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700261 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200262 }
263
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700264 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700265
Magnus Malmbornba98d922012-09-12 13:00:55 +0200266 sonext->next = si;
267 sonext = si;
268
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700269 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200270 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271}
272
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800273static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700274 if (si == nullptr) {
275 return;
276 }
277
278 if (si->base != 0 && si->size != 0) {
279 munmap(reinterpret_cast<void*>(si->base), si->size);
280 }
281
282 soinfo *prev = nullptr, *trav;
283
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700284 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700285
286 for (trav = solist; trav != nullptr; trav = trav->next) {
287 if (trav == si) {
288 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700289 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700290 prev = trav;
291 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800292
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700293 if (trav == nullptr) {
294 // si was not in solist
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700295 DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700296 return;
297 }
Elliott Hughes46882792012-08-03 16:49:39 -0700298
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700299 // clear links to/from si
300 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700301
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700302 // prev will never be null, because the first entry in solist is
303 // always the static libdl_info.
304 prev->next = si->next;
305 if (si == sonext) {
306 sonext = prev;
307 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700309 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700310 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800311}
312
Elliott Hughescade4c32012-12-20 14:42:14 -0800313static void parse_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700314 std::vector<std::string>* paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700315 paths->clear();
tony.ys_liub4474402015-07-29 18:00:22 +0800316 if (path != nullptr) {
317 *paths = android::base::Split(path, delimiters);
Elliott Hughescade4c32012-12-20 14:42:14 -0800318 }
319}
320
321static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700322 parse_path(path, ":", &g_ld_library_paths);
Elliott Hughescade4c32012-12-20 14:42:14 -0800323}
324
Evgenii Stepanov68650822015-06-10 13:38:39 -0700325void soinfo::set_dt_runpath(const char* path) {
326 if (!has_min_version(2)) return;
327 parse_path(path, ":", &dt_runpath_);
328
329 std::string origin = dirname(get_realpath());
330 // FIXME: add $LIB and $PLATFORM.
331 std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}};
332 for (std::string& s : dt_runpath_) {
333 size_t pos = 0;
334 while (pos < s.size()) {
335 pos = s.find("$", pos);
336 if (pos == std::string::npos) break;
337 for (const auto& subst : substs) {
338 const std::string& token = subst.first;
339 const std::string& replacement = subst.second;
340 if (s.substr(pos + 1, token.size()) == token) {
341 s.replace(pos, token.size() + 1, replacement);
342 // -1 to compensate for the ++pos below.
343 pos += replacement.size() - 1;
344 break;
345 } else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") {
346 s.replace(pos, token.size() + 3, replacement);
347 pos += replacement.size() - 1;
348 break;
349 }
350 }
351 // Skip $ in case it did not match any of the known substitutions.
352 ++pos;
353 }
354 }
355}
356
Elliott Hughescade4c32012-12-20 14:42:14 -0800357static void parse_LD_PRELOAD(const char* path) {
358 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700359 parse_path(path, " :", &g_ld_preload_names);
Elliott Hughescade4c32012-12-20 14:42:14 -0800360}
361
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700362static bool realpath_fd(int fd, std::string* realpath) {
363 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700364 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700365 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -0700366 PRINT("readlink('%s') failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700367 return false;
368 }
369
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700370 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700371 return true;
372}
373
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700374#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700375
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700376// For a given PC, find the .so that it belongs to.
377// Returns the base address of the .ARM.exidx section
378// for that .so, and the number of 8-byte entries
379// in that section (via *pcount).
380//
381// Intended to be called by libc's __gnu_Unwind_Find_exidx().
382//
383// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800384_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800385 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800386
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700387 for (soinfo* si = solist; si != 0; si = si->next) {
388 if ((addr >= si->base) && (addr < (si->base + si->size))) {
389 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800390 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800391 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700392 }
393 *pcount = 0;
394 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800395}
Elliott Hughes46882792012-08-03 16:49:39 -0700396
Christopher Ferris24053a42013-08-19 17:45:09 -0700397#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700398
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700399// Here, we only have to provide a callback to iterate across all the
400// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700401int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700402 int rv = 0;
403 for (soinfo* si = solist; si != nullptr; si = si->next) {
404 dl_phdr_info dl_info;
405 dl_info.dlpi_addr = si->link_map_head.l_addr;
406 dl_info.dlpi_name = si->link_map_head.l_name;
407 dl_info.dlpi_phdr = si->phdr;
408 dl_info.dlpi_phnum = si->phnum;
409 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
410 if (rv != 0) {
411 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800412 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700413 }
414 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800415}
Elliott Hughes46882792012-08-03 16:49:39 -0700416
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700417const ElfW(Versym)* soinfo::get_versym(size_t n) const {
418 if (has_min_version(2) && versym_ != nullptr) {
419 return versym_ + n;
420 }
421
422 return nullptr;
423}
424
425ElfW(Addr) soinfo::get_verneed_ptr() const {
426 if (has_min_version(2)) {
427 return verneed_ptr_;
428 }
429
430 return 0;
431}
432
433size_t soinfo::get_verneed_cnt() const {
434 if (has_min_version(2)) {
435 return verneed_cnt_;
436 }
437
438 return 0;
439}
440
441ElfW(Addr) soinfo::get_verdef_ptr() const {
442 if (has_min_version(2)) {
443 return verdef_ptr_;
444 }
445
446 return 0;
447}
448
449size_t soinfo::get_verdef_cnt() const {
450 if (has_min_version(2)) {
451 return verdef_cnt_;
452 }
453
454 return 0;
455}
456
457template<typename F>
458static bool for_each_verdef(const soinfo* si, F functor) {
459 if (!si->has_min_version(2)) {
460 return true;
461 }
462
463 uintptr_t verdef_ptr = si->get_verdef_ptr();
464 if (verdef_ptr == 0) {
465 return true;
466 }
467
468 size_t offset = 0;
469
470 size_t verdef_cnt = si->get_verdef_cnt();
471 for (size_t i = 0; i<verdef_cnt; ++i) {
472 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
473 size_t verdaux_offset = offset + verdef->vd_aux;
474 offset += verdef->vd_next;
475
476 if (verdef->vd_version != 1) {
Dmitriy Ivanov3d7bea12015-04-20 17:40:39 -0700477 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700478 i, verdef->vd_version, si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700479 return false;
480 }
481
482 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
483 // "this is the version of the file itself. It must not be used for
484 // matching a symbol. It can be used to match references."
485 //
486 // http://www.akkadia.org/drepper/symbol-versioning
487 continue;
488 }
489
490 if (verdef->vd_cnt == 0) {
491 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
492 return false;
493 }
494
495 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
496
497 if (functor(i, verdef, verdaux) == true) {
498 break;
499 }
500 }
501
502 return true;
503}
504
505bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const {
506 if (vi == nullptr) {
507 *versym = kVersymNotNeeded;
508 return true;
509 }
510
511 *versym = kVersymGlobal;
512
513 return for_each_verdef(this,
514 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
515 if (verdef->vd_hash == vi->elf_hash &&
516 strcmp(vi->name, get_string(verdaux->vda_name)) == 0) {
517 *versym = verdef->vd_ndx;
518 return true;
519 }
520
521 return false;
522 }
523 );
524}
525
526bool soinfo::find_symbol_by_name(SymbolName& symbol_name,
527 const version_info* vi,
528 const ElfW(Sym)** symbol) const {
529 uint32_t symbol_index;
530 bool success =
531 is_gnu_hash() ?
532 gnu_lookup(symbol_name, vi, &symbol_index) :
533 elf_lookup(symbol_name, vi, &symbol_index);
534
535 if (success) {
536 *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index;
537 }
538
539 return success;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800540}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800541
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800542static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
543 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
544 ELF_ST_BIND(s->st_info) == STB_WEAK) {
545 return s->st_shndx != SHN_UNDEF;
546 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
547 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700548 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800549 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800550
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800551 return false;
552}
553
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700554static const ElfW(Versym) kVersymHiddenBit = 0x8000;
555
556static inline bool is_versym_hidden(const ElfW(Versym)* versym) {
557 // the symbol is hidden if bit 15 of versym is set.
558 return versym != nullptr && (*versym & kVersymHiddenBit) != 0;
559}
560
561static inline bool check_symbol_version(const ElfW(Versym) verneed,
562 const ElfW(Versym)* verdef) {
563 return verneed == kVersymNotNeeded ||
564 verdef == nullptr ||
565 verneed == (*verdef & ~kVersymHiddenBit);
566}
567
568bool soinfo::gnu_lookup(SymbolName& symbol_name,
569 const version_info* vi,
570 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800571 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800572 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800573
574 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800575 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
576 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800577
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700578 *symbol_index = 0;
579
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700580 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700581 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700582
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800583 // test against bloom filter
584 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700585 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700586 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700587
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700588 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800589 }
590
591 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700592 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800593
594 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700595 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700596 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700597
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700598 return true;
599 }
600
601 // lookup versym for the version definition in this library
602 // note the difference between "version is not requested" (vi == nullptr)
603 // and "version not found". In the first case verneed is kVersymNotNeeded
604 // which implies that the default version can be accepted; the second case results in
605 // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols
606 // for this library and consider only *global* ones.
607 ElfW(Versym) verneed = 0;
608 if (!find_verdef_version_index(vi, &verneed)) {
609 return false;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800610 }
611
612 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800613 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700614 const ElfW(Versym)* verdef = get_versym(n);
615 // skip hidden versions when verneed == kVersymNotNeeded (0)
616 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
617 continue;
618 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700619 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700620 check_symbol_version(verneed, verdef) &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800621 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
622 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700623 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700624 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700625 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700626 *symbol_index = n;
627 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800628 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700629 } while ((gnu_chain_[n++] & 1) == 0);
630
631 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700632 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800633
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700634 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800635}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800636
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700637bool soinfo::elf_lookup(SymbolName& symbol_name,
638 const version_info* vi,
639 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800640 uint32_t hash = symbol_name.elf_hash();
641
642 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700643 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700644 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800645
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700646 ElfW(Versym) verneed = 0;
647 if (!find_verdef_version_index(vi, &verneed)) {
648 return false;
649 }
650
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800651 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
652 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700653 const ElfW(Versym)* verdef = get_versym(n);
654
655 // skip hidden versions when verneed == 0
656 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
657 continue;
658 }
659
660 if (check_symbol_version(verneed, verdef) &&
661 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700662 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800663 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700664 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700665 reinterpret_cast<void*>(s->st_value),
666 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700667 *symbol_index = n;
668 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800669 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800670 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800671
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700672 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700673 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700674 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700675
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700676 *symbol_index = 0;
677 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800678}
679
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700680soinfo::soinfo(const char* realpath, const struct stat* file_stat,
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700681 off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700682 memset(this, 0, sizeof(*this));
683
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700684 if (realpath != nullptr) {
685 realpath_ = realpath;
686 }
687
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800688 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800689 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700690
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700691 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800692 this->st_dev_ = file_stat->st_dev;
693 this->st_ino_ = file_stat->st_ino;
694 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700695 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700696
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800697 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700698}
699
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800700
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800701uint32_t SymbolName::elf_hash() {
702 if (!has_elf_hash_) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700703 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800704 uint32_t h = 0, g;
705
706 while (*name) {
707 h = (h << 4) + *name++;
708 g = h & 0xf0000000;
709 h ^= g;
710 h ^= g >> 24;
711 }
712
713 elf_hash_ = h;
714 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700715 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800716
717 return elf_hash_;
718}
719
720uint32_t SymbolName::gnu_hash() {
721 if (!has_gnu_hash_) {
722 uint32_t h = 5381;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700723 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800724 while (*name != 0) {
725 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
726 }
727
728 gnu_hash_ = h;
729 has_gnu_hash_ = true;
730 }
731
732 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800733}
734
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700735bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
736 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
737 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800738 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700739 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700740
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700741 /* "This element's presence in a shared object library alters the dynamic linker's
742 * symbol resolution algorithm for references within the library. Instead of starting
743 * a symbol search with the executable file, the dynamic linker starts from the shared
744 * object itself. If the shared object fails to supply the referenced symbol, the
745 * dynamic linker then searches the executable file and other shared objects as usual."
746 *
747 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
748 *
749 * Note that this is unlikely since static linker avoids generating
750 * relocations for -Bsymbolic linked dynamic executables.
751 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700752 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700753 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700754 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
755 return false;
756 }
757
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700758 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700759 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700760 }
761 }
762
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700763 // 1. Look for it in global_group
764 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700765 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700766 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700767 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700768 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700769 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
770 error = true;
771 return false;
772 }
773
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700774 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700775 *si_found_in = global_si;
776 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700777 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700778
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700779 return true;
780 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700781
782 if (error) {
783 return false;
784 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700785 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700786
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700787 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700788 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700789 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700790 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700791 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700792 // we already did this - skip
793 return true;
794 }
795
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700796 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700797 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700798 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
799 error = true;
800 return false;
801 }
802
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700803 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700804 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700805 return false;
806 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700807
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700808 return true;
809 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700810
811 if (error) {
812 return false;
813 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700814 }
815
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700816 if (s != nullptr) {
817 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
818 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700819 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
820 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700821 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700822 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700823
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700824 *symbol = s;
825 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700826}
827
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800828class ProtectedDataGuard {
829 public:
830 ProtectedDataGuard() {
831 if (ref_count_++ == 0) {
832 protect_data(PROT_READ | PROT_WRITE);
833 }
834 }
835
836 ~ProtectedDataGuard() {
837 if (ref_count_ == 0) { // overflow
838 __libc_fatal("Too many nested calls to dlopen()");
839 }
840
841 if (--ref_count_ == 0) {
842 protect_data(PROT_READ);
843 }
844 }
845 private:
846 void protect_data(int protection) {
847 g_soinfo_allocator.protect_all(protection);
848 g_soinfo_links_allocator.protect_all(protection);
849 }
850
851 static size_t ref_count_;
852};
853
854size_t ProtectedDataGuard::ref_count_ = 0;
855
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700856// Each size has it's own allocator.
857template<size_t size>
858class SizeBasedAllocator {
859 public:
860 static void* alloc() {
861 return allocator_.alloc();
862 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700863
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700864 static void free(void* ptr) {
865 allocator_.free(ptr);
866 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700867
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700868 private:
869 static LinkerBlockAllocator allocator_;
870};
871
872template<size_t size>
873LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
874
875template<typename T>
876class TypeBasedAllocator {
877 public:
878 static T* alloc() {
879 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
880 }
881
882 static void free(T* ptr) {
883 SizeBasedAllocator<sizeof(T)>::free(ptr);
884 }
885};
886
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700887class LoadTask {
888 public:
889 struct deleter_t {
890 void operator()(LoadTask* t) {
891 TypeBasedAllocator<LoadTask>::free(t);
892 }
893 };
894
895 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
896
897 static deleter_t deleter;
898
899 static LoadTask* create(const char* name, soinfo* needed_by) {
900 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
901 return new (ptr) LoadTask(name, needed_by);
902 }
903
904 const char* get_name() const {
905 return name_;
906 }
907
908 soinfo* get_needed_by() const {
909 return needed_by_;
910 }
911 private:
912 LoadTask(const char* name, soinfo* needed_by)
913 : name_(name), needed_by_(needed_by) {}
914
915 const char* name_;
916 soinfo* needed_by_;
917
918 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
919};
920
Ningsheng Jiane93be992014-09-16 15:22:10 +0800921LoadTask::deleter_t LoadTask::deleter;
922
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700923template <typename T>
924using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
925
926typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700927typedef linked_list_t<const char> StringLinkedList;
928typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700929
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700930
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700931// This function walks down the tree of soinfo dependencies
932// in breadth-first order and
933// * calls action(soinfo* si) for each node, and
934// * terminates walk if action returns false.
935//
936// walk_dependencies_tree returns false if walk was terminated
937// by the action and true otherwise.
938template<typename F>
939static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700940 SoinfoLinkedList visit_list;
941 SoinfoLinkedList visited;
942
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700943 for (size_t i = 0; i < root_soinfos_size; ++i) {
944 visit_list.push_back(root_soinfos[i]);
945 }
946
947 soinfo* si;
948 while ((si = visit_list.pop_front()) != nullptr) {
949 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700950 continue;
951 }
952
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700953 if (!action(si)) {
954 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700955 }
956
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700957 visited.push_back(si);
958
959 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700960 visit_list.push_back(child);
961 });
962 }
963
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700964 return true;
965}
966
967
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700968static const ElfW(Sym)* dlsym_handle_lookup(soinfo* root, soinfo* skip_until,
969 soinfo** found, SymbolName& symbol_name) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700970 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700971 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700972
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700973 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
974 if (skip_lookup) {
975 skip_lookup = current_soinfo != skip_until;
976 return true;
977 }
978
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700979 if (!current_soinfo->find_symbol_by_name(symbol_name, nullptr, &result)) {
980 result = nullptr;
981 return false;
982 }
983
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700984 if (result != nullptr) {
985 *found = current_soinfo;
986 return false;
987 }
988
989 return true;
990 });
991
992 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800993}
994
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700995// This is used by dlsym(3). It performs symbol lookup only within the
996// specified soinfo object and its dependencies in breadth first order.
997const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700998 // According to man dlopen(3) and posix docs in the case when si is handle
999 // of the main executable we need to search not only in the executable and its
1000 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
1001 //
1002 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
1003 // libraries and they are loaded in breath-first (correct) order we can just execute
1004 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
1005 if (si == somain) {
1006 return dlsym_linear_lookup(name, found, nullptr, RTLD_DEFAULT);
1007 }
1008
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001009 SymbolName symbol_name(name);
1010 return dlsym_handle_lookup(si, nullptr, found, symbol_name);
1011}
1012
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001013/* This is used by dlsym(3) to performs a global symbol lookup. If the
1014 start value is null (for RTLD_DEFAULT), the search starts at the
1015 beginning of the global solist. Otherwise the search starts at the
1016 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001017 */
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001018const ElfW(Sym)* dlsym_linear_lookup(const char* name,
1019 soinfo** found,
1020 soinfo* caller,
1021 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001022 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001023
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001024 soinfo* start = solist;
1025
1026 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -07001027 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001028 return nullptr;
1029 } else {
1030 start = caller->next;
1031 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001032 }
1033
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001034 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001035 for (soinfo* si = start; si != nullptr; si = si->next) {
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001036 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
1037 // if the library is opened by application with target api level <= 22
1038 // See http://b/21565766
1039 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() > 22) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001040 continue;
1041 }
1042
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001043 if (!si->find_symbol_by_name(symbol_name, nullptr, &s)) {
1044 return nullptr;
1045 }
1046
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001047 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -08001048 *found = si;
1049 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -06001050 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001051 }
Matt Fischer1698d9e2009-12-31 12:17:56 -06001052
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001053 // If not found - use dlsym_handle_lookup for caller's
1054 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001055 // case we already did it.
1056 if (s == nullptr && caller != nullptr &&
1057 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001058 return dlsym_handle_lookup(caller->get_local_group_root(),
1059 (handle == RTLD_NEXT) ? caller : nullptr, found, symbol_name);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001060 }
1061
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001062 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001063 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
1064 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -08001065 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001066
Elliott Hughescade4c32012-12-20 14:42:14 -08001067 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001068}
1069
Kito Chengfa8c05d2013-03-12 14:58:06 +08001070soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001071 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001072 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001073 if (address >= si->base && address - si->base < si->size) {
1074 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001075 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001076 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001077 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001078}
1079
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001080ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
1081 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
1082}
1083
1084static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
1085 return sym->st_shndx != SHN_UNDEF &&
1086 soaddr >= sym->st_value &&
1087 soaddr < sym->st_value + sym->st_size;
1088}
1089
1090ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001091 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001092
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001093 for (size_t i = 0; i < gnu_nbucket_; ++i) {
1094 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001095
1096 if (n == 0) {
1097 continue;
1098 }
1099
1100 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001101 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001102 if (symbol_matches_soaddr(sym, soaddr)) {
1103 return sym;
1104 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001105 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001106 }
1107
1108 return nullptr;
1109}
1110
1111ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001112 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001113
Kito Chengfa8c05d2013-03-12 14:58:06 +08001114 // Search the library's symbol table for any defined symbol which
1115 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001116 for (size_t i = 0; i < nchain_; ++i) {
1117 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001118 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001119 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001120 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001121 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001122
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001123 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001124}
1125
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001126class ZipArchiveCache {
1127 public:
1128 ZipArchiveCache() {}
1129 ~ZipArchiveCache();
1130
1131 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
1132 private:
1133 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
1134
1135 std::unordered_map<std::string, ZipArchiveHandle> cache_;
1136};
1137
1138bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
1139 std::string key(zip_path);
1140
1141 auto it = cache_.find(key);
1142 if (it != cache_.end()) {
1143 *handle = it->second;
1144 return true;
1145 }
1146
1147 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1148 if (fd == -1) {
1149 return false;
1150 }
1151
1152 if (OpenArchiveFd(fd, "", handle) != 0) {
1153 // invalid zip-file (?)
1154 close(fd);
1155 return false;
1156 }
1157
1158 cache_[key] = *handle;
1159 return true;
1160}
1161
1162ZipArchiveCache::~ZipArchiveCache() {
1163 for (auto it : cache_) {
1164 CloseArchive(it.second);
1165 }
1166}
1167
1168static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001169 const char* const input_path,
1170 off64_t* file_offset, std::string* realpath) {
1171 std::string normalized_path;
1172 if (!normalize_path(input_path, &normalized_path)) {
1173 return -1;
1174 }
1175
1176 const char* const path = normalized_path.c_str();
1177 TRACE("Trying zip file open from path '%s' -> normalized '%s'", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +00001178
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001179 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001180 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001181 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001182 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001183 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001184 if (separator == nullptr) {
1185 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001186 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001187
1188 char buf[512];
1189 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1190 PRINT("Warning: ignoring very long library path: %s", path);
1191 return -1;
1192 }
1193
1194 buf[separator - path] = '\0';
1195
1196 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001197 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001198 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1199 if (fd == -1) {
1200 return -1;
1201 }
1202
1203 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001204 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001205 // invalid zip-file (?)
1206 close(fd);
1207 return -1;
1208 }
1209
Simon Baldwinaef71952015-01-16 13:22:54 +00001210 ZipEntry entry;
1211
Yusuke Sato56f40fb2015-06-25 14:56:07 -07001212 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001213 // Entry was not found.
1214 close(fd);
1215 return -1;
1216 }
1217
1218 // Check if it is properly stored
1219 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1220 close(fd);
1221 return -1;
1222 }
1223
1224 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001225
1226 if (realpath_fd(fd, realpath)) {
1227 *realpath += separator;
1228 } else {
1229 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1230 normalized_path.c_str());
1231 *realpath = normalized_path;
1232 }
1233
Simon Baldwinaef71952015-01-16 13:22:54 +00001234 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001235}
1236
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001237static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1238 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1239 if (n < 0 || n >= static_cast<int>(buf_size)) {
1240 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1241 return false;
1242 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001243
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001244 return true;
1245}
1246
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001247static int open_library_on_default_path(const char* name, off64_t* file_offset, std::string* realpath) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001248 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001249 char buf[512];
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001250 if (!format_path(buf, sizeof(buf), g_default_ld_paths[i], name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001251 continue;
Simon Baldwinaef71952015-01-16 13:22:54 +00001252 }
1253
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001254 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1255 if (fd != -1) {
1256 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001257 if (!realpath_fd(fd, realpath)) {
1258 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1259 *realpath = buf;
1260 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001261 return fd;
1262 }
1263 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001264
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001265 return -1;
1266}
1267
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001268static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1269 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001270 const std::vector<std::string>& paths,
1271 std::string* realpath) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001272 for (const auto& path_str : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001273 char buf[512];
1274 const char* const path = path_str.c_str();
1275 if (!format_path(buf, sizeof(buf), path, name)) {
1276 continue;
1277 }
1278
1279 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001280 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001281 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001282 }
1283
1284 if (fd == -1) {
1285 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1286 if (fd != -1) {
1287 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001288 if (!realpath_fd(fd, realpath)) {
1289 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1290 *realpath = buf;
1291 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001292 }
1293 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001294
1295 if (fd != -1) {
1296 return fd;
1297 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001298 }
1299
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001300 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001301}
1302
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001303static int open_library(ZipArchiveCache* zip_archive_cache,
1304 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001305 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001306 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001307
Elliott Hughes124fae92012-10-31 14:20:03 -07001308 // 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 -07001309 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001310 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001311 int fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001312 if (fd != -1) {
1313 return fd;
1314 }
1315 }
1316
Elliott Hughes6971fe42012-11-01 22:59:19 -07001317 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
1318 if (fd != -1) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001319 *file_offset = 0;
Elliott Hughes6971fe42012-11-01 22:59:19 -07001320 }
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001321 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001322 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001323
Elliott Hughes124fae92012-10-31 14:20:03 -07001324 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001325 int fd = open_library_on_paths(zip_archive_cache, name, file_offset, g_ld_library_paths, realpath);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001326 if (fd == -1 && needed_by) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001327 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001328 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001329
Elliott Hughes124fae92012-10-31 14:20:03 -07001330 if (fd == -1) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001331 fd = open_library_on_default_path(name, file_offset, realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001332 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001333
Elliott Hughes124fae92012-10-31 14:20:03 -07001334 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001335}
1336
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001337static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
1338#if !defined(__LP64__)
1339 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001340 if (get_application_target_sdk_version() <= 22) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001341 const char* bname = basename(dt_needed);
1342 if (bname != dt_needed) {
1343 DL_WARN("'%s' library has invalid DT_NEEDED entry '%s'", sopath, dt_needed);
1344 }
1345
1346 return bname;
1347 }
1348#endif
1349 return dt_needed;
1350}
1351
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001352template<typename F>
1353static void for_each_dt_needed(const soinfo* si, F action) {
1354 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1355 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001356 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
Dima Zavin2e855792009-05-20 18:28:09 -07001357 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001358 }
1359}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001360
Spencer Low0346ad72015-04-22 18:06:51 -07001361static soinfo* load_library(int fd, off64_t file_offset,
1362 LoadTaskList& load_tasks,
Simon Baldwinaef71952015-01-16 13:22:54 +00001363 const char* name, int rtld_flags,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001364 const android_dlextinfo* extinfo,
1365 const std::string& realpath) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001366 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001367 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001368 return nullptr;
1369 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001370 if (file_offset < 0) {
1371 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
1372 return nullptr;
1373 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001374
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001375 struct stat file_stat;
1376 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001377 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001378 return nullptr;
1379 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001380 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001381 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1382 name, file_offset, file_stat.st_size);
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001383 return nullptr;
1384 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001385
1386 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001387 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1388 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
1389 for (soinfo* si = solist; si != nullptr; si = si->next) {
1390 if (si->get_st_dev() != 0 &&
1391 si->get_st_ino() != 0 &&
1392 si->get_st_dev() == file_stat.st_dev &&
1393 si->get_st_ino() == file_stat.st_ino &&
1394 si->get_file_offset() == file_offset) {
1395 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001396 "will return existing soinfo", name, si->get_realpath());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001397 return si;
1398 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001399 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001400 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001401
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001402 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001403 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001404 return nullptr;
1405 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001406
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001407 // Read the ELF header and load the segments.
Dmitriy Ivanov3f987f52015-06-25 15:51:41 -07001408 ElfReader elf_reader(realpath.c_str(), fd, file_offset, file_stat.st_size);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001409 if (!elf_reader.Load(extinfo)) {
1410 return nullptr;
1411 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001412
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001413 soinfo* si = soinfo_alloc(realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001414 if (si == nullptr) {
1415 return nullptr;
1416 }
1417 si->base = elf_reader.load_start();
1418 si->size = elf_reader.load_size();
1419 si->load_bias = elf_reader.load_bias();
1420 si->phnum = elf_reader.phdr_count();
1421 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001422
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001423 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001424 soinfo_free(si);
1425 return nullptr;
1426 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001427
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001428 for_each_dt_needed(si, [&] (const char* name) {
1429 load_tasks.push_back(LoadTask::create(name, si));
1430 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001431
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001432 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001433}
1434
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001435static soinfo* load_library(ZipArchiveCache* zip_archive_cache,
1436 LoadTaskList& load_tasks, const char* name,
Evgenii Stepanov68650822015-06-10 13:38:39 -07001437 soinfo* needed_by, int rtld_flags,
Spencer Low0346ad72015-04-22 18:06:51 -07001438 const android_dlextinfo* extinfo) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001439 off64_t file_offset;
1440 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001441 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001442 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001443 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1444 file_offset = extinfo->library_fd_offset;
1445 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001446
1447 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1448 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1449 "Will use given name.", name);
1450 realpath = name;
1451 }
1452 return load_library(extinfo->library_fd, file_offset, load_tasks, name, rtld_flags, extinfo, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001453 }
1454
1455 // Open the file.
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001456 int fd = open_library(zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001457 if (fd == -1) {
1458 DL_ERR("library \"%s\" not found", name);
1459 return nullptr;
1460 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001461 soinfo* result = load_library(fd, file_offset, load_tasks, name, rtld_flags, extinfo, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001462 close(fd);
1463 return result;
1464}
1465
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001466// Returns true if library was found and false in 2 cases
1467// 1. The library was found but loaded under different target_sdk_version
1468// (*candidate != nullptr)
1469// 2. The library was not found by soname (*candidate is nullptr)
1470static bool find_loaded_library_by_soname(const char* name, soinfo** candidate) {
1471 *candidate = nullptr;
1472
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001473 // Ignore filename with path.
1474 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001475 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001476 }
1477
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001478 uint32_t target_sdk_version = get_application_target_sdk_version();
1479
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001480 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001481 const char* soname = si->get_soname();
1482 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001483 // If the library was opened under different target sdk version
1484 // skip this step and try to reopen it. The exceptions are
1485 // "libdl.so" and global group. There is no point in skipping
1486 // them because relocation process is going to use them
1487 // in any case.
1488 bool is_libdl = si == solist;
1489 if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 ||
1490 !si->is_linked() || si->get_target_sdk_version() == target_sdk_version) {
1491 *candidate = si;
1492 return true;
1493 } else if (*candidate == nullptr) {
1494 // for the different sdk version - remember the first library.
1495 *candidate = si;
1496 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001497 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -07001498 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001499
1500 return false;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001501}
1502
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001503static soinfo* find_library_internal(ZipArchiveCache* zip_archive_cache,
1504 LoadTaskList& load_tasks, const char* name,
Evgenii Stepanov68650822015-06-10 13:38:39 -07001505 soinfo* needed_by, int rtld_flags,
1506 const android_dlextinfo* extinfo) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001507 soinfo* candidate;
1508
1509 if (find_loaded_library_by_soname(name, &candidate)) {
1510 return candidate;
1511 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001512
1513 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001514 // of this fact is done by load_library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001515 TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]",
1516 name, candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
1517
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001518 soinfo* si = load_library(zip_archive_cache, load_tasks, name, needed_by, rtld_flags, extinfo);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001519
1520 // In case we were unable to load the library but there
1521 // is a candidate loaded under the same soname but different
1522 // sdk level - return it anyways.
1523 if (si == nullptr && candidate != nullptr) {
1524 si = candidate;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001525 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001526
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001527 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001528}
1529
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001530static void soinfo_unload(soinfo* si);
1531
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001532// TODO: this is slightly unusual way to construct
1533// the global group for relocation. Not every RTLD_GLOBAL
1534// library is included in this group for backwards-compatibility
1535// reasons.
1536//
1537// This group consists of the main executable, LD_PRELOADs
1538// and libraries with the DF_1_GLOBAL flag set.
1539static soinfo::soinfo_list_t make_global_group() {
1540 soinfo::soinfo_list_t global_group;
1541 for (soinfo* si = somain; si != nullptr; si = si->next) {
1542 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1543 global_group.push_back(si);
1544 }
1545 }
1546
1547 return global_group;
1548}
1549
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001550// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1551// not their transitive dependencies) as children of the start_with library.
1552// This is false when find_libraries is called for dlopen(), when newly loaded
1553// libraries must form a disjoint tree.
1554static bool find_libraries(soinfo* start_with,
1555 const char* const library_names[],
1556 size_t library_names_count, soinfo* soinfos[],
1557 std::vector<soinfo*>* ld_preloads,
1558 size_t ld_preloads_count, int rtld_flags,
1559 const android_dlextinfo* extinfo,
1560 bool add_as_children) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001561 // Step 0: prepare.
1562 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001563 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001564 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001565 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001566 }
1567
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001568 // Construct global_group.
1569 soinfo::soinfo_list_t global_group = make_global_group();
1570
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001571 // If soinfos array is null allocate one on stack.
1572 // The array is needed in case of failure; for example
1573 // when library_names[] = {libone.so, libtwo.so} and libone.so
1574 // is loaded correctly but libtwo.so failed for some reason.
1575 // In this case libone.so should be unloaded on return.
1576 // See also implementation of failure_guard below.
1577
1578 if (soinfos == nullptr) {
1579 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1580 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1581 memset(soinfos, 0, soinfos_size);
1582 }
1583
1584 // list of libraries to link - see step 2.
1585 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001586
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001587 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001588 // Housekeeping
1589 load_tasks.for_each([] (LoadTask* t) {
1590 LoadTask::deleter(t);
1591 });
1592
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001593 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001594 soinfo_unload(soinfos[i]);
1595 }
1596 });
1597
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001598 ZipArchiveCache zip_archive_cache;
1599
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001600 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001601 for (LoadTask::unique_ptr task(load_tasks.pop_front());
1602 task.get() != nullptr; task.reset(load_tasks.pop_front())) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001603 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001604 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
1605
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001606 soinfo* si = find_library_internal(&zip_archive_cache, load_tasks,
1607 task->get_name(), needed_by, rtld_flags,
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001608 is_dt_needed ? nullptr : extinfo);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001609
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001610 if (si == nullptr) {
1611 return false;
1612 }
1613
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001614 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001615 needed_by->add_child(si);
1616 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001617
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001618 if (si->is_linked()) {
1619 si->increment_ref_count();
1620 }
1621
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001622 // When ld_preloads is not null, the first
1623 // ld_preloads_count libs are in fact ld_preloads.
1624 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001625 // Add LD_PRELOADed libraries to the global group for future runs.
1626 // There is no need to explicitly add them to the global group
1627 // for this run because they are going to appear in the local
1628 // group in the correct order.
1629 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001630 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001631 }
1632
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001633 if (soinfos_count < library_names_count) {
1634 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001635 }
1636 }
1637
1638 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001639 soinfo::soinfo_list_t local_group;
1640 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001641 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
1642 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001643 [&] (soinfo* si) {
1644 local_group.push_back(si);
1645 return true;
1646 });
1647
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001648 // We need to increment ref_count in case
1649 // the root of the local group was not linked.
1650 bool was_local_group_root_linked = local_group.front()->is_linked();
1651
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001652 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001653 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001654 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001655 return false;
1656 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001657 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001658 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001659
1660 return true;
1661 });
1662
1663 if (linked) {
1664 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001665 }
1666
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001667 if (!was_local_group_root_linked) {
1668 local_group.front()->increment_ref_count();
1669 }
1670
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001671 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001672}
1673
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001674static soinfo* find_library(const char* name, int rtld_flags,
1675 const android_dlextinfo* extinfo,
1676 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001677 soinfo* si;
1678
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001679 if (name == nullptr) {
1680 si = somain;
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001681 } else if (!find_libraries(needed_by, &name, 1, &si, nullptr, 0, rtld_flags,
1682 extinfo, /* add_as_children */ false)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001683 return nullptr;
1684 }
1685
Elliott Hughesd23736e2012-11-01 15:16:56 -07001686 return si;
1687}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001688
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001689static void soinfo_unload(soinfo* root) {
1690 // Note that the library can be loaded but not linked;
1691 // in which case there is no root but we still need
1692 // to walk the tree and unload soinfos involved.
1693 //
1694 // This happens on unsuccessful dlopen, when one of
1695 // the DT_NEEDED libraries could not be linked/found.
1696 if (root->is_linked()) {
1697 root = root->get_local_group_root();
1698 }
1699
1700 if (!root->can_unload()) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001701 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001702 return;
1703 }
1704
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001705 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001706
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001707 if (ref_count == 0) {
1708 soinfo::soinfo_list_t local_unload_list;
1709 soinfo::soinfo_list_t external_unload_list;
1710 soinfo::soinfo_list_t depth_first_list;
1711 depth_first_list.push_back(root);
1712 soinfo* si = nullptr;
1713
1714 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001715 if (local_unload_list.contains(si)) {
1716 continue;
1717 }
1718
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001719 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001720
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001721 if (si->has_min_version(0)) {
1722 soinfo* child = nullptr;
1723 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001724 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
1725 child->get_realpath(), child);
1726
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001727 if (local_unload_list.contains(child)) {
1728 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001729 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001730 external_unload_list.push_back(child);
1731 } else {
1732 depth_first_list.push_front(child);
1733 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001734 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001735 } else {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07001736#if !defined(__work_around_b_24465209__)
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001737 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001738#else
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001739 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001740 for_each_dt_needed(si, [&] (const char* library_name) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001741 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001742 si->get_realpath(), library_name);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001743
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001744 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr, nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001745 if (needed != nullptr) {
1746 // Not found: for example if symlink was deleted between dlopen and dlclose
1747 // Since we cannot really handle errors at this point - print and continue.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001748 PRINT("warning: couldn't find %s needed by %s on unload.",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001749 library_name, si->get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001750 return;
1751 } else if (local_unload_list.contains(needed)) {
1752 // already visited
1753 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001754 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001755 // external group
1756 external_unload_list.push_back(needed);
1757 } else {
1758 // local group
1759 depth_first_list.push_front(needed);
1760 }
1761 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001762#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001763 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001764 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001765
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001766 local_unload_list.for_each([](soinfo* si) {
1767 si->call_destructors();
1768 });
1769
1770 while ((si = local_unload_list.pop_front()) != nullptr) {
1771 notify_gdb_of_unload(si);
1772 soinfo_free(si);
1773 }
1774
1775 while ((si = external_unload_list.pop_front()) != nullptr) {
1776 soinfo_unload(si);
1777 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001778 } else {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001779 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
1780 root->get_realpath(), ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001781 }
1782}
1783
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001784void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001785 // Use basic string manipulation calls to avoid snprintf.
1786 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1787 // When debug malloc is enabled, this call returns 0. This in turn causes
1788 // snprintf to do nothing, which causes libraries to fail to load.
1789 // See b/17302493 for further details.
1790 // Once the above bug is fixed, this code can be modified to use
1791 // snprintf again.
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001792 size_t required_len = 0;
1793 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
1794 required_len += strlen(g_default_ld_paths[i]) + 1;
1795 }
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001796 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001797 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
1798 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001799 }
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001800 char* end = buffer;
1801 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
1802 if (i > 0) *end++ = ':';
1803 end = stpcpy(end, g_default_ld_paths[i]);
1804 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001805}
1806
Elliott Hughescade4c32012-12-20 14:42:14 -08001807void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001808 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001809}
1810
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001811soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo, soinfo *caller) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001812 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001813 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001814 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001815 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001816 if (extinfo != nullptr) {
1817 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1818 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1819 return nullptr;
1820 }
1821 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001822 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001823 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
1824 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001825 return nullptr;
1826 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001827 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001828
1829 ProtectedDataGuard guard;
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001830 soinfo* si = find_library(name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001831 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001832 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001833 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001834 return si;
1835}
1836
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001837void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001838 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001839 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001840}
1841
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001842static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1843 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1844 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1845 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001846 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
1847 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001848
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001849 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001850}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001851
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001852const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
1853 if (source_symver < 2 ||
1854 source_symver >= version_infos.size() ||
1855 version_infos[source_symver].name == nullptr) {
1856 return nullptr;
1857 }
1858
1859 return &version_infos[source_symver];
1860}
1861
1862void VersionTracker::add_version_info(size_t source_index,
1863 ElfW(Word) elf_hash,
1864 const char* ver_name,
1865 const soinfo* target_si) {
1866 if (source_index >= version_infos.size()) {
1867 version_infos.resize(source_index+1);
1868 }
1869
1870 version_infos[source_index].elf_hash = elf_hash;
1871 version_infos[source_index].name = ver_name;
1872 version_infos[source_index].target_si = target_si;
1873}
1874
1875bool VersionTracker::init_verneed(const soinfo* si_from) {
1876 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
1877
1878 if (verneed_ptr == 0) {
1879 return true;
1880 }
1881
1882 size_t verneed_cnt = si_from->get_verneed_cnt();
1883
1884 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
1885 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
1886 size_t vernaux_offset = offset + verneed->vn_aux;
1887 offset += verneed->vn_next;
1888
1889 if (verneed->vn_version != 1) {
1890 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
1891 return false;
1892 }
1893
1894 const char* target_soname = si_from->get_string(verneed->vn_file);
1895 // find it in dependencies
1896 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07001897 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001898 });
1899
1900 if (target_si == nullptr) {
1901 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001902 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001903 return false;
1904 }
1905
1906 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
1907 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
1908 vernaux_offset += vernaux->vna_next;
1909
1910 const ElfW(Word) elf_hash = vernaux->vna_hash;
1911 const char* ver_name = si_from->get_string(vernaux->vna_name);
1912 ElfW(Half) source_index = vernaux->vna_other;
1913
1914 add_version_info(source_index, elf_hash, ver_name, target_si);
1915 }
1916 }
1917
1918 return true;
1919}
1920
1921bool VersionTracker::init_verdef(const soinfo* si_from) {
1922 return for_each_verdef(si_from,
1923 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
1924 add_version_info(verdef->vd_ndx, verdef->vd_hash,
1925 si_from->get_string(verdaux->vda_name), si_from);
1926 return false;
1927 }
1928 );
1929}
1930
1931bool VersionTracker::init(const soinfo* si_from) {
1932 if (!si_from->has_min_version(2)) {
1933 return true;
1934 }
1935
1936 return init_verneed(si_from) && init_verdef(si_from);
1937}
1938
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07001939bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
1940 const char* sym_name, const version_info** vi) {
1941 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
1942 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
1943
1944 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
1945 *vi = version_tracker.get_version_info(sym_ver);
1946
1947 if (*vi == nullptr) {
1948 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001949 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07001950 return false;
1951 }
1952 } else {
1953 // there is no version info
1954 *vi = nullptr;
1955 }
1956
1957 return true;
1958}
1959
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001960#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001961#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001962static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
1963 return rela->r_addend;
1964}
1965#else
1966static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001967 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
1968 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001969 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
1970 }
1971 return 0;
1972}
1973#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001974
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001975template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07001976bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
1977 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001978 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
1979 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001980 if (rel == nullptr) {
1981 return false;
1982 }
1983
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001984 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
1985 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
1986
1987 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001988 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001989 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001990 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001991
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001992 DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001993 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001994 continue;
1995 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001996
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001997 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001998 soinfo* lsi = nullptr;
1999
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002000 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002001 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002002 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002003
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002004 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2005 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002006 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002007
2008 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2009 return false;
2010 }
2011
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002012 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002013 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002014 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002015 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002016 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002017 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002018 }
2019
2020 /* IHI0044C AAELF 4.5.1.1:
2021
2022 Libraries are not searched to resolve weak references.
2023 It is not an error for a weak reference to remain unsatisfied.
2024
2025 During linking, the value of an undefined weak reference is:
2026 - Zero if the relocation type is absolute
2027 - The address of the place if the relocation is pc-relative
2028 - The address of nominal base address if the relocation
2029 type is base-relative.
2030 */
2031
2032 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002033 case R_GENERIC_JUMP_SLOT:
2034 case R_GENERIC_GLOB_DAT:
2035 case R_GENERIC_RELATIVE:
2036 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002037#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002038 case R_AARCH64_ABS64:
2039 case R_AARCH64_ABS32:
2040 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002041#elif defined(__x86_64__)
2042 case R_X86_64_32:
2043 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002044#elif defined(__arm__)
2045 case R_ARM_ABS32:
2046#elif defined(__i386__)
2047 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002048#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002049 /*
2050 * The sym_addr was initialized to be zero above, or the relocation
2051 * code below does not care about value of sym_addr.
2052 * No need to do anything.
2053 */
2054 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002055#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002056 case R_X86_64_PC32:
2057 sym_addr = reloc;
2058 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002059#elif defined(__i386__)
2060 case R_386_PC32:
2061 sym_addr = reloc;
2062 break;
2063#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002064 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002065 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002066 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002067 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002068 } else { // We got a definition.
2069#if !defined(__LP64__)
2070 // When relocating dso with text_relocation .text segment is
2071 // not executable. We need to restore elf flags before resolving
2072 // STT_GNU_IFUNC symbol.
2073 bool protect_segments = has_text_relocations &&
2074 lsi == this &&
2075 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2076 if (protect_segments) {
2077 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2078 DL_ERR("can't protect segments for \"%s\": %s",
2079 get_realpath(), strerror(errno));
2080 return false;
2081 }
2082 }
2083#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002084 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002085#if !defined(__LP64__)
2086 if (protect_segments) {
2087 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2088 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2089 get_realpath(), strerror(errno));
2090 return false;
2091 }
2092 }
2093#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002094 }
2095 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002096 }
2097
2098 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002099 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002100 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002101 MARK(rel->r_offset);
2102 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2103 reinterpret_cast<void*>(reloc),
2104 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2105
2106 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002107 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002108 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002109 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002110 MARK(rel->r_offset);
2111 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2112 reinterpret_cast<void*>(reloc),
2113 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2114 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002115 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002116 case R_GENERIC_RELATIVE:
2117 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002118 MARK(rel->r_offset);
2119 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2120 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002121 reinterpret_cast<void*>(load_bias + addend));
2122 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002123 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002124 case R_GENERIC_IRELATIVE:
2125 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002126 MARK(rel->r_offset);
2127 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2128 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002129 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002130 {
2131#if !defined(__LP64__)
2132 // When relocating dso with text_relocation .text segment is
2133 // not executable. We need to restore elf flags for this
2134 // particular call.
2135 if (has_text_relocations) {
2136 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2137 DL_ERR("can't protect segments for \"%s\": %s",
2138 get_realpath(), strerror(errno));
2139 return false;
2140 }
2141 }
2142#endif
2143 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2144#if !defined(__LP64__)
2145 // Unprotect it afterwards...
2146 if (has_text_relocations) {
2147 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2148 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2149 get_realpath(), strerror(errno));
2150 return false;
2151 }
2152 }
2153#endif
2154 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2155 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002156 break;
2157
2158#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002159 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002160 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002161 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002162 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002163 reloc, (sym_addr + addend), sym_name);
2164 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002165 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002166 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002167 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002168 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002169 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002170 reloc, (sym_addr + addend), sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002171 {
2172 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
2173 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2174 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
2175 if ((min_value <= (reloc_value + (sym_addr + addend))) &&
2176 ((reloc_value + (sym_addr + addend)) <= max_value)) {
2177 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
2178 } else {
2179 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
2180 (reloc_value + (sym_addr + addend)), min_value, max_value);
2181 return false;
2182 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002183 }
2184 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002185 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002186 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002187 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002188 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002189 reloc, (sym_addr + addend), sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002190 {
2191 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
2192 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2193 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
2194 if ((min_value <= (reloc_value + (sym_addr + addend))) &&
2195 ((reloc_value + (sym_addr + addend)) <= max_value)) {
2196 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
2197 } else {
2198 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
2199 reloc_value + (sym_addr + addend), min_value, max_value);
2200 return false;
2201 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002202 }
2203 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002204 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002205 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002206 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002207 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002208 reloc, (sym_addr + addend), rel->r_offset, sym_name);
2209 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend) - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002210 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002211 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002212 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002213 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002214 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002215 reloc, (sym_addr + addend), rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002216 {
2217 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
2218 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2219 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
2220 if ((min_value <= (reloc_value + ((sym_addr + addend) - rel->r_offset))) &&
2221 ((reloc_value + ((sym_addr + addend) - rel->r_offset)) <= max_value)) {
2222 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
2223 } else {
2224 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
2225 reloc_value + ((sym_addr + addend) - rel->r_offset), min_value, max_value);
2226 return false;
2227 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002228 }
2229 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002230 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002231 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002232 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002233 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002234 reloc, (sym_addr + addend), rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002235 {
2236 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
2237 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2238 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
2239 if ((min_value <= (reloc_value + ((sym_addr + addend) - rel->r_offset))) &&
2240 ((reloc_value + ((sym_addr + addend) - rel->r_offset)) <= max_value)) {
2241 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
2242 } else {
2243 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
2244 reloc_value + ((sym_addr + addend) - rel->r_offset), min_value, max_value);
2245 return false;
2246 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002247 }
2248 break;
2249
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002250 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002251 /*
2252 * ET_EXEC is not supported so this should not happen.
2253 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002254 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002255 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002256 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002257 * R_AARCH64_COPY may only appear in executable objects where e_type is
2258 * set to ET_EXEC.
2259 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002260 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002261 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002262 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002263 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002264 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002265 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002266 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002267 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002268 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002269 break;
2270#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002271 case R_X86_64_32:
2272 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002273 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002274 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2275 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002276 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002277 break;
2278 case R_X86_64_64:
2279 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002280 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002281 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2282 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002283 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002284 break;
2285 case R_X86_64_PC32:
2286 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002287 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002288 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2289 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2290 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002291 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002292 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002293#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002294 case R_ARM_ABS32:
2295 count_relocation(kRelocAbsolute);
2296 MARK(rel->r_offset);
2297 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2298 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2299 break;
2300 case R_ARM_REL32:
2301 count_relocation(kRelocRelative);
2302 MARK(rel->r_offset);
2303 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2304 reloc, sym_addr, rel->r_offset, sym_name);
2305 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2306 break;
2307 case R_ARM_COPY:
2308 /*
2309 * ET_EXEC is not supported so this should not happen.
2310 *
2311 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2312 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002313 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002314 * R_ARM_COPY may only appear in executable objects where e_type is
2315 * set to ET_EXEC.
2316 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002317 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002318 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002319#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002320 case R_386_32:
2321 count_relocation(kRelocRelative);
2322 MARK(rel->r_offset);
2323 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2324 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2325 break;
2326 case R_386_PC32:
2327 count_relocation(kRelocRelative);
2328 MARK(rel->r_offset);
2329 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2330 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2331 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2332 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002333#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002334 default:
2335 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002336 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002337 }
2338 }
2339 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002340}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002341#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002342
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002343void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
2344 size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002345 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002346 return;
2347 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002348
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002349 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath());
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002350
2351 int begin = reverse ? (count - 1) : 0;
2352 int end = reverse ? -1 : count;
2353 int step = reverse ? -1 : 1;
2354
2355 for (int i = begin; i != end; i += step) {
2356 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002357 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002358 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002359
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002360 TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002361}
2362
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002363void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002364 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002365 return;
2366 }
2367
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002368 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07002369 function();
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002370 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04002371}
2372
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002373void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002374 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
2375 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002376 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002377}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002378
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002379void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002380 if (constructors_called) {
2381 return;
2382 }
Jesse Hallf5d16932012-01-30 15:39:57 -08002383
Elliott Hughesd23736e2012-11-01 15:16:56 -07002384 // We set constructors_called before actually calling the constructors, otherwise it doesn't
2385 // protect against recursive constructor calls. One simple example of constructor recursion
2386 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
2387 // 1. The program depends on libc, so libc's constructor is called here.
2388 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
2389 // 3. dlopen() calls the constructors on the newly created
2390 // soinfo for libc_malloc_debug_leak.so.
2391 // 4. The debug .so depends on libc, so CallConstructors is
2392 // called again with the libc soinfo. If it doesn't trigger the early-
2393 // out above, the libc constructor will be called again (recursively!).
2394 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002395
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002396 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002397 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07002398 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002399 get_realpath(), preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002400 }
2401
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002402 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002403 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002404 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002405
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002406 TRACE("\"%s\": calling constructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002407
2408 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002409 call_function("DT_INIT", init_func_);
2410 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002411}
David 'Digit' Turner82156792009-05-18 14:37:41 +02002412
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002413void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002414 if (!constructors_called) {
2415 return;
2416 }
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002417 TRACE("\"%s\": calling destructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002418
2419 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002420 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002421
2422 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002423 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002424
2425 // This is needed on second call to dlopen
2426 // after library has been unloaded with RTLD_NODELETE
2427 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002428}
2429
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002430void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002431 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002432 child->parents_.push_back(this);
2433 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002434 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002435}
2436
2437void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002438 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002439 return;
2440 }
2441
2442 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002443 children_.for_each([&] (soinfo* child) {
2444 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002445 return parent == this;
2446 });
2447 });
2448
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002449 parents_.for_each([&] (soinfo* parent) {
2450 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002451 return child == this;
2452 });
2453 });
2454
2455 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002456 parents_.clear();
2457 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002458}
2459
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002460dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002461 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002462 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002463 }
2464
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002465 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002466};
2467
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002468ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002469 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002470 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002471 }
2472
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002473 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002474}
2475
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002476off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002477 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002478 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002479 }
2480
2481 return 0;
2482}
2483
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002484uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07002485 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002486 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07002487 }
2488
2489 return 0;
2490}
2491
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002492uint32_t soinfo::get_dt_flags_1() const {
2493 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002494 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002495 }
2496
2497 return 0;
2498}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002499
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002500void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
2501 if (has_min_version(1)) {
2502 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002503 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002504 }
2505
2506 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002507 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002508 }
2509
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002510 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002511 }
2512}
2513
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002514const char* soinfo::get_realpath() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07002515#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002516 if (has_min_version(2)) {
2517 return realpath_.c_str();
2518 } else {
2519 return old_name_;
2520 }
2521#else
2522 return realpath_.c_str();
2523#endif
2524}
2525
2526const char* soinfo::get_soname() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07002527#if defined(__work_around_b_24465209__)
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002528 if (has_min_version(2)) {
2529 return soname_;
2530 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002531 return old_name_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002532 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002533#else
2534 return soname_;
2535#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002536}
2537
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002538// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002539// 'this->flags' does not have FLAG_NEW_SOINFO set.
2540static soinfo::soinfo_list_t g_empty_list;
2541
2542soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002543 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002544 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002545 }
2546
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002547 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002548}
2549
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002550const soinfo::soinfo_list_t& soinfo::get_children() const {
2551 if (has_min_version(0)) {
2552 return children_;
2553 }
2554
2555 return g_empty_list;
2556}
2557
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002558soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002559 if (has_min_version(0)) {
2560 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002561 }
2562
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002563 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002564}
2565
Evgenii Stepanov68650822015-06-10 13:38:39 -07002566static std::vector<std::string> g_empty_runpath;
2567
2568const std::vector<std::string>& soinfo::get_dt_runpath() const {
2569 if (has_min_version(2)) {
2570 return dt_runpath_;
2571 }
2572
2573 return g_empty_runpath;
2574}
2575
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002576ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002577 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
2578 return call_ifunc_resolver(s->st_value + load_bias);
2579 }
2580
2581 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
2582}
2583
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002584const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002585 if (has_min_version(1) && (index >= strtab_size_)) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002586 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002587 get_realpath(), strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002588 }
2589
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002590 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002591}
2592
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002593bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002594 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002595}
2596
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002597bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002598 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002599}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002600
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002601bool soinfo::is_linked() const {
2602 return (flags_ & FLAG_LINKED) != 0;
2603}
2604
2605bool soinfo::is_main_executable() const {
2606 return (flags_ & FLAG_EXE) != 0;
2607}
2608
2609void soinfo::set_linked() {
2610 flags_ |= FLAG_LINKED;
2611}
2612
2613void soinfo::set_linker_flag() {
2614 flags_ |= FLAG_LINKER;
2615}
2616
2617void soinfo::set_main_executable() {
2618 flags_ |= FLAG_EXE;
2619}
2620
2621void soinfo::increment_ref_count() {
2622 local_group_root_->ref_count_++;
2623}
2624
2625size_t soinfo::decrement_ref_count() {
2626 return --local_group_root_->ref_count_;
2627}
2628
2629soinfo* soinfo::get_local_group_root() const {
2630 return local_group_root_;
2631}
2632
Dmitriy Ivanov19133522015-06-02 17:36:54 -07002633// This function returns api-level at the time of
2634// dlopen/load. Note that libraries opened by system
2635// will always have 'current' api level.
2636uint32_t soinfo::get_target_sdk_version() const {
2637 if (!has_min_version(2)) {
2638 return __ANDROID_API__;
2639 }
2640
2641 return local_group_root_->target_sdk_version_;
2642}
2643
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002644bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002645 /* Extract dynamic section */
2646 ElfW(Word) dynamic_flags = 0;
2647 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002648
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002649 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002650 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002651 if (!relocating_linker) {
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07002652 INFO("[ linking %s ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002653 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002654 }
2655
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002656 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002657 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002658 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002659 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002660 return false;
2661 } else {
2662 if (!relocating_linker) {
2663 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002664 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002665 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002666
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002667#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002668 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2669 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002670#endif
2671
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002672 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002673 // Note that: "Except for the DT_NULL element at the end of the array,
2674 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2675 //
2676 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002677 uint32_t needed_count = 0;
2678 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2679 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2680 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2681 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002682 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002683 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002684 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002685
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002686 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002687 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2688 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2689 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2690 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002691 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002692
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002693 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002694 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002695 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002696 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2697 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002698
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002699 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002700 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002701 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002702 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2703 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002704
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002705 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002706 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002707 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002708 return false;
2709 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002710 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002711
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002712 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002713 break;
2714
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002715 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002716 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002717 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002718
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002719 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002720 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002721 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002722
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002723 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002724 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002725 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002726
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002727 case DT_SYMENT:
2728 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002729 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2730 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002731 return false;
2732 }
2733 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002734
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002735 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002736#if defined(USE_RELA)
2737 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002738 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002739 return false;
2740 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002741#else
2742 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002743 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002744 return false;
2745 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002746#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002747 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002748
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002749 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002750#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002751 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002752#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002753 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002754#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002755 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002756
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002757 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002758#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002759 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002760#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002761 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002762#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002763 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002764
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002765 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002766#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002767 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002768 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002769#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002770 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2771 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002772
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002773 case DT_DEBUG:
2774 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2775 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002776// FIXME: not working currently for N64
2777// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002778// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002779// read-only, but the DYNAMIC header claims it is writable.
2780#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002781 if ((dynamic_flags & PF_W) != 0) {
2782 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2783 }
Chris Dearman99186652014-02-06 20:36:51 -08002784#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002785 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002786#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002787 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002788 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002789 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002790
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002791 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002792 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002793 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002794
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002795 case DT_ANDROID_RELA:
2796 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2797 break;
2798
2799 case DT_ANDROID_RELASZ:
2800 android_relocs_size_ = d->d_un.d_val;
2801 break;
2802
2803 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002804 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002805 return false;
2806
2807 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002808 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002809 return false;
2810
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002811 case DT_RELAENT:
2812 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002813 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002814 return false;
2815 }
2816 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002817
2818 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002819 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002820 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002821
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002822 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002823 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002824 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002825
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002826 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002827 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002828 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002829
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002830#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002831 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002832 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002833 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002834
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002835 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002836 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002837 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002838
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002839 case DT_RELENT:
2840 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002841 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002842 return false;
2843 }
2844 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002845
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002846 case DT_ANDROID_REL:
2847 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2848 break;
2849
2850 case DT_ANDROID_RELSZ:
2851 android_relocs_size_ = d->d_un.d_val;
2852 break;
2853
2854 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002855 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002856 return false;
2857
2858 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002859 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002860 return false;
2861
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002862 // "Indicates that all RELATIVE relocations have been concatenated together,
2863 // and specifies the RELATIVE relocation count."
2864 //
2865 // TODO: Spec also mentions that this can be used to optimize relocation process;
2866 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002867 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002868 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002869
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002870 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002871 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002872 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002873
2874 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002875 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002876 return false;
2877
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002878#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002879 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002880 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002881 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002882 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002883
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002884 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002885 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002886 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002887 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002888
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002889 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002890 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002891 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002892 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002893
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002894 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002895 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002896 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002897
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002898 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002899 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002900 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002901 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002902
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002903 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002904 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002905 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002906
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002907 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002908 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002909 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002910 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002911
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002912 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002913 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002914 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002915
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002916 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002917#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002918 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002919 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002920#else
2921 has_text_relocations = true;
2922 break;
2923#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002924
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002925 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002926 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002927 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002928
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002929 case DT_NEEDED:
2930 ++needed_count;
2931 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002932
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002933 case DT_FLAGS:
2934 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002935#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002936 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002937 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002938#else
2939 has_text_relocations = true;
2940#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002941 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002942 if (d->d_un.d_val & DF_SYMBOLIC) {
2943 has_DT_SYMBOLIC = true;
2944 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002945 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002946
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002947 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002948 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002949
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002950 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -07002951 DL_WARN("%s: unsupported flags DT_FLAGS_1=%p", get_realpath(), reinterpret_cast<void*>(d->d_un.d_val));
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002952 }
2953 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002954#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002955 case DT_MIPS_RLD_MAP:
2956 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2957 {
2958 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2959 *dp = &_r_debug;
2960 }
2961 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002962 case DT_MIPS_RLD_MAP2:
2963 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2964 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002965 r_debug** dp = reinterpret_cast<r_debug**>(
2966 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08002967 *dp = &_r_debug;
2968 }
2969 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002970
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002971 case DT_MIPS_RLD_VERSION:
2972 case DT_MIPS_FLAGS:
2973 case DT_MIPS_BASE_ADDRESS:
2974 case DT_MIPS_UNREFEXTNO:
2975 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002976
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002977 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002978 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002979 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002980
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002981 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002982 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002983 break;
2984
2985 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002986 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002987 break;
2988#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002989 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2990 case DT_BIND_NOW:
2991 break;
2992
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002993 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002994 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
2995 break;
2996
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002997 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002998 verdef_ptr_ = load_bias + d->d_un.d_ptr;
2999 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003000 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003001 verdef_cnt_ = d->d_un.d_val;
3002 break;
3003
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003004 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003005 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3006 break;
3007
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003008 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003009 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003010 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003011
Evgenii Stepanov68650822015-06-10 13:38:39 -07003012 case DT_RUNPATH:
3013 // this is parsed after we have strtab initialized (see below).
3014 break;
3015
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003016 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003017 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003018 DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003019 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3020 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003021 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003022 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003023 }
3024
Duane Sandbc425c72015-06-01 16:29:14 -07003025#if defined(__mips__) && !defined(__LP64__)
3026 if (!mips_check_and_adjust_fp_modes()) {
3027 return false;
3028 }
3029#endif
3030
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003031 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003032 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003033
3034 // Sanity checks.
3035 if (relocating_linker && needed_count != 0) {
3036 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3037 return false;
3038 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003039 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003040 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003041 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003042 return false;
3043 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003044 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003045 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003046 return false;
3047 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003048 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003049 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003050 return false;
3051 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003052
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003053 // second pass - parse entries relying on strtab
3054 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003055 switch (d->d_tag) {
3056 case DT_SONAME:
3057 soname_ = get_string(d->d_un.d_val);
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003058#if defined(__work_around_b_24465209__)
Evgenii Stepanov68650822015-06-10 13:38:39 -07003059 strlcpy(old_name_, soname_, sizeof(old_name_));
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003060#endif
Evgenii Stepanov68650822015-06-10 13:38:39 -07003061 break;
3062 case DT_RUNPATH:
3063 // FIXME: $LIB, $PLATFORM unsupported.
3064 set_dt_runpath(get_string(d->d_un.d_val));
3065 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003066 }
3067 }
3068
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003069 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003070 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003071 // because they can't find dt_needed library by soname.
3072 // This workaround should keep them working. (applies only
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003073 // for apps targeting sdk version <=22). Make an exception for
3074 // the main executable and linker; they do not need to have dt_soname
3075 if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 &&
3076 get_application_target_sdk_version() <= 22) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003077 soname_ = basename(realpath_.c_str());
3078 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3079 get_realpath(), soname_);
3080 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003081 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003082}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003083
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003084bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3085 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003086
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003087 local_group_root_ = local_group.front();
3088 if (local_group_root_ == nullptr) {
3089 local_group_root_ = this;
3090 }
3091
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003092 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3093 target_sdk_version_ = get_application_target_sdk_version();
3094 }
3095
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003096 VersionTracker version_tracker;
3097
3098 if (!version_tracker.init(this)) {
3099 return false;
3100 }
3101
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003102#if !defined(__LP64__)
3103 if (has_text_relocations) {
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003104 // Fail if app is targeting sdk version > 22
Dmitriy Ivanov80687862015-10-09 13:58:46 -07003105 if (get_application_target_sdk_version() > 22) {
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003106 DL_ERR("%s: has text relocations", get_realpath());
3107 return false;
3108 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003109 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003110 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003111 DL_WARN("%s has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003112 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003113 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3114 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003115 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003116 return false;
3117 }
3118 }
3119#endif
3120
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003121 if (android_relocs_ != nullptr) {
3122 // check signature
3123 if (android_relocs_size_ > 3 &&
3124 android_relocs_[0] == 'A' &&
3125 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003126 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003127 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003128 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003129
3130 bool relocated = false;
3131 const uint8_t* packed_relocs = android_relocs_ + 4;
3132 const size_t packed_relocs_size = android_relocs_size_ - 4;
3133
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003134 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003135 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003136 packed_reloc_iterator<sleb128_decoder>(
3137 sleb128_decoder(packed_relocs, packed_relocs_size)),
3138 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003139
3140 if (!relocated) {
3141 return false;
3142 }
3143 } else {
3144 DL_ERR("bad android relocation header.");
3145 return false;
3146 }
3147 }
3148
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003149#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003150 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003151 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003152 if (!relocate(version_tracker,
3153 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003154 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003155 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003156 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003157 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003158 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003159 if (!relocate(version_tracker,
3160 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003161 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003162 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003163 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003164#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003165 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003166 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003167 if (!relocate(version_tracker,
3168 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003169 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003170 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003171 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003172 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003173 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003174 if (!relocate(version_tracker,
3175 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003176 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003177 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003178 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003179#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003180
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003181#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003182 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003183 return false;
3184 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003185#endif
3186
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003187 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003188
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003189#if !defined(__LP64__)
3190 if (has_text_relocations) {
3191 // All relocations are done, we can protect our segments back to read-only.
3192 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3193 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003194 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003195 return false;
3196 }
3197 }
3198#endif
3199
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003200 /* We can also turn on GNU RELRO protection */
3201 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3202 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003203 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003204 return false;
3205 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003206
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003207 /* Handle serializing/sharing the RELRO segment */
3208 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3209 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3210 extinfo->relro_fd) < 0) {
3211 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003212 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003213 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003214 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003215 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3216 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3217 extinfo->relro_fd) < 0) {
3218 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003219 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003220 return false;
3221 }
3222 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003223
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003224 notify_gdb_of_load(this);
3225 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003226}
3227
Nick Kralevich468319c2011-11-11 15:53:17 -08003228/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003229 * This function add vdso to internal dso list.
3230 * It helps to stack unwinding through signal handlers.
3231 * Also, it makes bionic more like glibc.
3232 */
Kito Cheng812fd422014-03-25 22:53:56 +08003233static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003234#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08003235 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003236 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08003237 return;
3238 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003239
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003240 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003241
Elliott Hughes0266ae52014-02-10 17:46:57 -08003242 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
3243 si->phnum = ehdr_vdso->e_phnum;
3244 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
3245 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003246 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003247
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003248 si->prelink_image();
3249 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003250#endif
3251}
3252
3253/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003254 * This is linker soinfo for GDB. See details below.
3255 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003256#if defined(__LP64__)
3257#define LINKER_PATH "/system/bin/linker64"
3258#else
3259#define LINKER_PATH "/system/bin/linker"
3260#endif
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003261
3262// This is done to avoid calling c-tor prematurely
3263// because soinfo c-tor needs memory allocator
3264// which might be initialized after global variables.
3265static uint8_t linker_soinfo_for_gdb_buf[sizeof(soinfo)] __attribute__((aligned(8)));
3266static soinfo* linker_soinfo_for_gdb = nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003267
3268/* gdb expects the linker to be in the debug shared object list.
3269 * Without this, gdb has trouble locating the linker's ".text"
3270 * and ".plt" sections. Gdb could also potentially use this to
3271 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
3272 * Don't use soinfo_alloc(), because the linker shouldn't
3273 * be on the soinfo list.
3274 */
3275static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003276 linker_soinfo_for_gdb = new (linker_soinfo_for_gdb_buf) soinfo(LINKER_PATH, nullptr, 0, 0);
3277
Dmitriy Ivanov175dae92015-06-10 19:46:19 -07003278 linker_soinfo_for_gdb->load_bias = linker_base;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003279
3280 /*
3281 * Set the dynamic field in the link map otherwise gdb will complain with
3282 * the following:
3283 * warning: .dynamic section for "/system/bin/linker" is not at the
3284 * expected address (wrong library or version mismatch?)
3285 */
3286 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
3287 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
3288 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003289 &linker_soinfo_for_gdb->dynamic, nullptr);
3290 insert_soinfo_into_debug_map(linker_soinfo_for_gdb);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003291}
3292
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003293static void init_default_ld_library_path() {
3294 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3295 somain->load_bias);
3296 const char* bname = basename(interp);
3297 if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0))
3298 g_default_ld_paths = kAsanDefaultLdPaths;
3299 else
3300 g_default_ld_paths = kDefaultLdPaths;
3301};
3302
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07003303extern "C" int __system_properties_init(void);
3304
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003305/*
Nick Kralevich468319c2011-11-11 15:53:17 -08003306 * This code is called after the linker has linked itself and
3307 * fixed it's own GOT. It is safe to make references to externs
3308 * and other non-local data at this point.
3309 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08003310static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003311#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003312 struct timeval t0, t1;
3313 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003314#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003315
Elliott Hughes1801db32015-06-08 18:04:00 -07003316 // Sanitize the environment.
3317 __libc_init_AT_SECURE(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01003318
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07003319 // Initialize system properties
3320 __system_properties_init(); // may use 'environ'
3321
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003322 debuggerd_init();
3323
3324 // Get a few environment variables.
Elliott Hughes1801db32015-06-08 18:04:00 -07003325 const char* LD_DEBUG = getenv("LD_DEBUG");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003326 if (LD_DEBUG != nullptr) {
3327 g_ld_debug_verbosity = atoi(LD_DEBUG);
3328 }
3329
Elliott Hughes1801db32015-06-08 18:04:00 -07003330 // These should have been sanitized by __libc_init_AT_SECURE, but the test
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003331 // doesn't cost us anything.
3332 const char* ldpath_env = nullptr;
3333 const char* ldpreload_env = nullptr;
Elliott Hughes1801db32015-06-08 18:04:00 -07003334 if (!getauxval(AT_SECURE)) {
3335 ldpath_env = getenv("LD_LIBRARY_PATH");
3336 ldpreload_env = getenv("LD_PRELOAD");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003337 }
3338
3339 INFO("[ android linker & debugger ]");
3340
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003341 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003342 if (si == nullptr) {
3343 exit(EXIT_FAILURE);
3344 }
3345
3346 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003347 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003348 link_map* map = &(si->link_map_head);
3349
3350 map->l_addr = 0;
3351 map->l_name = args.argv[0];
3352 map->l_prev = nullptr;
3353 map->l_next = nullptr;
3354
3355 _r_debug.r_map = map;
3356 r_debug_tail = map;
3357
3358 init_linker_info_for_gdb(linker_base);
3359
3360 // Extract information passed from the kernel.
3361 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
3362 si->phnum = args.getauxval(AT_PHNUM);
3363 si->entry = args.getauxval(AT_ENTRY);
3364
3365 /* Compute the value of si->base. We can't rely on the fact that
3366 * the first entry is the PHDR because this will not be true
3367 * for certain executables (e.g. some in the NDK unit test suite)
3368 */
3369 si->base = 0;
3370 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
3371 si->load_bias = 0;
3372 for (size_t i = 0; i < si->phnum; ++i) {
3373 if (si->phdr[i].p_type == PT_PHDR) {
3374 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
3375 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
3376 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07003377 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003378 }
3379 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07003380
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003381 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
3382 if (elf_hdr->e_type != ET_DYN) {
3383 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
3384 exit(EXIT_FAILURE);
3385 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003386
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003387 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
3388 parse_LD_LIBRARY_PATH(ldpath_env);
3389 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01003390
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003391 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003392
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003393 init_default_ld_library_path();
3394
Dmitriy Ivanov67181252015-01-07 15:48:25 -08003395 if (!si->prelink_image()) {
3396 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
3397 exit(EXIT_FAILURE);
3398 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003399
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003400 // add somain to global group
3401 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
3402
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003403 // Load ld_preloads and dependencies.
3404 StringLinkedList needed_library_name_list;
3405 size_t needed_libraries_count = 0;
3406 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07003407
3408 for (const auto& ld_preload_name : g_ld_preload_names) {
3409 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003410 ++needed_libraries_count;
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07003411 ++ld_preloads_count;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003412 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003413
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003414 for_each_dt_needed(si, [&](const char* name) {
3415 needed_library_name_list.push_back(name);
3416 ++needed_libraries_count;
3417 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003418
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003419 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003420
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003421 memset(needed_library_names, 0, sizeof(needed_library_names));
3422 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003423
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07003424 if (needed_libraries_count > 0 &&
3425 !find_libraries(si, needed_library_names, needed_libraries_count, nullptr,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07003426 &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr,
3427 /* add_as_children */ true)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003428 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
3429 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003430 } else if (needed_libraries_count == 0) {
3431 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
3432 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
3433 exit(EXIT_FAILURE);
3434 }
3435 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003436 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003437
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003438 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07003439
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08003440 {
3441 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06003442
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08003443 si->call_pre_init_constructors();
3444
3445 /* After the prelink_image, the si->load_bias is initialized.
3446 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
3447 * We need to update this value for so exe here. So Unwind_Backtrace
3448 * for some arch like x86 could work correctly within so exe.
3449 */
3450 map->l_addr = si->load_bias;
3451 si->call_constructors();
3452 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003453
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003454#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003455 gettimeofday(&t1, nullptr);
3456 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
3457 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
3458 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003459#endif
3460#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003461 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
3462 linker_stats.count[kRelocAbsolute],
3463 linker_stats.count[kRelocRelative],
3464 linker_stats.count[kRelocCopy],
3465 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003466#endif
3467#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003468 {
3469 unsigned n;
3470 unsigned i;
3471 unsigned count = 0;
3472 for (n = 0; n < 4096; n++) {
3473 if (bitmask[n]) {
3474 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003475#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003476 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003477#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003478 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003479#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003480 if (x & 1) {
3481 count++;
3482 }
3483 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003484 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003485 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003486 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003487 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
3488 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003489#endif
3490
3491#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003492 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003493#endif
3494
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003495 TRACE("[ Ready to execute '%s' @ %p ]", si->get_realpath(), reinterpret_cast<void*>(si->entry));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003496 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003497}
Nick Kralevich468319c2011-11-11 15:53:17 -08003498
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02003499/* Compute the load-bias of an existing executable. This shall only
3500 * be used to compute the load bias of an executable or shared library
3501 * that was loaded by the kernel itself.
3502 *
3503 * Input:
3504 * elf -> address of ELF header, assumed to be at the start of the file.
3505 * Return:
3506 * load bias, i.e. add the value of any p_vaddr in the file to get
3507 * the corresponding address in memory.
3508 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08003509static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
3510 ElfW(Addr) offset = elf->e_phoff;
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003511 const ElfW(Phdr)* phdr_table =
3512 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003513 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02003514
Elliott Hughes0266ae52014-02-10 17:46:57 -08003515 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08003516 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08003517 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02003518 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08003519 }
3520 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02003521}
3522
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07003523extern "C" void _start();
3524
Nick Kralevich468319c2011-11-11 15:53:17 -08003525/*
3526 * This is the entry point for the linker, called from begin.S. This
3527 * method is responsible for fixing the linker's own relocations, and
3528 * then calling __linker_init_post_relocation().
3529 *
3530 * Because this method is called before the linker has fixed it's own
3531 * relocations, any attempt to reference an extern variable, extern
3532 * function, or other GOT reference will generate a segfault.
3533 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08003534extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003535 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08003536
Elliott Hughes0266ae52014-02-10 17:46:57 -08003537 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07003538 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003539 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08003540 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08003541
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003542 soinfo linker_so(nullptr, nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08003543
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07003544 // If the linker is not acting as PT_INTERP entry_point is equal to
3545 // _start. Which means that the linker is running as an executable and
3546 // already linked by PT_INTERP.
3547 //
3548 // This happens when user tries to run 'adb shell /system/bin/linker'
3549 // see also https://code.google.com/p/android/issues/detail?id=63174
3550 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
3551 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
3552 }
3553
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003554 linker_so.base = linker_addr;
3555 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
3556 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003557 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003558 linker_so.phdr = phdr;
3559 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003560 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07003561
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003562 // This might not be obvious... The reasons why we pass g_empty_list
3563 // in place of local_group here are (1) we do not really need it, because
3564 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
3565 // itself without having to look into local_group and (2) allocators
3566 // are not yet initialized, and therefore we cannot use linked_list.push_*
3567 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003568 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003569 // It would be nice to print an error message, but if the linker
3570 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08003571 // call write() (because it involves a GOT reference). We may as
3572 // well try though...
3573 const char* msg = "CANNOT LINK EXECUTABLE: ";
3574 write(2, msg, strlen(msg));
3575 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
3576 write(2, "\n", 1);
3577 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003578 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07003579
Elliott Hughesd2948632015-07-21 11:57:09 -07003580 __libc_init_main_thread(args);
Dmitriy Ivanov14241402014-08-26 14:16:52 -07003581
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07003582 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003583 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07003584
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003585 // Initialize static variables. Note that in order to
3586 // get correct libdl_info we need to call constructors
3587 // before get_libdl_info().
3588 solist = get_libdl_info();
3589 sonext = get_libdl_info();
3590
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003591 // We have successfully fixed our own relocations. It's safe to run
3592 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07003593 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08003594 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003595
Elliott Hughes611f9562015-01-23 10:43:58 -08003596 INFO("[ jumping to _start ]");
3597
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003598 // Return the address that the calling assembly stub should jump to.
3599 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08003600}