blob: 442ecd909784dd98f9558d5e5b26dc7bc0dfc999 [file] [log] [blame]
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 * 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
29#include "linker_main.h"
30
Ryan Prichard701bd0c2018-11-21 16:23:03 -080031#include <link.h>
32#include <sys/auxv.h>
33
Dimitry Ivanov3f660572016-09-09 10:00:39 -070034#include "linker_debug.h"
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070035#include "linker_cfi.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070036#include "linker_gdb_support.h"
37#include "linker_globals.h"
38#include "linker_phdr.h"
39#include "linker_utils.h"
40
41#include "private/bionic_globals.h"
42#include "private/bionic_tls.h"
43#include "private/KernelArgumentBlock.h"
44
Ryan Prichard8f639a42018-10-01 23:10:05 -070045#include "android-base/unique_fd.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070046#include "android-base/strings.h"
47#include "android-base/stringprintf.h"
Dan Willemsen7ec52b12016-11-28 17:02:25 -080048#ifdef __ANDROID__
Josh Gao2a3b4fa2016-10-26 17:55:49 -070049#include "debuggerd/handler.h"
Dan Willemsen7ec52b12016-11-28 17:02:25 -080050#endif
Dimitry Ivanov3f660572016-09-09 10:00:39 -070051
Christopher Ferris7a3681e2017-04-24 17:48:32 -070052#include <async_safe/log.h>
Ryan Prichard701bd0c2018-11-21 16:23:03 -080053#include <bionic/libc_init_common.h>
Christopher Ferris7a3681e2017-04-24 17:48:32 -070054
Dimitry Ivanov3f660572016-09-09 10:00:39 -070055#include <vector>
56
Ryan Prichard8f639a42018-10-01 23:10:05 -070057__LIBC_HIDDEN__ extern "C" void _start();
Dimitry Ivanov3f660572016-09-09 10:00:39 -070058
59static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
60
Ryan Prichard9729f352018-07-13 22:40:26 -070061static void get_elf_base_from_phdr(const ElfW(Phdr)* phdr_table, size_t phdr_count,
62 ElfW(Addr)* base, ElfW(Addr)* load_bias);
63
Dimitry Ivanov3f660572016-09-09 10:00:39 -070064// These should be preserved static to avoid emitting
65// RELATIVE relocations for the part of the code running
66// before linker links itself.
67
68// TODO (dimtiry): remove somain, rename solist to solist_head
69static soinfo* solist;
70static soinfo* sonext;
71static soinfo* somain; // main process, always the one after libdl_info
Ryan Prichard04896452018-08-20 17:44:42 -070072static soinfo* solinker;
dimitry8b142562018-05-09 15:22:38 +020073static soinfo* vdso; // vdso if present
Dimitry Ivanov3f660572016-09-09 10:00:39 -070074
75void solist_add_soinfo(soinfo* si) {
76 sonext->next = si;
77 sonext = si;
78}
79
80bool solist_remove_soinfo(soinfo* si) {
81 soinfo *prev = nullptr, *trav;
82 for (trav = solist; trav != nullptr; trav = trav->next) {
83 if (trav == si) {
84 break;
85 }
86 prev = trav;
87 }
88
89 if (trav == nullptr) {
90 // si was not in solist
91 PRINT("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
92 return false;
93 }
94
95 // prev will never be null, because the first entry in solist is
96 // always the static libdl_info.
George Burgess IV70591002017-06-27 16:23:45 -070097 CHECK(prev != nullptr);
Dimitry Ivanov3f660572016-09-09 10:00:39 -070098 prev->next = si->next;
99 if (si == sonext) {
100 sonext = prev;
101 }
102
103 return true;
104}
105
106soinfo* solist_get_head() {
107 return solist;
108}
109
110soinfo* solist_get_somain() {
111 return somain;
112}
113
dimitry8b142562018-05-09 15:22:38 +0200114soinfo* solist_get_vdso() {
115 return vdso;
116}
117
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700118int g_ld_debug_verbosity;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700119
120static std::vector<std::string> g_ld_preload_names;
121
122static std::vector<soinfo*> g_ld_preloads;
123
124static void parse_path(const char* path, const char* delimiters,
125 std::vector<std::string>* resolved_paths) {
126 std::vector<std::string> paths;
127 split_path(path, delimiters, &paths);
128 resolve_paths(paths, resolved_paths);
129}
130
131static void parse_LD_LIBRARY_PATH(const char* path) {
132 std::vector<std::string> ld_libary_paths;
133 parse_path(path, ":", &ld_libary_paths);
134 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
135}
136
137static void parse_LD_PRELOAD(const char* path) {
138 g_ld_preload_names.clear();
139 if (path != nullptr) {
140 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
141 g_ld_preload_names = android::base::Split(path, " :");
Josh Gao44f6e182017-10-18 17:25:24 -0700142 g_ld_preload_names.erase(std::remove_if(g_ld_preload_names.begin(), g_ld_preload_names.end(),
Josh Gao27242c62017-10-20 17:45:13 -0700143 [](const std::string& s) { return s.empty(); }),
144 g_ld_preload_names.end());
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700145 }
146}
147
148// An empty list of soinfos
149static soinfo_list_t g_empty_list;
150
dimitryf9abbf62017-07-06 12:17:14 +0200151static void add_vdso(KernelArgumentBlock& args) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700152 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
153 if (ehdr_vdso == nullptr) {
154 return;
155 }
156
157 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
158
159 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
160 si->phnum = ehdr_vdso->e_phnum;
161 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
162 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
163 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
164
165 si->prelink_image();
166 si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr);
dimitryc18de1b2017-09-26 14:31:35 +0200167 // prevents accidental unloads...
168 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_NODELETE);
169 si->set_linked();
170 si->call_constructors();
dimitry8b142562018-05-09 15:22:38 +0200171
172 vdso = si;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700173}
174
Ryan Prichard04896452018-08-20 17:44:42 -0700175// Initializes an soinfo's link_map_head field using other fields from the
176// soinfo (phdr, phnum, load_bias).
177static void init_link_map_head(soinfo& info, const char* linker_path) {
178 auto& map = info.link_map_head;
179 map.l_addr = info.load_bias;
180 map.l_name = const_cast<char*>(linker_path);
181 phdr_table_get_dynamic_section(info.phdr, info.phnum, info.load_bias, &map.l_ld, nullptr);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700182}
183
184extern "C" int __system_properties_init(void);
185
Ryan Prichard8f639a42018-10-01 23:10:05 -0700186struct ExecutableInfo {
187 std::string path;
188 struct stat file_stat;
189 const ElfW(Phdr)* phdr;
190 size_t phdr_count;
191 ElfW(Addr) entry_point;
192};
193
194static ExecutableInfo get_executable_info(KernelArgumentBlock& args) {
195 ExecutableInfo result = {};
196
197 if (is_init()) {
198 // /proc fs is not mounted when init starts. Therefore we can't use
199 // /proc/self/exe for init.
200 stat("/init", &result.file_stat);
201 result.path = "/init";
202 } else {
203 // Stat "/proc/self/exe" instead of executable_path because
204 // the executable could be unlinked by this point and it should
205 // not cause a crash (see http://b/31084669)
206 if (TEMP_FAILURE_RETRY(stat("/proc/self/exe", &result.file_stat)) != 0) {
207 async_safe_fatal("unable to stat \"/proc/self/exe\": %s", strerror(errno));
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700208 }
Ryan Prichard8f639a42018-10-01 23:10:05 -0700209 char path[PATH_MAX];
210 ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path));
211 if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) {
212 async_safe_fatal("readlink('/proc/self/exe') failed: %s", strerror(errno));
213 }
214 result.path = std::string(path, path_len);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700215 }
216
Ryan Prichard8f639a42018-10-01 23:10:05 -0700217 result.phdr = reinterpret_cast<const ElfW(Phdr)*>(args.getauxval(AT_PHDR));
218 result.phdr_count = args.getauxval(AT_PHNUM);
219 result.entry_point = args.getauxval(AT_ENTRY);
220 return result;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700221}
222
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800223#if defined(__LP64__)
224static char kLinkerPath[] = "/system/bin/linker64";
225#else
226static char kLinkerPath[] = "/system/bin/linker";
227#endif
228
Ryan Prichard8f639a42018-10-01 23:10:05 -0700229__printflike(1, 2)
230static void __linker_error(const char* fmt, ...) {
231 va_list ap;
dimitry04f7a792017-09-29 11:52:17 +0200232
Ryan Prichard8f639a42018-10-01 23:10:05 -0700233 va_start(ap, fmt);
234 async_safe_format_fd_va_list(STDERR_FILENO, fmt, ap);
235 va_end(ap);
236
237 va_start(ap, fmt);
238 async_safe_format_log_va_list(ANDROID_LOG_FATAL, "linker", fmt, ap);
239 va_end(ap);
240
dimitry04f7a792017-09-29 11:52:17 +0200241 _exit(EXIT_FAILURE);
Elliott Hughesad2d0382017-07-31 11:43:34 -0700242}
243
Ryan Prichard8f639a42018-10-01 23:10:05 -0700244static void __linker_cannot_link(const char* argv0) {
245 __linker_error("CANNOT LINK EXECUTABLE \"%s\": %s\n",
246 argv0,
247 linker_get_error_buffer());
248}
249
250// Load an executable. Normally the kernel has already loaded the executable when the linker
251// starts. The linker can be invoked directly on an executable, though, and then the linker must
252// load it. This function doesn't load dependencies or resolve relocations.
253static ExecutableInfo load_executable(const char* orig_path) {
254 ExecutableInfo result = {};
255
256 if (orig_path[0] != '/') {
257 __linker_error("error: expected absolute path: \"%s\"\n", orig_path);
258 }
259
260 off64_t file_offset;
261 android::base::unique_fd fd(open_executable(orig_path, &file_offset, &result.path));
262 if (fd.get() == -1) {
263 __linker_error("error: unable to open file \"%s\"\n", orig_path);
264 }
265
266 if (TEMP_FAILURE_RETRY(fstat(fd.get(), &result.file_stat)) == -1) {
267 __linker_error("error: unable to stat \"%s\": %s\n", result.path.c_str(), strerror(errno));
268 }
269
270 ElfReader elf_reader;
271 if (!elf_reader.Read(result.path.c_str(), fd.get(), file_offset, result.file_stat.st_size)) {
272 __linker_error("error: %s\n", linker_get_error_buffer());
273 }
274 if (!elf_reader.Load(nullptr)) {
275 __linker_error("error: %s\n", linker_get_error_buffer());
276 }
277
278 result.phdr = elf_reader.loaded_phdr();
279 result.phdr_count = elf_reader.phdr_count();
280 result.entry_point = elf_reader.entry_point();
281 return result;
282}
283
284static ElfW(Addr) linker_main(KernelArgumentBlock& args, const char* exe_to_load) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800285 ProtectedDataGuard guard;
286
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700287#if TIMING
288 struct timeval t0, t1;
289 gettimeofday(&t0, 0);
290#endif
291
292 // Sanitize the environment.
293 __libc_init_AT_SECURE(args);
294
295 // Initialize system properties
296 __system_properties_init(); // may use 'environ'
297
298 // Register the debuggerd signal handler.
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800299#ifdef __ANDROID__
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700300 debuggerd_callbacks_t callbacks = {
301 .get_abort_message = []() {
Ryan Prichard7752bcb2018-11-22 02:41:04 -0800302 return __libc_shared_globals()->abort_msg;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700303 },
304 .post_dump = &notify_gdb_of_libraries,
305 };
306 debuggerd_init(&callbacks);
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800307#endif
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700308
309 g_linker_logger.ResetState();
310
311 // Get a few environment variables.
312 const char* LD_DEBUG = getenv("LD_DEBUG");
313 if (LD_DEBUG != nullptr) {
314 g_ld_debug_verbosity = atoi(LD_DEBUG);
315 }
316
317#if defined(__LP64__)
318 INFO("[ Android dynamic linker (64-bit) ]");
319#else
320 INFO("[ Android dynamic linker (32-bit) ]");
321#endif
322
323 // These should have been sanitized by __libc_init_AT_SECURE, but the test
324 // doesn't cost us anything.
325 const char* ldpath_env = nullptr;
326 const char* ldpreload_env = nullptr;
327 if (!getauxval(AT_SECURE)) {
328 ldpath_env = getenv("LD_LIBRARY_PATH");
329 if (ldpath_env != nullptr) {
330 INFO("[ LD_LIBRARY_PATH set to \"%s\" ]", ldpath_env);
331 }
332 ldpreload_env = getenv("LD_PRELOAD");
333 if (ldpreload_env != nullptr) {
334 INFO("[ LD_PRELOAD set to \"%s\" ]", ldpreload_env);
335 }
336 }
337
Ryan Prichard8f639a42018-10-01 23:10:05 -0700338 const ExecutableInfo exe_info = exe_to_load ? load_executable(exe_to_load) :
339 get_executable_info(args);
340
341 // Assign to a static variable for the sake of the debug map, which needs
342 // a C-style string to last until the program exits.
343 static std::string exe_path = exe_info.path;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700344
Ryan Prichard04896452018-08-20 17:44:42 -0700345 // Initialize the main exe's soinfo.
Ryan Prichard8f639a42018-10-01 23:10:05 -0700346 soinfo* si = soinfo_alloc(&g_default_namespace,
347 exe_path.c_str(), &exe_info.file_stat,
348 0, RTLD_GLOBAL);
Ryan Prichard04896452018-08-20 17:44:42 -0700349 somain = si;
Ryan Prichard8f639a42018-10-01 23:10:05 -0700350 si->phdr = exe_info.phdr;
351 si->phnum = exe_info.phdr_count;
Ryan Prichard04896452018-08-20 17:44:42 -0700352 get_elf_base_from_phdr(si->phdr, si->phnum, &si->base, &si->load_bias);
353 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
354 si->dynamic = nullptr;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700355 si->set_main_executable();
Ryan Prichard8f639a42018-10-01 23:10:05 -0700356 init_link_map_head(*si, exe_path.c_str());
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700357
358 // Register the main executable and the linker upfront to have
359 // gdb aware of them before loading the rest of the dependency
360 // tree.
Ryan Prichard04896452018-08-20 17:44:42 -0700361 //
362 // gdb expects the linker to be in the debug shared object list.
363 // Without this, gdb has trouble locating the linker's ".text"
364 // and ".plt" sections. Gdb could also potentially use this to
365 // relocate the offset of our exported 'rtld_db_dlactivity' symbol.
366 //
367 insert_link_map_into_debug_map(&si->link_map_head);
368 insert_link_map_into_debug_map(&solinker->link_map_head);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700369
Ryan Prichard14dd9922018-08-20 17:43:44 -0700370 add_vdso(args);
371
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700372 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
Elliott Hughes3bdb31b2017-01-07 10:38:20 -0800373
374 // We haven't supported non-PIE since Lollipop for security reasons.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700375 if (elf_hdr->e_type != ET_DYN) {
Elliott Hughesad2d0382017-07-31 11:43:34 -0700376 // We don't use async_safe_fatal here because we don't want a tombstone:
377 // even after several years we still find ourselves on app compatibility
Elliott Hughes3bdb31b2017-01-07 10:38:20 -0800378 // investigations because some app's trying to launch an executable that
379 // hasn't worked in at least three years, and we've "helpfully" dropped a
380 // tombstone for them. The tombstone never provided any detail relevant to
381 // fixing the problem anyway, and the utility of drawing extra attention
382 // to the problem is non-existent at this late date.
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700383 async_safe_format_fd(STDERR_FILENO,
Elliott Hughesad2d0382017-07-31 11:43:34 -0700384 "\"%s\": error: Android 5.0 and later only support "
385 "position-independent executables (-fPIE).\n",
386 g_argv[0]);
Dan Albert95e2e6f2017-01-31 16:02:43 -0800387 exit(EXIT_FAILURE);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700388 }
389
390 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
391 parse_LD_LIBRARY_PATH(ldpath_env);
392 parse_LD_PRELOAD(ldpreload_env);
393
Ryan Prichard8f639a42018-10-01 23:10:05 -0700394 std::vector<android_namespace_t*> namespaces = init_default_namespaces(exe_path.c_str());
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700395
Elliott Hughesad2d0382017-07-31 11:43:34 -0700396 if (!si->prelink_image()) __linker_cannot_link(g_argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700397
398 // add somain to global group
399 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Jiyong Park02586a22017-05-20 01:01:24 +0900400 // ... and add it to all other linked namespaces
401 for (auto linked_ns : namespaces) {
402 if (linked_ns != &g_default_namespace) {
403 linked_ns->add_soinfo(somain);
404 somain->add_secondary_namespace(linked_ns);
405 }
406 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700407
408 // Load ld_preloads and dependencies.
409 std::vector<const char*> needed_library_name_list;
410 size_t ld_preloads_count = 0;
411
412 for (const auto& ld_preload_name : g_ld_preload_names) {
413 needed_library_name_list.push_back(ld_preload_name.c_str());
414 ++ld_preloads_count;
415 }
416
417 for_each_dt_needed(si, [&](const char* name) {
418 needed_library_name_list.push_back(name);
419 });
420
421 const char** needed_library_names = &needed_library_name_list[0];
422 size_t needed_libraries_count = needed_library_name_list.size();
423
424 if (needed_libraries_count > 0 &&
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800425 !find_libraries(&g_default_namespace,
426 si,
427 needed_library_names,
428 needed_libraries_count,
429 nullptr,
430 &g_ld_preloads,
431 ld_preloads_count,
432 RTLD_GLOBAL,
433 nullptr,
434 true /* add_as_children */,
Jiyong Park02586a22017-05-20 01:01:24 +0900435 true /* search_linked_namespaces */,
Jiyong Park02586a22017-05-20 01:01:24 +0900436 &namespaces)) {
Elliott Hughesad2d0382017-07-31 11:43:34 -0700437 __linker_cannot_link(g_argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700438 } else if (needed_libraries_count == 0) {
439 if (!si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr)) {
Elliott Hughesad2d0382017-07-31 11:43:34 -0700440 __linker_cannot_link(g_argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700441 }
442 si->increment_ref_count();
443 }
444
Elliott Hughesad2d0382017-07-31 11:43:34 -0700445 if (!get_cfi_shadow()->InitialLinkDone(solist)) __linker_cannot_link(g_argv[0]);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700446
Ryan Prichard6631f9b2018-04-30 18:29:32 -0700447 // Store a pointer to the kernel argument block in a TLS slot to be
448 // picked up by the libc constructor.
449 __get_tls()[TLS_SLOT_BIONIC_PREINIT] = &args;
450
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800451 si->call_pre_init_constructors();
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800452 si->call_constructors();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700453
454#if TIMING
455 gettimeofday(&t1, nullptr);
456 PRINT("LINKER TIME: %s: %d microseconds", g_argv[0], (int) (
457 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
458 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
459#endif
460#if STATS
461 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", g_argv[0],
462 linker_stats.count[kRelocAbsolute],
463 linker_stats.count[kRelocRelative],
464 linker_stats.count[kRelocCopy],
465 linker_stats.count[kRelocSymbol]);
466#endif
467#if COUNT_PAGES
468 {
469 unsigned n;
470 unsigned i;
471 unsigned count = 0;
472 for (n = 0; n < 4096; n++) {
473 if (bitmask[n]) {
474 unsigned x = bitmask[n];
475#if defined(__LP64__)
476 for (i = 0; i < 32; i++) {
477#else
478 for (i = 0; i < 8; i++) {
479#endif
480 if (x & 1) {
481 count++;
482 }
483 x >>= 1;
484 }
485 }
486 }
487 PRINT("PAGES MODIFIED: %s: %d (%dKB)", g_argv[0], count, count * 4);
488 }
489#endif
490
491#if TIMING || STATS || COUNT_PAGES
492 fflush(stdout);
493#endif
494
Ryan Prichard8f639a42018-10-01 23:10:05 -0700495 ElfW(Addr) entry = exe_info.entry_point;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700496 TRACE("[ Ready to execute \"%s\" @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
497 return entry;
498}
499
500/* Compute the load-bias of an existing executable. This shall only
501 * be used to compute the load bias of an executable or shared library
502 * that was loaded by the kernel itself.
503 *
504 * Input:
505 * elf -> address of ELF header, assumed to be at the start of the file.
506 * Return:
507 * load bias, i.e. add the value of any p_vaddr in the file to get
508 * the corresponding address in memory.
509 */
510static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
511 ElfW(Addr) offset = elf->e_phoff;
512 const ElfW(Phdr)* phdr_table =
513 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
514 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
515
516 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
517 if (phdr->p_type == PT_LOAD) {
518 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
519 }
520 }
521 return 0;
522}
523
Ryan Prichard9729f352018-07-13 22:40:26 -0700524/* Find the load bias and base address of an executable or shared object loaded
525 * by the kernel. The ELF file's PHDR table must have a PT_PHDR entry.
526 *
527 * A VDSO doesn't have a PT_PHDR entry in its PHDR table.
528 */
529static void get_elf_base_from_phdr(const ElfW(Phdr)* phdr_table, size_t phdr_count,
530 ElfW(Addr)* base, ElfW(Addr)* load_bias) {
531 for (size_t i = 0; i < phdr_count; ++i) {
532 if (phdr_table[i].p_type == PT_PHDR) {
533 *load_bias = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_vaddr;
534 *base = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_offset;
535 return;
536 }
537 }
538 async_safe_fatal("Could not find a PHDR: broken executable?");
539}
540
Ryan Prichard742982d2018-05-30 22:32:17 -0700541static ElfW(Addr) __attribute__((noinline))
Ryan Prichard04896452018-08-20 17:44:42 -0700542__linker_init_post_relocation(KernelArgumentBlock& args, soinfo& linker_so);
Ryan Prichard742982d2018-05-30 22:32:17 -0700543
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700544/*
545 * This is the entry point for the linker, called from begin.S. This
546 * method is responsible for fixing the linker's own relocations, and
547 * then calling __linker_init_post_relocation().
548 *
549 * Because this method is called before the linker has fixed it's own
550 * relocations, any attempt to reference an extern variable, extern
551 * function, or other GOT reference will generate a segfault.
552 */
553extern "C" ElfW(Addr) __linker_init(void* raw_args) {
554 KernelArgumentBlock args(raw_args);
555
Ryan Prichard27475b52018-05-17 17:14:18 -0700556#if defined(__i386__)
557 __libc_init_sysinfo(args);
558#endif
559
Ryan Prichard8f639a42018-10-01 23:10:05 -0700560 // When the linker is run by itself (rather than as an interpreter for
561 // another program), AT_BASE is 0.
Ryan Prichard9729f352018-07-13 22:40:26 -0700562 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
563 if (linker_addr == 0) {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700564 // Detect an attempt to run the linker on itself (e.g.
565 // `linker64 /system/bin/linker64`). If the kernel loaded this instance of
566 // the linker, then AT_ENTRY will refer to &_start. If it doesn't, then
567 // something else must have loaded this instance of the linker. It's
568 // simpler if we only allow one copy of the linker to be loaded at a time.
569 if (args.getauxval(AT_ENTRY) != reinterpret_cast<uintptr_t>(&_start)) {
570 // The first linker already relocated this one and set up TLS, so we don't
571 // need further libc initialization.
572 __linker_error("error: linker cannot load itself\n");
573 }
574 // Otherwise, the AT_PHDR and AT_PHNUM aux values describe this linker
575 // instance, so use the phdr to find the linker's base address.
Ryan Prichard9729f352018-07-13 22:40:26 -0700576 ElfW(Addr) load_bias;
577 get_elf_base_from_phdr(
578 reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR)), args.getauxval(AT_PHNUM),
579 &linker_addr, &load_bias);
580 }
George Burgess IV70591002017-06-27 16:23:45 -0700581
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700582 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
583 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
584
Ryan Prichard04896452018-08-20 17:44:42 -0700585 soinfo tmp_linker_so(nullptr, nullptr, nullptr, 0, 0);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700586
Ryan Prichard04896452018-08-20 17:44:42 -0700587 tmp_linker_so.base = linker_addr;
588 tmp_linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
589 tmp_linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
590 tmp_linker_so.dynamic = nullptr;
591 tmp_linker_so.phdr = phdr;
592 tmp_linker_so.phnum = elf_hdr->e_phnum;
593 tmp_linker_so.set_linker_flag();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700594
595 // Prelink the linker so we can access linker globals.
Ryan Prichard04896452018-08-20 17:44:42 -0700596 if (!tmp_linker_so.prelink_image()) __linker_cannot_link(args.argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700597
598 // This might not be obvious... The reasons why we pass g_empty_list
599 // in place of local_group here are (1) we do not really need it, because
600 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
601 // itself without having to look into local_group and (2) allocators
602 // are not yet initialized, and therefore we cannot use linked_list.push_*
603 // functions at this point.
Ryan Prichard04896452018-08-20 17:44:42 -0700604 if (!tmp_linker_so.link_image(g_empty_list, g_empty_list, nullptr)) __linker_cannot_link(args.argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700605
Ryan Prichard04896452018-08-20 17:44:42 -0700606 return __linker_init_post_relocation(args, tmp_linker_so);
Ryan Prichard742982d2018-05-30 22:32:17 -0700607}
608
609/*
610 * This code is called after the linker has linked itself and fixed its own
611 * GOT. It is safe to make references to externs and other non-local data at
612 * this point. The compiler sometimes moves GOT references earlier in a
613 * function, so avoid inlining this function (http://b/80503879).
614 */
615static ElfW(Addr) __attribute__((noinline))
Ryan Prichard04896452018-08-20 17:44:42 -0700616__linker_init_post_relocation(KernelArgumentBlock& args, soinfo& tmp_linker_so) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700617 // Initialize the main thread (including TLS, so system calls really work).
618 __libc_init_main_thread(args);
619
620 // We didn't protect the linker's RELRO pages in link_image because we
621 // couldn't make system calls on x86 at that point, but we can now...
Ryan Prichard04896452018-08-20 17:44:42 -0700622 if (!tmp_linker_so.protect_relro()) __linker_cannot_link(args.argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700623
624 // Initialize the linker's static libc's globals
625 __libc_init_globals(args);
626
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700627 // Initialize the linker's own global variables
Ryan Prichard04896452018-08-20 17:44:42 -0700628 tmp_linker_so.call_constructors();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700629
Ryan Prichard8f639a42018-10-01 23:10:05 -0700630 // When the linker is run directly rather than acting as PT_INTERP, parse
631 // arguments and determine the executable to load. When it's instead acting
632 // as PT_INTERP, AT_ENTRY will refer to the loaded executable rather than the
633 // linker's _start.
634 const char* exe_to_load = nullptr;
635 if (args.getauxval(AT_ENTRY) == reinterpret_cast<uintptr_t>(&_start)) {
636 if (args.argc <= 1 || !strcmp(args.argv[1], "--help")) {
637 async_safe_format_fd(STDOUT_FILENO,
638 "Usage: %s program [arguments...]\n"
639 " %s path.zip!/program [arguments...]\n"
640 "\n"
641 "A helper program for linking dynamic executables. Typically, the kernel loads\n"
642 "this program because it's the PT_INTERP of a dynamic executable.\n"
643 "\n"
644 "This program can also be run directly to load and run a dynamic executable. The\n"
645 "executable can be inside a zip file if it's stored uncompressed and at a\n"
646 "page-aligned offset.\n",
647 args.argv[0], args.argv[0]);
648 exit(0);
649 }
650 exe_to_load = args.argv[1];
Ryan Prichardabf736a2018-11-22 02:40:17 -0800651 __libc_shared_globals()->initial_linker_arg_count = 1;
Dimitry Ivanov9b1cc4b2017-03-23 16:17:15 -0700652 }
653
Ryan Prichard8f639a42018-10-01 23:10:05 -0700654 // store argc/argv/envp to use them for calling constructors
Ryan Prichardabf736a2018-11-22 02:40:17 -0800655 g_argc = args.argc - __libc_shared_globals()->initial_linker_arg_count;
656 g_argv = args.argv + __libc_shared_globals()->initial_linker_arg_count;
Ryan Prichard8f639a42018-10-01 23:10:05 -0700657 g_envp = args.envp;
658
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700659 // Initialize static variables. Note that in order to
660 // get correct libdl_info we need to call constructors
661 // before get_libdl_info().
Ryan Prichard04896452018-08-20 17:44:42 -0700662 sonext = solist = solinker = get_libdl_info(kLinkerPath, tmp_linker_so);
663 g_default_namespace.add_soinfo(solinker);
664 init_link_map_head(*solinker, kLinkerPath);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700665
Ryan Prichard8f639a42018-10-01 23:10:05 -0700666 ElfW(Addr) start_address = linker_main(args, exe_to_load);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700667
668 INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
669
670 // Return the address that the calling assembly stub should jump to.
671 return start_address;
672}