blob: 08d3df4ffa7e8d6b600b48c8de8c12902fec8185 [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/*
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 Hughes42b2c6a2013-02-07 10:14:39 -080052#include "libc_init_common.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070053
Ryan Prichard701bd0c2018-11-21 16:23:03 -080054#include "private/bionic_auxv.h"
Josh Gao3c8fc2f2015-10-08 14:49:26 -070055#include "private/bionic_globals.h"
Christopher Ferris93ea09f2017-10-05 15:18:47 -070056#include "private/bionic_macros.h"
Josh Gaob6453c52016-06-29 16:47:53 -070057#include "private/bionic_ssp.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070058#include "private/bionic_tls.h"
59#include "private/KernelArgumentBlock.h"
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080060
61extern "C" {
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070062 extern void netdClientInit(void);
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070063 extern int __cxa_atexit(void (*)(void *), void *, void *);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080064};
65
Ryan Prichard27475b52018-05-17 17:14:18 -070066// Use an initializer so __libc_sysinfo will have a fallback implementation
67// while .preinit_array constructors run.
68#if defined(__i386__)
Elliott Hughes95bca3f2018-06-12 14:16:50 -070069static __attribute__((__naked__)) void __libc_int0x80() {
70 __asm__ volatile("int $0x80; ret");
71}
Ryan Prichard27475b52018-05-17 17:14:18 -070072__LIBC_HIDDEN__ void* __libc_sysinfo = reinterpret_cast<void*>(__libc_int0x80);
73#endif
74
Stephen Cranef4b1cbd2017-05-09 14:27:43 -070075// We need a helper function for __libc_preinit because compiling with LTO may
76// inline functions requiring a stack protector check, but __stack_chk_guard is
77// not initialized at the start of __libc_preinit. __libc_preinit_impl will run
78// after __stack_chk_guard is initialized and therefore can safely have a stack
79// protector.
80__attribute__((noinline))
81static void __libc_preinit_impl(KernelArgumentBlock& args) {
Ryan Prichard701bd0c2018-11-21 16:23:03 -080082 __libc_auxv = args.auxv;
83#if defined(__i386__)
84 __libc_init_sysinfo(args);
85#endif
Josh Gaof6e5b582018-06-01 15:30:54 -070086
Ryan Prichard701bd0c2018-11-21 16:23:03 -080087 __libc_shared_globals = args.shared_globals;
Stephen Cranef4b1cbd2017-05-09 14:27:43 -070088 __libc_init_globals(args);
89 __libc_init_common(args);
90
91 // Hooks for various libraries to let them know that we're starting up.
92 __libc_globals.mutate(__libc_init_malloc);
93 netdClientInit();
94}
95
Elliott Hughes1d01fe82017-10-23 10:07:55 -070096// We flag the __libc_preinit function as a constructor to ensure that
97// its address is listed in libc.so's .init_array section.
98// This ensures that the function is called by the dynamic linker as
99// soon as the shared library is loaded.
100// We give this constructor priority 1 because we want libc's constructor
101// to run before any others (such as the jemalloc constructor), and lower
102// is better (http://b/68046352).
103__attribute__((constructor(1))) static void __libc_preinit() {
Ryan Prichard6631f9b2018-04-30 18:29:32 -0700104 // Read the kernel argument block pointer from TLS, then clear the slot so no
105 // other initializer sees its value.
Elliott Hughes2a0b8732013-10-08 18:50:24 -0700106 void** tls = __get_tls();
Ryan Prichard6631f9b2018-04-30 18:29:32 -0700107 KernelArgumentBlock* args = static_cast<KernelArgumentBlock*>(tls[TLS_SLOT_BIONIC_PREINIT]);
108 tls[TLS_SLOT_BIONIC_PREINIT] = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800109
Josh Gaob6453c52016-06-29 16:47:53 -0700110 // The linker has initialized its copy of the global stack_chk_guard, and filled in the main
111 // thread's TLS slot with that value. Initialize the local global stack guard with its value.
112 __stack_chk_guard = reinterpret_cast<uintptr_t>(tls[TLS_SLOT_STACK_GUARD]);
113
Stephen Cranef4b1cbd2017-05-09 14:27:43 -0700114 __libc_preinit_impl(*args);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800115}
116
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800117// This function is called from the executable's _start entry point
Ryan Prichard6631f9b2018-04-30 18:29:32 -0700118// (see arch-$ARCH/bionic/crtbegin.c), which is itself called by the dynamic
119// linker after it has loaded all shared libraries the executable depends on.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800120//
121// Note that the dynamic linker has also run all constructors in the
122// executable at this point.
123__noreturn void __libc_init(void* raw_args,
Elliott Hughesd2867962014-06-03 15:22:34 -0700124 void (*onexit)(void) __unused,
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800125 int (*slingshot)(int, char**, char**),
Dmitriy Ivanov4b415552014-09-04 21:54:34 +0000126 structors_array_t const * const structors) {
Christopher Ferris93ea09f2017-10-05 15:18:47 -0700127 BIONIC_STOP_UNWIND;
Dmitriy Ivanov4b415552014-09-04 21:54:34 +0000128
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800129 KernelArgumentBlock args(raw_args);
130
131 // Several Linux ABIs don't pass the onexit pointer, and the ones that
132 // do never use it. Therefore, we ignore it.
133
Dmitriy Ivanov4b415552014-09-04 21:54:34 +0000134 // The executable may have its own destructors listed in its .fini_array
135 // so we need to ensure that these are called when the program exits
136 // normally.
137 if (structors->fini_array) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700138 __cxa_atexit(__libc_fini,structors->fini_array,nullptr);
Dmitriy Ivanov4b415552014-09-04 21:54:34 +0000139 }
140
Ryan Prichard8f639a42018-10-01 23:10:05 -0700141 exit(slingshot(args.argc - __libc_shared_globals->initial_linker_arg_count,
142 args.argv + __libc_shared_globals->initial_linker_arg_count,
143 args.envp));
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800144}