libc_init_common.cpp revision cef3faec0ea40fdfe58e425fd0be64f00de6a26d
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#include "libc_init_common.h" 30 31#include <elf.h> 32#include <errno.h> 33#include <stddef.h> 34#include <stdint.h> 35#include <stdio.h> 36#include <stdlib.h> 37#include <sys/auxv.h> 38#include <sys/time.h> 39#include <sys/resource.h> 40#include <unistd.h> 41 42#include "atexit.h" 43#include "private/bionic_auxv.h" 44#include "private/bionic_ssp.h" 45#include "private/bionic_tls.h" 46#include "private/KernelArgumentBlock.h" 47#include "pthread_internal.h" 48 49extern "C" abort_msg_t** __abort_message_ptr; 50extern "C" uintptr_t __get_sp(void); 51extern "C" int __system_properties_init(void); 52extern "C" int __set_tls(void* ptr); 53extern "C" int __set_tid_address(int* tid_address); 54 55// Not public, but well-known in the BSDs. 56const char* __progname; 57 58// Declared in <unistd.h>. 59char** environ; 60 61// Declared in "private/bionic_ssp.h". 62uintptr_t __stack_chk_guard = 0; 63 64static size_t get_main_thread_stack_size() { 65 rlimit stack_limit; 66 int rlimit_result = getrlimit(RLIMIT_STACK, &stack_limit); 67 if ((rlimit_result == 0) && 68 (stack_limit.rlim_cur != RLIM_INFINITY) && 69 (stack_limit.rlim_cur > PTHREAD_STACK_MIN)) { 70 return (stack_limit.rlim_cur & ~(PAGE_SIZE - 1)); 71 } 72 return PTHREAD_STACK_SIZE_DEFAULT; 73} 74 75/* Init TLS for the initial thread. Called by the linker _before_ libc is mapped 76 * in memory. Beware: all writes to libc globals from this function will 77 * apply to linker-private copies and will not be visible from libc later on. 78 * 79 * Note: this function creates a pthread_internal_t for the initial thread and 80 * stores the pointer in TLS, but does not add it to pthread's gThreadList. This 81 * has to be done later from libc itself (see __libc_init_common). 82 * 83 * This function also stores a pointer to the kernel argument block in a TLS slot to be 84 * picked up by the libc constructor. 85 */ 86void __libc_init_tls(KernelArgumentBlock& args) { 87 __libc_auxv = args.auxv; 88 89 uintptr_t stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE; 90 size_t stack_size = get_main_thread_stack_size(); 91 uintptr_t stack_bottom = stack_top - stack_size; 92 93 static void* tls[BIONIC_TLS_SLOTS]; 94 static pthread_internal_t main_thread; 95 main_thread.tls = tls; 96 97 // Tell the kernel to clear our tid field when we exit, so we're like any other pthread. 98 main_thread.tid = __set_tid_address(&main_thread.tid); 99 100 // We already have a stack, and we don't want to free it up on exit (because things like 101 // environment variables with global scope live on it). 102 pthread_attr_init(&main_thread.attr); 103 pthread_attr_setstack(&main_thread.attr, (void*) stack_bottom, stack_size); 104 main_thread.attr.flags = PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK | PTHREAD_ATTR_FLAG_MAIN_THREAD; 105 106 __init_thread(&main_thread, false); 107 __init_tls(&main_thread); 108 __set_tls(main_thread.tls); 109 tls[TLS_SLOT_BIONIC_PREINIT] = &args; 110 111 __init_alternate_signal_stack(&main_thread); 112} 113 114void __libc_init_common(KernelArgumentBlock& args) { 115 // Initialize various globals. 116 environ = args.envp; 117 errno = 0; 118 __libc_auxv = args.auxv; 119 __progname = args.argv[0] ? args.argv[0] : "<unknown>"; 120 __abort_message_ptr = args.abort_message_ptr; 121 122 // AT_RANDOM is a pointer to 16 bytes of randomness on the stack. 123 __stack_chk_guard = *reinterpret_cast<uintptr_t*>(getauxval(AT_RANDOM)); 124 125 // Get the main thread from TLS and add it to the thread list. 126 pthread_internal_t* main_thread = __get_thread(); 127 _pthread_internal_add(main_thread); 128 129 __system_properties_init(); // Requires 'environ'. 130} 131 132/* This function will be called during normal program termination 133 * to run the destructors that are listed in the .fini_array section 134 * of the executable, if any. 135 * 136 * 'fini_array' points to a list of function addresses. The first 137 * entry in the list has value -1, the last one has value 0. 138 */ 139void __libc_fini(void* array) { 140 void** fini_array = reinterpret_cast<void**>(array); 141 const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */ 142 143 /* Sanity check - first entry must be -1 */ 144 if (array == NULL || (size_t)fini_array[0] != minus1) { 145 return; 146 } 147 148 /* skip over it */ 149 fini_array += 1; 150 151 /* Count the number of destructors. */ 152 int count = 0; 153 while (fini_array[count] != NULL) { 154 ++count; 155 } 156 157 /* Now call each destructor in reverse order. */ 158 while (count > 0) { 159 void (*func)() = (void (*)()) fini_array[--count]; 160 161 /* Sanity check, any -1 in the list is ignored */ 162 if ((size_t)func == minus1) { 163 continue; 164 } 165 166 func(); 167 } 168 169#ifndef LIBC_STATIC 170 { 171 extern void __libc_postfini(void) __attribute__((weak)); 172 if (__libc_postfini) { 173 __libc_postfini(); 174 } 175 } 176#endif 177} 178