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 | /* |
| 30 | * libc_init_dynamic.c |
| 31 | * |
| 32 | * This source files provides two important functions for dynamic |
| 33 | * executables: |
| 34 | * |
| 35 | * - a C runtime initializer (__libc_preinit), which is called by |
| 36 | * the dynamic linker when libc.so is loaded. This happens before |
| 37 | * any other initializer (e.g. static C++ constructors in other |
| 38 | * shared libraries the program depends on). |
| 39 | * |
| 40 | * - a program launch function (__libc_init), which is called after |
| 41 | * all dynamic linking has been performed. Technically, it is called |
| 42 | * from arch-$ARCH/bionic/crtbegin_dynamic.S which is itself called |
| 43 | * by the dynamic linker after all libraries have been loaded and |
| 44 | * initialized. |
| 45 | */ |
| 46 | |
| 47 | #include <stddef.h> |
| 48 | #include <stdio.h> |
| 49 | #include <stdlib.h> |
| 50 | #include <stdint.h> |
| 51 | #include <elf.h> |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 52 | #include "libc_init_common.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 53 | |
Josh Gao | 3c8fc2f | 2015-10-08 14:49:26 -0700 | [diff] [blame] | 54 | #include "private/bionic_globals.h" |
Christopher Ferris | 93ea09f | 2017-10-05 15:18:47 -0700 | [diff] [blame] | 55 | #include "private/bionic_macros.h" |
Josh Gao | b6453c5 | 2016-06-29 16:47:53 -0700 | [diff] [blame] | 56 | #include "private/bionic_ssp.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 57 | #include "private/bionic_tls.h" |
| 58 | #include "private/KernelArgumentBlock.h" |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 59 | |
| 60 | extern "C" { |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 61 | extern void netdClientInit(void); |
Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 62 | extern int __cxa_atexit(void (*)(void *), void *, void *); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Ryan Prichard | 27475b5 | 2018-05-17 17:14:18 -0700 | [diff] [blame] | 65 | // Use an initializer so __libc_sysinfo will have a fallback implementation |
| 66 | // while .preinit_array constructors run. |
| 67 | #if defined(__i386__) |
Elliott Hughes | 95bca3f | 2018-06-12 14:16:50 -0700 | [diff] [blame] | 68 | static __attribute__((__naked__)) void __libc_int0x80() { |
| 69 | __asm__ volatile("int $0x80; ret"); |
| 70 | } |
Ryan Prichard | 27475b5 | 2018-05-17 17:14:18 -0700 | [diff] [blame] | 71 | __LIBC_HIDDEN__ void* __libc_sysinfo = reinterpret_cast<void*>(__libc_int0x80); |
| 72 | #endif |
| 73 | |
Stephen Crane | f4b1cbd | 2017-05-09 14:27:43 -0700 | [diff] [blame] | 74 | // We need a helper function for __libc_preinit because compiling with LTO may |
| 75 | // inline functions requiring a stack protector check, but __stack_chk_guard is |
| 76 | // not initialized at the start of __libc_preinit. __libc_preinit_impl will run |
| 77 | // after __stack_chk_guard is initialized and therefore can safely have a stack |
| 78 | // protector. |
| 79 | __attribute__((noinline)) |
| 80 | static void __libc_preinit_impl(KernelArgumentBlock& args) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 81 | __libc_shared_globals = args.shared_globals; |
| 82 | |
Stephen Crane | f4b1cbd | 2017-05-09 14:27:43 -0700 | [diff] [blame] | 83 | __libc_init_globals(args); |
| 84 | __libc_init_common(args); |
| 85 | |
| 86 | // Hooks for various libraries to let them know that we're starting up. |
| 87 | __libc_globals.mutate(__libc_init_malloc); |
| 88 | netdClientInit(); |
| 89 | } |
| 90 | |
Elliott Hughes | 1d01fe8 | 2017-10-23 10:07:55 -0700 | [diff] [blame] | 91 | // We flag the __libc_preinit function as a constructor to ensure that |
| 92 | // its address is listed in libc.so's .init_array section. |
| 93 | // This ensures that the function is called by the dynamic linker as |
| 94 | // soon as the shared library is loaded. |
| 95 | // We give this constructor priority 1 because we want libc's constructor |
| 96 | // to run before any others (such as the jemalloc constructor), and lower |
| 97 | // is better (http://b/68046352). |
| 98 | __attribute__((constructor(1))) static void __libc_preinit() { |
Ryan Prichard | 6631f9b | 2018-04-30 18:29:32 -0700 | [diff] [blame] | 99 | // Read the kernel argument block pointer from TLS, then clear the slot so no |
| 100 | // other initializer sees its value. |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 101 | void** tls = __get_tls(); |
Ryan Prichard | 6631f9b | 2018-04-30 18:29:32 -0700 | [diff] [blame] | 102 | KernelArgumentBlock* args = static_cast<KernelArgumentBlock*>(tls[TLS_SLOT_BIONIC_PREINIT]); |
| 103 | tls[TLS_SLOT_BIONIC_PREINIT] = nullptr; |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 104 | |
Josh Gao | b6453c5 | 2016-06-29 16:47:53 -0700 | [diff] [blame] | 105 | // The linker has initialized its copy of the global stack_chk_guard, and filled in the main |
| 106 | // thread's TLS slot with that value. Initialize the local global stack guard with its value. |
| 107 | __stack_chk_guard = reinterpret_cast<uintptr_t>(tls[TLS_SLOT_STACK_GUARD]); |
| 108 | |
Stephen Crane | f4b1cbd | 2017-05-09 14:27:43 -0700 | [diff] [blame] | 109 | __libc_preinit_impl(*args); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 112 | // This function is called from the executable's _start entry point |
Ryan Prichard | 6631f9b | 2018-04-30 18:29:32 -0700 | [diff] [blame] | 113 | // (see arch-$ARCH/bionic/crtbegin.c), which is itself called by the dynamic |
| 114 | // linker after it has loaded all shared libraries the executable depends on. |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 115 | // |
| 116 | // Note that the dynamic linker has also run all constructors in the |
| 117 | // executable at this point. |
| 118 | __noreturn void __libc_init(void* raw_args, |
Elliott Hughes | d286796 | 2014-06-03 15:22:34 -0700 | [diff] [blame] | 119 | void (*onexit)(void) __unused, |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 120 | int (*slingshot)(int, char**, char**), |
Dmitriy Ivanov | 4b41555 | 2014-09-04 21:54:34 +0000 | [diff] [blame] | 121 | structors_array_t const * const structors) { |
Christopher Ferris | 93ea09f | 2017-10-05 15:18:47 -0700 | [diff] [blame] | 122 | BIONIC_STOP_UNWIND; |
Dmitriy Ivanov | 4b41555 | 2014-09-04 21:54:34 +0000 | [diff] [blame] | 123 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 124 | KernelArgumentBlock args(raw_args); |
| 125 | |
| 126 | // Several Linux ABIs don't pass the onexit pointer, and the ones that |
| 127 | // do never use it. Therefore, we ignore it. |
| 128 | |
Dmitriy Ivanov | 4b41555 | 2014-09-04 21:54:34 +0000 | [diff] [blame] | 129 | // The executable may have its own destructors listed in its .fini_array |
| 130 | // so we need to ensure that these are called when the program exits |
| 131 | // normally. |
| 132 | if (structors->fini_array) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 133 | __cxa_atexit(__libc_fini,structors->fini_array,nullptr); |
Dmitriy Ivanov | 4b41555 | 2014-09-04 21:54:34 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame^] | 136 | exit(slingshot(args.argc - __libc_shared_globals->initial_linker_arg_count, |
| 137 | args.argv + __libc_shared_globals->initial_linker_arg_count, |
| 138 | args.envp)); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 139 | } |
Yabin Cui | ca48274 | 2016-01-25 17:38:44 -0800 | [diff] [blame] | 140 | |
| 141 | extern "C" uint32_t android_get_application_target_sdk_version(); |
| 142 | |
| 143 | uint32_t bionic_get_application_target_sdk_version() { |
| 144 | return android_get_application_target_sdk_version(); |
| 145 | } |