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