blob: d037a1832763e532cd963b836570874894ee4a5b [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
38#include "private/bionic_globals.h"
39#include "private/bionic_tls.h"
40#include "private/KernelArgumentBlock.h"
41
42#include "android-base/strings.h"
43#include "android-base/stringprintf.h"
Dan Willemsen7ec52b12016-11-28 17:02:25 -080044#ifdef __ANDROID__
Josh Gao2a3b4fa2016-10-26 17:55:49 -070045#include "debuggerd/handler.h"
Dan Willemsen7ec52b12016-11-28 17:02:25 -080046#endif
Dimitry Ivanov3f660572016-09-09 10:00:39 -070047
48#include <vector>
49
50extern void __libc_init_globals(KernelArgumentBlock&);
51extern void __libc_init_AT_SECURE(KernelArgumentBlock&);
52
53extern "C" void _start();
54
55static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
56
57// These should be preserved static to avoid emitting
58// RELATIVE relocations for the part of the code running
59// before linker links itself.
60
61// TODO (dimtiry): remove somain, rename solist to solist_head
62static soinfo* solist;
63static soinfo* sonext;
64static soinfo* somain; // main process, always the one after libdl_info
65
66void solist_add_soinfo(soinfo* si) {
67 sonext->next = si;
68 sonext = si;
69}
70
71bool solist_remove_soinfo(soinfo* si) {
72 soinfo *prev = nullptr, *trav;
73 for (trav = solist; trav != nullptr; trav = trav->next) {
74 if (trav == si) {
75 break;
76 }
77 prev = trav;
78 }
79
80 if (trav == nullptr) {
81 // si was not in solist
82 PRINT("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
83 return false;
84 }
85
86 // prev will never be null, because the first entry in solist is
87 // always the static libdl_info.
88 prev->next = si->next;
89 if (si == sonext) {
90 sonext = prev;
91 }
92
93 return true;
94}
95
96soinfo* solist_get_head() {
97 return solist;
98}
99
100soinfo* solist_get_somain() {
101 return somain;
102}
103
104int g_ld_debug_verbosity;
105abort_msg_t* g_abort_message = nullptr; // For debuggerd.
106
107static std::vector<std::string> g_ld_preload_names;
108
109static std::vector<soinfo*> g_ld_preloads;
110
111static void parse_path(const char* path, const char* delimiters,
112 std::vector<std::string>* resolved_paths) {
113 std::vector<std::string> paths;
114 split_path(path, delimiters, &paths);
115 resolve_paths(paths, resolved_paths);
116}
117
118static void parse_LD_LIBRARY_PATH(const char* path) {
119 std::vector<std::string> ld_libary_paths;
120 parse_path(path, ":", &ld_libary_paths);
121 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
122}
123
124static void parse_LD_PRELOAD(const char* path) {
125 g_ld_preload_names.clear();
126 if (path != nullptr) {
127 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
128 g_ld_preload_names = android::base::Split(path, " :");
129 std::remove_if(g_ld_preload_names.begin(),
130 g_ld_preload_names.end(),
131 [] (const std::string& s) { return s.empty(); });
132 }
133}
134
135// An empty list of soinfos
136static soinfo_list_t g_empty_list;
137
138static void add_vdso(KernelArgumentBlock& args __unused) {
139#if defined(AT_SYSINFO_EHDR)
140 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
141 if (ehdr_vdso == nullptr) {
142 return;
143 }
144
145 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
146
147 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
148 si->phnum = ehdr_vdso->e_phnum;
149 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
150 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
151 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
152
153 si->prelink_image();
154 si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr);
155#endif
156}
157
158/* gdb expects the linker to be in the debug shared object list.
159 * Without this, gdb has trouble locating the linker's ".text"
160 * and ".plt" sections. Gdb could also potentially use this to
161 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
162 * Note that the linker shouldn't be on the soinfo list.
163 */
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800164static void init_linker_info_for_gdb(ElfW(Addr) linker_base, char* linker_path) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700165 static link_map linker_link_map_for_gdb;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700166
167 linker_link_map_for_gdb.l_addr = linker_base;
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800168 linker_link_map_for_gdb.l_name = linker_path;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700169
170 /*
171 * Set the dynamic field in the link map otherwise gdb will complain with
172 * the following:
173 * warning: .dynamic section for "/system/bin/linker" is not at the
174 * expected address (wrong library or version mismatch?)
175 */
176 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
177 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
178 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
179 &linker_link_map_for_gdb.l_ld, nullptr);
180
181 insert_link_map_into_debug_map(&linker_link_map_for_gdb);
182}
183
184extern "C" int __system_properties_init(void);
185
186static const char* get_executable_path() {
187 static std::string executable_path;
188 if (executable_path.empty()) {
189 char path[PATH_MAX];
190 ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path));
191 if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) {
192 __libc_fatal("readlink('/proc/self/exe') failed: %s", strerror(errno));
193 }
194 executable_path = std::string(path, path_len);
195 }
196
197 return executable_path.c_str();
198}
199
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800200#if defined(__LP64__)
201static char kLinkerPath[] = "/system/bin/linker64";
202#else
203static char kLinkerPath[] = "/system/bin/linker";
204#endif
205
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700206/*
207 * This code is called after the linker has linked itself and
208 * fixed it's own GOT. It is safe to make references to externs
209 * and other non-local data at this point.
210 */
211static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
212#if TIMING
213 struct timeval t0, t1;
214 gettimeofday(&t0, 0);
215#endif
216
217 // Sanitize the environment.
218 __libc_init_AT_SECURE(args);
219
220 // Initialize system properties
221 __system_properties_init(); // may use 'environ'
222
223 // Register the debuggerd signal handler.
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800224#ifdef __ANDROID__
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700225 debuggerd_callbacks_t callbacks = {
226 .get_abort_message = []() {
227 return g_abort_message;
228 },
229 .post_dump = &notify_gdb_of_libraries,
230 };
231 debuggerd_init(&callbacks);
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800232#endif
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700233
234 g_linker_logger.ResetState();
235
236 // Get a few environment variables.
237 const char* LD_DEBUG = getenv("LD_DEBUG");
238 if (LD_DEBUG != nullptr) {
239 g_ld_debug_verbosity = atoi(LD_DEBUG);
240 }
241
242#if defined(__LP64__)
243 INFO("[ Android dynamic linker (64-bit) ]");
244#else
245 INFO("[ Android dynamic linker (32-bit) ]");
246#endif
247
248 // These should have been sanitized by __libc_init_AT_SECURE, but the test
249 // doesn't cost us anything.
250 const char* ldpath_env = nullptr;
251 const char* ldpreload_env = nullptr;
252 if (!getauxval(AT_SECURE)) {
253 ldpath_env = getenv("LD_LIBRARY_PATH");
254 if (ldpath_env != nullptr) {
255 INFO("[ LD_LIBRARY_PATH set to \"%s\" ]", ldpath_env);
256 }
257 ldpreload_env = getenv("LD_PRELOAD");
258 if (ldpreload_env != nullptr) {
259 INFO("[ LD_PRELOAD set to \"%s\" ]", ldpreload_env);
260 }
261 }
262
263 struct stat file_stat;
264 // Stat "/proc/self/exe" instead of executable_path because
265 // the executable could be unlinked by this point and it should
266 // not cause a crash (see http://b/31084669)
267 if (TEMP_FAILURE_RETRY(stat("/proc/self/exe", &file_stat)) != 0) {
268 __libc_fatal("unable to stat \"/proc/self/exe\": %s", strerror(errno));
269 }
270
271 const char* executable_path = get_executable_path();
272 soinfo* si = soinfo_alloc(&g_default_namespace, executable_path, &file_stat, 0, RTLD_GLOBAL);
273 if (si == nullptr) {
274 __libc_fatal("Couldn't allocate soinfo: out of memory?");
275 }
276
277 /* bootstrap the link map, the main exe always needs to be first */
278 si->set_main_executable();
279 link_map* map = &(si->link_map_head);
280
281 // Register the main executable and the linker upfront to have
282 // gdb aware of them before loading the rest of the dependency
283 // tree.
284 map->l_addr = 0;
285 map->l_name = const_cast<char*>(executable_path);
286 insert_link_map_into_debug_map(map);
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800287 init_linker_info_for_gdb(linker_base, kLinkerPath);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700288
289 // Extract information passed from the kernel.
290 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
291 si->phnum = args.getauxval(AT_PHNUM);
292
293 /* Compute the value of si->base. We can't rely on the fact that
294 * the first entry is the PHDR because this will not be true
295 * for certain executables (e.g. some in the NDK unit test suite)
296 */
297 si->base = 0;
298 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
299 si->load_bias = 0;
300 for (size_t i = 0; i < si->phnum; ++i) {
301 if (si->phdr[i].p_type == PT_PHDR) {
302 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
303 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
304 break;
305 }
306 }
307 si->dynamic = nullptr;
308
309 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
Elliott Hughes3bdb31b2017-01-07 10:38:20 -0800310
311 // We haven't supported non-PIE since Lollipop for security reasons.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700312 if (elf_hdr->e_type != ET_DYN) {
Elliott Hughes3bdb31b2017-01-07 10:38:20 -0800313 // We don't use __libc_fatal here because we don't want a tombstone: it's
314 // been several years now but we still find ourselves on app compatibility
315 // investigations because some app's trying to launch an executable that
316 // hasn't worked in at least three years, and we've "helpfully" dropped a
317 // tombstone for them. The tombstone never provided any detail relevant to
318 // fixing the problem anyway, and the utility of drawing extra attention
319 // to the problem is non-existent at this late date.
Elliott Hughes607d3072017-01-09 14:39:44 -0800320 __libc_format_fd(STDERR_FILENO,
Elliott Hughes3bdb31b2017-01-07 10:38:20 -0800321 "\"%s\": error: Android 5.0 and later only support "
Elliott Hughes607d3072017-01-09 14:39:44 -0800322 "position-independent executables (-fPIE).\n",
Elliott Hughes3bdb31b2017-01-07 10:38:20 -0800323 g_argv[0]);
Dan Albert95e2e6f2017-01-31 16:02:43 -0800324 exit(EXIT_FAILURE);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700325 }
326
327 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
328 parse_LD_LIBRARY_PATH(ldpath_env);
329 parse_LD_PRELOAD(ldpreload_env);
330
331 somain = si;
332
333 init_default_namespace();
334
335 if (!si->prelink_image()) {
336 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
337 }
338
339 // add somain to global group
340 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
341
342 // Load ld_preloads and dependencies.
343 std::vector<const char*> needed_library_name_list;
344 size_t ld_preloads_count = 0;
345
346 for (const auto& ld_preload_name : g_ld_preload_names) {
347 needed_library_name_list.push_back(ld_preload_name.c_str());
348 ++ld_preloads_count;
349 }
350
351 for_each_dt_needed(si, [&](const char* name) {
352 needed_library_name_list.push_back(name);
353 });
354
355 const char** needed_library_names = &needed_library_name_list[0];
356 size_t needed_libraries_count = needed_library_name_list.size();
357
358 if (needed_libraries_count > 0 &&
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800359 !find_libraries(&g_default_namespace,
360 si,
361 needed_library_names,
362 needed_libraries_count,
363 nullptr,
364 &g_ld_preloads,
365 ld_preloads_count,
366 RTLD_GLOBAL,
367 nullptr,
368 true /* add_as_children */,
369 true /* search_linked_namespaces */)) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700370 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
371 } else if (needed_libraries_count == 0) {
372 if (!si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr)) {
373 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
374 }
375 si->increment_ref_count();
376 }
377
378 add_vdso(args);
379
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700380 if (!get_cfi_shadow()->InitialLinkDone(solist)) {
381 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
382 }
383
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700384 {
385 ProtectedDataGuard guard;
386
387 si->call_pre_init_constructors();
388
389 /* After the prelink_image, the si->load_bias is initialized.
390 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
391 * We need to update this value for so exe here. So Unwind_Backtrace
392 * for some arch like x86 could work correctly within so exe.
393 */
394 map->l_addr = si->load_bias;
395 si->call_constructors();
396 }
397
398#if TIMING
399 gettimeofday(&t1, nullptr);
400 PRINT("LINKER TIME: %s: %d microseconds", g_argv[0], (int) (
401 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
402 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
403#endif
404#if STATS
405 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", g_argv[0],
406 linker_stats.count[kRelocAbsolute],
407 linker_stats.count[kRelocRelative],
408 linker_stats.count[kRelocCopy],
409 linker_stats.count[kRelocSymbol]);
410#endif
411#if COUNT_PAGES
412 {
413 unsigned n;
414 unsigned i;
415 unsigned count = 0;
416 for (n = 0; n < 4096; n++) {
417 if (bitmask[n]) {
418 unsigned x = bitmask[n];
419#if defined(__LP64__)
420 for (i = 0; i < 32; i++) {
421#else
422 for (i = 0; i < 8; i++) {
423#endif
424 if (x & 1) {
425 count++;
426 }
427 x >>= 1;
428 }
429 }
430 }
431 PRINT("PAGES MODIFIED: %s: %d (%dKB)", g_argv[0], count, count * 4);
432 }
433#endif
434
435#if TIMING || STATS || COUNT_PAGES
436 fflush(stdout);
437#endif
438
439 ElfW(Addr) entry = args.getauxval(AT_ENTRY);
440 TRACE("[ Ready to execute \"%s\" @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
441 return entry;
442}
443
444/* Compute the load-bias of an existing executable. This shall only
445 * be used to compute the load bias of an executable or shared library
446 * that was loaded by the kernel itself.
447 *
448 * Input:
449 * elf -> address of ELF header, assumed to be at the start of the file.
450 * Return:
451 * load bias, i.e. add the value of any p_vaddr in the file to get
452 * the corresponding address in memory.
453 */
454static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
455 ElfW(Addr) offset = elf->e_phoff;
456 const ElfW(Phdr)* phdr_table =
457 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
458 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
459
460 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
461 if (phdr->p_type == PT_LOAD) {
462 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
463 }
464 }
465 return 0;
466}
467
468static void __linker_cannot_link(const char* argv0) {
469 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", argv0, linker_get_error_buffer());
470}
471
472/*
473 * This is the entry point for the linker, called from begin.S. This
474 * method is responsible for fixing the linker's own relocations, and
475 * then calling __linker_init_post_relocation().
476 *
477 * Because this method is called before the linker has fixed it's own
478 * relocations, any attempt to reference an extern variable, extern
479 * function, or other GOT reference will generate a segfault.
480 */
481extern "C" ElfW(Addr) __linker_init(void* raw_args) {
482 KernelArgumentBlock args(raw_args);
483
484 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
485 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
486 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
487 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
488
489 soinfo linker_so(nullptr, nullptr, nullptr, 0, 0);
490
491 // If the linker is not acting as PT_INTERP entry_point is equal to
492 // _start. Which means that the linker is running as an executable and
493 // already linked by PT_INTERP.
494 //
495 // This happens when user tries to run 'adb shell /system/bin/linker'
496 // see also https://code.google.com/p/android/issues/detail?id=63174
497 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
498 __libc_format_fd(STDOUT_FILENO,
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -0800499 "This is %s, the helper program for dynamic executables.\n",
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700500 args.argv[0]);
501 exit(0);
502 }
503
504 linker_so.base = linker_addr;
505 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
506 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
507 linker_so.dynamic = nullptr;
508 linker_so.phdr = phdr;
509 linker_so.phnum = elf_hdr->e_phnum;
510 linker_so.set_linker_flag();
511
512 // Prelink the linker so we can access linker globals.
513 if (!linker_so.prelink_image()) __linker_cannot_link(args.argv[0]);
514
515 // This might not be obvious... The reasons why we pass g_empty_list
516 // in place of local_group here are (1) we do not really need it, because
517 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
518 // itself without having to look into local_group and (2) allocators
519 // are not yet initialized, and therefore we cannot use linked_list.push_*
520 // functions at this point.
521 if (!linker_so.link_image(g_empty_list, g_empty_list, nullptr)) __linker_cannot_link(args.argv[0]);
522
523#if defined(__i386__)
524 // On x86, we can't make system calls before this point.
525 // We can't move this up because this needs to assign to a global.
526 // Note that until we call __libc_init_main_thread below we have
527 // no TLS, so you shouldn't make a system call that can fail, because
528 // it will SEGV when it tries to set errno.
529 __libc_init_sysinfo(args);
530#endif
531
532 // Initialize the main thread (including TLS, so system calls really work).
533 __libc_init_main_thread(args);
534
535 // We didn't protect the linker's RELRO pages in link_image because we
536 // couldn't make system calls on x86 at that point, but we can now...
537 if (!linker_so.protect_relro()) __linker_cannot_link(args.argv[0]);
538
539 // Initialize the linker's static libc's globals
540 __libc_init_globals(args);
541
542 // store argc/argv/envp to use them for calling constructors
543 g_argc = args.argc;
544 g_argv = args.argv;
545 g_envp = args.envp;
546
547 // Initialize the linker's own global variables
548 linker_so.call_constructors();
549
550 // Initialize static variables. Note that in order to
551 // get correct libdl_info we need to call constructors
552 // before get_libdl_info().
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800553 sonext = solist = get_libdl_info(kLinkerPath);
554 g_default_namespace.add_soinfo(solist);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700555
556 // We have successfully fixed our own relocations. It's safe to run
557 // the main part of the linker now.
558 args.abort_message_ptr = &g_abort_message;
559 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
560
561 INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
562
563 // Return the address that the calling assembly stub should jump to.
564 return start_address;
565}