blob: f0e7b1b67d63f780a626aaaed23b997b7dc9354e [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
Collin Fijalkovich47d27aa2021-03-24 10:17:39 -070034#include "linker.h"
35#include "linker_cfi.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070036#include "linker_debug.h"
Ryan Prichard80e40f02019-10-31 19:54:46 -070037#include "linker_debuggerd.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070038#include "linker_gdb_support.h"
39#include "linker_globals.h"
40#include "linker_phdr.h"
Ryan Prichard339ecef2020-01-02 16:36:06 -080041#include "linker_relocate.h"
Peter Collingbourne1583cd22021-08-18 17:04:23 -070042#include "linker_relocs.h"
Ryan Prichard45d13492019-01-03 02:51:30 -080043#include "linker_tls.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070044#include "linker_utils.h"
45
Ryan Prichard249757b2019-11-01 17:18:28 -070046#include "private/bionic_auxv.h"
47#include "private/bionic_call_ifunc_resolver.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070048#include "private/bionic_globals.h"
49#include "private/bionic_tls.h"
50#include "private/KernelArgumentBlock.h"
51
Ryan Prichard8f639a42018-10-01 23:10:05 -070052#include "android-base/unique_fd.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070053#include "android-base/strings.h"
54#include "android-base/stringprintf.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070055
Christopher Ferris7a3681e2017-04-24 17:48:32 -070056#include <async_safe/log.h>
Ryan Prichard701bd0c2018-11-21 16:23:03 -080057#include <bionic/libc_init_common.h>
Ryan Prichard45d13492019-01-03 02:51:30 -080058#include <bionic/pthread_internal.h>
Christopher Ferris7a3681e2017-04-24 17:48:32 -070059
Dimitry Ivanov3f660572016-09-09 10:00:39 -070060#include <vector>
61
Ryan Prichard8f639a42018-10-01 23:10:05 -070062__LIBC_HIDDEN__ extern "C" void _start();
Dimitry Ivanov3f660572016-09-09 10:00:39 -070063
64static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
65
Ryan Prichard9729f352018-07-13 22:40:26 -070066static void get_elf_base_from_phdr(const ElfW(Phdr)* phdr_table, size_t phdr_count,
67 ElfW(Addr)* base, ElfW(Addr)* load_bias);
68
Vic Yang1bf62b22019-08-13 14:53:28 -070069static void set_bss_vma_name(soinfo* si);
70
Evgenii Stepanovf9fa32a2022-05-12 15:54:38 -070071void __libc_init_mte(const void* phdr_start, size_t phdr_count, uintptr_t load_bias,
72 void* stack_top);
Evgenii Stepanov8564b8d2020-12-15 13:55:32 -080073
Dimitry Ivanov3f660572016-09-09 10:00:39 -070074// These should be preserved static to avoid emitting
75// RELATIVE relocations for the part of the code running
76// before linker links itself.
77
78// TODO (dimtiry): remove somain, rename solist to solist_head
79static soinfo* solist;
80static soinfo* sonext;
81static soinfo* somain; // main process, always the one after libdl_info
Ryan Prichard04896452018-08-20 17:44:42 -070082static soinfo* solinker;
dimitry8b142562018-05-09 15:22:38 +020083static soinfo* vdso; // vdso if present
Dimitry Ivanov3f660572016-09-09 10:00:39 -070084
85void solist_add_soinfo(soinfo* si) {
86 sonext->next = si;
87 sonext = si;
88}
89
90bool solist_remove_soinfo(soinfo* si) {
91 soinfo *prev = nullptr, *trav;
92 for (trav = solist; trav != nullptr; trav = trav->next) {
93 if (trav == si) {
94 break;
95 }
96 prev = trav;
97 }
98
99 if (trav == nullptr) {
100 // si was not in solist
101 PRINT("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
102 return false;
103 }
104
105 // prev will never be null, because the first entry in solist is
106 // always the static libdl_info.
George Burgess IV70591002017-06-27 16:23:45 -0700107 CHECK(prev != nullptr);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700108 prev->next = si->next;
109 if (si == sonext) {
110 sonext = prev;
111 }
112
113 return true;
114}
115
116soinfo* solist_get_head() {
117 return solist;
118}
119
120soinfo* solist_get_somain() {
121 return somain;
122}
123
dimitry8b142562018-05-09 15:22:38 +0200124soinfo* solist_get_vdso() {
125 return vdso;
126}
127
Elliott Hughes90f96b92019-05-09 15:56:39 -0700128bool g_is_ldd;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700129int g_ld_debug_verbosity;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700130
131static std::vector<std::string> g_ld_preload_names;
132
133static std::vector<soinfo*> g_ld_preloads;
134
135static void parse_path(const char* path, const char* delimiters,
136 std::vector<std::string>* resolved_paths) {
137 std::vector<std::string> paths;
138 split_path(path, delimiters, &paths);
139 resolve_paths(paths, resolved_paths);
140}
141
142static void parse_LD_LIBRARY_PATH(const char* path) {
143 std::vector<std::string> ld_libary_paths;
144 parse_path(path, ":", &ld_libary_paths);
145 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
146}
147
148static void parse_LD_PRELOAD(const char* path) {
149 g_ld_preload_names.clear();
150 if (path != nullptr) {
151 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
152 g_ld_preload_names = android::base::Split(path, " :");
Josh Gao44f6e182017-10-18 17:25:24 -0700153 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 -0700154 [](const std::string& s) { return s.empty(); }),
155 g_ld_preload_names.end());
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700156 }
157}
158
159// An empty list of soinfos
160static soinfo_list_t g_empty_list;
161
Ryan Prichard07440a82018-11-22 03:16:06 -0800162static void add_vdso() {
163 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(getauxval(AT_SYSINFO_EHDR));
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700164 if (ehdr_vdso == nullptr) {
165 return;
166 }
167
168 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
169
170 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
171 si->phnum = ehdr_vdso->e_phnum;
172 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
173 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
174 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
175
176 si->prelink_image();
Ryan Prichard339ecef2020-01-02 16:36:06 -0800177 si->link_image(SymbolLookupList(si), si, nullptr, nullptr);
dimitryc18de1b2017-09-26 14:31:35 +0200178 // prevents accidental unloads...
179 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_NODELETE);
180 si->set_linked();
181 si->call_constructors();
dimitry8b142562018-05-09 15:22:38 +0200182
183 vdso = si;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700184}
185
Ryan Prichard04896452018-08-20 17:44:42 -0700186// Initializes an soinfo's link_map_head field using other fields from the
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700187// soinfo (phdr, phnum, load_bias). The soinfo's realpath must not change after
188// this function is called.
189static void init_link_map_head(soinfo& info) {
Ryan Prichard04896452018-08-20 17:44:42 -0700190 auto& map = info.link_map_head;
191 map.l_addr = info.load_bias;
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700192 map.l_name = const_cast<char*>(info.get_realpath());
Ryan Prichard04896452018-08-20 17:44:42 -0700193 phdr_table_get_dynamic_section(info.phdr, info.phnum, info.load_bias, &map.l_ld, nullptr);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700194}
195
196extern "C" int __system_properties_init(void);
197
Ryan Prichard8f639a42018-10-01 23:10:05 -0700198struct ExecutableInfo {
199 std::string path;
200 struct stat file_stat;
201 const ElfW(Phdr)* phdr;
202 size_t phdr_count;
203 ElfW(Addr) entry_point;
204};
205
Ryan Prichard07440a82018-11-22 03:16:06 -0800206static ExecutableInfo get_executable_info() {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700207 ExecutableInfo result = {};
208
Tom Cherry66bc4282018-11-08 13:40:52 -0800209 if (is_first_stage_init()) {
210 // /proc fs is not mounted when first stage init starts. Therefore we can't
211 // use /proc/self/exe for init.
Ryan Prichard8f639a42018-10-01 23:10:05 -0700212 stat("/init", &result.file_stat);
Tom Cherry66bc4282018-11-08 13:40:52 -0800213
214 // /init may be a symlink, so try to read it as such.
215 char path[PATH_MAX];
216 ssize_t path_len = readlink("/init", path, sizeof(path));
217 if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) {
218 result.path = "/init";
219 } else {
220 result.path = std::string(path, path_len);
221 }
Ryan Prichard8f639a42018-10-01 23:10:05 -0700222 } else {
223 // Stat "/proc/self/exe" instead of executable_path because
224 // the executable could be unlinked by this point and it should
225 // not cause a crash (see http://b/31084669)
226 if (TEMP_FAILURE_RETRY(stat("/proc/self/exe", &result.file_stat)) != 0) {
227 async_safe_fatal("unable to stat \"/proc/self/exe\": %s", strerror(errno));
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700228 }
Ryan Prichard8f639a42018-10-01 23:10:05 -0700229 char path[PATH_MAX];
230 ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path));
231 if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) {
232 async_safe_fatal("readlink('/proc/self/exe') failed: %s", strerror(errno));
233 }
234 result.path = std::string(path, path_len);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700235 }
236
Ryan Prichard07440a82018-11-22 03:16:06 -0800237 result.phdr = reinterpret_cast<const ElfW(Phdr)*>(getauxval(AT_PHDR));
238 result.phdr_count = getauxval(AT_PHNUM);
239 result.entry_point = getauxval(AT_ENTRY);
Ryan Prichard8f639a42018-10-01 23:10:05 -0700240 return result;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700241}
242
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800243#if defined(__LP64__)
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700244static char kFallbackLinkerPath[] = "/system/bin/linker64";
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800245#else
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700246static char kFallbackLinkerPath[] = "/system/bin/linker";
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800247#endif
248
Ryan Prichard8f639a42018-10-01 23:10:05 -0700249__printflike(1, 2)
250static void __linker_error(const char* fmt, ...) {
251 va_list ap;
dimitry04f7a792017-09-29 11:52:17 +0200252
Ryan Prichard8f639a42018-10-01 23:10:05 -0700253 va_start(ap, fmt);
254 async_safe_format_fd_va_list(STDERR_FILENO, fmt, ap);
255 va_end(ap);
256
257 va_start(ap, fmt);
258 async_safe_format_log_va_list(ANDROID_LOG_FATAL, "linker", fmt, ap);
259 va_end(ap);
260
dimitry04f7a792017-09-29 11:52:17 +0200261 _exit(EXIT_FAILURE);
Elliott Hughesad2d0382017-07-31 11:43:34 -0700262}
263
Ryan Prichard8f639a42018-10-01 23:10:05 -0700264static void __linker_cannot_link(const char* argv0) {
265 __linker_error("CANNOT LINK EXECUTABLE \"%s\": %s\n",
266 argv0,
267 linker_get_error_buffer());
268}
269
270// Load an executable. Normally the kernel has already loaded the executable when the linker
271// starts. The linker can be invoked directly on an executable, though, and then the linker must
272// load it. This function doesn't load dependencies or resolve relocations.
273static ExecutableInfo load_executable(const char* orig_path) {
274 ExecutableInfo result = {};
275
276 if (orig_path[0] != '/') {
277 __linker_error("error: expected absolute path: \"%s\"\n", orig_path);
278 }
279
280 off64_t file_offset;
281 android::base::unique_fd fd(open_executable(orig_path, &file_offset, &result.path));
282 if (fd.get() == -1) {
283 __linker_error("error: unable to open file \"%s\"\n", orig_path);
284 }
285
286 if (TEMP_FAILURE_RETRY(fstat(fd.get(), &result.file_stat)) == -1) {
287 __linker_error("error: unable to stat \"%s\": %s\n", result.path.c_str(), strerror(errno));
288 }
289
290 ElfReader elf_reader;
291 if (!elf_reader.Read(result.path.c_str(), fd.get(), file_offset, result.file_stat.st_size)) {
292 __linker_error("error: %s\n", linker_get_error_buffer());
293 }
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400294 address_space_params address_space;
295 if (!elf_reader.Load(&address_space)) {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700296 __linker_error("error: %s\n", linker_get_error_buffer());
297 }
298
299 result.phdr = elf_reader.loaded_phdr();
300 result.phdr_count = elf_reader.phdr_count();
301 result.entry_point = elf_reader.entry_point();
302 return result;
303}
304
Tamas Petz8d55d182020-02-24 14:15:25 +0100305static void platform_properties_init() {
306#if defined(__aarch64__)
307 const unsigned long hwcap2 = getauxval(AT_HWCAP2);
308 g_platform_properties.bti_supported = (hwcap2 & HWCAP2_BTI) != 0;
309#endif
310}
311
Ryan Prichard8f639a42018-10-01 23:10:05 -0700312static ElfW(Addr) linker_main(KernelArgumentBlock& args, const char* exe_to_load) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800313 ProtectedDataGuard guard;
314
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700315#if TIMING
316 struct timeval t0, t1;
317 gettimeofday(&t0, 0);
318#endif
319
320 // Sanitize the environment.
Ryan Prichard48b11592018-11-22 02:41:36 -0800321 __libc_init_AT_SECURE(args.envp);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700322
323 // Initialize system properties
324 __system_properties_init(); // may use 'environ'
325
Tamas Petz8d55d182020-02-24 14:15:25 +0100326 // Initialize platform properties.
327 platform_properties_init();
328
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700329 // Register the debuggerd signal handler.
Ryan Prichard80e40f02019-10-31 19:54:46 -0700330 linker_debuggerd_init();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700331
332 g_linker_logger.ResetState();
333
334 // Get a few environment variables.
335 const char* LD_DEBUG = getenv("LD_DEBUG");
336 if (LD_DEBUG != nullptr) {
337 g_ld_debug_verbosity = atoi(LD_DEBUG);
338 }
339
340#if defined(__LP64__)
341 INFO("[ Android dynamic linker (64-bit) ]");
342#else
343 INFO("[ Android dynamic linker (32-bit) ]");
344#endif
345
346 // These should have been sanitized by __libc_init_AT_SECURE, but the test
347 // doesn't cost us anything.
348 const char* ldpath_env = nullptr;
349 const char* ldpreload_env = nullptr;
350 if (!getauxval(AT_SECURE)) {
351 ldpath_env = getenv("LD_LIBRARY_PATH");
352 if (ldpath_env != nullptr) {
353 INFO("[ LD_LIBRARY_PATH set to \"%s\" ]", ldpath_env);
354 }
355 ldpreload_env = getenv("LD_PRELOAD");
356 if (ldpreload_env != nullptr) {
357 INFO("[ LD_PRELOAD set to \"%s\" ]", ldpreload_env);
358 }
359 }
360
Ryan Prichard8f639a42018-10-01 23:10:05 -0700361 const ExecutableInfo exe_info = exe_to_load ? load_executable(exe_to_load) :
Ryan Prichard07440a82018-11-22 03:16:06 -0800362 get_executable_info();
Ryan Prichard8f639a42018-10-01 23:10:05 -0700363
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700364 INFO("[ Linking executable \"%s\" ]", exe_info.path.c_str());
Martin Stjernholm95252ee2019-02-22 22:48:59 +0000365
Ryan Prichard04896452018-08-20 17:44:42 -0700366 // Initialize the main exe's soinfo.
Ryan Prichard8f639a42018-10-01 23:10:05 -0700367 soinfo* si = soinfo_alloc(&g_default_namespace,
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700368 exe_info.path.c_str(), &exe_info.file_stat,
Ryan Prichard8f639a42018-10-01 23:10:05 -0700369 0, RTLD_GLOBAL);
Ryan Prichard04896452018-08-20 17:44:42 -0700370 somain = si;
Ryan Prichard8f639a42018-10-01 23:10:05 -0700371 si->phdr = exe_info.phdr;
372 si->phnum = exe_info.phdr_count;
Ryan Prichard04896452018-08-20 17:44:42 -0700373 get_elf_base_from_phdr(si->phdr, si->phnum, &si->base, &si->load_bias);
374 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
375 si->dynamic = nullptr;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700376 si->set_main_executable();
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700377 init_link_map_head(*si);
378
Vic Yang1bf62b22019-08-13 14:53:28 -0700379 set_bss_vma_name(si);
380
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700381 // Use the executable's PT_INTERP string as the solinker filename in the
382 // dynamic linker's module list. gdb reads both PT_INTERP and the module list,
383 // and if the paths for the linker are different, gdb will report that the
384 // PT_INTERP linker path was unloaded once the module list is initialized.
385 // There are three situations to handle:
386 // - the APEX linker (/system/bin/linker[64] -> /apex/.../linker[64])
387 // - the ASAN linker (/system/bin/linker_asan[64] -> /apex/.../linker[64])
388 // - the bootstrap linker (/system/bin/bootstrap/linker[64])
389 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
390 somain->load_bias);
391 if (interp == nullptr) {
392 // This case can happen if the linker attempts to execute itself
393 // (e.g. "linker64 /system/bin/linker64").
394 interp = kFallbackLinkerPath;
395 }
396 solinker->set_realpath(interp);
397 init_link_map_head(*solinker);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700398
Tamas Petz8d55d182020-02-24 14:15:25 +0100399#if defined(__aarch64__)
400 if (exe_to_load == nullptr) {
401 // Kernel does not add PROT_BTI to executable pages of the loaded ELF.
402 // Apply appropriate protections here if it is needed.
403 auto note_gnu_property = GnuPropertySection(somain);
404 if (note_gnu_property.IsBTICompatible() &&
405 (phdr_table_protect_segments(somain->phdr, somain->phnum, somain->load_bias,
406 &note_gnu_property) < 0)) {
407 __linker_error("error: can't protect segments for \"%s\": %s", exe_info.path.c_str(),
408 strerror(errno));
409 }
410 }
Evgenii Stepanov8564b8d2020-12-15 13:55:32 -0800411
Evgenii Stepanovf9fa32a2022-05-12 15:54:38 -0700412 __libc_init_mte(somain->phdr, somain->phnum, somain->load_bias, args.argv);
Tamas Petz8d55d182020-02-24 14:15:25 +0100413#endif
414
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700415 // Register the main executable and the linker upfront to have
416 // gdb aware of them before loading the rest of the dependency
417 // tree.
Ryan Prichard04896452018-08-20 17:44:42 -0700418 //
419 // gdb expects the linker to be in the debug shared object list.
420 // Without this, gdb has trouble locating the linker's ".text"
421 // and ".plt" sections. Gdb could also potentially use this to
422 // relocate the offset of our exported 'rtld_db_dlactivity' symbol.
423 //
424 insert_link_map_into_debug_map(&si->link_map_head);
425 insert_link_map_into_debug_map(&solinker->link_map_head);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700426
Ryan Prichard07440a82018-11-22 03:16:06 -0800427 add_vdso();
Ryan Prichard14dd9922018-08-20 17:43:44 -0700428
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700429 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
Elliott Hughes3bdb31b2017-01-07 10:38:20 -0800430
431 // We haven't supported non-PIE since Lollipop for security reasons.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700432 if (elf_hdr->e_type != ET_DYN) {
Elliott Hughesad2d0382017-07-31 11:43:34 -0700433 // We don't use async_safe_fatal here because we don't want a tombstone:
434 // even after several years we still find ourselves on app compatibility
Elliott Hughes3bdb31b2017-01-07 10:38:20 -0800435 // investigations because some app's trying to launch an executable that
436 // hasn't worked in at least three years, and we've "helpfully" dropped a
437 // tombstone for them. The tombstone never provided any detail relevant to
438 // fixing the problem anyway, and the utility of drawing extra attention
439 // to the problem is non-existent at this late date.
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700440 async_safe_format_fd(STDERR_FILENO,
Elliott Hughesad2d0382017-07-31 11:43:34 -0700441 "\"%s\": error: Android 5.0 and later only support "
442 "position-independent executables (-fPIE).\n",
443 g_argv[0]);
Elliott Hughes90f96b92019-05-09 15:56:39 -0700444 _exit(EXIT_FAILURE);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700445 }
446
447 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
448 parse_LD_LIBRARY_PATH(ldpath_env);
449 parse_LD_PRELOAD(ldpreload_env);
450
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700451 std::vector<android_namespace_t*> namespaces = init_default_namespaces(exe_info.path.c_str());
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700452
Elliott Hughesad2d0382017-07-31 11:43:34 -0700453 if (!si->prelink_image()) __linker_cannot_link(g_argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700454
455 // add somain to global group
456 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
Jiyong Park02586a22017-05-20 01:01:24 +0900457 // ... and add it to all other linked namespaces
458 for (auto linked_ns : namespaces) {
459 if (linked_ns != &g_default_namespace) {
460 linked_ns->add_soinfo(somain);
461 somain->add_secondary_namespace(linked_ns);
462 }
463 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700464
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800465 linker_setup_exe_static_tls(g_argv[0]);
466
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700467 // Load ld_preloads and dependencies.
468 std::vector<const char*> needed_library_name_list;
469 size_t ld_preloads_count = 0;
470
471 for (const auto& ld_preload_name : g_ld_preload_names) {
472 needed_library_name_list.push_back(ld_preload_name.c_str());
473 ++ld_preloads_count;
474 }
475
476 for_each_dt_needed(si, [&](const char* name) {
477 needed_library_name_list.push_back(name);
478 });
479
480 const char** needed_library_names = &needed_library_name_list[0];
481 size_t needed_libraries_count = needed_library_name_list.size();
482
483 if (needed_libraries_count > 0 &&
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800484 !find_libraries(&g_default_namespace,
485 si,
486 needed_library_names,
487 needed_libraries_count,
488 nullptr,
489 &g_ld_preloads,
490 ld_preloads_count,
491 RTLD_GLOBAL,
492 nullptr,
493 true /* add_as_children */,
Jiyong Park02586a22017-05-20 01:01:24 +0900494 &namespaces)) {
Elliott Hughesad2d0382017-07-31 11:43:34 -0700495 __linker_cannot_link(g_argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700496 } else if (needed_libraries_count == 0) {
Ryan Prichard339ecef2020-01-02 16:36:06 -0800497 if (!si->link_image(SymbolLookupList(si), si, nullptr, nullptr)) {
Elliott Hughesad2d0382017-07-31 11:43:34 -0700498 __linker_cannot_link(g_argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700499 }
500 si->increment_ref_count();
501 }
502
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800503 linker_finalize_static_tls();
Ryan Prichard45d13492019-01-03 02:51:30 -0800504 __libc_init_main_thread_final();
505
Elliott Hughesad2d0382017-07-31 11:43:34 -0700506 if (!get_cfi_shadow()->InitialLinkDone(solist)) __linker_cannot_link(g_argv[0]);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700507
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800508 si->call_pre_init_constructors();
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800509 si->call_constructors();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700510
511#if TIMING
512 gettimeofday(&t1, nullptr);
Vic Yang7b9db342019-04-16 14:54:58 -0700513 PRINT("LINKER TIME: %s: %d microseconds", g_argv[0],
514 static_cast<int>(((static_cast<long long>(t1.tv_sec) * 1000000LL) +
515 static_cast<long long>(t1.tv_usec)) -
516 ((static_cast<long long>(t0.tv_sec) * 1000000LL) +
517 static_cast<long long>(t0.tv_usec))));
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700518#endif
519#if STATS
Vic Yang542db792019-07-25 10:39:27 -0700520 print_linker_stats();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700521#endif
Ryan Prichard78cd2832019-10-25 17:46:43 -0700522#if TIMING || STATS
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700523 fflush(stdout);
524#endif
525
Vic Yangbb7e1232019-01-29 20:23:16 -0800526 // We are about to hand control over to the executable loaded. We don't want
527 // to leave dirty pages behind unnecessarily.
528 purge_unused_memory();
529
Ryan Prichard8f639a42018-10-01 23:10:05 -0700530 ElfW(Addr) entry = exe_info.entry_point;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700531 TRACE("[ Ready to execute \"%s\" @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
532 return entry;
533}
534
535/* Compute the load-bias of an existing executable. This shall only
536 * be used to compute the load bias of an executable or shared library
537 * that was loaded by the kernel itself.
538 *
539 * Input:
540 * elf -> address of ELF header, assumed to be at the start of the file.
541 * Return:
542 * load bias, i.e. add the value of any p_vaddr in the file to get
543 * the corresponding address in memory.
544 */
545static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
546 ElfW(Addr) offset = elf->e_phoff;
547 const ElfW(Phdr)* phdr_table =
548 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
549 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
550
551 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
552 if (phdr->p_type == PT_LOAD) {
553 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
554 }
555 }
556 return 0;
557}
558
Ryan Prichard9729f352018-07-13 22:40:26 -0700559/* Find the load bias and base address of an executable or shared object loaded
560 * by the kernel. The ELF file's PHDR table must have a PT_PHDR entry.
561 *
562 * A VDSO doesn't have a PT_PHDR entry in its PHDR table.
563 */
564static void get_elf_base_from_phdr(const ElfW(Phdr)* phdr_table, size_t phdr_count,
565 ElfW(Addr)* base, ElfW(Addr)* load_bias) {
566 for (size_t i = 0; i < phdr_count; ++i) {
567 if (phdr_table[i].p_type == PT_PHDR) {
568 *load_bias = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_vaddr;
569 *base = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_offset;
570 return;
571 }
572 }
573 async_safe_fatal("Could not find a PHDR: broken executable?");
574}
575
Vic Yang1bf62b22019-08-13 14:53:28 -0700576/*
577 * Set anonymous VMA name for .bss section. For DSOs loaded by the linker, this
578 * is done by ElfReader. This function is here for DSOs loaded by the kernel,
579 * namely the linker itself and the main executable.
580 */
581static void set_bss_vma_name(soinfo* si) {
582 for (size_t i = 0; i < si->phnum; ++i) {
583 auto phdr = &si->phdr[i];
584
585 if (phdr->p_type != PT_LOAD) {
586 continue;
587 }
588
589 ElfW(Addr) seg_start = phdr->p_vaddr + si->load_bias;
590 ElfW(Addr) seg_page_end = PAGE_END(seg_start + phdr->p_memsz);
591 ElfW(Addr) seg_file_end = PAGE_END(seg_start + phdr->p_filesz);
592
593 if (seg_page_end > seg_file_end) {
594 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME,
595 reinterpret_cast<void*>(seg_file_end), seg_page_end - seg_file_end,
596 ".bss");
597 }
598 }
599}
600
Ryan Prichard249757b2019-11-01 17:18:28 -0700601#if defined(USE_RELA)
Peter Collingbourne1583cd22021-08-18 17:04:23 -0700602using RelType = ElfW(Rela);
603const unsigned kRelTag = DT_RELA;
604const unsigned kRelSzTag = DT_RELASZ;
Ryan Prichard249757b2019-11-01 17:18:28 -0700605#else
Peter Collingbourne1583cd22021-08-18 17:04:23 -0700606using RelType = ElfW(Rel);
607const unsigned kRelTag = DT_REL;
608const unsigned kRelSzTag = DT_RELSZ;
609#endif
Ryan Prichard249757b2019-11-01 17:18:28 -0700610
Peter Collingbourne1583cd22021-08-18 17:04:23 -0700611extern __LIBC_HIDDEN__ ElfW(Ehdr) __ehdr_start;
612
613static void call_ifunc_resolvers_for_section(RelType* begin, RelType* end) {
614 auto ehdr = reinterpret_cast<ElfW(Addr)>(&__ehdr_start);
615 for (RelType *r = begin; r != end; ++r) {
616 if (ELFW(R_TYPE)(r->r_info) != R_GENERIC_IRELATIVE) {
617 continue;
618 }
619 ElfW(Addr)* offset = reinterpret_cast<ElfW(Addr)*>(ehdr + r->r_offset);
620#if defined(USE_RELA)
621 ElfW(Addr) resolver = ehdr + r->r_addend;
622#else
623 ElfW(Addr) resolver = ehdr + *offset;
624#endif
Ryan Prichard249757b2019-11-01 17:18:28 -0700625 *offset = __bionic_call_ifunc_resolver(resolver);
626 }
627}
Peter Collingbourne1583cd22021-08-18 17:04:23 -0700628
629static void call_ifunc_resolvers() {
630 // Find the IRELATIVE relocations using the DT_JMPREL and DT_PLTRELSZ, or DT_RELA? and DT_RELA?SZ
631 // dynamic tags.
632 auto ehdr = reinterpret_cast<ElfW(Addr)>(&__ehdr_start);
633 auto* phdr = reinterpret_cast<ElfW(Phdr)*>(ehdr + __ehdr_start.e_phoff);
634 for (size_t i = 0; i != __ehdr_start.e_phnum; ++i) {
635 if (phdr[i].p_type != PT_DYNAMIC) {
636 continue;
637 }
638 auto *dyn = reinterpret_cast<ElfW(Dyn)*>(ehdr + phdr[i].p_vaddr);
639 ElfW(Addr) pltrel = 0, pltrelsz = 0, rel = 0, relsz = 0;
640 for (size_t j = 0, size = phdr[i].p_filesz / sizeof(ElfW(Dyn)); j != size; ++j) {
641 if (dyn[j].d_tag == DT_JMPREL) {
642 pltrel = dyn[j].d_un.d_ptr;
643 } else if (dyn[j].d_tag == DT_PLTRELSZ) {
644 pltrelsz = dyn[j].d_un.d_ptr;
645 } else if (dyn[j].d_tag == kRelTag) {
646 rel = dyn[j].d_un.d_ptr;
647 } else if (dyn[j].d_tag == kRelSzTag) {
648 relsz = dyn[j].d_un.d_ptr;
649 }
650 }
651 if (pltrel && pltrelsz) {
652 call_ifunc_resolvers_for_section(reinterpret_cast<RelType*>(ehdr + pltrel),
653 reinterpret_cast<RelType*>(ehdr + pltrel + pltrelsz));
654 }
655 if (rel && relsz) {
656 call_ifunc_resolvers_for_section(reinterpret_cast<RelType*>(ehdr + rel),
657 reinterpret_cast<RelType*>(ehdr + rel + relsz));
658 }
659 }
660}
Ryan Prichard249757b2019-11-01 17:18:28 -0700661
Ryan Prichard94a8e852019-11-05 14:19:18 -0800662// Usable before ifunc resolvers have been called. This function is compiled with -ffreestanding.
663static void linker_memclr(void* dst, size_t cnt) {
664 for (size_t i = 0; i < cnt; ++i) {
665 reinterpret_cast<char*>(dst)[i] = '\0';
666 }
667}
668
Ryan Prichard1990ba52019-02-07 21:31:31 -0800669// Detect an attempt to run the linker on itself. e.g.:
670// /system/bin/linker64 /system/bin/linker64
671// Use priority-1 to run this constructor before other constructors.
672__attribute__((constructor(1))) static void detect_self_exec() {
673 // Normally, the linker initializes the auxv global before calling its
674 // constructors. If the linker loads itself, though, the first loader calls
675 // the second loader's constructors before calling __linker_init.
676 if (__libc_shared_globals()->auxv != nullptr) {
677 return;
678 }
679#if defined(__i386__)
680 // We don't have access to the auxv struct from here, so use the int 0x80
681 // fallback.
682 __libc_sysinfo = reinterpret_cast<void*>(__libc_int0x80);
683#endif
684 __linker_error("error: linker cannot load itself\n");
685}
686
Ryan Prichard742982d2018-05-30 22:32:17 -0700687static ElfW(Addr) __attribute__((noinline))
Ryan Prichard04896452018-08-20 17:44:42 -0700688__linker_init_post_relocation(KernelArgumentBlock& args, soinfo& linker_so);
Ryan Prichard742982d2018-05-30 22:32:17 -0700689
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700690/*
691 * This is the entry point for the linker, called from begin.S. This
692 * method is responsible for fixing the linker's own relocations, and
693 * then calling __linker_init_post_relocation().
694 *
695 * Because this method is called before the linker has fixed it's own
696 * relocations, any attempt to reference an extern variable, extern
697 * function, or other GOT reference will generate a segfault.
698 */
699extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Ryan Prichard9cfca862018-11-22 02:44:09 -0800700 // Initialize TLS early so system calls and errno work.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700701 KernelArgumentBlock args(raw_args);
Ryan Prichard94a8e852019-11-05 14:19:18 -0800702 bionic_tcb temp_tcb __attribute__((uninitialized));
703 linker_memclr(&temp_tcb, sizeof(temp_tcb));
Ryan Prichard45d13492019-01-03 02:51:30 -0800704 __libc_init_main_thread_early(args, &temp_tcb);
Ryan Prichard27475b52018-05-17 17:14:18 -0700705
Ryan Prichard8f639a42018-10-01 23:10:05 -0700706 // When the linker is run by itself (rather than as an interpreter for
707 // another program), AT_BASE is 0.
Ryan Prichard07440a82018-11-22 03:16:06 -0800708 ElfW(Addr) linker_addr = getauxval(AT_BASE);
Ryan Prichard9729f352018-07-13 22:40:26 -0700709 if (linker_addr == 0) {
Ryan Prichard1990ba52019-02-07 21:31:31 -0800710 // The AT_PHDR and AT_PHNUM aux values describe this linker instance, so use
711 // the phdr to find the linker's base address.
Ryan Prichard9729f352018-07-13 22:40:26 -0700712 ElfW(Addr) load_bias;
713 get_elf_base_from_phdr(
Ryan Prichard07440a82018-11-22 03:16:06 -0800714 reinterpret_cast<ElfW(Phdr)*>(getauxval(AT_PHDR)), getauxval(AT_PHNUM),
Ryan Prichard9729f352018-07-13 22:40:26 -0700715 &linker_addr, &load_bias);
716 }
George Burgess IV70591002017-06-27 16:23:45 -0700717
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700718 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
719 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
720
Ryan Prichard249757b2019-11-01 17:18:28 -0700721 // string.h functions must not be used prior to calling the linker's ifunc resolvers.
Peter Collingbourne1583cd22021-08-18 17:04:23 -0700722 call_ifunc_resolvers();
Ryan Prichard249757b2019-11-01 17:18:28 -0700723
Ryan Prichard04896452018-08-20 17:44:42 -0700724 soinfo tmp_linker_so(nullptr, nullptr, nullptr, 0, 0);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700725
Ryan Prichard04896452018-08-20 17:44:42 -0700726 tmp_linker_so.base = linker_addr;
727 tmp_linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
Peter Collingbourne1583cd22021-08-18 17:04:23 -0700728 tmp_linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Ryan Prichard04896452018-08-20 17:44:42 -0700729 tmp_linker_so.dynamic = nullptr;
730 tmp_linker_so.phdr = phdr;
731 tmp_linker_so.phnum = elf_hdr->e_phnum;
732 tmp_linker_so.set_linker_flag();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700733
734 // Prelink the linker so we can access linker globals.
Ryan Prichard04896452018-08-20 17:44:42 -0700735 if (!tmp_linker_so.prelink_image()) __linker_cannot_link(args.argv[0]);
Ryan Prichard339ecef2020-01-02 16:36:06 -0800736 if (!tmp_linker_so.link_image(SymbolLookupList(&tmp_linker_so), &tmp_linker_so, nullptr, nullptr)) __linker_cannot_link(args.argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700737
Ryan Prichard04896452018-08-20 17:44:42 -0700738 return __linker_init_post_relocation(args, tmp_linker_so);
Ryan Prichard742982d2018-05-30 22:32:17 -0700739}
740
741/*
742 * This code is called after the linker has linked itself and fixed its own
743 * GOT. It is safe to make references to externs and other non-local data at
744 * this point. The compiler sometimes moves GOT references earlier in a
745 * function, so avoid inlining this function (http://b/80503879).
746 */
747static ElfW(Addr) __attribute__((noinline))
Ryan Prichard04896452018-08-20 17:44:42 -0700748__linker_init_post_relocation(KernelArgumentBlock& args, soinfo& tmp_linker_so) {
Ryan Prichard9cfca862018-11-22 02:44:09 -0800749 // Finish initializing the main thread.
Ryan Prichard07440a82018-11-22 03:16:06 -0800750 __libc_init_main_thread_late();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700751
752 // We didn't protect the linker's RELRO pages in link_image because we
753 // couldn't make system calls on x86 at that point, but we can now...
Ryan Prichard04896452018-08-20 17:44:42 -0700754 if (!tmp_linker_so.protect_relro()) __linker_cannot_link(args.argv[0]);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700755
Vic Yang1bf62b22019-08-13 14:53:28 -0700756 // And we can set VMA name for the bss section now
757 set_bss_vma_name(&tmp_linker_so);
758
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700759 // Initialize the linker's static libc's globals
Ryan Prichard07440a82018-11-22 03:16:06 -0800760 __libc_init_globals();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700761
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700762 // Initialize the linker's own global variables
Ryan Prichard04896452018-08-20 17:44:42 -0700763 tmp_linker_so.call_constructors();
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700764
Ryan Prichardbb1e3732021-01-12 23:09:10 -0800765 // Setting the linker soinfo's soname can allocate heap memory, so delay it until here.
766 for (const ElfW(Dyn)* d = tmp_linker_so.dynamic; d->d_tag != DT_NULL; ++d) {
767 if (d->d_tag == DT_SONAME) {
768 tmp_linker_so.set_soname(tmp_linker_so.get_string(d->d_un.d_val));
769 }
770 }
771
Ryan Prichard8f639a42018-10-01 23:10:05 -0700772 // When the linker is run directly rather than acting as PT_INTERP, parse
773 // arguments and determine the executable to load. When it's instead acting
774 // as PT_INTERP, AT_ENTRY will refer to the loaded executable rather than the
775 // linker's _start.
776 const char* exe_to_load = nullptr;
Ryan Prichard07440a82018-11-22 03:16:06 -0800777 if (getauxval(AT_ENTRY) == reinterpret_cast<uintptr_t>(&_start)) {
Elliott Hughes90f96b92019-05-09 15:56:39 -0700778 if (args.argc == 3 && !strcmp(args.argv[1], "--list")) {
779 // We're being asked to behave like ldd(1).
780 g_is_ldd = true;
781 exe_to_load = args.argv[2];
782 } else if (args.argc <= 1 || !strcmp(args.argv[1], "--help")) {
Ryan Prichard8f639a42018-10-01 23:10:05 -0700783 async_safe_format_fd(STDOUT_FILENO,
Elliott Hughes90f96b92019-05-09 15:56:39 -0700784 "Usage: %s [--list] PROGRAM [ARGS-FOR-PROGRAM...]\n"
785 " %s [--list] path.zip!/PROGRAM [ARGS-FOR-PROGRAM...]\n"
Ryan Prichard8f639a42018-10-01 23:10:05 -0700786 "\n"
787 "A helper program for linking dynamic executables. Typically, the kernel loads\n"
788 "this program because it's the PT_INTERP of a dynamic executable.\n"
789 "\n"
790 "This program can also be run directly to load and run a dynamic executable. The\n"
791 "executable can be inside a zip file if it's stored uncompressed and at a\n"
Elliott Hughes90f96b92019-05-09 15:56:39 -0700792 "page-aligned offset.\n"
793 "\n"
794 "The --list option gives behavior equivalent to ldd(1) on other systems.\n",
Ryan Prichard8f639a42018-10-01 23:10:05 -0700795 args.argv[0], args.argv[0]);
Elliott Hughes90f96b92019-05-09 15:56:39 -0700796 _exit(EXIT_SUCCESS);
797 } else {
798 exe_to_load = args.argv[1];
799 __libc_shared_globals()->initial_linker_arg_count = 1;
Ryan Prichard8f639a42018-10-01 23:10:05 -0700800 }
Dimitry Ivanov9b1cc4b2017-03-23 16:17:15 -0700801 }
802
Ryan Prichard8f639a42018-10-01 23:10:05 -0700803 // store argc/argv/envp to use them for calling constructors
Ryan Prichardabf736a2018-11-22 02:40:17 -0800804 g_argc = args.argc - __libc_shared_globals()->initial_linker_arg_count;
805 g_argv = args.argv + __libc_shared_globals()->initial_linker_arg_count;
Ryan Prichard8f639a42018-10-01 23:10:05 -0700806 g_envp = args.envp;
Ryan Prichard48b11592018-11-22 02:41:36 -0800807 __libc_shared_globals()->init_progname = g_argv[0];
Ryan Prichard8f639a42018-10-01 23:10:05 -0700808
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700809 // Initialize static variables. Note that in order to
810 // get correct libdl_info we need to call constructors
811 // before get_libdl_info().
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700812 sonext = solist = solinker = get_libdl_info(tmp_linker_so);
Ryan Prichard04896452018-08-20 17:44:42 -0700813 g_default_namespace.add_soinfo(solinker);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700814
Ryan Prichard8f639a42018-10-01 23:10:05 -0700815 ElfW(Addr) start_address = linker_main(args, exe_to_load);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700816
Elliott Hughes90f96b92019-05-09 15:56:39 -0700817 if (g_is_ldd) _exit(EXIT_SUCCESS);
818
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700819 INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
820
821 // Return the address that the calling assembly stub should jump to.
822 return start_address;
823}