blob: ff9940ef76ea7f3330e6d71505679f53f30fd893 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 Project1dc9e472009-03-03 19:28:35 -080029#include "libc_init_common.h"
30
Elliott Hughes642331b2013-03-06 15:03:53 -080031#include <elf.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <errno.h>
Elliott Hughes642331b2013-03-06 15:03:53 -080033#include <stddef.h>
34#include <stdint.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <sys/auxv.h>
Brian Carlstrom322e7bc2013-09-12 21:47:20 -070038#include <sys/time.h>
39#include <sys/resource.h>
Elliott Hughes642331b2013-03-06 15:03:53 -080040#include <unistd.h>
41
Elliott Hughes642331b2013-03-06 15:03:53 -080042#include "private/bionic_auxv.h"
43#include "private/bionic_ssp.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070044#include "private/bionic_tls.h"
Elliott Hughes642331b2013-03-06 15:03:53 -080045#include "private/KernelArgumentBlock.h"
46#include "pthread_internal.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047
Elliott Hughes0d787c12013-04-04 13:46:46 -070048extern "C" abort_msg_t** __abort_message_ptr;
Serban Constantinescu7f70c9b2013-10-13 22:07:26 +010049extern "C" uintptr_t __get_sp(void);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080050extern "C" int __system_properties_init(void);
Elliott Hughes70b24b12013-11-15 11:51:07 -080051extern "C" int __set_tls(void* ptr);
Elliott Hughes877ec6d2013-11-15 17:40:18 -080052extern "C" int __set_tid_address(int* tid_address);
David 'Digit' Turner3a654b12009-06-03 19:32:37 +020053
Dan Albertb3aaf392014-08-13 13:11:58 -070054__LIBC_HIDDEN__ void __libc_init_vdso();
Elliott Hughes625993d2014-07-15 16:53:13 -070055
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080056// Not public, but well-known in the BSDs.
Elliott Hughese4ccf5a2013-02-07 12:06:44 -080057const char* __progname;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080058
Elliott Hughesd3920b32013-02-07 18:39:34 -080059// Declared in <unistd.h>.
Elliott Hughes4f251be2012-11-01 16:33:29 -070060char** environ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061
Elliott Hugheseb847bc2013-10-09 15:50:50 -070062// Declared in "private/bionic_ssp.h".
Elliott Hughes642331b2013-03-06 15:03:53 -080063uintptr_t __stack_chk_guard = 0;
64
Brian Carlstrom50af69e2013-09-13 16:34:43 -070065static size_t get_main_thread_stack_size() {
66 rlimit stack_limit;
Brian Carlstrom322e7bc2013-09-12 21:47:20 -070067 int rlimit_result = getrlimit(RLIMIT_STACK, &stack_limit);
Brian Carlstrom50af69e2013-09-13 16:34:43 -070068 if ((rlimit_result == 0) &&
69 (stack_limit.rlim_cur != RLIM_INFINITY) &&
70 (stack_limit.rlim_cur > PTHREAD_STACK_MIN)) {
71 return (stack_limit.rlim_cur & ~(PAGE_SIZE - 1));
Brian Carlstrom322e7bc2013-09-12 21:47:20 -070072 }
Brian Carlstrom50af69e2013-09-13 16:34:43 -070073 return PTHREAD_STACK_SIZE_DEFAULT;
Brian Carlstrom322e7bc2013-09-12 21:47:20 -070074}
75
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040076/* Init TLS for the initial thread. Called by the linker _before_ libc is mapped
77 * in memory. Beware: all writes to libc globals from this function will
78 * apply to linker-private copies and will not be visible from libc later on.
79 *
80 * Note: this function creates a pthread_internal_t for the initial thread and
Elliott Hughes1728b232014-05-14 10:02:03 -070081 * stores the pointer in TLS, but does not add it to pthread's thread list. This
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040082 * has to be done later from libc itself (see __libc_init_common).
83 *
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080084 * This function also stores a pointer to the kernel argument block in a TLS slot to be
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040085 * picked up by the libc constructor.
86 */
Elliott Hughesd3920b32013-02-07 18:39:34 -080087void __libc_init_tls(KernelArgumentBlock& args) {
88 __libc_auxv = args.auxv;
89
Elliott Hughes40eabe22013-02-14 18:59:37 -080090 static void* tls[BIONIC_TLS_SLOTS];
Elliott Hughes877ec6d2013-11-15 17:40:18 -080091 static pthread_internal_t main_thread;
92 main_thread.tls = tls;
93
94 // Tell the kernel to clear our tid field when we exit, so we're like any other pthread.
Elliott Hughes7086ad62014-06-19 16:39:01 -070095 // As a side-effect, this tells us our pid (which is the same as the main thread's tid).
Elliott Hughes877ec6d2013-11-15 17:40:18 -080096 main_thread.tid = __set_tid_address(&main_thread.tid);
Elliott Hughes7086ad62014-06-19 16:39:01 -070097 main_thread.set_cached_pid(main_thread.tid);
Elliott Hughes877ec6d2013-11-15 17:40:18 -080098
Elliott Hughes7086ad62014-06-19 16:39:01 -070099 // Work out the extent of the main thread's stack.
100 uintptr_t stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE;
101 size_t stack_size = get_main_thread_stack_size();
102 void* stack_bottom = reinterpret_cast<void*>(stack_top - stack_size);
103
104 // We don't want to free the main thread's stack even when the main thread exits
105 // because things like environment variables with global scope live on it.
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800106 pthread_attr_init(&main_thread.attr);
Elliott Hughes7086ad62014-06-19 16:39:01 -0700107 pthread_attr_setstack(&main_thread.attr, stack_bottom, stack_size);
Elliott Hughescef3fae2013-11-19 16:52:24 -0800108 main_thread.attr.flags = PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK | PTHREAD_ATTR_FLAG_MAIN_THREAD;
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800109
Elliott Hughescef3fae2013-11-19 16:52:24 -0800110 __init_thread(&main_thread, false);
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800111 __init_tls(&main_thread);
112 __set_tls(main_thread.tls);
Elliott Hughes40eabe22013-02-14 18:59:37 -0800113 tls[TLS_SLOT_BIONIC_PREINIT] = &args;
Elliott Hughes70b24b12013-11-15 11:51:07 -0800114
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800115 __init_alternate_signal_stack(&main_thread);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +0400116}
117
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800118void __libc_init_common(KernelArgumentBlock& args) {
119 // Initialize various globals.
120 environ = args.envp;
121 errno = 0;
122 __libc_auxv = args.auxv;
Elliott Hughese4ccf5a2013-02-07 12:06:44 -0800123 __progname = args.argv[0] ? args.argv[0] : "<unknown>";
Elliott Hughes0d787c12013-04-04 13:46:46 -0700124 __abort_message_ptr = args.abort_message_ptr;
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +0400125
Elliott Hughes642331b2013-03-06 15:03:53 -0800126 // AT_RANDOM is a pointer to 16 bytes of randomness on the stack.
127 __stack_chk_guard = *reinterpret_cast<uintptr_t*>(getauxval(AT_RANDOM));
128
Elliott Hughes4f251be2012-11-01 16:33:29 -0700129 // Get the main thread from TLS and add it to the thread list.
130 pthread_internal_t* main_thread = __get_thread();
Elliott Hughes4f251be2012-11-01 16:33:29 -0700131 _pthread_internal_add(main_thread);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +0400132
Elliott Hughes4f251be2012-11-01 16:33:29 -0700133 __system_properties_init(); // Requires 'environ'.
Elliott Hughes625993d2014-07-15 16:53:13 -0700134
135 __libc_init_vdso();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136}
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200137
138/* This function will be called during normal program termination
139 * to run the destructors that are listed in the .fini_array section
140 * of the executable, if any.
141 *
142 * 'fini_array' points to a list of function addresses. The first
143 * entry in the list has value -1, the last one has value 0.
144 */
Elliott Hughes4f251be2012-11-01 16:33:29 -0700145void __libc_fini(void* array) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800146 void** fini_array = reinterpret_cast<void**>(array);
Elliott Hughes4f251be2012-11-01 16:33:29 -0700147 const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200148
Elliott Hughes4f251be2012-11-01 16:33:29 -0700149 /* Sanity check - first entry must be -1 */
150 if (array == NULL || (size_t)fini_array[0] != minus1) {
151 return;
152 }
153
154 /* skip over it */
155 fini_array += 1;
156
157 /* Count the number of destructors. */
158 int count = 0;
159 while (fini_array[count] != NULL) {
160 ++count;
161 }
162
163 /* Now call each destructor in reverse order. */
164 while (count > 0) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800165 void (*func)() = (void (*)()) fini_array[--count];
Elliott Hughes4f251be2012-11-01 16:33:29 -0700166
167 /* Sanity check, any -1 in the list is ignored */
168 if ((size_t)func == minus1) {
169 continue;
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200170 }
171
Elliott Hughes4f251be2012-11-01 16:33:29 -0700172 func();
173 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700174
175#ifndef LIBC_STATIC
Elliott Hughes4f251be2012-11-01 16:33:29 -0700176 {
177 extern void __libc_postfini(void) __attribute__((weak));
178 if (__libc_postfini) {
179 __libc_postfini();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700180 }
Elliott Hughes4f251be2012-11-01 16:33:29 -0700181 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700182#endif
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200183}