The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -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 | */ |
| 28 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 29 | #include "libc_init_common.h" |
| 30 | |
Elliott Hughes | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 31 | #include <elf.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 32 | #include <errno.h> |
Elliott Hughes | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 33 | #include <stddef.h> |
| 34 | #include <stdint.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <sys/auxv.h> |
Brian Carlstrom | 322e7bc | 2013-09-12 21:47:20 -0700 | [diff] [blame] | 38 | #include <sys/time.h> |
Elliott Hughes | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 39 | #include <unistd.h> |
| 40 | |
Elliott Hughes | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 41 | #include "private/bionic_auxv.h" |
| 42 | #include "private/bionic_ssp.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 43 | #include "private/bionic_tls.h" |
Elliott Hughes | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 44 | #include "private/KernelArgumentBlock.h" |
| 45 | #include "pthread_internal.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 46 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 47 | extern "C" abort_msg_t** __abort_message_ptr; |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 48 | extern "C" int __system_properties_init(void); |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 49 | extern "C" int __set_tls(void* ptr); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 50 | extern "C" int __set_tid_address(int* tid_address); |
David 'Digit' Turner | 3a654b1 | 2009-06-03 19:32:37 +0200 | [diff] [blame] | 51 | |
Dan Albert | b3aaf39 | 2014-08-13 13:11:58 -0700 | [diff] [blame] | 52 | __LIBC_HIDDEN__ void __libc_init_vdso(); |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 53 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 54 | // Not public, but well-known in the BSDs. |
Elliott Hughes | e4ccf5a | 2013-02-07 12:06:44 -0800 | [diff] [blame] | 55 | const char* __progname; |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 56 | |
Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 57 | // Declared in <unistd.h>. |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 58 | char** environ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 59 | |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 60 | // Declared in "private/bionic_ssp.h". |
Elliott Hughes | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 61 | uintptr_t __stack_chk_guard = 0; |
| 62 | |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 63 | /* Init TLS for the initial thread. Called by the linker _before_ libc is mapped |
| 64 | * in memory. Beware: all writes to libc globals from this function will |
| 65 | * apply to linker-private copies and will not be visible from libc later on. |
| 66 | * |
| 67 | * Note: this function creates a pthread_internal_t for the initial thread and |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 68 | * stores the pointer in TLS, but does not add it to pthread's thread list. This |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 69 | * has to be done later from libc itself (see __libc_init_common). |
| 70 | * |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 71 | * This function also stores a pointer to the kernel argument block in a TLS slot to be |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 72 | * picked up by the libc constructor. |
| 73 | */ |
Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 74 | void __libc_init_tls(KernelArgumentBlock& args) { |
| 75 | __libc_auxv = args.auxv; |
| 76 | |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 77 | static void* tls[BIONIC_TLS_SLOTS]; |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 78 | static pthread_internal_t main_thread; |
| 79 | main_thread.tls = tls; |
| 80 | |
| 81 | // Tell the kernel to clear our tid field when we exit, so we're like any other pthread. |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 82 | // As a side-effect, this tells us our pid (which is the same as the main thread's tid). |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 83 | main_thread.tid = __set_tid_address(&main_thread.tid); |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 84 | main_thread.set_cached_pid(main_thread.tid); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 85 | |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 86 | // We don't want to free the main thread's stack even when the main thread exits |
| 87 | // because things like environment variables with global scope live on it. |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 88 | // We also can't free the pthread_internal_t itself, since that lives on the main |
| 89 | // thread's stack rather than on the heap. |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 90 | pthread_attr_init(&main_thread.attr); |
Elliott Hughes | cef3fae | 2013-11-19 16:52:24 -0800 | [diff] [blame] | 91 | main_thread.attr.flags = PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK | PTHREAD_ATTR_FLAG_MAIN_THREAD; |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 92 | main_thread.attr.guard_size = 0; // The main thread has no guard page. |
| 93 | main_thread.attr.stack_size = 0; // User code should never see this; we'll compute it when asked. |
| 94 | // TODO: the main thread's sched_policy and sched_priority need to be queried. |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 95 | |
Elliott Hughes | cef3fae | 2013-11-19 16:52:24 -0800 | [diff] [blame] | 96 | __init_thread(&main_thread, false); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 97 | __init_tls(&main_thread); |
| 98 | __set_tls(main_thread.tls); |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 99 | tls[TLS_SLOT_BIONIC_PREINIT] = &args; |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 100 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 101 | __init_alternate_signal_stack(&main_thread); |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 102 | } |
| 103 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 104 | void __libc_init_common(KernelArgumentBlock& args) { |
| 105 | // Initialize various globals. |
| 106 | environ = args.envp; |
| 107 | errno = 0; |
| 108 | __libc_auxv = args.auxv; |
Elliott Hughes | e4ccf5a | 2013-02-07 12:06:44 -0800 | [diff] [blame] | 109 | __progname = args.argv[0] ? args.argv[0] : "<unknown>"; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 110 | __abort_message_ptr = args.abort_message_ptr; |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 111 | |
Elliott Hughes | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 112 | // AT_RANDOM is a pointer to 16 bytes of randomness on the stack. |
| 113 | __stack_chk_guard = *reinterpret_cast<uintptr_t*>(getauxval(AT_RANDOM)); |
| 114 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 115 | // Get the main thread from TLS and add it to the thread list. |
| 116 | pthread_internal_t* main_thread = __get_thread(); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 117 | _pthread_internal_add(main_thread); |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 118 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 119 | __system_properties_init(); // Requires 'environ'. |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 120 | |
| 121 | __libc_init_vdso(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 122 | } |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 123 | |
| 124 | /* This function will be called during normal program termination |
| 125 | * to run the destructors that are listed in the .fini_array section |
| 126 | * of the executable, if any. |
| 127 | * |
| 128 | * 'fini_array' points to a list of function addresses. The first |
| 129 | * entry in the list has value -1, the last one has value 0. |
| 130 | */ |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 131 | void __libc_fini(void* array) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 132 | void** fini_array = reinterpret_cast<void**>(array); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 133 | const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */ |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 134 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 135 | /* Sanity check - first entry must be -1 */ |
| 136 | if (array == NULL || (size_t)fini_array[0] != minus1) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | /* skip over it */ |
| 141 | fini_array += 1; |
| 142 | |
| 143 | /* Count the number of destructors. */ |
| 144 | int count = 0; |
| 145 | while (fini_array[count] != NULL) { |
| 146 | ++count; |
| 147 | } |
| 148 | |
| 149 | /* Now call each destructor in reverse order. */ |
| 150 | while (count > 0) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 151 | void (*func)() = (void (*)()) fini_array[--count]; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 152 | |
| 153 | /* Sanity check, any -1 in the list is ignored */ |
| 154 | if ((size_t)func == minus1) { |
| 155 | continue; |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 158 | func(); |
| 159 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 160 | |
| 161 | #ifndef LIBC_STATIC |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 162 | { |
| 163 | extern void __libc_postfini(void) __attribute__((weak)); |
| 164 | if (__libc_postfini) { |
| 165 | __libc_postfini(); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 166 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 167 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 168 | #endif |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 169 | } |