blob: 266d6fa1125e86ec15875ab774fc5e6b05e10e2d [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>
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080033#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <elf.h>
35#include <asm/page.h>
36#include "pthread_internal.h"
37#include "atexit.h"
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080038#include "KernelArgumentBlock.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039#include "libc_init_common.h"
40
41#include <bionic_tls.h>
42#include <errno.h>
Nick Kralevich2c5153b2013-01-11 14:43:05 -080043#include <private/bionic_auxv.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080045extern "C" unsigned __get_sp(void);
46extern "C" int __system_properties_init(void);
David 'Digit' Turner3a654b12009-06-03 19:32:37 +020047
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080048// Not public, but well-known in the BSDs.
Elliott Hughese4ccf5a2013-02-07 12:06:44 -080049const char* __progname;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080050
Elliott Hughesd3920b32013-02-07 18:39:34 -080051// Declared in <unistd.h>.
Elliott Hughes4f251be2012-11-01 16:33:29 -070052char** environ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080054// Declared in <asm/page.h>.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055unsigned int __page_size = PAGE_SIZE;
56unsigned int __page_shift = PAGE_SHIFT;
57
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040058/* Init TLS for the initial thread. Called by the linker _before_ libc is mapped
59 * in memory. Beware: all writes to libc globals from this function will
60 * apply to linker-private copies and will not be visible from libc later on.
61 *
62 * Note: this function creates a pthread_internal_t for the initial thread and
63 * stores the pointer in TLS, but does not add it to pthread's gThreadList. This
64 * has to be done later from libc itself (see __libc_init_common).
65 *
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080066 * This function also stores a pointer to the kernel argument block in a TLS slot to be
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040067 * picked up by the libc constructor.
68 */
Elliott Hughesd3920b32013-02-07 18:39:34 -080069void __libc_init_tls(KernelArgumentBlock& args) {
70 __libc_auxv = args.auxv;
71
Elliott Hughes4f251be2012-11-01 16:33:29 -070072 unsigned stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE;
73 unsigned stack_size = 128 * 1024;
74 unsigned stack_bottom = stack_top - stack_size;
David 'Digit' Turnerb56b5652009-07-18 01:11:10 +020075
Elliott Hughes4f251be2012-11-01 16:33:29 -070076 static pthread_internal_t thread;
Elliott Hughes6d339182013-02-12 16:36:04 -080077 pthread_attr_init(&thread.attr);
78 pthread_attr_setstack(&thread.attr, (void*) stack_bottom, stack_size);
79 _init_thread(&thread, gettid(), false);
David 'Digit' Turner3a654b12009-06-03 19:32:37 +020080
Elliott Hughes4f251be2012-11-01 16:33:29 -070081 static void* tls_area[BIONIC_TLS_SLOTS];
82 __init_tls(tls_area, &thread);
Elliott Hughesd3920b32013-02-07 18:39:34 -080083 tls_area[TLS_SLOT_BIONIC_PREINIT] = &args;
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040084}
85
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080086void __libc_init_common(KernelArgumentBlock& args) {
87 // Initialize various globals.
88 environ = args.envp;
89 errno = 0;
90 __libc_auxv = args.auxv;
Elliott Hughese4ccf5a2013-02-07 12:06:44 -080091 __progname = args.argv[0] ? args.argv[0] : "<unknown>";
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040092
Elliott Hughes4f251be2012-11-01 16:33:29 -070093 // Get the main thread from TLS and add it to the thread list.
94 pthread_internal_t* main_thread = __get_thread();
95 main_thread->allocated_on_heap = false;
96 _pthread_internal_add(main_thread);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +040097
Elliott Hughes4f251be2012-11-01 16:33:29 -070098 __system_properties_init(); // Requires 'environ'.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099}
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200100
101/* This function will be called during normal program termination
102 * to run the destructors that are listed in the .fini_array section
103 * of the executable, if any.
104 *
105 * 'fini_array' points to a list of function addresses. The first
106 * entry in the list has value -1, the last one has value 0.
107 */
Elliott Hughes4f251be2012-11-01 16:33:29 -0700108void __libc_fini(void* array) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800109 void** fini_array = reinterpret_cast<void**>(array);
Elliott Hughes4f251be2012-11-01 16:33:29 -0700110 const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200111
Elliott Hughes4f251be2012-11-01 16:33:29 -0700112 /* Sanity check - first entry must be -1 */
113 if (array == NULL || (size_t)fini_array[0] != minus1) {
114 return;
115 }
116
117 /* skip over it */
118 fini_array += 1;
119
120 /* Count the number of destructors. */
121 int count = 0;
122 while (fini_array[count] != NULL) {
123 ++count;
124 }
125
126 /* Now call each destructor in reverse order. */
127 while (count > 0) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800128 void (*func)() = (void (*)()) fini_array[--count];
Elliott Hughes4f251be2012-11-01 16:33:29 -0700129
130 /* Sanity check, any -1 in the list is ignored */
131 if ((size_t)func == minus1) {
132 continue;
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200133 }
134
Elliott Hughes4f251be2012-11-01 16:33:29 -0700135 func();
136 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700137
138#ifndef LIBC_STATIC
Elliott Hughes4f251be2012-11-01 16:33:29 -0700139 {
140 extern void __libc_postfini(void) __attribute__((weak));
141 if (__libc_postfini) {
142 __libc_postfini();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700143 }
Elliott Hughes4f251be2012-11-01 16:33:29 -0700144 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700145#endif
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200146}