| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 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 | */ | 
| Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 28 |  | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 29 | /* | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 30 | * This source files provides two important functions for dynamic | 
|  | 31 | * executables: | 
|  | 32 | * | 
|  | 33 | * - a C runtime initializer (__libc_preinit), which is called by | 
|  | 34 | *   the dynamic linker when libc.so is loaded. This happens before | 
|  | 35 | *   any other initializer (e.g. static C++ constructors in other | 
|  | 36 | *   shared libraries the program depends on). | 
|  | 37 | * | 
|  | 38 | * - a program launch function (__libc_init), which is called after | 
| Elliott Hughes | f9930b7 | 2020-02-10 10:30:38 -0800 | [diff] [blame] | 39 | *   all dynamic linking has been performed. | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 40 | */ | 
|  | 41 |  | 
| Florian Mayer | e65e193 | 2024-02-15 22:20:54 +0000 | [diff] [blame] | 42 | #include <elf.h> | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 43 | #include <stddef.h> | 
| Florian Mayer | e65e193 | 2024-02-15 22:20:54 +0000 | [diff] [blame] | 44 | #include <stdint.h> | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 45 | #include <stdio.h> | 
|  | 46 | #include <stdlib.h> | 
| Florian Mayer | e65e193 | 2024-02-15 22:20:54 +0000 | [diff] [blame] | 47 | #include "bionic/pthread_internal.h" | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 48 | #include "libc_init_common.h" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 49 |  | 
| Peter Collingbourne | 2659d7b | 2021-03-05 13:31:41 -0800 | [diff] [blame] | 50 | #include "private/bionic_defs.h" | 
| Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 51 | #include "private/bionic_elf_tls.h" | 
| Josh Gao | 3c8fc2f | 2015-10-08 14:49:26 -0700 | [diff] [blame] | 52 | #include "private/bionic_globals.h" | 
| Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 53 | #include "platform/bionic/macros.h" | 
| Josh Gao | b6453c5 | 2016-06-29 16:47:53 -0700 | [diff] [blame] | 54 | #include "private/bionic_ssp.h" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 55 | #include "private/bionic_tls.h" | 
|  | 56 | #include "private/KernelArgumentBlock.h" | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 57 |  | 
|  | 58 | extern "C" { | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 59 | extern void netdClientInit(void); | 
| Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 60 | extern int __cxa_atexit(void (*)(void *), void *, void *); | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 61 | }; | 
|  | 62 |  | 
| Florian Mayer | e65e193 | 2024-02-15 22:20:54 +0000 | [diff] [blame] | 63 | void memtag_stack_dlopen_callback() { | 
| Florian Mayer | 05c16e7 | 2024-04-30 18:13:14 +0000 | [diff] [blame] | 64 | async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "remapping stacks as PROT_MTE"); | 
| Florian Mayer | e65e193 | 2024-02-15 22:20:54 +0000 | [diff] [blame] | 65 | __pthread_internal_remap_stack_with_mte(); | 
|  | 66 | } | 
|  | 67 |  | 
| Ryan Prichard | 27475b5 | 2018-05-17 17:14:18 -0700 | [diff] [blame] | 68 | // Use an initializer so __libc_sysinfo will have a fallback implementation | 
|  | 69 | // while .preinit_array constructors run. | 
|  | 70 | #if defined(__i386__) | 
|  | 71 | __LIBC_HIDDEN__ void* __libc_sysinfo = reinterpret_cast<void*>(__libc_int0x80); | 
|  | 72 | #endif | 
|  | 73 |  | 
| Peter Collingbourne | 6533208 | 2019-08-05 16:16:14 -0700 | [diff] [blame] | 74 | extern "C" __attribute__((weak)) void __hwasan_library_loaded(ElfW(Addr) base, | 
|  | 75 | const ElfW(Phdr)* phdr, | 
|  | 76 | ElfW(Half) phnum); | 
|  | 77 | extern "C" __attribute__((weak)) void __hwasan_library_unloaded(ElfW(Addr) base, | 
|  | 78 | const ElfW(Phdr)* phdr, | 
|  | 79 | ElfW(Half) phnum); | 
|  | 80 |  | 
| Stephen Crane | f4b1cbd | 2017-05-09 14:27:43 -0700 | [diff] [blame] | 81 | // We need a helper function for __libc_preinit because compiling with LTO may | 
|  | 82 | // inline functions requiring a stack protector check, but __stack_chk_guard is | 
|  | 83 | // not initialized at the start of __libc_preinit. __libc_preinit_impl will run | 
|  | 84 | // after __stack_chk_guard is initialized and therefore can safely have a stack | 
|  | 85 | // protector. | 
|  | 86 | __attribute__((noinline)) | 
| Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 87 | static void __libc_preinit_impl() { | 
| Ryan Prichard | 701bd0c | 2018-11-21 16:23:03 -0800 | [diff] [blame] | 88 | #if defined(__i386__) | 
| Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 89 | __libc_init_sysinfo(); | 
| Ryan Prichard | 701bd0c | 2018-11-21 16:23:03 -0800 | [diff] [blame] | 90 | #endif | 
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 91 |  | 
| Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 92 | // Register libc.so's copy of the TLS generation variable so the linker can | 
|  | 93 | // update it when it loads or unloads a shared object. | 
|  | 94 | TlsModules& tls_modules = __libc_shared_globals()->tls_modules; | 
|  | 95 | tls_modules.generation_libc_so = &__libc_tls_generation_copy; | 
|  | 96 | __libc_tls_generation_copy = tls_modules.generation; | 
|  | 97 |  | 
| Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 98 | __libc_init_globals(); | 
| Ryan Prichard | 48b1159 | 2018-11-22 02:41:36 -0800 | [diff] [blame] | 99 | __libc_init_common(); | 
| Evgenii Stepanov | 8564b8d | 2020-12-15 13:55:32 -0800 | [diff] [blame] | 100 | __libc_init_scudo(); | 
| Stephen Crane | f4b1cbd | 2017-05-09 14:27:43 -0700 | [diff] [blame] | 101 |  | 
| Florian Mayer | 6c1c342 | 2022-01-31 18:31:48 -0800 | [diff] [blame] | 102 | #if __has_feature(hwaddress_sanitizer) | 
|  | 103 | // Notify the HWASan runtime library whenever a library is loaded or unloaded | 
|  | 104 | // so that it can update its shadow memory. | 
|  | 105 | // This has to happen before _libc_init_malloc which might dlopen to load | 
|  | 106 | // profiler libraries. | 
|  | 107 | __libc_shared_globals()->load_hook = __hwasan_library_loaded; | 
|  | 108 | __libc_shared_globals()->unload_hook = __hwasan_library_unloaded; | 
|  | 109 | #endif | 
|  | 110 |  | 
| Stephen Crane | f4b1cbd | 2017-05-09 14:27:43 -0700 | [diff] [blame] | 111 | // Hooks for various libraries to let them know that we're starting up. | 
|  | 112 | __libc_globals.mutate(__libc_init_malloc); | 
| Peter Collingbourne | 6533208 | 2019-08-05 16:16:14 -0700 | [diff] [blame] | 113 |  | 
| Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 114 | // Install reserved signal handlers for assisting the platform's profilers. | 
|  | 115 | __libc_init_profiling_handlers(); | 
|  | 116 |  | 
| Mitch Phillips | 1d2aadc | 2019-11-14 16:02:09 -0800 | [diff] [blame] | 117 | __libc_init_fork_handler(); | 
|  | 118 |  | 
| Peter Collingbourne | 2659d7b | 2021-03-05 13:31:41 -0800 | [diff] [blame] | 119 | __libc_shared_globals()->set_target_sdk_version_hook = __libc_set_target_sdk_version; | 
|  | 120 |  | 
| Stephen Crane | f4b1cbd | 2017-05-09 14:27:43 -0700 | [diff] [blame] | 121 | netdClientInit(); | 
|  | 122 | } | 
|  | 123 |  | 
| Elliott Hughes | 1d01fe8 | 2017-10-23 10:07:55 -0700 | [diff] [blame] | 124 | // We flag the __libc_preinit function as a constructor to ensure that | 
|  | 125 | // its address is listed in libc.so's .init_array section. | 
|  | 126 | // This ensures that the function is called by the dynamic linker as | 
|  | 127 | // soon as the shared library is loaded. | 
|  | 128 | // We give this constructor priority 1 because we want libc's constructor | 
|  | 129 | // to run before any others (such as the jemalloc constructor), and lower | 
|  | 130 | // is better (http://b/68046352). | 
|  | 131 | __attribute__((constructor(1))) static void __libc_preinit() { | 
| Josh Gao | b6453c5 | 2016-06-29 16:47:53 -0700 | [diff] [blame] | 132 | // The linker has initialized its copy of the global stack_chk_guard, and filled in the main | 
|  | 133 | // thread's TLS slot with that value. Initialize the local global stack guard with its value. | 
| Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 134 | __stack_chk_guard = reinterpret_cast<uintptr_t>(__get_tls()[TLS_SLOT_STACK_GUARD]); | 
| Josh Gao | b6453c5 | 2016-06-29 16:47:53 -0700 | [diff] [blame] | 135 |  | 
| Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 136 | __libc_preinit_impl(); | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 137 | } | 
|  | 138 |  | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 139 | // This function is called from the executable's _start entry point | 
| Ryan Prichard | 6631f9b | 2018-04-30 18:29:32 -0700 | [diff] [blame] | 140 | // (see arch-$ARCH/bionic/crtbegin.c), which is itself called by the dynamic | 
|  | 141 | // linker after it has loaded all shared libraries the executable depends on. | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 142 | // | 
|  | 143 | // Note that the dynamic linker has also run all constructors in the | 
|  | 144 | // executable at this point. | 
|  | 145 | __noreturn void __libc_init(void* raw_args, | 
| Elliott Hughes | d286796 | 2014-06-03 15:22:34 -0700 | [diff] [blame] | 146 | void (*onexit)(void) __unused, | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 147 | int (*slingshot)(int, char**, char**), | 
| Dmitriy Ivanov | 4b41555 | 2014-09-04 21:54:34 +0000 | [diff] [blame] | 148 | structors_array_t const * const structors) { | 
| Christopher Ferris | 93ea09f | 2017-10-05 15:18:47 -0700 | [diff] [blame] | 149 | BIONIC_STOP_UNWIND; | 
| Dmitriy Ivanov | 4b41555 | 2014-09-04 21:54:34 +0000 | [diff] [blame] | 150 |  | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 151 | KernelArgumentBlock args(raw_args); | 
|  | 152 |  | 
|  | 153 | // Several Linux ABIs don't pass the onexit pointer, and the ones that | 
|  | 154 | // do never use it.  Therefore, we ignore it. | 
|  | 155 |  | 
| Dmitriy Ivanov | 4b41555 | 2014-09-04 21:54:34 +0000 | [diff] [blame] | 156 | // The executable may have its own destructors listed in its .fini_array | 
|  | 157 | // so we need to ensure that these are called when the program exits | 
|  | 158 | // normally. | 
|  | 159 | if (structors->fini_array) { | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 160 | __cxa_atexit(__libc_fini,structors->fini_array,nullptr); | 
| Dmitriy Ivanov | 4b41555 | 2014-09-04 21:54:34 +0000 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Florian Mayer | 408e170 | 2022-05-12 13:06:04 -0700 | [diff] [blame] | 163 | __libc_init_mte_late(); | 
|  | 164 |  | 
| Florian Mayer | e65e193 | 2024-02-15 22:20:54 +0000 | [diff] [blame] | 165 | // This roundabout way is needed so we don't use the static libc linked into the linker, which | 
|  | 166 | // will not affect the process. | 
|  | 167 | __libc_shared_globals()->memtag_stack_dlopen_callback = memtag_stack_dlopen_callback; | 
|  | 168 |  | 
| Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 169 | exit(slingshot(args.argc - __libc_shared_globals()->initial_linker_arg_count, | 
|  | 170 | args.argv + __libc_shared_globals()->initial_linker_arg_count, | 
| Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 171 | args.envp)); | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 172 | } | 
| Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 173 |  | 
|  | 174 | extern "C" libc_shared_globals* __loader_shared_globals(); | 
|  | 175 |  | 
|  | 176 | __LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals() { | 
|  | 177 | return __loader_shared_globals(); | 
|  | 178 | } |