blob: 295484b824269d8446f5ec5b89b80fdb79a9dd68 [file] [log] [blame]
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001/*
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 Hughes42d949f2016-01-06 19:51:43 -080028
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080029/*
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080030 * 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 Hughesf9930b72020-02-10 10:30:38 -080039 * all dynamic linking has been performed.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080040 */
41
Florian Mayera453c2d2024-02-09 00:40:45 +000042#include <elf.h>
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080043#include <stddef.h>
Florian Mayera453c2d2024-02-09 00:40:45 +000044#include <stdint.h>
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080045#include <stdio.h>
46#include <stdlib.h>
Florian Mayera453c2d2024-02-09 00:40:45 +000047#include "bionic/pthread_internal.h"
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080048#include "libc_init_common.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070049
Peter Collingbourne2659d7b2021-03-05 13:31:41 -080050#include "private/bionic_defs.h"
Ryan Prichard16455b52019-01-18 01:00:59 -080051#include "private/bionic_elf_tls.h"
Josh Gao3c8fc2f2015-10-08 14:49:26 -070052#include "private/bionic_globals.h"
Josh Gao4956c372019-12-19 16:35:51 -080053#include "platform/bionic/macros.h"
Josh Gaob6453c52016-06-29 16:47:53 -070054#include "private/bionic_ssp.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070055#include "private/bionic_tls.h"
56#include "private/KernelArgumentBlock.h"
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080057
58extern "C" {
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070059 extern void netdClientInit(void);
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070060 extern int __cxa_atexit(void (*)(void *), void *, void *);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080061};
62
Florian Mayera453c2d2024-02-09 00:40:45 +000063void memtag_stack_dlopen_callback() {
64 __pthread_internal_remap_stack_with_mte();
65}
66
Ryan Prichard27475b52018-05-17 17:14:18 -070067// Use an initializer so __libc_sysinfo will have a fallback implementation
68// while .preinit_array constructors run.
69#if defined(__i386__)
70__LIBC_HIDDEN__ void* __libc_sysinfo = reinterpret_cast<void*>(__libc_int0x80);
71#endif
72
Peter Collingbourne65332082019-08-05 16:16:14 -070073extern "C" __attribute__((weak)) void __hwasan_library_loaded(ElfW(Addr) base,
74 const ElfW(Phdr)* phdr,
75 ElfW(Half) phnum);
76extern "C" __attribute__((weak)) void __hwasan_library_unloaded(ElfW(Addr) base,
77 const ElfW(Phdr)* phdr,
78 ElfW(Half) phnum);
79
Stephen Cranef4b1cbd2017-05-09 14:27:43 -070080// We need a helper function for __libc_preinit because compiling with LTO may
81// inline functions requiring a stack protector check, but __stack_chk_guard is
82// not initialized at the start of __libc_preinit. __libc_preinit_impl will run
83// after __stack_chk_guard is initialized and therefore can safely have a stack
84// protector.
85__attribute__((noinline))
Ryan Prichard07440a82018-11-22 03:16:06 -080086static void __libc_preinit_impl() {
Ryan Prichard701bd0c2018-11-21 16:23:03 -080087#if defined(__i386__)
Ryan Prichard07440a82018-11-22 03:16:06 -080088 __libc_init_sysinfo();
Ryan Prichard701bd0c2018-11-21 16:23:03 -080089#endif
Josh Gaof6e5b582018-06-01 15:30:54 -070090
Ryan Prichard16455b52019-01-18 01:00:59 -080091 // Register libc.so's copy of the TLS generation variable so the linker can
92 // update it when it loads or unloads a shared object.
93 TlsModules& tls_modules = __libc_shared_globals()->tls_modules;
94 tls_modules.generation_libc_so = &__libc_tls_generation_copy;
95 __libc_tls_generation_copy = tls_modules.generation;
96
Ryan Prichard07440a82018-11-22 03:16:06 -080097 __libc_init_globals();
Ryan Prichard48b11592018-11-22 02:41:36 -080098 __libc_init_common();
Evgenii Stepanov8564b8d2020-12-15 13:55:32 -080099 __libc_init_scudo();
Stephen Cranef4b1cbd2017-05-09 14:27:43 -0700100
Florian Mayer6c1c3422022-01-31 18:31:48 -0800101#if __has_feature(hwaddress_sanitizer)
102 // Notify the HWASan runtime library whenever a library is loaded or unloaded
103 // so that it can update its shadow memory.
104 // This has to happen before _libc_init_malloc which might dlopen to load
105 // profiler libraries.
106 __libc_shared_globals()->load_hook = __hwasan_library_loaded;
107 __libc_shared_globals()->unload_hook = __hwasan_library_unloaded;
108#endif
109
Stephen Cranef4b1cbd2017-05-09 14:27:43 -0700110 // Hooks for various libraries to let them know that we're starting up.
111 __libc_globals.mutate(__libc_init_malloc);
Peter Collingbourne65332082019-08-05 16:16:14 -0700112
Ryan Savitski175c8862020-01-02 19:54:57 +0000113 // Install reserved signal handlers for assisting the platform's profilers.
114 __libc_init_profiling_handlers();
115
Mitch Phillips1d2aadc2019-11-14 16:02:09 -0800116 __libc_init_fork_handler();
117
Peter Collingbourne2659d7b2021-03-05 13:31:41 -0800118 __libc_shared_globals()->set_target_sdk_version_hook = __libc_set_target_sdk_version;
119
Stephen Cranef4b1cbd2017-05-09 14:27:43 -0700120 netdClientInit();
121}
122
Elliott Hughes1d01fe82017-10-23 10:07:55 -0700123// We flag the __libc_preinit function as a constructor to ensure that
124// its address is listed in libc.so's .init_array section.
125// This ensures that the function is called by the dynamic linker as
126// soon as the shared library is loaded.
127// We give this constructor priority 1 because we want libc's constructor
128// to run before any others (such as the jemalloc constructor), and lower
129// is better (http://b/68046352).
130__attribute__((constructor(1))) static void __libc_preinit() {
Josh Gaob6453c52016-06-29 16:47:53 -0700131 // The linker has initialized its copy of the global stack_chk_guard, and filled in the main
132 // thread's TLS slot with that value. Initialize the local global stack guard with its value.
Ryan Prichard07440a82018-11-22 03:16:06 -0800133 __stack_chk_guard = reinterpret_cast<uintptr_t>(__get_tls()[TLS_SLOT_STACK_GUARD]);
Josh Gaob6453c52016-06-29 16:47:53 -0700134
Ryan Prichard07440a82018-11-22 03:16:06 -0800135 __libc_preinit_impl();
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800136}
137
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800138// This function is called from the executable's _start entry point
Ryan Prichard6631f9b2018-04-30 18:29:32 -0700139// (see arch-$ARCH/bionic/crtbegin.c), which is itself called by the dynamic
140// linker after it has loaded all shared libraries the executable depends on.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800141//
142// Note that the dynamic linker has also run all constructors in the
143// executable at this point.
144__noreturn void __libc_init(void* raw_args,
Elliott Hughesd2867962014-06-03 15:22:34 -0700145 void (*onexit)(void) __unused,
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800146 int (*slingshot)(int, char**, char**),
Dmitriy Ivanov4b415552014-09-04 21:54:34 +0000147 structors_array_t const * const structors) {
Christopher Ferris93ea09f2017-10-05 15:18:47 -0700148 BIONIC_STOP_UNWIND;
Dmitriy Ivanov4b415552014-09-04 21:54:34 +0000149
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800150 KernelArgumentBlock args(raw_args);
151
152 // Several Linux ABIs don't pass the onexit pointer, and the ones that
153 // do never use it. Therefore, we ignore it.
154
Dmitriy Ivanov4b415552014-09-04 21:54:34 +0000155 // The executable may have its own destructors listed in its .fini_array
156 // so we need to ensure that these are called when the program exits
157 // normally.
158 if (structors->fini_array) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700159 __cxa_atexit(__libc_fini,structors->fini_array,nullptr);
Dmitriy Ivanov4b415552014-09-04 21:54:34 +0000160 }
161
Florian Mayer408e1702022-05-12 13:06:04 -0700162 __libc_init_mte_late();
163
Florian Mayera453c2d2024-02-09 00:40:45 +0000164 // This roundabout way is needed so we don't use the static libc linked into the linker, which
165 // will not affect the process.
166 __libc_shared_globals()->memtag_stack_dlopen_callback = memtag_stack_dlopen_callback;
167
Ryan Prichardabf736a2018-11-22 02:40:17 -0800168 exit(slingshot(args.argc - __libc_shared_globals()->initial_linker_arg_count,
169 args.argv + __libc_shared_globals()->initial_linker_arg_count,
Ryan Prichard8f639a42018-10-01 23:10:05 -0700170 args.envp));
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800171}
Ryan Prichardabf736a2018-11-22 02:40:17 -0800172
173extern "C" libc_shared_globals* __loader_shared_globals();
174
175__LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals() {
176 return __loader_shared_globals();
177}