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 */
28
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>
52 #include "libc_init_common.h"
53
54 #include "private/bionic_elf_tls.h"
55 #include "private/bionic_globals.h"
56 #include "private/bionic_macros.h"
57 #include "private/bionic_ssp.h"
58 #include "private/bionic_tls.h"
59 #include "private/KernelArgumentBlock.h"
60
61 extern "C" {
62 extern void netdClientInit(void);
63 extern int __cxa_atexit(void (*)(void *), void *, void *);
64 };
65
66 // Use an initializer so __libc_sysinfo will have a fallback implementation
67 // while .preinit_array constructors run.
68 #if defined(__i386__)
69 __LIBC_HIDDEN__ void* __libc_sysinfo = reinterpret_cast<void*>(__libc_int0x80);
70 #endif
71
72 // We need a helper function for __libc_preinit because compiling with LTO may
73 // inline functions requiring a stack protector check, but __stack_chk_guard is
74 // not initialized at the start of __libc_preinit. __libc_preinit_impl will run
75 // after __stack_chk_guard is initialized and therefore can safely have a stack
76 // protector.
77 __attribute__((noinline))
__libc_preinit_impl()78 static void __libc_preinit_impl() {
79 #if defined(__i386__)
80 __libc_init_sysinfo();
81 #endif
82
83 // Register libc.so's copy of the TLS generation variable so the linker can
84 // update it when it loads or unloads a shared object.
85 TlsModules& tls_modules = __libc_shared_globals()->tls_modules;
86 tls_modules.generation_libc_so = &__libc_tls_generation_copy;
87 __libc_tls_generation_copy = tls_modules.generation;
88
89 __libc_init_globals();
90 __libc_init_common();
91
92 // Hooks for various libraries to let them know that we're starting up.
93 __libc_globals.mutate(__libc_init_malloc);
94 netdClientInit();
95 }
96
97 // We flag the __libc_preinit function as a constructor to ensure that
98 // its address is listed in libc.so's .init_array section.
99 // This ensures that the function is called by the dynamic linker as
100 // soon as the shared library is loaded.
101 // We give this constructor priority 1 because we want libc's constructor
102 // to run before any others (such as the jemalloc constructor), and lower
103 // is better (http://b/68046352).
__libc_preinit()104 __attribute__((constructor(1))) static void __libc_preinit() {
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>(__get_tls()[TLS_SLOT_STACK_GUARD]);
108
109 __libc_preinit_impl();
110 }
111
112 // This function is called from the executable's _start entry point
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.
115 //
116 // Note that the dynamic linker has also run all constructors in the
117 // executable at this point.
__libc_init(void * raw_args,void (* onexit)(void)__unused,int (* slingshot)(int,char **,char **),structors_array_t const * const structors)118 __noreturn void __libc_init(void* raw_args,
119 void (*onexit)(void) __unused,
120 int (*slingshot)(int, char**, char**),
121 structors_array_t const * const structors) {
122 BIONIC_STOP_UNWIND;
123
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
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) {
133 __cxa_atexit(__libc_fini,structors->fini_array,nullptr);
134 }
135
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));
139 }
140
141 extern "C" libc_shared_globals* __loader_shared_globals();
142
__libc_shared_globals()143 __LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals() {
144 return __loader_shared_globals();
145 }
146