• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <link.h>
32 #include <stdlib.h>
33 #include <sys/auxv.h>
34 #include <sys/prctl.h>
35 
36 #include "linker.h"
37 #include "linker_auxv.h"
38 #include "linker_cfi.h"
39 #include "linker_debug.h"
40 #include "linker_debuggerd.h"
41 #include "linker_gdb_support.h"
42 #include "linker_globals.h"
43 #include "linker_phdr.h"
44 #include "linker_relocate.h"
45 #include "linker_relocs.h"
46 #include "linker_tls.h"
47 #include "linker_utils.h"
48 
49 #include "platform/bionic/macros.h"
50 #include "private/KernelArgumentBlock.h"
51 #include "private/bionic_call_ifunc_resolver.h"
52 #include "private/bionic_globals.h"
53 #include "private/bionic_tls.h"
54 
55 #include "android-base/unique_fd.h"
56 #include "android-base/strings.h"
57 #include "android-base/stringprintf.h"
58 
59 #include <async_safe/log.h>
60 #include <bionic/libc_init_common.h>
61 #include <bionic/pthread_internal.h>
62 
63 #include <vector>
64 
65 __LIBC_HIDDEN__ extern "C" void _start();
66 
67 static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
68 
69 static void get_elf_base_from_phdr(const ElfW(Phdr)* phdr_table, size_t phdr_count,
70                                    ElfW(Addr)* base, ElfW(Addr)* load_bias);
71 
72 static void set_bss_vma_name(soinfo* si);
73 
74 void __libc_init_mte(const memtag_dynamic_entries_t* memtag_dynamic_entries, const void* phdr_start,
75                      size_t phdr_count, uintptr_t load_bias);
76 
77 void __libc_init_mte_stack(void* stack_top);
78 
__linker_cannot_link(const char * argv0)79 static void __linker_cannot_link(const char* argv0) {
80   __linker_error("CANNOT LINK EXECUTABLE \"%s\": %s", argv0, linker_get_error_buffer());
81 }
82 
83 // These should be preserved static to avoid emitting
84 // RELATIVE relocations for the part of the code running
85 // before linker links itself.
86 
87 // TODO (dimtiry): remove somain, rename solist to solist_head
88 static soinfo* solist;
89 static soinfo* sonext;
90 static soinfo* somain; // main process, always the one after libdl_info
91 static soinfo* solinker;
92 static soinfo* vdso; // vdso if present
93 
solist_add_soinfo(soinfo * si)94 void solist_add_soinfo(soinfo* si) {
95   sonext->next = si;
96   sonext = si;
97 }
98 
solist_remove_soinfo(soinfo * si)99 bool solist_remove_soinfo(soinfo* si) {
100   soinfo *prev = nullptr, *trav;
101   for (trav = solist; trav != nullptr; trav = trav->next) {
102     if (trav == si) {
103       break;
104     }
105     prev = trav;
106   }
107 
108   if (trav == nullptr) {
109     // si was not in solist
110     DL_WARN("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
111     return false;
112   }
113 
114   // prev will never be null, because the first entry in solist is
115   // always the static libdl_info.
116   CHECK(prev != nullptr);
117   prev->next = si->next;
118   if (si == sonext) {
119     sonext = prev;
120   }
121 
122   return true;
123 }
124 
solist_get_head()125 soinfo* solist_get_head() {
126   return solist;
127 }
128 
solist_get_somain()129 soinfo* solist_get_somain() {
130   return somain;
131 }
132 
solist_get_vdso()133 soinfo* solist_get_vdso() {
134   return vdso;
135 }
136 
137 bool g_is_ldd;
138 
139 static std::vector<std::string> g_ld_preload_names;
140 
141 static std::vector<soinfo*> g_ld_preloads;
142 
parse_path(const char * path,const char * delimiters,std::vector<std::string> * resolved_paths)143 static void parse_path(const char* path, const char* delimiters,
144                        std::vector<std::string>* resolved_paths) {
145   std::vector<std::string> paths;
146   split_path(path, delimiters, &paths);
147   resolve_paths(paths, resolved_paths);
148 }
149 
parse_LD_LIBRARY_PATH(const char * path)150 static void parse_LD_LIBRARY_PATH(const char* path) {
151   std::vector<std::string> ld_libary_paths;
152   parse_path(path, ":", &ld_libary_paths);
153   g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
154 }
155 
parse_LD_PRELOAD(const char * path)156 static void parse_LD_PRELOAD(const char* path) {
157   g_ld_preload_names.clear();
158   if (path != nullptr) {
159     // We have historically supported ':' as well as ' ' in LD_PRELOAD.
160     g_ld_preload_names = android::base::Split(path, " :");
161     g_ld_preload_names.erase(std::remove_if(g_ld_preload_names.begin(), g_ld_preload_names.end(),
162                                             [](const std::string& s) { return s.empty(); }),
163                              g_ld_preload_names.end());
164   }
165 }
166 
167 // An empty list of soinfos
168 static soinfo_list_t g_empty_list;
169 
add_vdso()170 static void add_vdso() {
171   ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(getauxval(AT_SYSINFO_EHDR));
172   if (ehdr_vdso == nullptr) {
173     return;
174   }
175 
176   vdso = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
177 
178   vdso->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
179   vdso->phnum = ehdr_vdso->e_phnum;
180   vdso->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
181   vdso->size = phdr_table_get_load_size(vdso->phdr, vdso->phnum);
182   vdso->load_bias = get_elf_exec_load_bias(ehdr_vdso);
183 
184   if (!vdso->prelink_image() || !vdso->link_image(SymbolLookupList(vdso), vdso, nullptr, nullptr)) {
185     __linker_cannot_link(g_argv[0]);
186   }
187 
188   // Prevent accidental unloads...
189   vdso->set_dt_flags_1(vdso->get_dt_flags_1() | DF_1_NODELETE);
190   vdso->set_linked();
191 }
192 
193 // Initializes an soinfo's link_map_head field using other fields from the
194 // soinfo (phdr, phnum, load_bias). The soinfo's realpath must not change after
195 // this function is called.
init_link_map_head(soinfo & info)196 static void init_link_map_head(soinfo& info) {
197   auto& map = info.link_map_head;
198   map.l_addr = info.load_bias;
199   map.l_name = const_cast<char*>(info.get_realpath());
200   phdr_table_get_dynamic_section(info.phdr, info.phnum, info.load_bias, &map.l_ld, nullptr);
201 }
202 
203 extern "C" int __system_properties_init(void);
204 
205 struct ExecutableInfo {
206   std::string path;
207   struct stat file_stat;
208   const ElfW(Phdr)* phdr;
209   size_t phdr_count;
210   ElfW(Addr) entry_point;
211   bool should_pad_segments;
212 };
213 
get_executable_info(const char * arg_path)214 static ExecutableInfo get_executable_info(const char* arg_path) {
215   ExecutableInfo result = {};
216   char const* exe_path = "/proc/self/exe";
217 
218   // Stat "/proc/self/exe" instead of executable_path because
219   // the executable could be unlinked by this point and it should
220   // not cause a crash (see http://b/31084669)
221   if (TEMP_FAILURE_RETRY(stat(exe_path, &result.file_stat) == -1)) {
222     // Fallback to argv[0] for the case where /proc isn't available
223     if (TEMP_FAILURE_RETRY(stat(arg_path, &result.file_stat) == -1)) {
224       async_safe_fatal("unable to stat either \"/proc/self/exe\" or \"%s\": %m", arg_path);
225     }
226     exe_path = arg_path;
227   }
228 
229   // Path might be a symlink; we need the target so that we get the right
230   // linker configuration later.
231   char sym_path[PATH_MAX];
232   result.path = std::string(realpath(exe_path, sym_path) != nullptr ? sym_path : exe_path);
233 
234   result.phdr = reinterpret_cast<const ElfW(Phdr)*>(getauxval(AT_PHDR));
235   result.phdr_count = getauxval(AT_PHNUM);
236   result.entry_point = getauxval(AT_ENTRY);
237   return result;
238 }
239 
240 // Load an executable. Normally the kernel has already loaded the executable when the linker
241 // starts. The linker can be invoked directly on an executable, though, and then the linker must
242 // load it. This function doesn't load dependencies or resolve relocations.
load_executable(const char * orig_path)243 static ExecutableInfo load_executable(const char* orig_path) {
244   ExecutableInfo result = {};
245 
246   if (orig_path[0] != '/') {
247     __linker_error("error: expected absolute path: \"%s\"", orig_path);
248   }
249 
250   off64_t file_offset;
251   android::base::unique_fd fd(open_executable(orig_path, &file_offset, &result.path));
252   if (fd.get() == -1) {
253     __linker_error("error: unable to open file \"%s\"", orig_path);
254   }
255 
256   if (TEMP_FAILURE_RETRY(fstat(fd.get(), &result.file_stat)) == -1) {
257     __linker_error("error: unable to stat \"%s\": %m", result.path.c_str());
258   }
259 
260   ElfReader elf_reader;
261   if (!elf_reader.Read(result.path.c_str(), fd.get(), file_offset, result.file_stat.st_size)) {
262     __linker_error("error: %s", linker_get_error_buffer());
263   }
264   address_space_params address_space;
265   if (!elf_reader.Load(&address_space)) {
266     __linker_error("error: %s", linker_get_error_buffer());
267   }
268 
269   result.phdr = elf_reader.loaded_phdr();
270   result.phdr_count = elf_reader.phdr_count();
271   result.entry_point = elf_reader.entry_point();
272   result.should_pad_segments = elf_reader.should_pad_segments();
273   return result;
274 }
275 
platform_properties_init()276 static void platform_properties_init() {
277 #if defined(__aarch64__)
278   const unsigned long hwcap2 = getauxval(AT_HWCAP2);
279   g_platform_properties.bti_supported = (hwcap2 & HWCAP2_BTI) != 0;
280 #endif
281 }
282 
linker_main(KernelArgumentBlock & args,const char * exe_to_load)283 static ElfW(Addr) linker_main(KernelArgumentBlock& args, const char* exe_to_load) {
284   ProtectedDataGuard guard;
285 
286   timeval t0, t1;
287   gettimeofday(&t0, nullptr);
288 
289   // Sanitize the environment.
290   __libc_init_AT_SECURE(args.envp);
291 
292   // Initialize system properties
293   __system_properties_init(); // may use 'environ'
294 
295   // Initialize platform properties.
296   platform_properties_init();
297 
298   // Register the debuggerd signal handler.
299   linker_debuggerd_init();
300 
301   g_linker_logger.ResetState();
302 
303   // Enable debugging logs?
304   const char* LD_DEBUG = getenv("LD_DEBUG");
305   if (LD_DEBUG != nullptr) init_LD_DEBUG(LD_DEBUG);
306 
307   if (getenv("LD_SHOW_AUXV") != nullptr) ld_show_auxv(args.auxv);
308 
309   LD_DEBUG(any, "[ Android dynamic linker (" ABI_STRING ") ]");
310 
311   // These should have been sanitized by __libc_init_AT_SECURE, but the test
312   // doesn't cost us anything.
313   const char* ldpath_env = nullptr;
314   const char* ldpreload_env = nullptr;
315   if (!getauxval(AT_SECURE)) {
316     ldpath_env = getenv("LD_LIBRARY_PATH");
317     if (ldpath_env != nullptr) {
318       LD_DEBUG(any, "[ LD_LIBRARY_PATH set to \"%s\" ]", ldpath_env);
319     }
320     ldpreload_env = getenv("LD_PRELOAD");
321     if (ldpreload_env != nullptr) {
322       LD_DEBUG(any, "[ LD_PRELOAD set to \"%s\" ]", ldpreload_env);
323     }
324   }
325 
326   const ExecutableInfo exe_info = exe_to_load ? load_executable(exe_to_load) :
327                                                 get_executable_info(args.argv[0]);
328 
329   LD_DEBUG(any, "[ Linking executable \"%s\" ]", exe_info.path.c_str());
330 
331   // Initialize the main exe's soinfo.
332   soinfo* si = soinfo_alloc(&g_default_namespace,
333                             exe_info.path.c_str(), &exe_info.file_stat,
334                             0, RTLD_GLOBAL);
335   somain = si;
336   si->phdr = exe_info.phdr;
337   si->phnum = exe_info.phdr_count;
338   si->set_should_pad_segments(exe_info.should_pad_segments);
339   get_elf_base_from_phdr(si->phdr, si->phnum, &si->base, &si->load_bias);
340   si->size = phdr_table_get_load_size(si->phdr, si->phnum);
341   si->dynamic = nullptr;
342   si->set_main_executable();
343   init_link_map_head(*si);
344 
345   set_bss_vma_name(si);
346 
347   // Use the executable's PT_INTERP string as the solinker filename in the
348   // dynamic linker's module list. gdb reads both PT_INTERP and the module list,
349   // and if the paths for the linker are different, gdb will report that the
350   // PT_INTERP linker path was unloaded once the module list is initialized.
351   // There are three situations to handle:
352   //  - the APEX linker (/system/bin/linker[64] -> /apex/.../linker[64])
353   //  - the ASAN linker (/system/bin/linker_asan[64] -> /apex/.../linker[64])
354   //  - the bootstrap linker (/system/bin/bootstrap/linker[64])
355   const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
356                                                        somain->load_bias);
357   if (interp == nullptr) {
358     // This case can happen if the linker attempts to execute itself
359     // (e.g. "linker64 /system/bin/linker64").
360 #if defined(__LP64__)
361 #define DEFAULT_INTERP "/system/bin/linker64"
362 #else
363 #define DEFAULT_INTERP "/system/bin/linker"
364 #endif
365     interp = DEFAULT_INTERP;
366   }
367   solinker->set_realpath(interp);
368   init_link_map_head(*solinker);
369   init_sanitizer_mode(interp);
370 
371 #if defined(__aarch64__)
372   __libc_init_mte(somain->memtag_dynamic_entries(), somain->phdr, somain->phnum, somain->load_bias);
373 
374   if (exe_to_load == nullptr) {
375     // Kernel does not add PROT_BTI to executable pages of the loaded ELF.
376     // Apply appropriate protections here if it is needed.
377     auto note_gnu_property = GnuPropertySection(somain);
378     if (note_gnu_property.IsBTICompatible() &&
379         (phdr_table_protect_segments(
380              somain->phdr, somain->phnum, somain->load_bias, somain->should_pad_segments(),
381              somain->should_use_16kib_app_compat(), &note_gnu_property) < 0)) {
382       __linker_error("error: can't protect segments for \"%s\": %m", exe_info.path.c_str());
383     }
384   }
385 #endif
386 
387   // Register the main executable and the linker upfront to have
388   // gdb aware of them before loading the rest of the dependency
389   // tree.
390   //
391   // gdb expects the linker to be in the debug shared object list.
392   // Without this, gdb has trouble locating the linker's ".text"
393   // and ".plt" sections. Gdb could also potentially use this to
394   // relocate the offset of our exported 'rtld_db_dlactivity' symbol.
395   //
396   insert_link_map_into_debug_map(&si->link_map_head);
397   insert_link_map_into_debug_map(&solinker->link_map_head);
398 
399   add_vdso();
400 
401   ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
402 
403   // For security reasons we dropped non-PIE support in API level 21,
404   // and the NDK no longer supports earlier API levels.
405   if (elf_hdr->e_type != ET_DYN) {
406     __linker_error("error: %s: Android only supports position-independent "
407                    "executables (-fPIE)", exe_info.path.c_str());
408   }
409 
410   // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
411   parse_LD_LIBRARY_PATH(ldpath_env);
412   parse_LD_PRELOAD(ldpreload_env);
413 
414   std::vector<android_namespace_t*> namespaces = init_default_namespaces(exe_info.path.c_str());
415 
416   if (!si->prelink_image()) __linker_cannot_link(g_argv[0]);
417 
418   // add somain to global group
419   si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
420   // ... and add it to all other linked namespaces
421   for (auto linked_ns : namespaces) {
422     if (linked_ns != &g_default_namespace) {
423       linked_ns->add_soinfo(somain);
424       somain->add_secondary_namespace(linked_ns);
425     }
426   }
427 
428   linker_setup_exe_static_tls(g_argv[0]);
429 
430   // Load ld_preloads and dependencies.
431   std::vector<const char*> needed_library_name_list;
432   size_t ld_preloads_count = 0;
433 
434   for (const auto& ld_preload_name : g_ld_preload_names) {
435     needed_library_name_list.push_back(ld_preload_name.c_str());
436     ++ld_preloads_count;
437   }
438 
439   for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
440     if (d->d_tag == DT_NEEDED) {
441       const char* name = fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath());
442       needed_library_name_list.push_back(name);
443     }
444   }
445 
446   const char** needed_library_names = &needed_library_name_list[0];
447   size_t needed_libraries_count = needed_library_name_list.size();
448 
449   if (needed_libraries_count > 0 &&
450       !find_libraries(&g_default_namespace,
451                       si,
452                       needed_library_names,
453                       needed_libraries_count,
454                       nullptr,
455                       &g_ld_preloads,
456                       ld_preloads_count,
457                       RTLD_GLOBAL,
458                       nullptr,
459                       true /* add_as_children */,
460                       &namespaces)) {
461     __linker_cannot_link(g_argv[0]);
462   } else if (needed_libraries_count == 0) {
463     if (!si->link_image(SymbolLookupList(si), si, nullptr, nullptr)) {
464       __linker_cannot_link(g_argv[0]);
465     }
466     si->increment_ref_count();
467   }
468 
469   // Exit early for ldd. We don't want to run the code that was loaded, so skip
470   // the constructor calls. Skip CFI setup because it would call __cfi_init in
471   // libdl.so.
472   if (g_is_ldd) _exit(EXIT_SUCCESS);
473 
474 #if defined(__aarch64__)
475   // This has to happen after the find_libraries, which will have collected any possible
476   // libraries that request memtag_stack in the dynamic section.
477   __libc_init_mte_stack(args.argv);
478 #endif
479 
480   linker_finalize_static_tls();
481   __libc_init_main_thread_final();
482 
483   if (!get_cfi_shadow()->InitialLinkDone(solist)) __linker_cannot_link(g_argv[0]);
484 
485   si->call_pre_init_constructors();
486   si->call_constructors();
487 
488   if (g_linker_debug_config.timing) {
489     gettimeofday(&t1, nullptr);
490     long long t0_us = (t0.tv_sec * 1000000LL) + t0.tv_usec;
491     long long t1_us = (t1.tv_sec * 1000000LL) + t1.tv_usec;
492     LD_DEBUG(timing, "LINKER TIME: %s: %lld microseconds", g_argv[0], t1_us - t0_us);
493   }
494   if (g_linker_debug_config.statistics) {
495     print_linker_stats();
496   }
497 
498   // We are about to hand control over to the executable loaded.  We don't want
499   // to leave dirty pages behind unnecessarily.
500   purge_unused_memory();
501 
502   ElfW(Addr) entry = exe_info.entry_point;
503   LD_DEBUG(any, "[ Ready to execute \"%s\" @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
504   return entry;
505 }
506 
507 /* Compute the load-bias of an existing executable. This shall only
508  * be used to compute the load bias of an executable or shared library
509  * that was loaded by the kernel itself.
510  *
511  * Input:
512  *    elf    -> address of ELF header, assumed to be at the start of the file.
513  * Return:
514  *    load bias, i.e. add the value of any p_vaddr in the file to get
515  *    the corresponding address in memory.
516  */
get_elf_exec_load_bias(const ElfW (Ehdr)* elf)517 static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
518   ElfW(Addr) offset = elf->e_phoff;
519   const ElfW(Phdr)* phdr_table =
520       reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
521   const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
522 
523   for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
524     if (phdr->p_type == PT_LOAD) {
525       return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
526     }
527   }
528   return 0;
529 }
530 
531 /* Find the load bias and base address of an executable or shared object loaded
532  * by the kernel. The ELF file's PHDR table must have a PT_PHDR entry.
533  *
534  * A VDSO doesn't have a PT_PHDR entry in its PHDR table.
535  */
get_elf_base_from_phdr(const ElfW (Phdr)* phdr_table,size_t phdr_count,ElfW (Addr)* base,ElfW (Addr)* load_bias)536 static void get_elf_base_from_phdr(const ElfW(Phdr)* phdr_table, size_t phdr_count,
537                                    ElfW(Addr)* base, ElfW(Addr)* load_bias) {
538   for (size_t i = 0; i < phdr_count; ++i) {
539     if (phdr_table[i].p_type == PT_PHDR) {
540       *load_bias = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_vaddr;
541       *base = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_offset;
542       return;
543     }
544   }
545   async_safe_fatal("Could not find a PHDR: broken executable?");
546 }
547 
548 /*
549  * Set anonymous VMA name for .bss section.  For DSOs loaded by the linker, this
550  * is done by ElfReader.  This function is here for DSOs loaded by the kernel,
551  * namely the linker itself and the main executable.
552  */
set_bss_vma_name(soinfo * si)553 static void set_bss_vma_name(soinfo* si) {
554   for (size_t i = 0; i < si->phnum; ++i) {
555     auto phdr = &si->phdr[i];
556 
557     if (phdr->p_type != PT_LOAD) {
558       continue;
559     }
560 
561     ElfW(Addr) seg_start = phdr->p_vaddr + si->load_bias;
562     ElfW(Addr) seg_page_end = page_end(seg_start + phdr->p_memsz);
563     ElfW(Addr) seg_file_end = page_end(seg_start + phdr->p_filesz);
564 
565     if (seg_page_end > seg_file_end) {
566       prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME,
567             reinterpret_cast<void*>(seg_file_end), seg_page_end - seg_file_end,
568             ".bss");
569     }
570   }
571 }
572 
573 #if defined(USE_RELA)
574 using RelType = ElfW(Rela);
575 const unsigned kRelTag = DT_RELA;
576 const unsigned kRelSzTag = DT_RELASZ;
577 #else
578 using RelType = ElfW(Rel);
579 const unsigned kRelTag = DT_REL;
580 const unsigned kRelSzTag = DT_RELSZ;
581 #endif
582 
583 extern __LIBC_HIDDEN__ ElfW(Ehdr) __ehdr_start;
584 
call_ifunc_resolvers_for_section(RelType * begin,RelType * end)585 static void call_ifunc_resolvers_for_section(RelType* begin, RelType* end) {
586   auto ehdr = reinterpret_cast<ElfW(Addr)>(&__ehdr_start);
587   for (RelType *r = begin; r != end; ++r) {
588     if (ELFW(R_TYPE)(r->r_info) != R_GENERIC_IRELATIVE) {
589       continue;
590     }
591     ElfW(Addr)* offset = reinterpret_cast<ElfW(Addr)*>(ehdr + r->r_offset);
592 #if defined(USE_RELA)
593     ElfW(Addr) resolver = ehdr + r->r_addend;
594 #else
595     ElfW(Addr) resolver = ehdr + *offset;
596 #endif
597     *offset = __bionic_call_ifunc_resolver(resolver);
598   }
599 }
600 
relocate_linker()601 static void relocate_linker() {
602   // The linker should only have relative relocations (in RELR) and IRELATIVE
603   // relocations. Find the IRELATIVE relocations using the DT_JMPREL and
604   // DT_PLTRELSZ, or DT_RELA/DT_RELASZ (DT_REL/DT_RELSZ on ILP32).
605   auto ehdr = reinterpret_cast<ElfW(Addr)>(&__ehdr_start);
606   auto* phdr = reinterpret_cast<ElfW(Phdr)*>(ehdr + __ehdr_start.e_phoff);
607   for (size_t i = 0; i != __ehdr_start.e_phnum; ++i) {
608     if (phdr[i].p_type != PT_DYNAMIC) {
609       continue;
610     }
611     auto *dyn = reinterpret_cast<ElfW(Dyn)*>(ehdr + phdr[i].p_vaddr);
612     ElfW(Addr) relr = 0, relrsz = 0, pltrel = 0, pltrelsz = 0, rel = 0, relsz = 0;
613     for (size_t j = 0, size = phdr[i].p_filesz / sizeof(ElfW(Dyn)); j != size; ++j) {
614       const auto tag = dyn[j].d_tag;
615       const auto val = dyn[j].d_un.d_ptr;
616       // We don't currently handle IRELATIVE relocations in DT_ANDROID_REL[A].
617       // We disabled DT_ANDROID_REL[A] at build time; verify that it was actually disabled.
618       CHECK(tag != DT_ANDROID_REL && tag != DT_ANDROID_RELA);
619       if (tag == DT_RELR || tag == DT_ANDROID_RELR) {
620         relr = val;
621       } else if (tag == DT_RELRSZ || tag == DT_ANDROID_RELRSZ) {
622         relrsz = val;
623       } else if (tag == DT_JMPREL) {
624         pltrel = val;
625       } else if (tag == DT_PLTRELSZ) {
626         pltrelsz = val;
627       } else if (tag == kRelTag) {
628         rel = val;
629       } else if (tag == kRelSzTag) {
630         relsz = val;
631       }
632     }
633     // Apply RELR relocations first so that the GOT is initialized for ifunc
634     // resolvers.
635     if (relr && relrsz) {
636       // Nothing has tagged the memtag globals here, so it is pointless either
637       // way to handle them, the tags will be zero anyway.
638       // That is moot though, because the linker does not use memtag_globals
639       // in the first place.
640       relocate_relr(reinterpret_cast<ElfW(Relr*)>(ehdr + relr),
641                     reinterpret_cast<ElfW(Relr*)>(ehdr + relr + relrsz), ehdr,
642                     /*has_memtag_globals=*/ false);
643     }
644     if (pltrel && pltrelsz) {
645       call_ifunc_resolvers_for_section(reinterpret_cast<RelType*>(ehdr + pltrel),
646                                        reinterpret_cast<RelType*>(ehdr + pltrel + pltrelsz));
647     }
648     if (rel && relsz) {
649       call_ifunc_resolvers_for_section(reinterpret_cast<RelType*>(ehdr + rel),
650                                        reinterpret_cast<RelType*>(ehdr + rel + relsz));
651     }
652   }
653 }
654 
655 // Usable before ifunc resolvers have been called. This function is compiled with -ffreestanding.
linker_memclr(void * dst,size_t cnt)656 static void linker_memclr(void* dst, size_t cnt) {
657   for (size_t i = 0; i < cnt; ++i) {
658     reinterpret_cast<char*>(dst)[i] = '\0';
659   }
660 }
661 
662 // Remapping MTE globals segments happens before the linker relocates itself, and so can't use
663 // memcpy() from string.h. This function is compiled with -ffreestanding.
linker_memcpy(void * dst,const void * src,size_t n)664 void linker_memcpy(void* dst, const void* src, size_t n) {
665   char* dst_bytes = reinterpret_cast<char*>(dst);
666   const char* src_bytes = reinterpret_cast<const char*>(src);
667   for (size_t i = 0; i < n; ++i) {
668     dst_bytes[i] = src_bytes[i];
669   }
670 }
671 
672 // Detect an attempt to run the linker on itself. e.g.:
673 //   /system/bin/linker64 /system/bin/linker64
674 // Use priority-1 to run this constructor before other constructors.
detect_self_exec()675 __attribute__((constructor(1))) static void detect_self_exec() {
676   // Normally, the linker initializes the auxv global before calling its
677   // constructors. If the linker loads itself, though, the first loader calls
678   // the second loader's constructors before calling __linker_init.
679   if (__libc_shared_globals()->auxv != nullptr) {
680     return;
681   }
682 #if defined(__i386__)
683   // We don't have access to the auxv struct from here, so use the int 0x80
684   // fallback.
685   __libc_sysinfo = reinterpret_cast<void*>(__libc_int0x80);
686 #endif
687   __linker_error("error: linker cannot load itself");
688 }
689 
690 static ElfW(Addr) __attribute__((noinline))
691 __linker_init_post_relocation(KernelArgumentBlock& args, soinfo& linker_so);
692 
693 /*
694  * This is the entry point for the linker, called from begin.S. This
695  * method is responsible for fixing the linker's own relocations, and
696  * then calling __linker_init_post_relocation().
697  *
698  * Because this method is called before the linker has fixed it's own
699  * relocations, any attempt to reference an extern variable, extern
700  * function, or other GOT reference will generate a segfault.
701  */
__linker_init(void * raw_args)702 extern "C" ElfW(Addr) __linker_init(void* raw_args) {
703   // Unlock the loader mutex immediately before transferring to the executable's
704   // entry point. This must happen after destructors are called in this function
705   // (e.g. ~soinfo), so declare this variable very early.
706   struct DlMutexUnlocker {
707     ~DlMutexUnlocker() { pthread_mutex_unlock(&g_dl_mutex); }
708   } unlocker;
709 
710   // Initialize TLS early so system calls and errno work.
711   KernelArgumentBlock args(raw_args);
712   bionic_tcb temp_tcb __attribute__((uninitialized));
713   linker_memclr(&temp_tcb, sizeof(temp_tcb));
714   __libc_init_main_thread_early(args, &temp_tcb);
715 
716   // When the linker is run by itself (rather than as an interpreter for
717   // another program), AT_BASE is 0.
718   ElfW(Addr) linker_addr = getauxval(AT_BASE);
719   if (linker_addr == 0) {
720     // The AT_PHDR and AT_PHNUM aux values describe this linker instance, so use
721     // the phdr to find the linker's base address.
722     ElfW(Addr) load_bias;
723     get_elf_base_from_phdr(
724       reinterpret_cast<ElfW(Phdr)*>(getauxval(AT_PHDR)), getauxval(AT_PHNUM),
725       &linker_addr, &load_bias);
726   }
727 
728   ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
729   ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
730 
731   // Relocate the linker. This step will initialize the GOT, which is needed for
732   // accessing non-hidden global variables. (On some targets, the stack
733   // protector uses GOT accesses rather than TLS.) Relocating the linker will
734   // also call the linker's ifunc resolvers so that string.h functions can be
735   // used.
736   relocate_linker();
737 
738   soinfo tmp_linker_so(nullptr, nullptr, nullptr, 0, 0);
739 
740   tmp_linker_so.base = linker_addr;
741   tmp_linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
742   tmp_linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
743   tmp_linker_so.dynamic = nullptr;
744   tmp_linker_so.phdr = phdr;
745   tmp_linker_so.phnum = elf_hdr->e_phnum;
746   tmp_linker_so.set_linker_flag();
747 
748   if (!tmp_linker_so.prelink_image()) __linker_cannot_link(args.argv[0]);
749   // There is special logic in soinfo::relocate to avoid duplicating the
750   // relocations we did in relocate_linker().
751   if (!tmp_linker_so.link_image(SymbolLookupList(&tmp_linker_so), &tmp_linker_so, nullptr, nullptr)) __linker_cannot_link(args.argv[0]);
752 
753   return __linker_init_post_relocation(args, tmp_linker_so);
754 }
755 
756 /*
757  * This code is called after the linker has linked itself and fixed its own
758  * GOT. It is safe to make references to externs and other non-local data at
759  * this point. The compiler sometimes moves GOT references earlier in a
760  * function, so avoid inlining this function (http://b/80503879).
761  */
762 static ElfW(Addr) __attribute__((noinline))
__linker_init_post_relocation(KernelArgumentBlock & args,soinfo & tmp_linker_so)763 __linker_init_post_relocation(KernelArgumentBlock& args, soinfo& tmp_linker_so) {
764   // Finish initializing the main thread.
765   __libc_init_main_thread_late();
766 
767   // We didn't protect the linker's RELRO pages in link_image because we
768   // couldn't make system calls on x86 at that point, but we can now...
769   if (!tmp_linker_so.protect_relro()) __linker_cannot_link(args.argv[0]);
770 
771   // And we can set VMA name for the bss section now
772   set_bss_vma_name(&tmp_linker_so);
773 
774   // Initialize the linker's static libc's globals
775   __libc_init_globals();
776 
777   // A constructor could spawn a thread that calls into the loader, so as soon
778   // as we've called a constructor, we need to hold the lock until transferring
779   // to the entry point.
780   pthread_mutex_lock(&g_dl_mutex);
781 
782   // Initialize the linker's own global variables
783   tmp_linker_so.call_constructors();
784 
785   // Setting the linker soinfo's soname can allocate heap memory, so delay it until here.
786   for (const ElfW(Dyn)* d = tmp_linker_so.dynamic; d->d_tag != DT_NULL; ++d) {
787     if (d->d_tag == DT_SONAME) {
788       tmp_linker_so.set_soname(tmp_linker_so.get_string(d->d_un.d_val));
789     }
790   }
791 
792   // When the linker is run directly rather than acting as PT_INTERP, parse
793   // arguments and determine the executable to load. When it's instead acting
794   // as PT_INTERP, AT_ENTRY will refer to the loaded executable rather than the
795   // linker's _start.
796   const char* exe_to_load = nullptr;
797   if (getauxval(AT_ENTRY) == reinterpret_cast<uintptr_t>(&_start)) {
798     if (args.argc == 3 && !strcmp(args.argv[1], "--list")) {
799       // We're being asked to behave like ldd(1).
800       g_is_ldd = true;
801       exe_to_load = args.argv[2];
802     } else if (args.argc <= 1 || !strcmp(args.argv[1], "--help")) {
803       async_safe_format_fd(STDOUT_FILENO,
804          "Usage: %s [--list] PROGRAM [ARGS-FOR-PROGRAM...]\n"
805          "       %s [--list] path.zip!/PROGRAM [ARGS-FOR-PROGRAM...]\n"
806          "\n"
807          "A helper program for linking dynamic executables. Typically, the kernel loads\n"
808          "this program because it's the PT_INTERP of a dynamic executable.\n"
809          "\n"
810          "This program can also be run directly to load and run a dynamic executable. The\n"
811          "executable can be inside a zip file if it's stored uncompressed and at a\n"
812          "page-aligned offset.\n"
813          "\n"
814          "The --list option gives behavior equivalent to ldd(1) on other systems.\n",
815          args.argv[0], args.argv[0]);
816       _exit(EXIT_SUCCESS);
817     } else {
818       exe_to_load = args.argv[1];
819       __libc_shared_globals()->initial_linker_arg_count = 1;
820     }
821   }
822 
823   // store argc/argv/envp to use them for calling constructors
824   g_argc = args.argc - __libc_shared_globals()->initial_linker_arg_count;
825   g_argv = args.argv + __libc_shared_globals()->initial_linker_arg_count;
826   g_envp = args.envp;
827   __libc_shared_globals()->init_progname = g_argv[0];
828 
829   // Initialize static variables. Note that in order to
830   // get correct libdl_info we need to call constructors
831   // before get_libdl_info().
832   sonext = solist = solinker = get_libdl_info(tmp_linker_so);
833   g_default_namespace.add_soinfo(solinker);
834 
835   ElfW(Addr) start_address = linker_main(args, exe_to_load);
836 
837   LD_DEBUG(any, "[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
838 
839   // Return the address that the calling assembly stub should jump to.
840   return start_address;
841 }
842