blob: f5cb1e53bdbf92e88c1e739f0228440f4567a93b [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 <asm/page.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <bionic_tls.h>
Elliott Hughes642331b2013-03-06 15:03:53 -080033#include <elf.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <errno.h>
Elliott Hughes642331b2013-03-06 15:03:53 -080035#include <stddef.h>
36#include <stdint.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <sys/auxv.h>
Brian Carlstrom322e7bc2013-09-12 21:47:20 -070040#include <sys/time.h>
41#include <sys/resource.h>
Elliott Hughes642331b2013-03-06 15:03:53 -080042#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 Project1dc9e472009-03-03 19:28:35 -080049
Elliott Hughes0d787c12013-04-04 13:46:46 -070050extern "C" abort_msg_t** __abort_message_ptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080051extern "C" unsigned __get_sp(void);
52extern "C" int __system_properties_init(void);
David 'Digit' Turner3a654b12009-06-03 19:32:37 +020053
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080054// Not public, but well-known in the BSDs.
Elliott Hughese4ccf5a2013-02-07 12:06:44 -080055const char* __progname;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080056
Elliott Hughesd3920b32013-02-07 18:39:34 -080057// Declared in <unistd.h>.
Elliott Hughes4f251be2012-11-01 16:33:29 -070058char** environ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059
Elliott Hughes642331b2013-03-06 15:03:53 -080060// Declared in <private/bionic_ssp.h>.
61uintptr_t __stack_chk_guard = 0;
62
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080063// Declared in <asm/page.h>.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064unsigned int __page_size = PAGE_SIZE;
65unsigned int __page_shift = PAGE_SHIFT;
66
Brian Carlstrom50af69e2013-09-13 16:34:43 -070067static size_t get_main_thread_stack_size() {
68 rlimit stack_limit;
Brian Carlstrom322e7bc2013-09-12 21:47:20 -070069 int rlimit_result = getrlimit(RLIMIT_STACK, &stack_limit);
Brian Carlstrom50af69e2013-09-13 16:34:43 -070070 if ((rlimit_result == 0) &&
71 (stack_limit.rlim_cur != RLIM_INFINITY) &&
72 (stack_limit.rlim_cur > PTHREAD_STACK_MIN)) {
73 return (stack_limit.rlim_cur & ~(PAGE_SIZE - 1));
Brian Carlstrom322e7bc2013-09-12 21:47:20 -070074 }
Brian Carlstrom50af69e2013-09-13 16:34:43 -070075 return PTHREAD_STACK_SIZE_DEFAULT;
Brian Carlstrom322e7bc2013-09-12 21:47:20 -070076}
77
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040078/* Init TLS for the initial thread. Called by the linker _before_ libc is mapped
79 * in memory. Beware: all writes to libc globals from this function will
80 * apply to linker-private copies and will not be visible from libc later on.
81 *
82 * Note: this function creates a pthread_internal_t for the initial thread and
83 * stores the pointer in TLS, but does not add it to pthread's gThreadList. This
84 * has to be done later from libc itself (see __libc_init_common).
85 *
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080086 * This function also stores a pointer to the kernel argument block in a TLS slot to be
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040087 * picked up by the libc constructor.
88 */
Elliott Hughesd3920b32013-02-07 18:39:34 -080089void __libc_init_tls(KernelArgumentBlock& args) {
90 __libc_auxv = args.auxv;
91
Brian Carlstrom322e7bc2013-09-12 21:47:20 -070092 uintptr_t stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE;
Brian Carlstrom50af69e2013-09-13 16:34:43 -070093 size_t stack_size = get_main_thread_stack_size();
Brian Carlstrom322e7bc2013-09-12 21:47:20 -070094 uintptr_t stack_bottom = stack_top - stack_size;
David 'Digit' Turnerb56b5652009-07-18 01:11:10 +020095
Elliott Hughes40eabe22013-02-14 18:59:37 -080096 static void* tls[BIONIC_TLS_SLOTS];
Elliott Hughes4f251be2012-11-01 16:33:29 -070097 static pthread_internal_t thread;
Elliott Hughes40eabe22013-02-14 18:59:37 -080098 thread.tid = gettid();
99 thread.tls = tls;
Elliott Hughes6d339182013-02-12 16:36:04 -0800100 pthread_attr_init(&thread.attr);
101 pthread_attr_setstack(&thread.attr, (void*) stack_bottom, stack_size);
Elliott Hughes40eabe22013-02-14 18:59:37 -0800102 _init_thread(&thread, false);
103 __init_tls(&thread);
104 tls[TLS_SLOT_BIONIC_PREINIT] = &args;
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +0400105}
106
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800107void __libc_init_common(KernelArgumentBlock& args) {
108 // Initialize various globals.
109 environ = args.envp;
110 errno = 0;
111 __libc_auxv = args.auxv;
Elliott Hughese4ccf5a2013-02-07 12:06:44 -0800112 __progname = args.argv[0] ? args.argv[0] : "<unknown>";
Elliott Hughes0d787c12013-04-04 13:46:46 -0700113 __abort_message_ptr = args.abort_message_ptr;
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +0400114
Elliott Hughes642331b2013-03-06 15:03:53 -0800115 // AT_RANDOM is a pointer to 16 bytes of randomness on the stack.
116 __stack_chk_guard = *reinterpret_cast<uintptr_t*>(getauxval(AT_RANDOM));
117
Elliott Hughes4f251be2012-11-01 16:33:29 -0700118 // Get the main thread from TLS and add it to the thread list.
119 pthread_internal_t* main_thread = __get_thread();
120 main_thread->allocated_on_heap = false;
121 _pthread_internal_add(main_thread);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +0400122
Elliott Hughes4f251be2012-11-01 16:33:29 -0700123 __system_properties_init(); // Requires 'environ'.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124}
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200125
126/* This function will be called during normal program termination
127 * to run the destructors that are listed in the .fini_array section
128 * of the executable, if any.
129 *
130 * 'fini_array' points to a list of function addresses. The first
131 * entry in the list has value -1, the last one has value 0.
132 */
Elliott Hughes4f251be2012-11-01 16:33:29 -0700133void __libc_fini(void* array) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800134 void** fini_array = reinterpret_cast<void**>(array);
Elliott Hughes4f251be2012-11-01 16:33:29 -0700135 const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200136
Elliott Hughes4f251be2012-11-01 16:33:29 -0700137 /* Sanity check - first entry must be -1 */
138 if (array == NULL || (size_t)fini_array[0] != minus1) {
139 return;
140 }
141
142 /* skip over it */
143 fini_array += 1;
144
145 /* Count the number of destructors. */
146 int count = 0;
147 while (fini_array[count] != NULL) {
148 ++count;
149 }
150
151 /* Now call each destructor in reverse order. */
152 while (count > 0) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800153 void (*func)() = (void (*)()) fini_array[--count];
Elliott Hughes4f251be2012-11-01 16:33:29 -0700154
155 /* Sanity check, any -1 in the list is ignored */
156 if ((size_t)func == minus1) {
157 continue;
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200158 }
159
Elliott Hughes4f251be2012-11-01 16:33:29 -0700160 func();
161 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700162
163#ifndef LIBC_STATIC
Elliott Hughes4f251be2012-11-01 16:33:29 -0700164 {
165 extern void __libc_postfini(void) __attribute__((weak));
166 if (__libc_postfini) {
167 __libc_postfini();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700168 }
Elliott Hughes4f251be2012-11-01 16:33:29 -0700169 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700170#endif
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200171}