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 | |
| 29 | #include <stddef.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <stdint.h> |
| 33 | #include <elf.h> |
| 34 | #include <asm/page.h> |
| 35 | #include "pthread_internal.h" |
| 36 | #include "atexit.h" |
| 37 | #include "libc_init_common.h" |
| 38 | |
| 39 | #include <bionic_tls.h> |
| 40 | #include <errno.h> |
Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 41 | #include <private/bionic_auxv.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 | |
David 'Digit' Turner | 3a654b1 | 2009-06-03 19:32:37 +0200 | [diff] [blame] | 43 | extern unsigned __get_sp(void); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 44 | extern pid_t gettid(void); |
David 'Digit' Turner | 3a654b1 | 2009-06-03 19:32:37 +0200 | [diff] [blame] | 45 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 46 | char* __progname; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 47 | char** environ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 48 | |
| 49 | /* from asm/page.h */ |
| 50 | unsigned int __page_size = PAGE_SIZE; |
| 51 | unsigned int __page_shift = PAGE_SHIFT; |
| 52 | |
| 53 | |
| 54 | int __system_properties_init(void); |
| 55 | |
Nick Kralevich | e3a49a8 | 2013-01-14 14:46:26 -0800 | [diff] [blame] | 56 | static Elf32_auxv_t* get_aux_from_elfdata(uintptr_t* elf_data) { |
| 57 | int argc = *elf_data; |
| 58 | char** argv = (char**) (elf_data + 1); |
| 59 | char** envp = argv + argc + 1; |
| 60 | |
| 61 | // The auxiliary vector is at the end of the environment block |
| 62 | while(*envp != NULL) { |
| 63 | envp++; |
| 64 | } |
| 65 | /* The end of the environment block is marked by two NULL pointers */ |
| 66 | envp++; |
| 67 | |
| 68 | return (Elf32_auxv_t*) envp; |
| 69 | } |
| 70 | |
| 71 | |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 72 | /* Init TLS for the initial thread. Called by the linker _before_ libc is mapped |
| 73 | * in memory. Beware: all writes to libc globals from this function will |
| 74 | * apply to linker-private copies and will not be visible from libc later on. |
| 75 | * |
| 76 | * Note: this function creates a pthread_internal_t for the initial thread and |
| 77 | * stores the pointer in TLS, but does not add it to pthread's gThreadList. This |
| 78 | * has to be done later from libc itself (see __libc_init_common). |
| 79 | * |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 80 | * This function also stores the elf_data argument in a specific TLS slot to be later |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 81 | * picked up by the libc constructor. |
| 82 | */ |
Nick Kralevich | e3a49a8 | 2013-01-14 14:46:26 -0800 | [diff] [blame] | 83 | void __libc_init_tls(uintptr_t* elf_data) { |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 84 | unsigned stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE; |
| 85 | unsigned stack_size = 128 * 1024; |
| 86 | unsigned stack_bottom = stack_top - stack_size; |
David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 87 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 88 | pthread_attr_t thread_attr; |
| 89 | pthread_attr_init(&thread_attr); |
| 90 | pthread_attr_setstack(&thread_attr, (void*) stack_bottom, stack_size); |
David 'Digit' Turner | 3a654b1 | 2009-06-03 19:32:37 +0200 | [diff] [blame] | 91 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 92 | static pthread_internal_t thread; |
| 93 | _init_thread(&thread, gettid(), &thread_attr, (void*) stack_bottom, false); |
David 'Digit' Turner | 3a654b1 | 2009-06-03 19:32:37 +0200 | [diff] [blame] | 94 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 95 | static void* tls_area[BIONIC_TLS_SLOTS]; |
Nick Kralevich | e3a49a8 | 2013-01-14 14:46:26 -0800 | [diff] [blame] | 96 | __libc_auxv = get_aux_from_elfdata(elf_data); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 97 | __init_tls(tls_area, &thread); |
| 98 | tls_area[TLS_SLOT_BIONIC_PREINIT] = elf_data; |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 99 | } |
| 100 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 101 | void __libc_init_common(uintptr_t* elf_data) { |
| 102 | int argc = *elf_data; |
| 103 | char** argv = (char**) (elf_data + 1); |
| 104 | char** envp = argv + argc + 1; |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 105 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 106 | // Get the main thread from TLS and add it to the thread list. |
| 107 | pthread_internal_t* main_thread = __get_thread(); |
| 108 | main_thread->allocated_on_heap = false; |
| 109 | _pthread_internal_add(main_thread); |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 110 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 111 | // Set various globals. |
| 112 | errno = 0; |
| 113 | __progname = argv[0] ? argv[0] : "<unknown>"; |
| 114 | environ = envp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 115 | |
Nick Kralevich | e3a49a8 | 2013-01-14 14:46:26 -0800 | [diff] [blame] | 116 | __libc_auxv = get_aux_from_elfdata(elf_data); |
Nick Kralevich | 2c5153b | 2013-01-11 14:43:05 -0800 | [diff] [blame] | 117 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 118 | __system_properties_init(); // Requires 'environ'. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 119 | } |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 120 | |
| 121 | /* This function will be called during normal program termination |
| 122 | * to run the destructors that are listed in the .fini_array section |
| 123 | * of the executable, if any. |
| 124 | * |
| 125 | * 'fini_array' points to a list of function addresses. The first |
| 126 | * entry in the list has value -1, the last one has value 0. |
| 127 | */ |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 128 | void __libc_fini(void* array) { |
| 129 | void** fini_array = array; |
| 130 | const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */ |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 131 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 132 | /* Sanity check - first entry must be -1 */ |
| 133 | if (array == NULL || (size_t)fini_array[0] != minus1) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | /* skip over it */ |
| 138 | fini_array += 1; |
| 139 | |
| 140 | /* Count the number of destructors. */ |
| 141 | int count = 0; |
| 142 | while (fini_array[count] != NULL) { |
| 143 | ++count; |
| 144 | } |
| 145 | |
| 146 | /* Now call each destructor in reverse order. */ |
| 147 | while (count > 0) { |
| 148 | void (*func)() = (void (*)) fini_array[--count]; |
| 149 | |
| 150 | /* Sanity check, any -1 in the list is ignored */ |
| 151 | if ((size_t)func == minus1) { |
| 152 | continue; |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 155 | func(); |
| 156 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 157 | |
| 158 | #ifndef LIBC_STATIC |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 159 | { |
| 160 | extern void __libc_postfini(void) __attribute__((weak)); |
| 161 | if (__libc_postfini) { |
| 162 | __libc_postfini(); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 163 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 164 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 165 | #endif |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 166 | } |