blob: 86e1eb574a5bc1eb967591c8329b189c9b32e414 [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
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 Kralevich2c5153b2013-01-11 14:43:05 -080041#include <private/bionic_auxv.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042
David 'Digit' Turner3a654b12009-06-03 19:32:37 +020043extern unsigned __get_sp(void);
Elliott Hughes4f251be2012-11-01 16:33:29 -070044extern pid_t gettid(void);
David 'Digit' Turner3a654b12009-06-03 19:32:37 +020045
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046char* __progname;
Elliott Hughes4f251be2012-11-01 16:33:29 -070047char** environ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048
49/* from asm/page.h */
50unsigned int __page_size = PAGE_SIZE;
51unsigned int __page_shift = PAGE_SHIFT;
52
53
54int __system_properties_init(void);
55
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040056/* Init TLS for the initial thread. Called by the linker _before_ libc is mapped
57 * in memory. Beware: all writes to libc globals from this function will
58 * apply to linker-private copies and will not be visible from libc later on.
59 *
60 * Note: this function creates a pthread_internal_t for the initial thread and
61 * stores the pointer in TLS, but does not add it to pthread's gThreadList. This
62 * has to be done later from libc itself (see __libc_init_common).
63 *
Elliott Hughes4f251be2012-11-01 16:33:29 -070064 * This function also stores the elf_data argument in a specific TLS slot to be later
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040065 * picked up by the libc constructor.
66 */
Elliott Hughes4f251be2012-11-01 16:33:29 -070067void __libc_init_tls(unsigned** elf_data) {
68 unsigned stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE;
69 unsigned stack_size = 128 * 1024;
70 unsigned stack_bottom = stack_top - stack_size;
David 'Digit' Turnerb56b5652009-07-18 01:11:10 +020071
Elliott Hughes4f251be2012-11-01 16:33:29 -070072 pthread_attr_t thread_attr;
73 pthread_attr_init(&thread_attr);
74 pthread_attr_setstack(&thread_attr, (void*) stack_bottom, stack_size);
David 'Digit' Turner3a654b12009-06-03 19:32:37 +020075
Elliott Hughes4f251be2012-11-01 16:33:29 -070076 static pthread_internal_t thread;
77 _init_thread(&thread, gettid(), &thread_attr, (void*) stack_bottom, false);
David 'Digit' Turner3a654b12009-06-03 19:32:37 +020078
Elliott Hughes4f251be2012-11-01 16:33:29 -070079 static void* tls_area[BIONIC_TLS_SLOTS];
80 __init_tls(tls_area, &thread);
81 tls_area[TLS_SLOT_BIONIC_PREINIT] = elf_data;
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040082}
83
Elliott Hughes4f251be2012-11-01 16:33:29 -070084void __libc_init_common(uintptr_t* elf_data) {
85 int argc = *elf_data;
86 char** argv = (char**) (elf_data + 1);
87 char** envp = argv + argc + 1;
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040088
Elliott Hughes4f251be2012-11-01 16:33:29 -070089 // Get the main thread from TLS and add it to the thread list.
90 pthread_internal_t* main_thread = __get_thread();
91 main_thread->allocated_on_heap = false;
92 _pthread_internal_add(main_thread);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040093
Elliott Hughes4f251be2012-11-01 16:33:29 -070094 // Set various globals.
95 errno = 0;
96 __progname = argv[0] ? argv[0] : "<unknown>";
97 environ = envp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098
Nick Kralevich2c5153b2013-01-11 14:43:05 -080099 // The auxiliary vector is at the end of the environment block
100 while(*envp != NULL) {
101 envp++;
102 }
103 /* The end of the environment block is marked by two NULL pointers */
104 envp++;
105
106 __libc_auxv = (Elf32_auxv_t*) envp;
107
Elliott Hughes4f251be2012-11-01 16:33:29 -0700108 __system_properties_init(); // Requires 'environ'.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109}
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200110
111/* This function will be called during normal program termination
112 * to run the destructors that are listed in the .fini_array section
113 * of the executable, if any.
114 *
115 * 'fini_array' points to a list of function addresses. The first
116 * entry in the list has value -1, the last one has value 0.
117 */
Elliott Hughes4f251be2012-11-01 16:33:29 -0700118void __libc_fini(void* array) {
119 void** fini_array = array;
120 const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200121
Elliott Hughes4f251be2012-11-01 16:33:29 -0700122 /* Sanity check - first entry must be -1 */
123 if (array == NULL || (size_t)fini_array[0] != minus1) {
124 return;
125 }
126
127 /* skip over it */
128 fini_array += 1;
129
130 /* Count the number of destructors. */
131 int count = 0;
132 while (fini_array[count] != NULL) {
133 ++count;
134 }
135
136 /* Now call each destructor in reverse order. */
137 while (count > 0) {
138 void (*func)() = (void (*)) fini_array[--count];
139
140 /* Sanity check, any -1 in the list is ignored */
141 if ((size_t)func == minus1) {
142 continue;
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200143 }
144
Elliott Hughes4f251be2012-11-01 16:33:29 -0700145 func();
146 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700147
148#ifndef LIBC_STATIC
Elliott Hughes4f251be2012-11-01 16:33:29 -0700149 {
150 extern void __libc_postfini(void) __attribute__((weak));
151 if (__libc_postfini) {
152 __libc_postfini();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700153 }
Elliott Hughes4f251be2012-11-01 16:33:29 -0700154 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700155#endif
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200156}