blob: 3e59218b837d715b187a430a22f736b041013246 [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"
tony.ys_liub4474402015-07-29 18:00:22 +080061
62#include "base/strings.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000063#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
Elliott Hughes1801db32015-06-08 18:04:00 -070065extern void __libc_init_AT_SECURE(KernelArgumentBlock&);
66
67// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080068#undef ELF_ST_TYPE
69#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
70
Elliott Hughes0266ae52014-02-10 17:46:57 -080071static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070073static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
74static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020075
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070076static soinfo* solist;
77static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -070078static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079
Elliott Hughes1728b232014-05-14 10:02:03 -070080static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070081#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -070082 "/vendor/lib64",
83 "/system/lib64",
84#else
Elliott Hughes124fae92012-10-31 14:20:03 -070085 "/vendor/lib",
86 "/system/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -070087#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -070088 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -070089};
David Bartleybc3a5c22009-06-02 18:27:28 -070090
Evgenii Stepanovd640b222015-07-10 17:54:01 -070091static const char* const kAsanDefaultLdPaths[] = {
92#if defined(__LP64__)
93 "/data/vendor/lib64",
94 "/vendor/lib64",
95 "/data/lib64",
96 "/system/lib64",
97#else
98 "/data/vendor/lib",
99 "/vendor/lib",
100 "/data/lib",
101 "/system/lib",
102#endif
103 nullptr
104};
105
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700106static const ElfW(Versym) kVersymNotNeeded = 0;
107static const ElfW(Versym) kVersymGlobal = 1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -0700108static const char* const kZipFileSeparator = "!/";
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700109
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700110static const char* const* g_default_ld_paths;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700111static std::vector<std::string> g_ld_library_paths;
112static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800113
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700114static std::vector<soinfo*> g_ld_preloads;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600115
Elliott Hughes1728b232014-05-14 10:02:03 -0700116__LIBC_HIDDEN__ int g_ld_debug_verbosity;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700118__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Elliott Hughes0d787c12013-04-04 13:46:46 -0700119
Evgenii Stepanov68650822015-06-10 13:38:39 -0700120static std::string dirname(const char *path) {
121 const char* last_slash = strrchr(path, '/');
122 if (last_slash == path) return "/";
123 else if (last_slash == nullptr) return ".";
124 else
125 return std::string(path, last_slash - path);
126}
127
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700129struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700130 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700131};
132
133static linker_stats_t linker_stats;
134
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800135void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700136 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700137}
138#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800139void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700140}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141#endif
142
143#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800144uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145#endif
146
Dima Zavin2e855792009-05-20 18:28:09 -0700147static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700148
Elliott Hughes650be4e2013-03-05 18:47:58 -0800149char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700150 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700151}
152
Elliott Hughes650be4e2013-03-05 18:47:58 -0800153size_t linker_get_error_buffer_size() {
154 return sizeof(__linker_dl_err_buf);
155}
156
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700157// This function is an empty stub where GDB locates a breakpoint to get notified
158// about linker activity.
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700159extern "C"
160void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161
Elliott Hughes1728b232014-05-14 10:02:03 -0700162static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700163static r_debug _r_debug =
164 {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
165
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800166static link_map* r_debug_tail = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800168static void insert_soinfo_into_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700169 // Copy the necessary fields into the debug structure.
170 link_map* map = &(info->link_map_head);
171 map->l_addr = info->load_bias;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700172 // link_map l_name field is not const.
173 map->l_name = const_cast<char*>(info->get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700174 map->l_ld = info->dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700176 // Stick the new library at the end of the list.
177 // gdb tends to care more about libc than it does
178 // about leaf libraries, and ordering it this way
179 // reduces the back-and-forth over the wire.
180 if (r_debug_tail) {
181 r_debug_tail->l_next = map;
182 map->l_prev = r_debug_tail;
183 map->l_next = 0;
184 } else {
185 _r_debug.r_map = map;
186 map->l_prev = 0;
187 map->l_next = 0;
188 }
189 r_debug_tail = map;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190}
191
Elliott Hughesbedfe382012-08-14 14:07:59 -0700192static void remove_soinfo_from_debug_map(soinfo* info) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700193 link_map* map = &(info->link_map_head);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700194
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700195 if (r_debug_tail == map) {
196 r_debug_tail = map->l_prev;
197 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700198
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700199 if (map->l_prev) {
200 map->l_prev->l_next = map->l_next;
201 }
202 if (map->l_next) {
203 map->l_next->l_prev = map->l_prev;
204 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700205}
206
Elliott Hughesbedfe382012-08-14 14:07:59 -0700207static void notify_gdb_of_load(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800208 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700209 // GDB already knows about the main executable
210 return;
211 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700213 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700215 _r_debug.r_state = r_debug::RT_ADD;
216 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700218 insert_soinfo_into_debug_map(info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700220 _r_debug.r_state = r_debug::RT_CONSISTENT;
221 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700222}
223
Elliott Hughesbedfe382012-08-14 14:07:59 -0700224static void notify_gdb_of_unload(soinfo* info) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800225 if (info->is_main_executable()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700226 // GDB already knows about the main executable
227 return;
228 }
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700229
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700230 ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700231
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700232 _r_debug.r_state = r_debug::RT_DELETE;
233 rtld_db_dlactivity();
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700234
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700235 remove_soinfo_from_debug_map(info);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700236
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700237 _r_debug.r_state = r_debug::RT_CONSISTENT;
238 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239}
240
Elliott Hughes18a206c2012-10-29 17:37:13 -0700241void notify_gdb_of_libraries() {
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800242 _r_debug.r_state = r_debug::RT_ADD;
243 rtld_db_dlactivity();
244 _r_debug.r_state = r_debug::RT_CONSISTENT;
245 rtld_db_dlactivity();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246}
247
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700248LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
249 return g_soinfo_links_allocator.alloc();
250}
251
252void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
253 g_soinfo_links_allocator.free(entry);
254}
255
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700256static soinfo* soinfo_alloc(const char* name, struct stat* file_stat,
257 off64_t file_offset, uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700258 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200259 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700260 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200261 }
262
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700263 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700264
Magnus Malmbornba98d922012-09-12 13:00:55 +0200265 sonext->next = si;
266 sonext = si;
267
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700268 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200269 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800270}
271
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800272static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700273 if (si == nullptr) {
274 return;
275 }
276
277 if (si->base != 0 && si->size != 0) {
278 munmap(reinterpret_cast<void*>(si->base), si->size);
279 }
280
281 soinfo *prev = nullptr, *trav;
282
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700283 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700284
285 for (trav = solist; trav != nullptr; trav = trav->next) {
286 if (trav == si) {
287 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700288 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700289 prev = trav;
290 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800291
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700292 if (trav == nullptr) {
293 // si was not in solist
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700294 DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700295 return;
296 }
Elliott Hughes46882792012-08-03 16:49:39 -0700297
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700298 // clear links to/from si
299 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700300
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700301 // prev will never be null, because the first entry in solist is
302 // always the static libdl_info.
303 prev->next = si->next;
304 if (si == sonext) {
305 sonext = prev;
306 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800307
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700308 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700309 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800310}
311
Elliott Hughescade4c32012-12-20 14:42:14 -0800312static void parse_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700313 std::vector<std::string>* paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700314 paths->clear();
tony.ys_liub4474402015-07-29 18:00:22 +0800315 if (path != nullptr) {
316 *paths = android::base::Split(path, delimiters);
Elliott Hughescade4c32012-12-20 14:42:14 -0800317 }
318}
319
320static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700321 parse_path(path, ":", &g_ld_library_paths);
Elliott Hughescade4c32012-12-20 14:42:14 -0800322}
323
Evgenii Stepanov68650822015-06-10 13:38:39 -0700324void soinfo::set_dt_runpath(const char* path) {
325 if (!has_min_version(2)) return;
326 parse_path(path, ":", &dt_runpath_);
327
328 std::string origin = dirname(get_realpath());
329 // FIXME: add $LIB and $PLATFORM.
330 std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}};
331 for (std::string& s : dt_runpath_) {
332 size_t pos = 0;
333 while (pos < s.size()) {
334 pos = s.find("$", pos);
335 if (pos == std::string::npos) break;
336 for (const auto& subst : substs) {
337 const std::string& token = subst.first;
338 const std::string& replacement = subst.second;
339 if (s.substr(pos + 1, token.size()) == token) {
340 s.replace(pos, token.size() + 1, replacement);
341 // -1 to compensate for the ++pos below.
342 pos += replacement.size() - 1;
343 break;
344 } else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") {
345 s.replace(pos, token.size() + 3, replacement);
346 pos += replacement.size() - 1;
347 break;
348 }
349 }
350 // Skip $ in case it did not match any of the known substitutions.
351 ++pos;
352 }
353 }
354}
355
Elliott Hughescade4c32012-12-20 14:42:14 -0800356static void parse_LD_PRELOAD(const char* path) {
357 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700358 parse_path(path, " :", &g_ld_preload_names);
Elliott Hughescade4c32012-12-20 14:42:14 -0800359}
360
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700361static bool realpath_fd(int fd, std::string* realpath) {
362 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
363 snprintf(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
364 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -0700365 PRINT("readlink('%s') failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700366 return false;
367 }
368
369 *realpath = std::string(&buf[0]);
370 return true;
371}
372
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700373#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700374
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700375// For a given PC, find the .so that it belongs to.
376// Returns the base address of the .ARM.exidx section
377// for that .so, and the number of 8-byte entries
378// in that section (via *pcount).
379//
380// Intended to be called by libc's __gnu_Unwind_Find_exidx().
381//
382// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800383_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800384 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700386 for (soinfo* si = solist; si != 0; si = si->next) {
387 if ((addr >= si->base) && (addr < (si->base + si->size))) {
388 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800389 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800390 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700391 }
392 *pcount = 0;
393 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800394}
Elliott Hughes46882792012-08-03 16:49:39 -0700395
Christopher Ferris24053a42013-08-19 17:45:09 -0700396#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700397
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700398// Here, we only have to provide a callback to iterate across all the
399// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700400int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700401 int rv = 0;
402 for (soinfo* si = solist; si != nullptr; si = si->next) {
403 dl_phdr_info dl_info;
404 dl_info.dlpi_addr = si->link_map_head.l_addr;
405 dl_info.dlpi_name = si->link_map_head.l_name;
406 dl_info.dlpi_phdr = si->phdr;
407 dl_info.dlpi_phnum = si->phnum;
408 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
409 if (rv != 0) {
410 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700412 }
413 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800414}
Elliott Hughes46882792012-08-03 16:49:39 -0700415
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700416const ElfW(Versym)* soinfo::get_versym(size_t n) const {
417 if (has_min_version(2) && versym_ != nullptr) {
418 return versym_ + n;
419 }
420
421 return nullptr;
422}
423
424ElfW(Addr) soinfo::get_verneed_ptr() const {
425 if (has_min_version(2)) {
426 return verneed_ptr_;
427 }
428
429 return 0;
430}
431
432size_t soinfo::get_verneed_cnt() const {
433 if (has_min_version(2)) {
434 return verneed_cnt_;
435 }
436
437 return 0;
438}
439
440ElfW(Addr) soinfo::get_verdef_ptr() const {
441 if (has_min_version(2)) {
442 return verdef_ptr_;
443 }
444
445 return 0;
446}
447
448size_t soinfo::get_verdef_cnt() const {
449 if (has_min_version(2)) {
450 return verdef_cnt_;
451 }
452
453 return 0;
454}
455
456template<typename F>
457static bool for_each_verdef(const soinfo* si, F functor) {
458 if (!si->has_min_version(2)) {
459 return true;
460 }
461
462 uintptr_t verdef_ptr = si->get_verdef_ptr();
463 if (verdef_ptr == 0) {
464 return true;
465 }
466
467 size_t offset = 0;
468
469 size_t verdef_cnt = si->get_verdef_cnt();
470 for (size_t i = 0; i<verdef_cnt; ++i) {
471 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
472 size_t verdaux_offset = offset + verdef->vd_aux;
473 offset += verdef->vd_next;
474
475 if (verdef->vd_version != 1) {
Dmitriy Ivanov3d7bea12015-04-20 17:40:39 -0700476 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700477 i, verdef->vd_version, si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700478 return false;
479 }
480
481 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
482 // "this is the version of the file itself. It must not be used for
483 // matching a symbol. It can be used to match references."
484 //
485 // http://www.akkadia.org/drepper/symbol-versioning
486 continue;
487 }
488
489 if (verdef->vd_cnt == 0) {
490 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
491 return false;
492 }
493
494 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
495
496 if (functor(i, verdef, verdaux) == true) {
497 break;
498 }
499 }
500
501 return true;
502}
503
504bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const {
505 if (vi == nullptr) {
506 *versym = kVersymNotNeeded;
507 return true;
508 }
509
510 *versym = kVersymGlobal;
511
512 return for_each_verdef(this,
513 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
514 if (verdef->vd_hash == vi->elf_hash &&
515 strcmp(vi->name, get_string(verdaux->vda_name)) == 0) {
516 *versym = verdef->vd_ndx;
517 return true;
518 }
519
520 return false;
521 }
522 );
523}
524
525bool soinfo::find_symbol_by_name(SymbolName& symbol_name,
526 const version_info* vi,
527 const ElfW(Sym)** symbol) const {
528 uint32_t symbol_index;
529 bool success =
530 is_gnu_hash() ?
531 gnu_lookup(symbol_name, vi, &symbol_index) :
532 elf_lookup(symbol_name, vi, &symbol_index);
533
534 if (success) {
535 *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index;
536 }
537
538 return success;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800539}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800540
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800541static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
542 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
543 ELF_ST_BIND(s->st_info) == STB_WEAK) {
544 return s->st_shndx != SHN_UNDEF;
545 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
546 DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700547 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800548 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800549
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800550 return false;
551}
552
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700553static const ElfW(Versym) kVersymHiddenBit = 0x8000;
554
555static inline bool is_versym_hidden(const ElfW(Versym)* versym) {
556 // the symbol is hidden if bit 15 of versym is set.
557 return versym != nullptr && (*versym & kVersymHiddenBit) != 0;
558}
559
560static inline bool check_symbol_version(const ElfW(Versym) verneed,
561 const ElfW(Versym)* verdef) {
562 return verneed == kVersymNotNeeded ||
563 verdef == nullptr ||
564 verneed == (*verdef & ~kVersymHiddenBit);
565}
566
567bool soinfo::gnu_lookup(SymbolName& symbol_name,
568 const version_info* vi,
569 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800570 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800571 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800572
573 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800574 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
575 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800576
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700577 *symbol_index = 0;
578
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700579 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700580 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700581
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800582 // test against bloom filter
583 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700584 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700585 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700586
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700587 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800588 }
589
590 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700591 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800592
593 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700594 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700595 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700596
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700597 return true;
598 }
599
600 // lookup versym for the version definition in this library
601 // note the difference between "version is not requested" (vi == nullptr)
602 // and "version not found". In the first case verneed is kVersymNotNeeded
603 // which implies that the default version can be accepted; the second case results in
604 // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols
605 // for this library and consider only *global* ones.
606 ElfW(Versym) verneed = 0;
607 if (!find_verdef_version_index(vi, &verneed)) {
608 return false;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800609 }
610
611 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800612 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700613 const ElfW(Versym)* verdef = get_versym(n);
614 // skip hidden versions when verneed == kVersymNotNeeded (0)
615 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
616 continue;
617 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700618 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700619 check_symbol_version(verneed, verdef) &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800620 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
621 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700622 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700623 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700624 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700625 *symbol_index = n;
626 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800627 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700628 } while ((gnu_chain_[n++] & 1) == 0);
629
630 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700631 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800632
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700633 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800634}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800635
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700636bool soinfo::elf_lookup(SymbolName& symbol_name,
637 const version_info* vi,
638 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800639 uint32_t hash = symbol_name.elf_hash();
640
641 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700642 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700643 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800644
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700645 ElfW(Versym) verneed = 0;
646 if (!find_verdef_version_index(vi, &verneed)) {
647 return false;
648 }
649
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800650 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
651 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700652 const ElfW(Versym)* verdef = get_versym(n);
653
654 // skip hidden versions when verneed == 0
655 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
656 continue;
657 }
658
659 if (check_symbol_version(verneed, verdef) &&
660 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700661 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800662 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700663 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700664 reinterpret_cast<void*>(s->st_value),
665 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700666 *symbol_index = n;
667 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800668 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800669 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800670
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700671 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700672 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700673 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700674
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700675 *symbol_index = 0;
676 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800677}
678
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700679soinfo::soinfo(const char* realpath, const struct stat* file_stat,
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700680 off64_t file_offset, int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700681 memset(this, 0, sizeof(*this));
682
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700683 if (realpath != nullptr) {
684 realpath_ = realpath;
685 }
686
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800687 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800688 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700689
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700690 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800691 this->st_dev_ = file_stat->st_dev;
692 this->st_ino_ = file_stat->st_ino;
693 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700694 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700695
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800696 this->rtld_flags_ = rtld_flags;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700697}
698
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800699
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800700uint32_t SymbolName::elf_hash() {
701 if (!has_elf_hash_) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700702 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800703 uint32_t h = 0, g;
704
705 while (*name) {
706 h = (h << 4) + *name++;
707 g = h & 0xf0000000;
708 h ^= g;
709 h ^= g >> 24;
710 }
711
712 elf_hash_ = h;
713 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700714 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800715
716 return elf_hash_;
717}
718
719uint32_t SymbolName::gnu_hash() {
720 if (!has_gnu_hash_) {
721 uint32_t h = 5381;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700722 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800723 while (*name != 0) {
724 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
725 }
726
727 gnu_hash_ = h;
728 has_gnu_hash_ = true;
729 }
730
731 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800732}
733
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700734bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
735 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
736 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800737 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700738 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700739
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700740 /* "This element's presence in a shared object library alters the dynamic linker's
741 * symbol resolution algorithm for references within the library. Instead of starting
742 * a symbol search with the executable file, the dynamic linker starts from the shared
743 * object itself. If the shared object fails to supply the referenced symbol, the
744 * dynamic linker then searches the executable file and other shared objects as usual."
745 *
746 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
747 *
748 * Note that this is unlikely since static linker avoids generating
749 * relocations for -Bsymbolic linked dynamic executables.
750 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700751 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700752 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700753 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
754 return false;
755 }
756
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700757 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700758 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700759 }
760 }
761
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700762 // 1. Look for it in global_group
763 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700764 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700765 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700766 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700767 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700768 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
769 error = true;
770 return false;
771 }
772
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700773 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700774 *si_found_in = global_si;
775 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700776 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700777
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700778 return true;
779 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700780
781 if (error) {
782 return false;
783 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700784 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700785
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700786 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700787 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700788 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700789 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700790 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700791 // we already did this - skip
792 return true;
793 }
794
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700795 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700796 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700797 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
798 error = true;
799 return false;
800 }
801
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700802 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700803 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700804 return false;
805 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700806
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700807 return true;
808 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700809
810 if (error) {
811 return false;
812 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700813 }
814
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700815 if (s != nullptr) {
816 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
817 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700818 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
819 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700820 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700821 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700822
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700823 *symbol = s;
824 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700825}
826
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800827class ProtectedDataGuard {
828 public:
829 ProtectedDataGuard() {
830 if (ref_count_++ == 0) {
831 protect_data(PROT_READ | PROT_WRITE);
832 }
833 }
834
835 ~ProtectedDataGuard() {
836 if (ref_count_ == 0) { // overflow
837 __libc_fatal("Too many nested calls to dlopen()");
838 }
839
840 if (--ref_count_ == 0) {
841 protect_data(PROT_READ);
842 }
843 }
844 private:
845 void protect_data(int protection) {
846 g_soinfo_allocator.protect_all(protection);
847 g_soinfo_links_allocator.protect_all(protection);
848 }
849
850 static size_t ref_count_;
851};
852
853size_t ProtectedDataGuard::ref_count_ = 0;
854
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700855// Each size has it's own allocator.
856template<size_t size>
857class SizeBasedAllocator {
858 public:
859 static void* alloc() {
860 return allocator_.alloc();
861 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700862
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700863 static void free(void* ptr) {
864 allocator_.free(ptr);
865 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700866
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700867 private:
868 static LinkerBlockAllocator allocator_;
869};
870
871template<size_t size>
872LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
873
874template<typename T>
875class TypeBasedAllocator {
876 public:
877 static T* alloc() {
878 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
879 }
880
881 static void free(T* ptr) {
882 SizeBasedAllocator<sizeof(T)>::free(ptr);
883 }
884};
885
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700886class LoadTask {
887 public:
888 struct deleter_t {
889 void operator()(LoadTask* t) {
890 TypeBasedAllocator<LoadTask>::free(t);
891 }
892 };
893
894 typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
895
896 static deleter_t deleter;
897
898 static LoadTask* create(const char* name, soinfo* needed_by) {
899 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
900 return new (ptr) LoadTask(name, needed_by);
901 }
902
903 const char* get_name() const {
904 return name_;
905 }
906
907 soinfo* get_needed_by() const {
908 return needed_by_;
909 }
910 private:
911 LoadTask(const char* name, soinfo* needed_by)
912 : name_(name), needed_by_(needed_by) {}
913
914 const char* name_;
915 soinfo* needed_by_;
916
917 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
918};
919
Ningsheng Jiane93be992014-09-16 15:22:10 +0800920LoadTask::deleter_t LoadTask::deleter;
921
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700922template <typename T>
923using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
924
925typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700926typedef linked_list_t<const char> StringLinkedList;
927typedef linked_list_t<LoadTask> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700928
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700929
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700930// This function walks down the tree of soinfo dependencies
931// in breadth-first order and
932// * calls action(soinfo* si) for each node, and
933// * terminates walk if action returns false.
934//
935// walk_dependencies_tree returns false if walk was terminated
936// by the action and true otherwise.
937template<typename F>
938static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700939 SoinfoLinkedList visit_list;
940 SoinfoLinkedList visited;
941
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700942 for (size_t i = 0; i < root_soinfos_size; ++i) {
943 visit_list.push_back(root_soinfos[i]);
944 }
945
946 soinfo* si;
947 while ((si = visit_list.pop_front()) != nullptr) {
948 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700949 continue;
950 }
951
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700952 if (!action(si)) {
953 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700954 }
955
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700956 visited.push_back(si);
957
958 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700959 visit_list.push_back(child);
960 });
961 }
962
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700963 return true;
964}
965
966
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700967static const ElfW(Sym)* dlsym_handle_lookup(soinfo* root, soinfo* skip_until,
968 soinfo** found, SymbolName& symbol_name) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700969 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700970 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700971
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700972 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
973 if (skip_lookup) {
974 skip_lookup = current_soinfo != skip_until;
975 return true;
976 }
977
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700978 if (!current_soinfo->find_symbol_by_name(symbol_name, nullptr, &result)) {
979 result = nullptr;
980 return false;
981 }
982
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700983 if (result != nullptr) {
984 *found = current_soinfo;
985 return false;
986 }
987
988 return true;
989 });
990
991 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800992}
993
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700994// This is used by dlsym(3). It performs symbol lookup only within the
995// specified soinfo object and its dependencies in breadth first order.
996const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700997 // According to man dlopen(3) and posix docs in the case when si is handle
998 // of the main executable we need to search not only in the executable and its
999 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
1000 //
1001 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
1002 // libraries and they are loaded in breath-first (correct) order we can just execute
1003 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
1004 if (si == somain) {
1005 return dlsym_linear_lookup(name, found, nullptr, RTLD_DEFAULT);
1006 }
1007
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001008 SymbolName symbol_name(name);
1009 return dlsym_handle_lookup(si, nullptr, found, symbol_name);
1010}
1011
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001012/* This is used by dlsym(3) to performs a global symbol lookup. If the
1013 start value is null (for RTLD_DEFAULT), the search starts at the
1014 beginning of the global solist. Otherwise the search starts at the
1015 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001016 */
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001017const ElfW(Sym)* dlsym_linear_lookup(const char* name,
1018 soinfo** found,
1019 soinfo* caller,
1020 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001021 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001022
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001023 soinfo* start = solist;
1024
1025 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -07001026 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001027 return nullptr;
1028 } else {
1029 start = caller->next;
1030 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001031 }
1032
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001033 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001034 for (soinfo* si = start; si != nullptr; si = si->next) {
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001035 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
1036 // if the library is opened by application with target api level <= 22
1037 // See http://b/21565766
1038 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() > 22) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001039 continue;
1040 }
1041
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001042 if (!si->find_symbol_by_name(symbol_name, nullptr, &s)) {
1043 return nullptr;
1044 }
1045
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001046 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -08001047 *found = si;
1048 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -06001049 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001050 }
Matt Fischer1698d9e2009-12-31 12:17:56 -06001051
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001052 // If not found - use dlsym_handle_lookup for caller's
1053 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001054 // case we already did it.
1055 if (s == nullptr && caller != nullptr &&
1056 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001057 return dlsym_handle_lookup(caller->get_local_group_root(),
1058 (handle == RTLD_NEXT) ? caller : nullptr, found, symbol_name);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001059 }
1060
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001061 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001062 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
1063 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -08001064 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001065
Elliott Hughescade4c32012-12-20 14:42:14 -08001066 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001067}
1068
Kito Chengfa8c05d2013-03-12 14:58:06 +08001069soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001070 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001071 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001072 if (address >= si->base && address - si->base < si->size) {
1073 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001074 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001075 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001076 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001077}
1078
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001079ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
1080 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
1081}
1082
1083static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
1084 return sym->st_shndx != SHN_UNDEF &&
1085 soaddr >= sym->st_value &&
1086 soaddr < sym->st_value + sym->st_size;
1087}
1088
1089ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001090 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001091
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001092 for (size_t i = 0; i < gnu_nbucket_; ++i) {
1093 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001094
1095 if (n == 0) {
1096 continue;
1097 }
1098
1099 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001100 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001101 if (symbol_matches_soaddr(sym, soaddr)) {
1102 return sym;
1103 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001104 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001105 }
1106
1107 return nullptr;
1108}
1109
1110ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001111 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001112
Kito Chengfa8c05d2013-03-12 14:58:06 +08001113 // Search the library's symbol table for any defined symbol which
1114 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001115 for (size_t i = 0; i < nchain_; ++i) {
1116 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001117 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001118 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001119 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001120 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001121
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001122 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001123}
1124
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001125class ZipArchiveCache {
1126 public:
1127 ZipArchiveCache() {}
1128 ~ZipArchiveCache();
1129
1130 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
1131 private:
1132 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
1133
1134 std::unordered_map<std::string, ZipArchiveHandle> cache_;
1135};
1136
1137bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
1138 std::string key(zip_path);
1139
1140 auto it = cache_.find(key);
1141 if (it != cache_.end()) {
1142 *handle = it->second;
1143 return true;
1144 }
1145
1146 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1147 if (fd == -1) {
1148 return false;
1149 }
1150
1151 if (OpenArchiveFd(fd, "", handle) != 0) {
1152 // invalid zip-file (?)
1153 close(fd);
1154 return false;
1155 }
1156
1157 cache_[key] = *handle;
1158 return true;
1159}
1160
1161ZipArchiveCache::~ZipArchiveCache() {
1162 for (auto it : cache_) {
1163 CloseArchive(it.second);
1164 }
1165}
1166
1167static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
1168 const char* const path,
Simon Baldwinaef71952015-01-16 13:22:54 +00001169 off64_t* file_offset) {
1170 TRACE("Trying zip file open from path '%s'", path);
1171
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001172 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001173 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001174 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001175 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001176 const char* separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001177 if (separator == nullptr) {
1178 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001179 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001180
1181 char buf[512];
1182 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1183 PRINT("Warning: ignoring very long library path: %s", path);
1184 return -1;
1185 }
1186
1187 buf[separator - path] = '\0';
1188
1189 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001190 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001191 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1192 if (fd == -1) {
1193 return -1;
1194 }
1195
1196 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001197 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001198 // invalid zip-file (?)
1199 close(fd);
1200 return -1;
1201 }
1202
Simon Baldwinaef71952015-01-16 13:22:54 +00001203 ZipEntry entry;
1204
Yusuke Sato56f40fb2015-06-25 14:56:07 -07001205 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001206 // Entry was not found.
1207 close(fd);
1208 return -1;
1209 }
1210
1211 // Check if it is properly stored
1212 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1213 close(fd);
1214 return -1;
1215 }
1216
1217 *file_offset = entry.offset;
1218 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001219}
1220
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001221static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1222 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1223 if (n < 0 || n >= static_cast<int>(buf_size)) {
1224 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1225 return false;
1226 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001227
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001228 return true;
1229}
1230
1231static int open_library_on_default_path(const char* name, off64_t* file_offset) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001232 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001233 char buf[512];
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001234 if (!format_path(buf, sizeof(buf), g_default_ld_paths[i], name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001235 continue;
Simon Baldwinaef71952015-01-16 13:22:54 +00001236 }
1237
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001238 int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1239 if (fd != -1) {
1240 *file_offset = 0;
1241 return fd;
1242 }
1243 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001244
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001245 return -1;
1246}
1247
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001248static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1249 const char* name, off64_t* file_offset,
Evgenii Stepanov68650822015-06-10 13:38:39 -07001250 const std::vector<std::string>& paths) {
1251 for (const auto& path_str : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001252 char buf[512];
1253 const char* const path = path_str.c_str();
1254 if (!format_path(buf, sizeof(buf), path, name)) {
1255 continue;
1256 }
1257
1258 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001259 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001260 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset);
Simon Baldwinaef71952015-01-16 13:22:54 +00001261 }
1262
1263 if (fd == -1) {
1264 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1265 if (fd != -1) {
1266 *file_offset = 0;
1267 }
1268 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001269
1270 if (fd != -1) {
1271 return fd;
1272 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001273 }
1274
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001275 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001276}
1277
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001278static int open_library(ZipArchiveCache* zip_archive_cache,
1279 const char* name, soinfo *needed_by,
1280 off64_t* file_offset) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001281 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001282
Elliott Hughes124fae92012-10-31 14:20:03 -07001283 // 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 -07001284 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001285 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001286 int fd = open_library_in_zipfile(zip_archive_cache, name, file_offset);
Simon Baldwinaef71952015-01-16 13:22:54 +00001287 if (fd != -1) {
1288 return fd;
1289 }
1290 }
1291
Elliott Hughes6971fe42012-11-01 22:59:19 -07001292 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
1293 if (fd != -1) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001294 *file_offset = 0;
Elliott Hughes6971fe42012-11-01 22:59:19 -07001295 }
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001296 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001297 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001298
Elliott Hughes124fae92012-10-31 14:20:03 -07001299 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001300 int fd = open_library_on_paths(zip_archive_cache, name, file_offset, g_ld_library_paths);
Evgenii Stepanov68650822015-06-10 13:38:39 -07001301 if (fd == -1 && needed_by) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001302 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath());
Evgenii Stepanov68650822015-06-10 13:38:39 -07001303 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001304
Elliott Hughes124fae92012-10-31 14:20:03 -07001305 if (fd == -1) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001306 fd = open_library_on_default_path(name, file_offset);
Elliott Hughes124fae92012-10-31 14:20:03 -07001307 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001308
Elliott Hughes124fae92012-10-31 14:20:03 -07001309 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001310}
1311
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001312static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
1313#if !defined(__LP64__)
1314 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001315 if (get_application_target_sdk_version() <= 22) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001316 const char* bname = basename(dt_needed);
1317 if (bname != dt_needed) {
1318 DL_WARN("'%s' library has invalid DT_NEEDED entry '%s'", sopath, dt_needed);
1319 }
1320
1321 return bname;
1322 }
1323#endif
1324 return dt_needed;
1325}
1326
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001327template<typename F>
1328static void for_each_dt_needed(const soinfo* si, F action) {
1329 for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1330 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001331 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
Dima Zavin2e855792009-05-20 18:28:09 -07001332 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001333 }
1334}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001335
Spencer Low0346ad72015-04-22 18:06:51 -07001336static soinfo* load_library(int fd, off64_t file_offset,
1337 LoadTaskList& load_tasks,
Simon Baldwinaef71952015-01-16 13:22:54 +00001338 const char* name, int rtld_flags,
1339 const android_dlextinfo* extinfo) {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001340 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001341 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001342 return nullptr;
1343 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001344 if (file_offset < 0) {
1345 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
1346 return nullptr;
1347 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001348
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001349 struct stat file_stat;
1350 if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001351 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001352 return nullptr;
1353 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001354 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001355 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1356 name, file_offset, file_stat.st_size);
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001357 return nullptr;
1358 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001359
1360 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001361 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1362 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
1363 for (soinfo* si = solist; si != nullptr; si = si->next) {
1364 if (si->get_st_dev() != 0 &&
1365 si->get_st_ino() != 0 &&
1366 si->get_st_dev() == file_stat.st_dev &&
1367 si->get_st_ino() == file_stat.st_ino &&
1368 si->get_file_offset() == file_offset) {
1369 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001370 "will return existing soinfo", name, si->get_realpath());
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001371 return si;
1372 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001373 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001374 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001375
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001376 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001377 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001378 return nullptr;
1379 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001380
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001381 std::string realpath = name;
1382 if (!realpath_fd(fd, &realpath)) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -07001383 PRINT("warning: unable to get realpath for the library \"%s\". Will use given name.", name);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001384 realpath = name;
1385 }
1386
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001387 // Read the ELF header and load the segments.
Dmitriy Ivanov3f987f52015-06-25 15:51:41 -07001388 ElfReader elf_reader(realpath.c_str(), fd, file_offset, file_stat.st_size);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001389 if (!elf_reader.Load(extinfo)) {
1390 return nullptr;
1391 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001392
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001393 soinfo* si = soinfo_alloc(realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001394 if (si == nullptr) {
1395 return nullptr;
1396 }
1397 si->base = elf_reader.load_start();
1398 si->size = elf_reader.load_size();
1399 si->load_bias = elf_reader.load_bias();
1400 si->phnum = elf_reader.phdr_count();
1401 si->phdr = elf_reader.loaded_phdr();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001402
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001403 if (!si->prelink_image()) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001404 soinfo_free(si);
1405 return nullptr;
1406 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001407
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001408 for_each_dt_needed(si, [&] (const char* name) {
1409 load_tasks.push_back(LoadTask::create(name, si));
1410 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001411
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001412 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001413}
1414
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001415static soinfo* load_library(ZipArchiveCache* zip_archive_cache,
1416 LoadTaskList& load_tasks, const char* name,
Evgenii Stepanov68650822015-06-10 13:38:39 -07001417 soinfo* needed_by, int rtld_flags,
Spencer Low0346ad72015-04-22 18:06:51 -07001418 const android_dlextinfo* extinfo) {
1419 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
1420 off64_t file_offset = 0;
1421 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1422 file_offset = extinfo->library_fd_offset;
1423 }
1424 return load_library(extinfo->library_fd, file_offset, load_tasks, name, rtld_flags, extinfo);
1425 }
1426
1427 // Open the file.
1428 off64_t file_offset;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001429 int fd = open_library(zip_archive_cache, name, needed_by, &file_offset);
Spencer Low0346ad72015-04-22 18:06:51 -07001430 if (fd == -1) {
1431 DL_ERR("library \"%s\" not found", name);
1432 return nullptr;
1433 }
1434 soinfo* result = load_library(fd, file_offset, load_tasks, name, rtld_flags, extinfo);
1435 close(fd);
1436 return result;
1437}
1438
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001439// Returns true if library was found and false in 2 cases
1440// 1. The library was found but loaded under different target_sdk_version
1441// (*candidate != nullptr)
1442// 2. The library was not found by soname (*candidate is nullptr)
1443static bool find_loaded_library_by_soname(const char* name, soinfo** candidate) {
1444 *candidate = nullptr;
1445
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001446 // Ignore filename with path.
1447 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001448 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001449 }
1450
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001451 uint32_t target_sdk_version = get_application_target_sdk_version();
1452
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001453 for (soinfo* si = solist; si != nullptr; si = si->next) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001454 const char* soname = si->get_soname();
1455 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001456 // If the library was opened under different target sdk version
1457 // skip this step and try to reopen it. The exceptions are
1458 // "libdl.so" and global group. There is no point in skipping
1459 // them because relocation process is going to use them
1460 // in any case.
1461 bool is_libdl = si == solist;
1462 if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 ||
1463 !si->is_linked() || si->get_target_sdk_version() == target_sdk_version) {
1464 *candidate = si;
1465 return true;
1466 } else if (*candidate == nullptr) {
1467 // for the different sdk version - remember the first library.
1468 *candidate = si;
1469 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001470 }
Dmitriy Ivanov489e4982014-05-19 15:19:52 -07001471 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001472
1473 return false;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001474}
1475
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001476static soinfo* find_library_internal(ZipArchiveCache* zip_archive_cache,
1477 LoadTaskList& load_tasks, const char* name,
Evgenii Stepanov68650822015-06-10 13:38:39 -07001478 soinfo* needed_by, int rtld_flags,
1479 const android_dlextinfo* extinfo) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001480 soinfo* candidate;
1481
1482 if (find_loaded_library_by_soname(name, &candidate)) {
1483 return candidate;
1484 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001485
1486 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001487 // of this fact is done by load_library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001488 TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]",
1489 name, candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
1490
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001491 soinfo* si = load_library(zip_archive_cache, load_tasks, name, needed_by, rtld_flags, extinfo);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001492
1493 // In case we were unable to load the library but there
1494 // is a candidate loaded under the same soname but different
1495 // sdk level - return it anyways.
1496 if (si == nullptr && candidate != nullptr) {
1497 si = candidate;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001498 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001499
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001500 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001501}
1502
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001503static void soinfo_unload(soinfo* si);
1504
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001505// TODO: this is slightly unusual way to construct
1506// the global group for relocation. Not every RTLD_GLOBAL
1507// library is included in this group for backwards-compatibility
1508// reasons.
1509//
1510// This group consists of the main executable, LD_PRELOADs
1511// and libraries with the DF_1_GLOBAL flag set.
1512static soinfo::soinfo_list_t make_global_group() {
1513 soinfo::soinfo_list_t global_group;
1514 for (soinfo* si = somain; si != nullptr; si = si->next) {
1515 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1516 global_group.push_back(si);
1517 }
1518 }
1519
1520 return global_group;
1521}
1522
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001523// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1524// not their transitive dependencies) as children of the start_with library.
1525// This is false when find_libraries is called for dlopen(), when newly loaded
1526// libraries must form a disjoint tree.
1527static bool find_libraries(soinfo* start_with,
1528 const char* const library_names[],
1529 size_t library_names_count, soinfo* soinfos[],
1530 std::vector<soinfo*>* ld_preloads,
1531 size_t ld_preloads_count, int rtld_flags,
1532 const android_dlextinfo* extinfo,
1533 bool add_as_children) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001534 // Step 0: prepare.
1535 LoadTaskList load_tasks;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001536 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001537 const char* name = library_names[i];
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001538 load_tasks.push_back(LoadTask::create(name, start_with));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001539 }
1540
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001541 // Construct global_group.
1542 soinfo::soinfo_list_t global_group = make_global_group();
1543
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001544 // If soinfos array is null allocate one on stack.
1545 // The array is needed in case of failure; for example
1546 // when library_names[] = {libone.so, libtwo.so} and libone.so
1547 // is loaded correctly but libtwo.so failed for some reason.
1548 // In this case libone.so should be unloaded on return.
1549 // See also implementation of failure_guard below.
1550
1551 if (soinfos == nullptr) {
1552 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1553 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1554 memset(soinfos, 0, soinfos_size);
1555 }
1556
1557 // list of libraries to link - see step 2.
1558 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001559
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001560 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001561 // Housekeeping
1562 load_tasks.for_each([] (LoadTask* t) {
1563 LoadTask::deleter(t);
1564 });
1565
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001566 for (size_t i = 0; i<soinfos_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001567 soinfo_unload(soinfos[i]);
1568 }
1569 });
1570
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001571 ZipArchiveCache zip_archive_cache;
1572
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001573 // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001574 for (LoadTask::unique_ptr task(load_tasks.pop_front());
1575 task.get() != nullptr; task.reset(load_tasks.pop_front())) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07001576 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001577 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
1578
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001579 soinfo* si = find_library_internal(&zip_archive_cache, load_tasks,
1580 task->get_name(), needed_by, rtld_flags,
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001581 is_dt_needed ? nullptr : extinfo);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001582
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001583 if (si == nullptr) {
1584 return false;
1585 }
1586
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001587 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001588 needed_by->add_child(si);
1589 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001590
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001591 if (si->is_linked()) {
1592 si->increment_ref_count();
1593 }
1594
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001595 // When ld_preloads is not null, the first
1596 // ld_preloads_count libs are in fact ld_preloads.
1597 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001598 // Add LD_PRELOADed libraries to the global group for future runs.
1599 // There is no need to explicitly add them to the global group
1600 // for this run because they are going to appear in the local
1601 // group in the correct order.
1602 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001603 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001604 }
1605
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001606 if (soinfos_count < library_names_count) {
1607 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001608 }
1609 }
1610
1611 // Step 2: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001612 soinfo::soinfo_list_t local_group;
1613 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001614 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
1615 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001616 [&] (soinfo* si) {
1617 local_group.push_back(si);
1618 return true;
1619 });
1620
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001621 // We need to increment ref_count in case
1622 // the root of the local group was not linked.
1623 bool was_local_group_root_linked = local_group.front()->is_linked();
1624
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001625 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001626 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001627 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001628 return false;
1629 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001630 si->set_linked();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001631 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001632
1633 return true;
1634 });
1635
1636 if (linked) {
1637 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001638 }
1639
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001640 if (!was_local_group_root_linked) {
1641 local_group.front()->increment_ref_count();
1642 }
1643
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001644 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001645}
1646
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001647static soinfo* find_library(const char* name, int rtld_flags,
1648 const android_dlextinfo* extinfo,
1649 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001650 soinfo* si;
1651
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001652 if (name == nullptr) {
1653 si = somain;
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001654 } else if (!find_libraries(needed_by, &name, 1, &si, nullptr, 0, rtld_flags,
1655 extinfo, /* add_as_children */ false)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001656 return nullptr;
1657 }
1658
Elliott Hughesd23736e2012-11-01 15:16:56 -07001659 return si;
1660}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001661
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001662static void soinfo_unload(soinfo* root) {
1663 // Note that the library can be loaded but not linked;
1664 // in which case there is no root but we still need
1665 // to walk the tree and unload soinfos involved.
1666 //
1667 // This happens on unsuccessful dlopen, when one of
1668 // the DT_NEEDED libraries could not be linked/found.
1669 if (root->is_linked()) {
1670 root = root->get_local_group_root();
1671 }
1672
1673 if (!root->can_unload()) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001674 TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001675 return;
1676 }
1677
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001678 size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001679
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001680 if (ref_count == 0) {
1681 soinfo::soinfo_list_t local_unload_list;
1682 soinfo::soinfo_list_t external_unload_list;
1683 soinfo::soinfo_list_t depth_first_list;
1684 depth_first_list.push_back(root);
1685 soinfo* si = nullptr;
1686
1687 while ((si = depth_first_list.pop_front()) != nullptr) {
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001688 if (local_unload_list.contains(si)) {
1689 continue;
1690 }
1691
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001692 local_unload_list.push_back(si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001693
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001694 if (si->has_min_version(0)) {
1695 soinfo* child = nullptr;
1696 while ((child = si->get_children().pop_front()) != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001697 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
1698 child->get_realpath(), child);
1699
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001700 if (local_unload_list.contains(child)) {
1701 continue;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001702 } else if (child->is_linked() && child->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001703 external_unload_list.push_back(child);
1704 } else {
1705 depth_first_list.push_front(child);
1706 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001707 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001708 } else {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07001709#if !defined(__work_around_b_24465209__)
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001710 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001711#else
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001712 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001713 for_each_dt_needed(si, [&] (const char* library_name) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001714 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001715 si->get_realpath(), library_name);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001716
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001717 soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr, nullptr);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001718 if (needed != nullptr) {
1719 // Not found: for example if symlink was deleted between dlopen and dlclose
1720 // Since we cannot really handle errors at this point - print and continue.
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001721 PRINT("warning: couldn't find %s needed by %s on unload.",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001722 library_name, si->get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001723 return;
1724 } else if (local_unload_list.contains(needed)) {
1725 // already visited
1726 return;
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001727 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001728 // external group
1729 external_unload_list.push_back(needed);
1730 } else {
1731 // local group
1732 depth_first_list.push_front(needed);
1733 }
1734 });
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001735#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001736 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001737 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001738
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001739 local_unload_list.for_each([](soinfo* si) {
1740 si->call_destructors();
1741 });
1742
1743 while ((si = local_unload_list.pop_front()) != nullptr) {
1744 notify_gdb_of_unload(si);
1745 soinfo_free(si);
1746 }
1747
1748 while ((si = external_unload_list.pop_front()) != nullptr) {
1749 soinfo_unload(si);
1750 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001751 } else {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001752 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
1753 root->get_realpath(), ref_count);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001754 }
1755}
1756
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001757void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001758 // Use basic string manipulation calls to avoid snprintf.
1759 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1760 // When debug malloc is enabled, this call returns 0. This in turn causes
1761 // snprintf to do nothing, which causes libraries to fail to load.
1762 // See b/17302493 for further details.
1763 // Once the above bug is fixed, this code can be modified to use
1764 // snprintf again.
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001765 size_t required_len = 0;
1766 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
1767 required_len += strlen(g_default_ld_paths[i]) + 1;
1768 }
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001769 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001770 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
1771 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001772 }
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001773 char* end = buffer;
1774 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
1775 if (i > 0) *end++ = ':';
1776 end = stpcpy(end, g_default_ld_paths[i]);
1777 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001778}
1779
Elliott Hughescade4c32012-12-20 14:42:14 -08001780void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001781 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001782}
1783
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001784soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo, soinfo *caller) {
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001785 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001786 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001787 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001788 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001789 if (extinfo != nullptr) {
1790 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1791 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1792 return nullptr;
1793 }
1794 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001795 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001796 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
1797 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001798 return nullptr;
1799 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001800 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001801
1802 ProtectedDataGuard guard;
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001803 soinfo* si = find_library(name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001804 if (si != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001805 si->call_constructors();
Elliott Hughesd23736e2012-11-01 15:16:56 -07001806 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001807 return si;
1808}
1809
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001810void do_dlclose(soinfo* si) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001811 ProtectedDataGuard guard;
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001812 soinfo_unload(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001813}
1814
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001815static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
1816 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
1817 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
1818 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001819 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
1820 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001821
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07001822 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001823}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001824
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001825const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
1826 if (source_symver < 2 ||
1827 source_symver >= version_infos.size() ||
1828 version_infos[source_symver].name == nullptr) {
1829 return nullptr;
1830 }
1831
1832 return &version_infos[source_symver];
1833}
1834
1835void VersionTracker::add_version_info(size_t source_index,
1836 ElfW(Word) elf_hash,
1837 const char* ver_name,
1838 const soinfo* target_si) {
1839 if (source_index >= version_infos.size()) {
1840 version_infos.resize(source_index+1);
1841 }
1842
1843 version_infos[source_index].elf_hash = elf_hash;
1844 version_infos[source_index].name = ver_name;
1845 version_infos[source_index].target_si = target_si;
1846}
1847
1848bool VersionTracker::init_verneed(const soinfo* si_from) {
1849 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
1850
1851 if (verneed_ptr == 0) {
1852 return true;
1853 }
1854
1855 size_t verneed_cnt = si_from->get_verneed_cnt();
1856
1857 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
1858 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
1859 size_t vernaux_offset = offset + verneed->vn_aux;
1860 offset += verneed->vn_next;
1861
1862 if (verneed->vn_version != 1) {
1863 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
1864 return false;
1865 }
1866
1867 const char* target_soname = si_from->get_string(verneed->vn_file);
1868 // find it in dependencies
1869 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07001870 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001871 });
1872
1873 if (target_si == nullptr) {
1874 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001875 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001876 return false;
1877 }
1878
1879 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
1880 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
1881 vernaux_offset += vernaux->vna_next;
1882
1883 const ElfW(Word) elf_hash = vernaux->vna_hash;
1884 const char* ver_name = si_from->get_string(vernaux->vna_name);
1885 ElfW(Half) source_index = vernaux->vna_other;
1886
1887 add_version_info(source_index, elf_hash, ver_name, target_si);
1888 }
1889 }
1890
1891 return true;
1892}
1893
1894bool VersionTracker::init_verdef(const soinfo* si_from) {
1895 return for_each_verdef(si_from,
1896 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
1897 add_version_info(verdef->vd_ndx, verdef->vd_hash,
1898 si_from->get_string(verdaux->vda_name), si_from);
1899 return false;
1900 }
1901 );
1902}
1903
1904bool VersionTracker::init(const soinfo* si_from) {
1905 if (!si_from->has_min_version(2)) {
1906 return true;
1907 }
1908
1909 return init_verneed(si_from) && init_verdef(si_from);
1910}
1911
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07001912bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
1913 const char* sym_name, const version_info** vi) {
1914 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
1915 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
1916
1917 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
1918 *vi = version_tracker.get_version_info(sym_ver);
1919
1920 if (*vi == nullptr) {
1921 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001922 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07001923 return false;
1924 }
1925 } else {
1926 // there is no version info
1927 *vi = nullptr;
1928 }
1929
1930 return true;
1931}
1932
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001933#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07001934#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001935static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
1936 return rela->r_addend;
1937}
1938#else
1939static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001940 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
1941 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001942 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
1943 }
1944 return 0;
1945}
1946#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001947
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001948template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07001949bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
1950 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08001951 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
1952 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001953 if (rel == nullptr) {
1954 return false;
1955 }
1956
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001957 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
1958 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
1959
1960 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08001961 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001962 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08001963 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001964
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001965 DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08001966 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001967 continue;
1968 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001969
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001970 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001971 soinfo* lsi = nullptr;
1972
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001973 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001974 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07001975 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001976
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07001977 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
1978 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001979 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07001980
1981 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
1982 return false;
1983 }
1984
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001985 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001986 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001987 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001988 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001989 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001990 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001991 }
1992
1993 /* IHI0044C AAELF 4.5.1.1:
1994
1995 Libraries are not searched to resolve weak references.
1996 It is not an error for a weak reference to remain unsatisfied.
1997
1998 During linking, the value of an undefined weak reference is:
1999 - Zero if the relocation type is absolute
2000 - The address of the place if the relocation is pc-relative
2001 - The address of nominal base address if the relocation
2002 type is base-relative.
2003 */
2004
2005 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002006 case R_GENERIC_JUMP_SLOT:
2007 case R_GENERIC_GLOB_DAT:
2008 case R_GENERIC_RELATIVE:
2009 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002010#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002011 case R_AARCH64_ABS64:
2012 case R_AARCH64_ABS32:
2013 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002014#elif defined(__x86_64__)
2015 case R_X86_64_32:
2016 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002017#elif defined(__arm__)
2018 case R_ARM_ABS32:
2019#elif defined(__i386__)
2020 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002021#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002022 /*
2023 * The sym_addr was initialized to be zero above, or the relocation
2024 * code below does not care about value of sym_addr.
2025 * No need to do anything.
2026 */
2027 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002028#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002029 case R_X86_64_PC32:
2030 sym_addr = reloc;
2031 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002032#elif defined(__i386__)
2033 case R_386_PC32:
2034 sym_addr = reloc;
2035 break;
2036#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002037 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002038 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002039 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002040 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002041 } else { // We got a definition.
2042#if !defined(__LP64__)
2043 // When relocating dso with text_relocation .text segment is
2044 // not executable. We need to restore elf flags before resolving
2045 // STT_GNU_IFUNC symbol.
2046 bool protect_segments = has_text_relocations &&
2047 lsi == this &&
2048 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2049 if (protect_segments) {
2050 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2051 DL_ERR("can't protect segments for \"%s\": %s",
2052 get_realpath(), strerror(errno));
2053 return false;
2054 }
2055 }
2056#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002057 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002058#if !defined(__LP64__)
2059 if (protect_segments) {
2060 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2061 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2062 get_realpath(), strerror(errno));
2063 return false;
2064 }
2065 }
2066#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002067 }
2068 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002069 }
2070
2071 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002072 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002073 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002074 MARK(rel->r_offset);
2075 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2076 reinterpret_cast<void*>(reloc),
2077 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2078
2079 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002080 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002081 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002082 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002083 MARK(rel->r_offset);
2084 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2085 reinterpret_cast<void*>(reloc),
2086 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2087 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002088 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002089 case R_GENERIC_RELATIVE:
2090 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002091 MARK(rel->r_offset);
2092 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2093 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002094 reinterpret_cast<void*>(load_bias + addend));
2095 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002096 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002097 case R_GENERIC_IRELATIVE:
2098 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002099 MARK(rel->r_offset);
2100 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2101 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002102 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002103 {
2104#if !defined(__LP64__)
2105 // When relocating dso with text_relocation .text segment is
2106 // not executable. We need to restore elf flags for this
2107 // particular call.
2108 if (has_text_relocations) {
2109 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2110 DL_ERR("can't protect segments for \"%s\": %s",
2111 get_realpath(), strerror(errno));
2112 return false;
2113 }
2114 }
2115#endif
2116 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2117#if !defined(__LP64__)
2118 // Unprotect it afterwards...
2119 if (has_text_relocations) {
2120 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2121 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2122 get_realpath(), strerror(errno));
2123 return false;
2124 }
2125 }
2126#endif
2127 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2128 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002129 break;
2130
2131#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002132 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002133 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002134 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002135 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002136 reloc, (sym_addr + addend), sym_name);
2137 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002138 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002139 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002140 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002141 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002142 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002143 reloc, (sym_addr + addend), sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002144 {
2145 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
2146 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2147 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
2148 if ((min_value <= (reloc_value + (sym_addr + addend))) &&
2149 ((reloc_value + (sym_addr + addend)) <= max_value)) {
2150 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
2151 } else {
2152 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
2153 (reloc_value + (sym_addr + addend)), min_value, max_value);
2154 return false;
2155 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002156 }
2157 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002158 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002159 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002160 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002161 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002162 reloc, (sym_addr + addend), sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002163 {
2164 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
2165 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2166 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
2167 if ((min_value <= (reloc_value + (sym_addr + addend))) &&
2168 ((reloc_value + (sym_addr + addend)) <= max_value)) {
2169 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend);
2170 } else {
2171 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
2172 reloc_value + (sym_addr + addend), min_value, max_value);
2173 return false;
2174 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002175 }
2176 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002177 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002178 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002179 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002180 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002181 reloc, (sym_addr + addend), rel->r_offset, sym_name);
2182 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend) - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002183 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002184 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002185 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002186 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002187 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002188 reloc, (sym_addr + addend), rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002189 {
2190 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
2191 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2192 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
2193 if ((min_value <= (reloc_value + ((sym_addr + addend) - rel->r_offset))) &&
2194 ((reloc_value + ((sym_addr + addend) - rel->r_offset)) <= max_value)) {
2195 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
2196 } else {
2197 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
2198 reloc_value + ((sym_addr + addend) - rel->r_offset), min_value, max_value);
2199 return false;
2200 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002201 }
2202 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002203 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002204 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002205 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002206 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002207 reloc, (sym_addr + addend), rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002208 {
2209 const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc);
2210 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2211 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
2212 if ((min_value <= (reloc_value + ((sym_addr + addend) - rel->r_offset))) &&
2213 ((reloc_value + ((sym_addr + addend) - rel->r_offset)) <= max_value)) {
2214 *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset);
2215 } else {
2216 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
2217 reloc_value + ((sym_addr + addend) - rel->r_offset), min_value, max_value);
2218 return false;
2219 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002220 }
2221 break;
2222
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002223 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002224 /*
2225 * ET_EXEC is not supported so this should not happen.
2226 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002227 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002228 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002229 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002230 * R_AARCH64_COPY may only appear in executable objects where e_type is
2231 * set to ET_EXEC.
2232 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002233 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002234 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002235 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002236 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002237 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002238 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002239 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002240 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002241 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002242 break;
2243#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002244 case R_X86_64_32:
2245 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002246 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002247 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2248 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002249 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002250 break;
2251 case R_X86_64_64:
2252 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002253 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002254 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2255 static_cast<size_t>(sym_addr), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002256 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002257 break;
2258 case R_X86_64_PC32:
2259 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002260 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002261 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2262 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2263 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002264 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002265 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002266#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002267 case R_ARM_ABS32:
2268 count_relocation(kRelocAbsolute);
2269 MARK(rel->r_offset);
2270 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2271 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2272 break;
2273 case R_ARM_REL32:
2274 count_relocation(kRelocRelative);
2275 MARK(rel->r_offset);
2276 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2277 reloc, sym_addr, rel->r_offset, sym_name);
2278 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2279 break;
2280 case R_ARM_COPY:
2281 /*
2282 * ET_EXEC is not supported so this should not happen.
2283 *
2284 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2285 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002286 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002287 * R_ARM_COPY may only appear in executable objects where e_type is
2288 * set to ET_EXEC.
2289 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002290 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002291 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002292#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002293 case R_386_32:
2294 count_relocation(kRelocRelative);
2295 MARK(rel->r_offset);
2296 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2297 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2298 break;
2299 case R_386_PC32:
2300 count_relocation(kRelocRelative);
2301 MARK(rel->r_offset);
2302 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2303 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2304 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2305 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002306#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002307 default:
2308 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002309 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002310 }
2311 }
2312 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002313}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002314#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002315
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002316void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
2317 size_t count, bool reverse) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002318 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002319 return;
2320 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002321
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002322 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath());
Elliott Hughesca0c11b2013-03-12 10:40:45 -07002323
2324 int begin = reverse ? (count - 1) : 0;
2325 int end = reverse ? -1 : count;
2326 int step = reverse ? -1 : 1;
2327
2328 for (int i = begin; i != end; i += step) {
2329 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002330 call_function("function", functions[i]);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002331 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02002332
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002333 TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002334}
2335
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002336void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002337 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002338 return;
2339 }
2340
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002341 TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07002342 function();
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002343 TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath());
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04002344}
2345
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002346void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002347 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
2348 // but ignored in a shared library.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002349 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002350}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002351
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002352void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07002353 if (constructors_called) {
2354 return;
2355 }
Jesse Hallf5d16932012-01-30 15:39:57 -08002356
Elliott Hughesd23736e2012-11-01 15:16:56 -07002357 // We set constructors_called before actually calling the constructors, otherwise it doesn't
2358 // protect against recursive constructor calls. One simple example of constructor recursion
2359 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
2360 // 1. The program depends on libc, so libc's constructor is called here.
2361 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
2362 // 3. dlopen() calls the constructors on the newly created
2363 // soinfo for libc_malloc_debug_leak.so.
2364 // 4. The debug .so depends on libc, so CallConstructors is
2365 // called again with the libc soinfo. If it doesn't trigger the early-
2366 // out above, the libc constructor will be called again (recursively!).
2367 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002368
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002369 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002370 // The GNU dynamic linker silently ignores these, but we warn the developer.
Elliott Hughesc6200592013-09-30 18:43:46 -07002371 PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002372 get_realpath(), preinit_array_count_);
Elliott Hughesd23736e2012-11-01 15:16:56 -07002373 }
2374
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002375 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002376 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002377 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002378
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002379 TRACE("\"%s\": calling constructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002380
2381 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002382 call_function("DT_INIT", init_func_);
2383 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04002384}
David 'Digit' Turner82156792009-05-18 14:37:41 +02002385
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002386void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002387 if (!constructors_called) {
2388 return;
2389 }
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002390 TRACE("\"%s\": calling destructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002391
2392 // DT_FINI_ARRAY must be parsed in reverse order.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002393 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
Elliott Hughes8147d3c2013-05-09 14:19:58 -07002394
2395 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002396 call_function("DT_FINI", fini_func_);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002397
2398 // This is needed on second call to dlopen
2399 // after library has been unloaded with RTLD_NODELETE
2400 constructors_called = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002401}
2402
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002403void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002404 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002405 child->parents_.push_back(this);
2406 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002407 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002408}
2409
2410void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002411 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002412 return;
2413 }
2414
2415 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002416 children_.for_each([&] (soinfo* child) {
2417 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002418 return parent == this;
2419 });
2420 });
2421
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002422 parents_.for_each([&] (soinfo* parent) {
2423 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002424 return child == this;
2425 });
2426 });
2427
2428 // 2. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002429 parents_.clear();
2430 children_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002431}
2432
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002433dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002434 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002435 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002436 }
2437
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002438 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002439};
2440
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002441ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002442 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002443 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002444 }
2445
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002446 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002447}
2448
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002449off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002450 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002451 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002452 }
2453
2454 return 0;
2455}
2456
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002457uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07002458 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002459 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07002460 }
2461
2462 return 0;
2463}
2464
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002465uint32_t soinfo::get_dt_flags_1() const {
2466 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002467 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002468 }
2469
2470 return 0;
2471}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002472
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002473void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
2474 if (has_min_version(1)) {
2475 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002476 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002477 }
2478
2479 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002480 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002481 }
2482
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002483 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002484 }
2485}
2486
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002487const char* soinfo::get_realpath() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07002488#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002489 if (has_min_version(2)) {
2490 return realpath_.c_str();
2491 } else {
2492 return old_name_;
2493 }
2494#else
2495 return realpath_.c_str();
2496#endif
2497}
2498
2499const char* soinfo::get_soname() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07002500#if defined(__work_around_b_24465209__)
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002501 if (has_min_version(2)) {
2502 return soname_;
2503 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002504 return old_name_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002505 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002506#else
2507 return soname_;
2508#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002509}
2510
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002511// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002512// 'this->flags' does not have FLAG_NEW_SOINFO set.
2513static soinfo::soinfo_list_t g_empty_list;
2514
2515soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002516 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002517 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002518 }
2519
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07002520 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002521}
2522
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002523const soinfo::soinfo_list_t& soinfo::get_children() const {
2524 if (has_min_version(0)) {
2525 return children_;
2526 }
2527
2528 return g_empty_list;
2529}
2530
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002531soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002532 if (has_min_version(0)) {
2533 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002534 }
2535
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002536 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002537}
2538
Evgenii Stepanov68650822015-06-10 13:38:39 -07002539static std::vector<std::string> g_empty_runpath;
2540
2541const std::vector<std::string>& soinfo::get_dt_runpath() const {
2542 if (has_min_version(2)) {
2543 return dt_runpath_;
2544 }
2545
2546 return g_empty_runpath;
2547}
2548
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002549ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002550 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
2551 return call_ifunc_resolver(s->st_value + load_bias);
2552 }
2553
2554 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
2555}
2556
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002557const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002558 if (has_min_version(1) && (index >= strtab_size_)) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002559 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002560 get_realpath(), strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002561 }
2562
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002563 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002564}
2565
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002566bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002567 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002568}
2569
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002570bool soinfo::can_unload() const {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002571 return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002572}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002573
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002574bool soinfo::is_linked() const {
2575 return (flags_ & FLAG_LINKED) != 0;
2576}
2577
2578bool soinfo::is_main_executable() const {
2579 return (flags_ & FLAG_EXE) != 0;
2580}
2581
2582void soinfo::set_linked() {
2583 flags_ |= FLAG_LINKED;
2584}
2585
2586void soinfo::set_linker_flag() {
2587 flags_ |= FLAG_LINKER;
2588}
2589
2590void soinfo::set_main_executable() {
2591 flags_ |= FLAG_EXE;
2592}
2593
2594void soinfo::increment_ref_count() {
2595 local_group_root_->ref_count_++;
2596}
2597
2598size_t soinfo::decrement_ref_count() {
2599 return --local_group_root_->ref_count_;
2600}
2601
2602soinfo* soinfo::get_local_group_root() const {
2603 return local_group_root_;
2604}
2605
Dmitriy Ivanov19133522015-06-02 17:36:54 -07002606// This function returns api-level at the time of
2607// dlopen/load. Note that libraries opened by system
2608// will always have 'current' api level.
2609uint32_t soinfo::get_target_sdk_version() const {
2610 if (!has_min_version(2)) {
2611 return __ANDROID_API__;
2612 }
2613
2614 return local_group_root_->target_sdk_version_;
2615}
2616
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002617bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002618 /* Extract dynamic section */
2619 ElfW(Word) dynamic_flags = 0;
2620 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002621
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002622 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002623 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002624 if (!relocating_linker) {
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07002625 INFO("[ linking %s ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002626 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002627 }
2628
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002629 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002630 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002631 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002632 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002633 return false;
2634 } else {
2635 if (!relocating_linker) {
2636 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002637 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002638 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002639
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002640#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002641 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2642 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002643#endif
2644
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002645 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002646 // Note that: "Except for the DT_NULL element at the end of the array,
2647 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2648 //
2649 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002650 uint32_t needed_count = 0;
2651 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2652 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2653 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2654 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002655 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002656 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002657 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002658
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002659 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002660 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2661 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2662 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2663 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002664 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002665
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002666 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002667 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002668 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002669 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2670 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002671
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002672 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002673 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002674 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002675 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2676 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002677
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002678 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002679 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002680 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002681 return false;
2682 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002683 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002684
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002685 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002686 break;
2687
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002688 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002689 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002690 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002691
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002692 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002693 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002694 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002695
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002696 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002697 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002698 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002699
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002700 case DT_SYMENT:
2701 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002702 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2703 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002704 return false;
2705 }
2706 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002707
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002708 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002709#if defined(USE_RELA)
2710 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002711 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002712 return false;
2713 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002714#else
2715 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002716 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002717 return false;
2718 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002719#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002720 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002721
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002722 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002723#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002724 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002725#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002726 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002727#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002728 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002729
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002730 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002731#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002732 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002733#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002734 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002735#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002736 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002737
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002738 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002739#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002740 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002741 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002742#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002743 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2744 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002745
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002746 case DT_DEBUG:
2747 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2748 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002749// FIXME: not working currently for N64
2750// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002751// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002752// read-only, but the DYNAMIC header claims it is writable.
2753#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002754 if ((dynamic_flags & PF_W) != 0) {
2755 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2756 }
Chris Dearman99186652014-02-06 20:36:51 -08002757#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002758 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002759#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002760 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002761 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002762 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002763
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002764 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002765 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002766 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002767
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002768 case DT_ANDROID_RELA:
2769 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2770 break;
2771
2772 case DT_ANDROID_RELASZ:
2773 android_relocs_size_ = d->d_un.d_val;
2774 break;
2775
2776 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002777 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002778 return false;
2779
2780 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002781 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002782 return false;
2783
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002784 case DT_RELAENT:
2785 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002786 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002787 return false;
2788 }
2789 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002790
2791 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002792 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002793 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002794
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002795 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002796 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002797 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002798
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002799 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002800 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002801 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002802
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002803#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002804 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002805 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002806 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002807
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002808 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002809 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002810 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002811
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002812 case DT_RELENT:
2813 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002814 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002815 return false;
2816 }
2817 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002818
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002819 case DT_ANDROID_REL:
2820 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2821 break;
2822
2823 case DT_ANDROID_RELSZ:
2824 android_relocs_size_ = d->d_un.d_val;
2825 break;
2826
2827 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002828 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002829 return false;
2830
2831 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002832 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002833 return false;
2834
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002835 // "Indicates that all RELATIVE relocations have been concatenated together,
2836 // and specifies the RELATIVE relocation count."
2837 //
2838 // TODO: Spec also mentions that this can be used to optimize relocation process;
2839 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002840 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002841 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002842
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002843 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002844 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002845 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002846
2847 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002848 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002849 return false;
2850
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002851#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002852 case DT_INIT:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002853 init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002854 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002855 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002856
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002857 case DT_FINI:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002858 fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002859 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002860 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002861
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002862 case DT_INIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002863 init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002864 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002865 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002866
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002867 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002868 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002869 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002870
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002871 case DT_FINI_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002872 fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002873 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002874 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002875
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002876 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002877 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002878 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002879
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002880 case DT_PREINIT_ARRAY:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002881 preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002882 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002883 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002884
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002885 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002886 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
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_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002890#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002891 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002892 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002893#else
2894 has_text_relocations = true;
2895 break;
2896#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002897
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002898 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002899 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002900 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002901
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002902 case DT_NEEDED:
2903 ++needed_count;
2904 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002905
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002906 case DT_FLAGS:
2907 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002908#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002909 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002910 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00002911#else
2912 has_text_relocations = true;
2913#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002914 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07002915 if (d->d_un.d_val & DF_SYMBOLIC) {
2916 has_DT_SYMBOLIC = true;
2917 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002918 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002919
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002920 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002921 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002922
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002923 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -07002924 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 -07002925 }
2926 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002927#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002928 case DT_MIPS_RLD_MAP:
2929 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
2930 {
2931 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
2932 *dp = &_r_debug;
2933 }
2934 break;
Raghu Gandham68815722014-12-18 19:12:19 -08002935 case DT_MIPS_RLD_MAP2:
2936 // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
2937 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002938 r_debug** dp = reinterpret_cast<r_debug**>(
2939 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08002940 *dp = &_r_debug;
2941 }
2942 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002943
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002944 case DT_MIPS_RLD_VERSION:
2945 case DT_MIPS_FLAGS:
2946 case DT_MIPS_BASE_ADDRESS:
2947 case DT_MIPS_UNREFEXTNO:
2948 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002949
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002950 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002951 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002952 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002953
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002954 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002955 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002956 break;
2957
2958 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002959 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002960 break;
2961#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002962 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
2963 case DT_BIND_NOW:
2964 break;
2965
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002966 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002967 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
2968 break;
2969
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002970 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002971 verdef_ptr_ = load_bias + d->d_un.d_ptr;
2972 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002973 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002974 verdef_cnt_ = d->d_un.d_val;
2975 break;
2976
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002977 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002978 verneed_ptr_ = load_bias + d->d_un.d_ptr;
2979 break;
2980
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03002981 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002982 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002983 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002984
Evgenii Stepanov68650822015-06-10 13:38:39 -07002985 case DT_RUNPATH:
2986 // this is parsed after we have strtab initialized (see below).
2987 break;
2988
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002989 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002990 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002991 DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07002992 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2993 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002994 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08002995 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002996 }
2997
Duane Sandbc425c72015-06-01 16:29:14 -07002998#if defined(__mips__) && !defined(__LP64__)
2999 if (!mips_check_and_adjust_fp_modes()) {
3000 return false;
3001 }
3002#endif
3003
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003004 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003005 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003006
3007 // Sanity checks.
3008 if (relocating_linker && needed_count != 0) {
3009 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3010 return false;
3011 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003012 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003013 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003014 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003015 return false;
3016 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003017 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003018 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003019 return false;
3020 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003021 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003022 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003023 return false;
3024 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003025
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003026 // second pass - parse entries relying on strtab
3027 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003028 switch (d->d_tag) {
3029 case DT_SONAME:
3030 soname_ = get_string(d->d_un.d_val);
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003031#if defined(__work_around_b_24465209__)
Evgenii Stepanov68650822015-06-10 13:38:39 -07003032 strlcpy(old_name_, soname_, sizeof(old_name_));
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003033#endif
Evgenii Stepanov68650822015-06-10 13:38:39 -07003034 break;
3035 case DT_RUNPATH:
3036 // FIXME: $LIB, $PLATFORM unsupported.
3037 set_dt_runpath(get_string(d->d_un.d_val));
3038 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003039 }
3040 }
3041
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003042 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003043 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003044 // because they can't find dt_needed library by soname.
3045 // This workaround should keep them working. (applies only
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003046 // for apps targeting sdk version <=22). Make an exception for
3047 // the main executable and linker; they do not need to have dt_soname
3048 if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 &&
3049 get_application_target_sdk_version() <= 22) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003050 soname_ = basename(realpath_.c_str());
3051 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3052 get_realpath(), soname_);
3053 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003054 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003055}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003056
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003057bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3058 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003059
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003060 local_group_root_ = local_group.front();
3061 if (local_group_root_ == nullptr) {
3062 local_group_root_ = this;
3063 }
3064
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003065 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3066 target_sdk_version_ = get_application_target_sdk_version();
3067 }
3068
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003069 VersionTracker version_tracker;
3070
3071 if (!version_tracker.init(this)) {
3072 return false;
3073 }
3074
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003075#if !defined(__LP64__)
3076 if (has_text_relocations) {
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003077 // Fail if app is targeting sdk version > 22
3078 // TODO (dimitry): remove != __ANDROID_API__ check once http://b/20020312 is fixed
3079 if (get_application_target_sdk_version() != __ANDROID_API__
3080 && get_application_target_sdk_version() > 22) {
3081 DL_ERR("%s: has text relocations", get_realpath());
3082 return false;
3083 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003084 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003085 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003086 DL_WARN("%s has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003087 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003088 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3089 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003090 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003091 return false;
3092 }
3093 }
3094#endif
3095
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003096 if (android_relocs_ != nullptr) {
3097 // check signature
3098 if (android_relocs_size_ > 3 &&
3099 android_relocs_[0] == 'A' &&
3100 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003101 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003102 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003103 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003104
3105 bool relocated = false;
3106 const uint8_t* packed_relocs = android_relocs_ + 4;
3107 const size_t packed_relocs_size = android_relocs_size_ - 4;
3108
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003109 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003110 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003111 packed_reloc_iterator<sleb128_decoder>(
3112 sleb128_decoder(packed_relocs, packed_relocs_size)),
3113 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003114
3115 if (!relocated) {
3116 return false;
3117 }
3118 } else {
3119 DL_ERR("bad android relocation header.");
3120 return false;
3121 }
3122 }
3123
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003124#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003125 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003126 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003127 if (!relocate(version_tracker,
3128 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003129 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003130 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003131 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003132 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003133 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003134 if (!relocate(version_tracker,
3135 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003136 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003137 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003138 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003139#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003140 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003141 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003142 if (!relocate(version_tracker,
3143 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003144 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003145 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003146 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003147 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003148 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003149 if (!relocate(version_tracker,
3150 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003151 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003152 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003153 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003154#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003155
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003156#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003157 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003158 return false;
3159 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003160#endif
3161
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003162 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003163
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003164#if !defined(__LP64__)
3165 if (has_text_relocations) {
3166 // All relocations are done, we can protect our segments back to read-only.
3167 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3168 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003169 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003170 return false;
3171 }
3172 }
3173#endif
3174
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003175 /* We can also turn on GNU RELRO protection */
3176 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3177 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003178 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003179 return false;
3180 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003181
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003182 /* Handle serializing/sharing the RELRO segment */
3183 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3184 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3185 extinfo->relro_fd) < 0) {
3186 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003187 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003188 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003189 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003190 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3191 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3192 extinfo->relro_fd) < 0) {
3193 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003194 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003195 return false;
3196 }
3197 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003198
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003199 notify_gdb_of_load(this);
3200 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003201}
3202
Nick Kralevich468319c2011-11-11 15:53:17 -08003203/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003204 * This function add vdso to internal dso list.
3205 * It helps to stack unwinding through signal handlers.
3206 * Also, it makes bionic more like glibc.
3207 */
Kito Cheng812fd422014-03-25 22:53:56 +08003208static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003209#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08003210 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003211 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08003212 return;
3213 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003214
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003215 soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003216
Elliott Hughes0266ae52014-02-10 17:46:57 -08003217 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
3218 si->phnum = ehdr_vdso->e_phnum;
3219 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
3220 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003221 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04003222
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003223 si->prelink_image();
3224 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04003225#endif
3226}
3227
3228/*
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003229 * This is linker soinfo for GDB. See details below.
3230 */
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003231#if defined(__LP64__)
3232#define LINKER_PATH "/system/bin/linker64"
3233#else
3234#define LINKER_PATH "/system/bin/linker"
3235#endif
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003236
3237// This is done to avoid calling c-tor prematurely
3238// because soinfo c-tor needs memory allocator
3239// which might be initialized after global variables.
3240static uint8_t linker_soinfo_for_gdb_buf[sizeof(soinfo)] __attribute__((aligned(8)));
3241static soinfo* linker_soinfo_for_gdb = nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003242
3243/* gdb expects the linker to be in the debug shared object list.
3244 * Without this, gdb has trouble locating the linker's ".text"
3245 * and ".plt" sections. Gdb could also potentially use this to
3246 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
3247 * Don't use soinfo_alloc(), because the linker shouldn't
3248 * be on the soinfo list.
3249 */
3250static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003251 linker_soinfo_for_gdb = new (linker_soinfo_for_gdb_buf) soinfo(LINKER_PATH, nullptr, 0, 0);
3252
Dmitriy Ivanov175dae92015-06-10 19:46:19 -07003253 linker_soinfo_for_gdb->load_bias = linker_base;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003254
3255 /*
3256 * Set the dynamic field in the link map otherwise gdb will complain with
3257 * the following:
3258 * warning: .dynamic section for "/system/bin/linker" is not at the
3259 * expected address (wrong library or version mismatch?)
3260 */
3261 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
3262 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
3263 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003264 &linker_soinfo_for_gdb->dynamic, nullptr);
3265 insert_soinfo_into_debug_map(linker_soinfo_for_gdb);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003266}
3267
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003268static void init_default_ld_library_path() {
3269 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3270 somain->load_bias);
3271 const char* bname = basename(interp);
3272 if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0))
3273 g_default_ld_paths = kAsanDefaultLdPaths;
3274 else
3275 g_default_ld_paths = kDefaultLdPaths;
3276};
3277
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07003278extern "C" int __system_properties_init(void);
3279
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003280/*
Nick Kralevich468319c2011-11-11 15:53:17 -08003281 * This code is called after the linker has linked itself and
3282 * fixed it's own GOT. It is safe to make references to externs
3283 * and other non-local data at this point.
3284 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08003285static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003286#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003287 struct timeval t0, t1;
3288 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04003289#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003290
Elliott Hughes1801db32015-06-08 18:04:00 -07003291 // Sanitize the environment.
3292 __libc_init_AT_SECURE(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01003293
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07003294 // Initialize system properties
3295 __system_properties_init(); // may use 'environ'
3296
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003297 debuggerd_init();
3298
3299 // Get a few environment variables.
Elliott Hughes1801db32015-06-08 18:04:00 -07003300 const char* LD_DEBUG = getenv("LD_DEBUG");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003301 if (LD_DEBUG != nullptr) {
3302 g_ld_debug_verbosity = atoi(LD_DEBUG);
3303 }
3304
Elliott Hughes1801db32015-06-08 18:04:00 -07003305 // These should have been sanitized by __libc_init_AT_SECURE, but the test
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003306 // doesn't cost us anything.
3307 const char* ldpath_env = nullptr;
3308 const char* ldpreload_env = nullptr;
Elliott Hughes1801db32015-06-08 18:04:00 -07003309 if (!getauxval(AT_SECURE)) {
3310 ldpath_env = getenv("LD_LIBRARY_PATH");
3311 ldpreload_env = getenv("LD_PRELOAD");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003312 }
3313
3314 INFO("[ android linker & debugger ]");
3315
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003316 soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003317 if (si == nullptr) {
3318 exit(EXIT_FAILURE);
3319 }
3320
3321 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003322 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003323 link_map* map = &(si->link_map_head);
3324
3325 map->l_addr = 0;
3326 map->l_name = args.argv[0];
3327 map->l_prev = nullptr;
3328 map->l_next = nullptr;
3329
3330 _r_debug.r_map = map;
3331 r_debug_tail = map;
3332
3333 init_linker_info_for_gdb(linker_base);
3334
3335 // Extract information passed from the kernel.
3336 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
3337 si->phnum = args.getauxval(AT_PHNUM);
3338 si->entry = args.getauxval(AT_ENTRY);
3339
3340 /* Compute the value of si->base. We can't rely on the fact that
3341 * the first entry is the PHDR because this will not be true
3342 * for certain executables (e.g. some in the NDK unit test suite)
3343 */
3344 si->base = 0;
3345 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
3346 si->load_bias = 0;
3347 for (size_t i = 0; i < si->phnum; ++i) {
3348 if (si->phdr[i].p_type == PT_PHDR) {
3349 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
3350 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
3351 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07003352 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003353 }
3354 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07003355
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003356 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
3357 if (elf_hdr->e_type != ET_DYN) {
3358 __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
3359 exit(EXIT_FAILURE);
3360 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003361
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003362 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
3363 parse_LD_LIBRARY_PATH(ldpath_env);
3364 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01003365
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003366 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003367
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003368 init_default_ld_library_path();
3369
Dmitriy Ivanov67181252015-01-07 15:48:25 -08003370 if (!si->prelink_image()) {
3371 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
3372 exit(EXIT_FAILURE);
3373 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003374
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003375 // add somain to global group
3376 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
3377
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003378 // Load ld_preloads and dependencies.
3379 StringLinkedList needed_library_name_list;
3380 size_t needed_libraries_count = 0;
3381 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07003382
3383 for (const auto& ld_preload_name : g_ld_preload_names) {
3384 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003385 ++needed_libraries_count;
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07003386 ++ld_preloads_count;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003387 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003388
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003389 for_each_dt_needed(si, [&](const char* name) {
3390 needed_library_name_list.push_back(name);
3391 ++needed_libraries_count;
3392 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003393
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003394 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003395
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003396 memset(needed_library_names, 0, sizeof(needed_library_names));
3397 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003398
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07003399 if (needed_libraries_count > 0 &&
3400 !find_libraries(si, needed_library_names, needed_libraries_count, nullptr,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07003401 &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr,
3402 /* add_as_children */ true)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003403 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
3404 exit(EXIT_FAILURE);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003405 } else if (needed_libraries_count == 0) {
3406 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
3407 __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
3408 exit(EXIT_FAILURE);
3409 }
3410 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003411 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003412
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003413 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07003414
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08003415 {
3416 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06003417
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08003418 si->call_pre_init_constructors();
3419
3420 /* After the prelink_image, the si->load_bias is initialized.
3421 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
3422 * We need to update this value for so exe here. So Unwind_Backtrace
3423 * for some arch like x86 could work correctly within so exe.
3424 */
3425 map->l_addr = si->load_bias;
3426 si->call_constructors();
3427 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003428
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003429#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003430 gettimeofday(&t1, nullptr);
3431 PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
3432 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
3433 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003434#endif
3435#if STATS
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003436 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
3437 linker_stats.count[kRelocAbsolute],
3438 linker_stats.count[kRelocRelative],
3439 linker_stats.count[kRelocCopy],
3440 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003441#endif
3442#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003443 {
3444 unsigned n;
3445 unsigned i;
3446 unsigned count = 0;
3447 for (n = 0; n < 4096; n++) {
3448 if (bitmask[n]) {
3449 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003450#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003451 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003452#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003453 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003454#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003455 if (x & 1) {
3456 count++;
3457 }
3458 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003459 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003460 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003461 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003462 PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
3463 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003464#endif
3465
3466#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003467 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003468#endif
3469
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003470 TRACE("[ Ready to execute '%s' @ %p ]", si->get_realpath(), reinterpret_cast<void*>(si->entry));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003471 return si->entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003472}
Nick Kralevich468319c2011-11-11 15:53:17 -08003473
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02003474/* Compute the load-bias of an existing executable. This shall only
3475 * be used to compute the load bias of an executable or shared library
3476 * that was loaded by the kernel itself.
3477 *
3478 * Input:
3479 * elf -> address of ELF header, assumed to be at the start of the file.
3480 * Return:
3481 * load bias, i.e. add the value of any p_vaddr in the file to get
3482 * the corresponding address in memory.
3483 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08003484static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
3485 ElfW(Addr) offset = elf->e_phoff;
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003486 const ElfW(Phdr)* phdr_table =
3487 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003488 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02003489
Elliott Hughes0266ae52014-02-10 17:46:57 -08003490 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08003491 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08003492 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02003493 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08003494 }
3495 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02003496}
3497
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07003498extern "C" void _start();
3499
Nick Kralevich468319c2011-11-11 15:53:17 -08003500/*
3501 * This is the entry point for the linker, called from begin.S. This
3502 * method is responsible for fixing the linker's own relocations, and
3503 * then calling __linker_init_post_relocation().
3504 *
3505 * Because this method is called before the linker has fixed it's own
3506 * relocations, any attempt to reference an extern variable, extern
3507 * function, or other GOT reference will generate a segfault.
3508 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08003509extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003510 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08003511
Elliott Hughes0266ae52014-02-10 17:46:57 -08003512 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07003513 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003514 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08003515 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08003516
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003517 soinfo linker_so(nullptr, nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08003518
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07003519 // If the linker is not acting as PT_INTERP entry_point is equal to
3520 // _start. Which means that the linker is running as an executable and
3521 // already linked by PT_INTERP.
3522 //
3523 // This happens when user tries to run 'adb shell /system/bin/linker'
3524 // see also https://code.google.com/p/android/issues/detail?id=63174
3525 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
3526 __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
3527 }
3528
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003529 linker_so.base = linker_addr;
3530 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
3531 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003532 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003533 linker_so.phdr = phdr;
3534 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003535 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07003536
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003537 // This might not be obvious... The reasons why we pass g_empty_list
3538 // in place of local_group here are (1) we do not really need it, because
3539 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
3540 // itself without having to look into local_group and (2) allocators
3541 // are not yet initialized, and therefore we cannot use linked_list.push_*
3542 // functions at this point.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003543 if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003544 // It would be nice to print an error message, but if the linker
3545 // can't link itself, there's no guarantee that we'll be able to
Elliott Hughesb93702a2013-12-21 16:07:45 -08003546 // call write() (because it involves a GOT reference). We may as
3547 // well try though...
3548 const char* msg = "CANNOT LINK EXECUTABLE: ";
3549 write(2, msg, strlen(msg));
3550 write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
3551 write(2, "\n", 1);
3552 _exit(EXIT_FAILURE);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003553 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07003554
Elliott Hughesd2948632015-07-21 11:57:09 -07003555 __libc_init_main_thread(args);
Dmitriy Ivanov14241402014-08-26 14:16:52 -07003556
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07003557 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003558 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07003559
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003560 // Initialize static variables. Note that in order to
3561 // get correct libdl_info we need to call constructors
3562 // before get_libdl_info().
3563 solist = get_libdl_info();
3564 sonext = get_libdl_info();
3565
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003566 // We have successfully fixed our own relocations. It's safe to run
3567 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07003568 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08003569 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003570
Elliott Hughes611f9562015-01-23 10:43:58 -08003571 INFO("[ jumping to _start ]");
3572
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08003573 // Return the address that the calling assembly stub should jump to.
3574 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08003575}