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 <asm/page.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 32 | #include <bionic_tls.h> |
Elliott Hughes | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 33 | #include <elf.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 34 | #include <errno.h> |
Elliott Hughes | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 35 | #include <stddef.h> |
| 36 | #include <stdint.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <sys/auxv.h> |
Brian Carlstrom | 322e7bc | 2013-09-12 21:47:20 -0700 | [diff] [blame^] | 40 | #include <sys/time.h> |
| 41 | #include <sys/resource.h> |
Elliott Hughes | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 42 | #include <unistd.h> |
| 43 | |
| 44 | #include "atexit.h" |
| 45 | #include "private/bionic_auxv.h" |
| 46 | #include "private/bionic_ssp.h" |
| 47 | #include "private/KernelArgumentBlock.h" |
| 48 | #include "pthread_internal.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 50 | extern "C" abort_msg_t** __abort_message_ptr; |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 51 | extern "C" unsigned __get_sp(void); |
| 52 | extern "C" int __system_properties_init(void); |
David 'Digit' Turner | 3a654b1 | 2009-06-03 19:32:37 +0200 | [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 | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 60 | // Declared in <private/bionic_ssp.h>. |
| 61 | uintptr_t __stack_chk_guard = 0; |
| 62 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 63 | // Declared in <asm/page.h>. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | unsigned int __page_size = PAGE_SIZE; |
| 65 | unsigned int __page_shift = PAGE_SHIFT; |
| 66 | |
Brian Carlstrom | 322e7bc | 2013-09-12 21:47:20 -0700 | [diff] [blame^] | 67 | static size_t get_stack_size() { |
| 68 | const size_t minimal_stack_size = 128 * 1024; |
| 69 | size_t stack_size = minimal_stack_size; |
| 70 | struct rlimit stack_limit; |
| 71 | int rlimit_result = getrlimit(RLIMIT_STACK, &stack_limit); |
| 72 | if ((rlimit_result == 0) && (stack_limit.rlim_cur != RLIM_INFINITY)) { |
| 73 | stack_size = stack_limit.rlim_cur; |
| 74 | stack_size = (stack_size & ~(PAGE_SIZE - 1)); |
| 75 | if (stack_size < minimal_stack_size) { |
| 76 | stack_size = minimal_stack_size; |
| 77 | } |
| 78 | } |
| 79 | return stack_size; |
| 80 | } |
| 81 | |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 82 | /* Init TLS for the initial thread. Called by the linker _before_ libc is mapped |
| 83 | * in memory. Beware: all writes to libc globals from this function will |
| 84 | * apply to linker-private copies and will not be visible from libc later on. |
| 85 | * |
| 86 | * Note: this function creates a pthread_internal_t for the initial thread and |
| 87 | * stores the pointer in TLS, but does not add it to pthread's gThreadList. This |
| 88 | * has to be done later from libc itself (see __libc_init_common). |
| 89 | * |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 90 | * 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] | 91 | * picked up by the libc constructor. |
| 92 | */ |
Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 93 | void __libc_init_tls(KernelArgumentBlock& args) { |
| 94 | __libc_auxv = args.auxv; |
| 95 | |
Brian Carlstrom | 322e7bc | 2013-09-12 21:47:20 -0700 | [diff] [blame^] | 96 | uintptr_t stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE; |
| 97 | size_t stack_size = get_stack_size(); |
| 98 | uintptr_t stack_bottom = stack_top - stack_size; |
David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 99 | |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 100 | static void* tls[BIONIC_TLS_SLOTS]; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 101 | static pthread_internal_t thread; |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 102 | thread.tid = gettid(); |
| 103 | thread.tls = tls; |
Elliott Hughes | 6d33918 | 2013-02-12 16:36:04 -0800 | [diff] [blame] | 104 | pthread_attr_init(&thread.attr); |
| 105 | pthread_attr_setstack(&thread.attr, (void*) stack_bottom, stack_size); |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 106 | _init_thread(&thread, false); |
| 107 | __init_tls(&thread); |
| 108 | tls[TLS_SLOT_BIONIC_PREINIT] = &args; |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 109 | } |
| 110 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 111 | void __libc_init_common(KernelArgumentBlock& args) { |
| 112 | // Initialize various globals. |
| 113 | environ = args.envp; |
| 114 | errno = 0; |
| 115 | __libc_auxv = args.auxv; |
Elliott Hughes | e4ccf5a | 2013-02-07 12:06:44 -0800 | [diff] [blame] | 116 | __progname = args.argv[0] ? args.argv[0] : "<unknown>"; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 117 | __abort_message_ptr = args.abort_message_ptr; |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 118 | |
Elliott Hughes | 642331b | 2013-03-06 15:03:53 -0800 | [diff] [blame] | 119 | // AT_RANDOM is a pointer to 16 bytes of randomness on the stack. |
| 120 | __stack_chk_guard = *reinterpret_cast<uintptr_t*>(getauxval(AT_RANDOM)); |
| 121 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 122 | // Get the main thread from TLS and add it to the thread list. |
| 123 | pthread_internal_t* main_thread = __get_thread(); |
| 124 | main_thread->allocated_on_heap = false; |
| 125 | _pthread_internal_add(main_thread); |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 126 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 127 | __system_properties_init(); // Requires 'environ'. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 128 | } |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 129 | |
| 130 | /* This function will be called during normal program termination |
| 131 | * to run the destructors that are listed in the .fini_array section |
| 132 | * of the executable, if any. |
| 133 | * |
| 134 | * 'fini_array' points to a list of function addresses. The first |
| 135 | * entry in the list has value -1, the last one has value 0. |
| 136 | */ |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 137 | void __libc_fini(void* array) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 138 | void** fini_array = reinterpret_cast<void**>(array); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 139 | const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */ |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 140 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 141 | /* Sanity check - first entry must be -1 */ |
| 142 | if (array == NULL || (size_t)fini_array[0] != minus1) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | /* skip over it */ |
| 147 | fini_array += 1; |
| 148 | |
| 149 | /* Count the number of destructors. */ |
| 150 | int count = 0; |
| 151 | while (fini_array[count] != NULL) { |
| 152 | ++count; |
| 153 | } |
| 154 | |
| 155 | /* Now call each destructor in reverse order. */ |
| 156 | while (count > 0) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 157 | void (*func)() = (void (*)()) fini_array[--count]; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 158 | |
| 159 | /* Sanity check, any -1 in the list is ignored */ |
| 160 | if ((size_t)func == minus1) { |
| 161 | continue; |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 164 | func(); |
| 165 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 166 | |
| 167 | #ifndef LIBC_STATIC |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 168 | { |
| 169 | extern void __libc_postfini(void) __attribute__((weak)); |
| 170 | if (__libc_postfini) { |
| 171 | __libc_postfini(); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 172 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 173 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 174 | #endif |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 175 | } |