blob: f7c496a2527929a461777cbbc475bcf852bd7fea [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
31#include "linker_debug.h"
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070032#include "linker_cfi.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070033#include "linker_gdb_support.h"
34#include "linker_globals.h"
35#include "linker_phdr.h"
36#include "linker_utils.h"
37
Ryan Prichard8f639a42018-10-01 23:10:05 -070038#include "private/bionic_auxv.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070039#include "private/bionic_globals.h"
40#include "private/bionic_tls.h"
41#include "private/KernelArgumentBlock.h"
42
Ryan Prichard8f639a42018-10-01 23:10:05 -070043#include "android-base/unique_fd.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070044#include "android-base/strings.h"
45#include "android-base/stringprintf.h"
Dan Willemsen7ec52b12016-11-28 17:02:25 -080046#ifdef __ANDROID__
Josh Gao2a3b4fa2016-10-26 17:55:49 -070047#include "debuggerd/handler.h"
Dan Willemsen7ec52b12016-11-28 17:02:25 -080048#endif
Dimitry Ivanov3f660572016-09-09 10:00:39 -070049
Christopher Ferris7a3681e2017-04-24 17:48:32 -070050#include <async_safe/log.h>
51
Dimitry Ivanov3f660572016-09-09 10:00:39 -070052#include <vector>
53
54extern void __libc_init_globals(KernelArgumentBlock&);
55extern void __libc_init_AT_SECURE(KernelArgumentBlock&);
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;
119abort_msg_t* g_abort_message = nullptr; // For debuggerd.
120
121static std::vector<std::string> g_ld_preload_names;
122
123static std::vector<soinfo*> g_ld_preloads;
124
125static void parse_path(const char* path, const char* delimiters,
126 std::vector<std::string>* resolved_paths) {
127 std::vector<std::string> paths;
128 split_path(path, delimiters, &paths);
129 resolve_paths(paths, resolved_paths);
130}
131
132static void parse_LD_LIBRARY_PATH(const char* path) {
133 std::vector<std::string> ld_libary_paths;
134 parse_path(path, ":", &ld_libary_paths);
135 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
136}
137
138static void parse_LD_PRELOAD(const char* path) {
139 g_ld_preload_names.clear();
140 if (path != nullptr) {
141 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
142 g_ld_preload_names = android::base::Split(path, " :");
Josh Gao44f6e182017-10-18 17:25:24 -0700143 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 -0700144 [](const std::string& s) { return s.empty(); }),
145 g_ld_preload_names.end());
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700146 }
147}
148
149// An empty list of soinfos
150static soinfo_list_t g_empty_list;
151
dimitryf9abbf62017-07-06 12:17:14 +0200152static void add_vdso(KernelArgumentBlock& args) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700153 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
154 if (ehdr_vdso == nullptr) {
155 return;
156 }
157
158 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
159
160 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
161 si->phnum = ehdr_vdso->e_phnum;
162 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
163 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
164 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
165
166 si->prelink_image();
167 si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr);
dimitryc18de1b2017-09-26 14:31:35 +0200168 // prevents accidental unloads...
169 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_NODELETE);
170 si->set_linked();
171 si->call_constructors();
dimitry8b142562018-05-09 15:22:38 +0200172
173 vdso = si;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700174}
175
Ryan Prichard04896452018-08-20 17:44:42 -0700176// Initializes an soinfo's link_map_head field using other fields from the
177// soinfo (phdr, phnum, load_bias).
178static void init_link_map_head(soinfo& info, const char* linker_path) {
179 auto& map = info.link_map_head;
180 map.l_addr = info.load_bias;
181 map.l_name = const_cast<char*>(linker_path);
182 phdr_table_get_dynamic_section(info.phdr, info.phnum, info.load_bias, &map.l_ld, nullptr);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700183}
184
185extern "C" int __system_properties_init(void);
186
Ryan Prichard8f639a42018-10-01 23:10:05 -0700187struct ExecutableInfo {
188 std::string path;
189 struct stat file_stat;
190 const ElfW(Phdr)* phdr;
191 size_t phdr_count;
192 ElfW(Addr) entry_point;
193};
194
195static ExecutableInfo get_executable_info(KernelArgumentBlock& args) {
196 ExecutableInfo result = {};
197
198 if (is_init()) {
199 // /proc fs is not mounted when init starts. Therefore we can't use
200 // /proc/self/exe for init.
201 stat("/init", &result.file_stat);
202 result.path = "/init";
203 } else {
204 // Stat "/proc/self/exe" instead of executable_path because
205 // the executable could be unlinked by this point and it should
206 // not cause a crash (see http://b/31084669)
207 if (TEMP_FAILURE_RETRY(stat("/proc/self/exe", &result.file_stat)) != 0) {
208 async_safe_fatal("unable to stat \"/proc/self/exe\": %s", strerror(errno));
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700209 }
Ryan Prichard8f639a42018-10-01 23:10:05 -0700210 char path[PATH_MAX];
211 ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path));
212 if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) {
213 async_safe_fatal("readlink('/proc/self/exe') failed: %s", strerror(errno));
214 }
215 result.path = std::string(path, path_len);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700216 }
217
Ryan Prichard8f639a42018-10-01 23:10:05 -0700218 result.phdr = reinterpret_cast<const ElfW(Phdr)*>(args.getauxval(AT_PHDR));
219 result.phdr_count = args.getauxval(AT_PHNUM);
220 result.entry_point = args.getauxval(AT_ENTRY);
221 return result;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700222}
223
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800224#if defined(__LP64__)
225static char kLinkerPath[] = "/system/bin/linker64";
226#else
227static char kLinkerPath[] = "/system/bin/linker";
228#endif
229
Ryan Prichard8f639a42018-10-01 23:10:05 -0700230__printflike(1, 2)
231static void __linker_error(const char* fmt, ...) {
232 va_list ap;
dimitry04f7a792017-09-29 11:52:17 +0200233
Ryan Prichard8f639a42018-10-01 23:10:05 -0700234 va_start(ap, fmt);
235 async_safe_format_fd_va_list(STDERR_FILENO, fmt, ap);
236 va_end(ap);
237
238 va_start(ap, fmt);
239 async_safe_format_log_va_list(ANDROID_LOG_FATAL, "linker", fmt, ap);
240 va_end(ap);
241
dimitry04f7a792017-09-29 11:52:17 +0200242 _exit(EXIT_FAILURE);
Elliott Hughesad2d0382017-07-31 11:43:34 -0700243}
244
Ryan Prichard8f639a42018-10-01 23:10:05 -0700245static void __linker_cannot_link(const char* argv0) {
246 __linker_error("CANNOT LINK EXECUTABLE \"%s\": %s\n",
247 argv0,
248 linker_get_error_buffer());
249}
250
251// Load an executable. Normally the kernel has already loaded the executable when the linker
252// starts. The linker can be invoked directly on an executable, though, and then the linker must
253// load it. This function doesn't load dependencies or resolve relocations.
254static ExecutableInfo load_executable(const char* orig_path) {
255 ExecutableInfo result = {};
256
257 if (orig_path[0] != '/') {
258 __linker_error("error: expected absolute path: \"%s\"\n", orig_path);
259 }
260
261 off64_t file_offset;
262 android::base::unique_fd fd(open_executable(orig_path, &file_offset, &result.path));
263 if (fd.get() == -1) {
264 __linker_error("error: unable to open file \"%s\"\n", orig_path);
265 }
266
267 if (TEMP_FAILURE_RETRY(fstat(fd.get(), &result.file_stat)) == -1) {
268 __linker_error("error: unable to stat \"%s\": %s\n", result.path.c_str(), strerror(errno));
269 }
270
271 ElfReader elf_reader;
272 if (!elf_reader.Read(result.path.c_str(), fd.get(), file_offset, result.file_stat.st_size)) {
273 __linker_error("error: %s\n", linker_get_error_buffer());
274 }
275 if (!elf_reader.Load(nullptr)) {
276 __linker_error("error: %s\n", linker_get_error_buffer());
277 }
278
279 result.phdr = elf_reader.loaded_phdr();
280 result.phdr_count = elf_reader.phdr_count();
281 result.entry_point = elf_reader.entry_point();
282 return result;
283}
284
285static ElfW(Addr) linker_main(KernelArgumentBlock& args, const char* exe_to_load) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800286 ProtectedDataGuard guard;
287
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700288#if TIMING
289 struct timeval t0, t1;
290 gettimeofday(&t0, 0);
291#endif
292
293 // Sanitize the environment.
294 __libc_init_AT_SECURE(args);
295
296 // Initialize system properties
297 __system_properties_init(); // may use 'environ'
298
299 // Register the debuggerd signal handler.
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800300#ifdef __ANDROID__
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700301 debuggerd_callbacks_t callbacks = {
302 .get_abort_message = []() {
303 return g_abort_message;
304 },
305 .post_dump = &notify_gdb_of_libraries,
306 };
307 debuggerd_init(&callbacks);
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800308#endif
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700309
310 g_linker_logger.ResetState();
311
312 // Get a few environment variables.
313 const char* LD_DEBUG = getenv("LD_DEBUG");
314 if (LD_DEBUG != nullptr) {
315 g_ld_debug_verbosity = atoi(LD_DEBUG);
316 }
317
318#if defined(__LP64__)
319 INFO("[ Android dynamic linker (64-bit) ]");
320#else
321 INFO("[ Android dynamic linker (32-bit) ]");
322#endif
323
324 // These should have been sanitized by __libc_init_AT_SECURE, but the test
325 // doesn't cost us anything.
326 const char* ldpath_env = nullptr;
327 const char* ldpreload_env = nullptr;
328 if (!getauxval(AT_SECURE)) {
329 ldpath_env = getenv("LD_LIBRARY_PATH");
330 if (ldpath_env != nullptr) {
331 INFO("[ LD_LIBRARY_PATH set to \"%s\" ]", ldpath_env);
332 }
333 ldpreload_env = getenv("LD_PRELOAD");
334 if (ldpreload_env != nullptr) {
335 INFO("[ LD_PRELOAD set to \"%s\" ]", ldpreload_env);
336 }
337 }
338
Ryan Prichard8f639a42018-10-01 23:10:05 -0700339 const ExecutableInfo exe_info = exe_to_load ? load_executable(exe_to_load) :
340 get_executable_info(args);
341
342 // Assign to a static variable for the sake of the debug map, which needs
343 // a C-style string to last until the program exits.
344 static std::string exe_path = exe_info.path;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700345
Ryan Prichard04896452018-08-20 17:44:42 -0700346 // Initialize the main exe's soinfo.
Ryan Prichard8f639a42018-10-01 23:10:05 -0700347 soinfo* si = soinfo_alloc(&g_default_namespace,
348 exe_path.c_str(), &exe_info.file_stat,
349 0, RTLD_GLOBAL);
Ryan Prichard04896452018-08-20 17:44:42 -0700350 somain = si;
Ryan Prichard8f639a42018-10-01 23:10:05 -0700351 si->phdr = exe_info.phdr;
352 si->phnum = exe_info.phdr_count;
Ryan Prichard04896452018-08-20 17:44:42 -0700353 get_elf_base_from_phdr(si->phdr, si->phnum, &si->base, &si->load_bias);
354 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
355 si->dynamic = nullptr;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700356 si->set_main_executable();
Ryan Prichard8f639a42018-10-01 23:10:05 -0700357 init_link_map_head(*si, exe_path.c_str());
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700358
359 // Register the main executable and the linker upfront to have
360 // gdb aware of them before loading the rest of the dependency
361 // tree.
Ryan Prichard04896452018-08-20 17:44:42 -0700362 //
363 // gdb expects the linker to be in the debug shared object list.
364 // Without this, gdb has trouble locating the linker's ".text"
365 // and ".plt" sections. Gdb could also potentially use this to
366 // relocate the offset of our exported 'rtld_db_dlactivity' symbol.
367 //
368 insert_link_map_into_debug_map(&si->link_map_head);
369 insert_link_map_into_debug_map(&solinker->link_map_head);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700370
Ryan Prichard14dd9922018-08-20 17:43:44 -0700371 add_vdso(args);
372
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700373 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
Elliott Hughes3bdb31b2017-01-07 10:38:20 -0800374
375 // We haven't supported non-PIE since Lollipop for security reasons.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700376 if (elf_hdr->e_type != ET_DYN) {
Elliott Hughesad2d0382017-07-31 11:43:34 -0700377 // We don't use async_safe_fatal here because we don't want a tombstone:
378 // even after several years we still find ourselves on app compatibility
Elliott Hughes3bdb31b2017-01-07 10:38:20 -0800379 // investigations because some app's trying to launch an executable that
380 // hasn't worked in at least three years, and we've "helpfully" dropped a
381 // tombstone for them. The tombstone never provided any detail relevant to
382 // fixing the problem anyway, and the utility of drawing extra attention
383 // to the problem is non-existent at this late date.
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700384 async_safe_format_fd(STDERR_FILENO,
Elliott Hughesad2d0382017-07-31 11:43:34 -0700385 "\"%s\": error: Android 5.0 and later only support "
386 "position-independent executables (-fPIE).\n",
387 g_argv[0]);
Dan Albert95e2e6f2017-01-31 16:02:43 -0800388 exit(EXIT_FAILURE);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700389 }
390
391 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
392 parse_LD_LIBRARY_PATH(ldpath_env);
393 parse_LD_PRELOAD(ldpreload_env);
394
Ryan Prichard8f639a42018-10-01 23:10:05 -0700395 std::vector<android_namespace_t*> namespaces = init_default_namespaces(exe_path.c_str());
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700396
Elliott Hughesad2d0382017-07-31 11:43:34 -0700397 if (!si->prelink_image()) __linker_cannot_link(g_argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700398
399 // add somain to global group
400 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Jiyong Park02586a22017-05-20 01:01:24 +0900401 // ... and add it to all other linked namespaces
402 for (auto linked_ns : namespaces) {
403 if (linked_ns != &g_default_namespace) {
404 linked_ns->add_soinfo(somain);
405 somain->add_secondary_namespace(linked_ns);
406 }
407 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700408
409 // Load ld_preloads and dependencies.
410 std::vector<const char*> needed_library_name_list;
411 size_t ld_preloads_count = 0;
412
413 for (const auto& ld_preload_name : g_ld_preload_names) {
414 needed_library_name_list.push_back(ld_preload_name.c_str());
415 ++ld_preloads_count;
416 }
417
418 for_each_dt_needed(si, [&](const char* name) {
419 needed_library_name_list.push_back(name);
420 });
421
422 const char** needed_library_names = &needed_library_name_list[0];
423 size_t needed_libraries_count = needed_library_name_list.size();
424
425 if (needed_libraries_count > 0 &&
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800426 !find_libraries(&g_default_namespace,
427 si,
428 needed_library_names,
429 needed_libraries_count,
430 nullptr,
431 &g_ld_preloads,
432 ld_preloads_count,
433 RTLD_GLOBAL,
434 nullptr,
435 true /* add_as_children */,
Jiyong Park02586a22017-05-20 01:01:24 +0900436 true /* search_linked_namespaces */,
Jiyong Park02586a22017-05-20 01:01:24 +0900437 &namespaces)) {
Elliott Hughesad2d0382017-07-31 11:43:34 -0700438 __linker_cannot_link(g_argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700439 } else if (needed_libraries_count == 0) {
440 if (!si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr)) {
Elliott Hughesad2d0382017-07-31 11:43:34 -0700441 __linker_cannot_link(g_argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700442 }
443 si->increment_ref_count();
444 }
445
Elliott Hughesad2d0382017-07-31 11:43:34 -0700446 if (!get_cfi_shadow()->InitialLinkDone(solist)) __linker_cannot_link(g_argv[0]);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700447
Ryan Prichard6631f9b2018-04-30 18:29:32 -0700448 // Store a pointer to the kernel argument block in a TLS slot to be
449 // picked up by the libc constructor.
450 __get_tls()[TLS_SLOT_BIONIC_PREINIT] = &args;
451
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800452 si->call_pre_init_constructors();
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800453 si->call_constructors();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700454
455#if TIMING
456 gettimeofday(&t1, nullptr);
457 PRINT("LINKER TIME: %s: %d microseconds", g_argv[0], (int) (
458 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
459 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
460#endif
461#if STATS
462 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", g_argv[0],
463 linker_stats.count[kRelocAbsolute],
464 linker_stats.count[kRelocRelative],
465 linker_stats.count[kRelocCopy],
466 linker_stats.count[kRelocSymbol]);
467#endif
468#if COUNT_PAGES
469 {
470 unsigned n;
471 unsigned i;
472 unsigned count = 0;
473 for (n = 0; n < 4096; n++) {
474 if (bitmask[n]) {
475 unsigned x = bitmask[n];
476#if defined(__LP64__)
477 for (i = 0; i < 32; i++) {
478#else
479 for (i = 0; i < 8; i++) {
480#endif
481 if (x & 1) {
482 count++;
483 }
484 x >>= 1;
485 }
486 }
487 }
488 PRINT("PAGES MODIFIED: %s: %d (%dKB)", g_argv[0], count, count * 4);
489 }
490#endif
491
492#if TIMING || STATS || COUNT_PAGES
493 fflush(stdout);
494#endif
495
Ryan Prichard8f639a42018-10-01 23:10:05 -0700496 ElfW(Addr) entry = exe_info.entry_point;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700497 TRACE("[ Ready to execute \"%s\" @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
498 return entry;
499}
500
501/* Compute the load-bias of an existing executable. This shall only
502 * be used to compute the load bias of an executable or shared library
503 * that was loaded by the kernel itself.
504 *
505 * Input:
506 * elf -> address of ELF header, assumed to be at the start of the file.
507 * Return:
508 * load bias, i.e. add the value of any p_vaddr in the file to get
509 * the corresponding address in memory.
510 */
511static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
512 ElfW(Addr) offset = elf->e_phoff;
513 const ElfW(Phdr)* phdr_table =
514 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
515 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
516
517 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
518 if (phdr->p_type == PT_LOAD) {
519 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
520 }
521 }
522 return 0;
523}
524
Ryan Prichard9729f352018-07-13 22:40:26 -0700525/* Find the load bias and base address of an executable or shared object loaded
526 * by the kernel. The ELF file's PHDR table must have a PT_PHDR entry.
527 *
528 * A VDSO doesn't have a PT_PHDR entry in its PHDR table.
529 */
530static void get_elf_base_from_phdr(const ElfW(Phdr)* phdr_table, size_t phdr_count,
531 ElfW(Addr)* base, ElfW(Addr)* load_bias) {
532 for (size_t i = 0; i < phdr_count; ++i) {
533 if (phdr_table[i].p_type == PT_PHDR) {
534 *load_bias = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_vaddr;
535 *base = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_offset;
536 return;
537 }
538 }
539 async_safe_fatal("Could not find a PHDR: broken executable?");
540}
541
Ryan Prichard742982d2018-05-30 22:32:17 -0700542static ElfW(Addr) __attribute__((noinline))
Ryan Prichard04896452018-08-20 17:44:42 -0700543__linker_init_post_relocation(KernelArgumentBlock& args, soinfo& linker_so);
Ryan Prichard742982d2018-05-30 22:32:17 -0700544
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700545/*
546 * This is the entry point for the linker, called from begin.S. This
547 * method is responsible for fixing the linker's own relocations, and
548 * then calling __linker_init_post_relocation().
549 *
550 * Because this method is called before the linker has fixed it's own
551 * relocations, any attempt to reference an extern variable, extern
552 * function, or other GOT reference will generate a segfault.
553 */
554extern "C" ElfW(Addr) __linker_init(void* raw_args) {
555 KernelArgumentBlock args(raw_args);
556
Ryan Prichard27475b52018-05-17 17:14:18 -0700557#if defined(__i386__)
558 __libc_init_sysinfo(args);
559#endif
560
Ryan Prichard8f639a42018-10-01 23:10:05 -0700561 // When the linker is run by itself (rather than as an interpreter for
562 // another program), AT_BASE is 0.
Ryan Prichard9729f352018-07-13 22:40:26 -0700563 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
564 if (linker_addr == 0) {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700565 // Detect an attempt to run the linker on itself (e.g.
566 // `linker64 /system/bin/linker64`). If the kernel loaded this instance of
567 // the linker, then AT_ENTRY will refer to &_start. If it doesn't, then
568 // something else must have loaded this instance of the linker. It's
569 // simpler if we only allow one copy of the linker to be loaded at a time.
570 if (args.getauxval(AT_ENTRY) != reinterpret_cast<uintptr_t>(&_start)) {
571 // The first linker already relocated this one and set up TLS, so we don't
572 // need further libc initialization.
573 __linker_error("error: linker cannot load itself\n");
574 }
575 // Otherwise, the AT_PHDR and AT_PHNUM aux values describe this linker
576 // instance, so use the phdr to find the linker's base address.
Ryan Prichard9729f352018-07-13 22:40:26 -0700577 ElfW(Addr) load_bias;
578 get_elf_base_from_phdr(
579 reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR)), args.getauxval(AT_PHNUM),
580 &linker_addr, &load_bias);
581 }
George Burgess IV70591002017-06-27 16:23:45 -0700582
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700583 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
584 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
585
Ryan Prichard04896452018-08-20 17:44:42 -0700586 soinfo tmp_linker_so(nullptr, nullptr, nullptr, 0, 0);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700587
Ryan Prichard04896452018-08-20 17:44:42 -0700588 tmp_linker_so.base = linker_addr;
589 tmp_linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
590 tmp_linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
591 tmp_linker_so.dynamic = nullptr;
592 tmp_linker_so.phdr = phdr;
593 tmp_linker_so.phnum = elf_hdr->e_phnum;
594 tmp_linker_so.set_linker_flag();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700595
596 // Prelink the linker so we can access linker globals.
Ryan Prichard04896452018-08-20 17:44:42 -0700597 if (!tmp_linker_so.prelink_image()) __linker_cannot_link(args.argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700598
599 // This might not be obvious... The reasons why we pass g_empty_list
600 // in place of local_group here are (1) we do not really need it, because
601 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
602 // itself without having to look into local_group and (2) allocators
603 // are not yet initialized, and therefore we cannot use linked_list.push_*
604 // functions at this point.
Ryan Prichard04896452018-08-20 17:44:42 -0700605 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 -0700606
Ryan Prichard04896452018-08-20 17:44:42 -0700607 return __linker_init_post_relocation(args, tmp_linker_so);
Ryan Prichard742982d2018-05-30 22:32:17 -0700608}
609
610/*
611 * This code is called after the linker has linked itself and fixed its own
612 * GOT. It is safe to make references to externs and other non-local data at
613 * this point. The compiler sometimes moves GOT references earlier in a
614 * function, so avoid inlining this function (http://b/80503879).
615 */
616static ElfW(Addr) __attribute__((noinline))
Ryan Prichard04896452018-08-20 17:44:42 -0700617__linker_init_post_relocation(KernelArgumentBlock& args, soinfo& tmp_linker_so) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700618 // Initialize the main thread (including TLS, so system calls really work).
619 __libc_init_main_thread(args);
620
621 // We didn't protect the linker's RELRO pages in link_image because we
622 // couldn't make system calls on x86 at that point, but we can now...
Ryan Prichard04896452018-08-20 17:44:42 -0700623 if (!tmp_linker_so.protect_relro()) __linker_cannot_link(args.argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700624
Josh Gaof6e5b582018-06-01 15:30:54 -0700625 // Initialize the linker/libc.so shared global inside the linker.
626 static libc_shared_globals shared_globals;
627 __libc_shared_globals = &shared_globals;
628 __libc_init_shared_globals(&shared_globals);
629 args.shared_globals = __libc_shared_globals;
630
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700631 // Initialize the linker's static libc's globals
632 __libc_init_globals(args);
633
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700634 // Initialize the linker's own global variables
Ryan Prichard04896452018-08-20 17:44:42 -0700635 tmp_linker_so.call_constructors();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700636
Ryan Prichard8f639a42018-10-01 23:10:05 -0700637 // When the linker is run directly rather than acting as PT_INTERP, parse
638 // arguments and determine the executable to load. When it's instead acting
639 // as PT_INTERP, AT_ENTRY will refer to the loaded executable rather than the
640 // linker's _start.
641 const char* exe_to_load = nullptr;
642 if (args.getauxval(AT_ENTRY) == reinterpret_cast<uintptr_t>(&_start)) {
643 if (args.argc <= 1 || !strcmp(args.argv[1], "--help")) {
644 async_safe_format_fd(STDOUT_FILENO,
645 "Usage: %s program [arguments...]\n"
646 " %s path.zip!/program [arguments...]\n"
647 "\n"
648 "A helper program for linking dynamic executables. Typically, the kernel loads\n"
649 "this program because it's the PT_INTERP of a dynamic executable.\n"
650 "\n"
651 "This program can also be run directly to load and run a dynamic executable. The\n"
652 "executable can be inside a zip file if it's stored uncompressed and at a\n"
653 "page-aligned offset.\n",
654 args.argv[0], args.argv[0]);
655 exit(0);
656 }
657 exe_to_load = args.argv[1];
658 __libc_shared_globals->initial_linker_arg_count = 1;
Dimitry Ivanov9b1cc4b2017-03-23 16:17:15 -0700659 }
660
Ryan Prichard8f639a42018-10-01 23:10:05 -0700661 // store argc/argv/envp to use them for calling constructors
662 g_argc = args.argc - __libc_shared_globals->initial_linker_arg_count;
663 g_argv = args.argv + __libc_shared_globals->initial_linker_arg_count;
664 g_envp = args.envp;
665
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700666 // Initialize static variables. Note that in order to
667 // get correct libdl_info we need to call constructors
668 // before get_libdl_info().
Ryan Prichard04896452018-08-20 17:44:42 -0700669 sonext = solist = solinker = get_libdl_info(kLinkerPath, tmp_linker_so);
670 g_default_namespace.add_soinfo(solinker);
671 init_link_map_head(*solinker, kLinkerPath);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700672
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700673 args.abort_message_ptr = &g_abort_message;
Ryan Prichard8f639a42018-10-01 23:10:05 -0700674 ElfW(Addr) start_address = linker_main(args, exe_to_load);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700675
676 INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
677
678 // Return the address that the calling assembly stub should jump to.
679 return start_address;
680}