• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "libc_init_common.h"
30 
31 #include <asm/page.h>
32 #include <bionic_tls.h>
33 #include <elf.h>
34 #include <errno.h>
35 #include <stddef.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/auxv.h>
40 #include <unistd.h>
41 
42 #include "atexit.h"
43 #include "private/bionic_auxv.h"
44 #include "private/bionic_ssp.h"
45 #include "private/KernelArgumentBlock.h"
46 #include "pthread_internal.h"
47 
48 extern "C" abort_msg_t** __abort_message_ptr;
49 extern "C" unsigned __get_sp(void);
50 extern "C" int __system_properties_init(void);
51 
52 // Not public, but well-known in the BSDs.
53 const char* __progname;
54 
55 // Declared in <unistd.h>.
56 char** environ;
57 
58 // Declared in <private/bionic_ssp.h>.
59 uintptr_t __stack_chk_guard = 0;
60 
61 // Declared in <asm/page.h>.
62 unsigned int __page_size = PAGE_SIZE;
63 unsigned int __page_shift = PAGE_SHIFT;
64 
65 /* Init TLS for the initial thread. Called by the linker _before_ libc is mapped
66  * in memory. Beware: all writes to libc globals from this function will
67  * apply to linker-private copies and will not be visible from libc later on.
68  *
69  * Note: this function creates a pthread_internal_t for the initial thread and
70  * stores the pointer in TLS, but does not add it to pthread's gThreadList. This
71  * has to be done later from libc itself (see __libc_init_common).
72  *
73  * This function also stores a pointer to the kernel argument block in a TLS slot to be
74  * picked up by the libc constructor.
75  */
__libc_init_tls(KernelArgumentBlock & args)76 void __libc_init_tls(KernelArgumentBlock& args) {
77   __libc_auxv = args.auxv;
78 
79   unsigned stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE;
80   unsigned stack_size = 128 * 1024;
81   unsigned stack_bottom = stack_top - stack_size;
82 
83   static void* tls[BIONIC_TLS_SLOTS];
84   static pthread_internal_t thread;
85   thread.tid = gettid();
86   thread.tls = tls;
87   pthread_attr_init(&thread.attr);
88   pthread_attr_setstack(&thread.attr, (void*) stack_bottom, stack_size);
89   _init_thread(&thread, false);
90   __init_tls(&thread);
91   tls[TLS_SLOT_BIONIC_PREINIT] = &args;
92 }
93 
__libc_init_common(KernelArgumentBlock & args)94 void __libc_init_common(KernelArgumentBlock& args) {
95   // Initialize various globals.
96   environ = args.envp;
97   errno = 0;
98   __libc_auxv = args.auxv;
99   __progname = args.argv[0] ? args.argv[0] : "<unknown>";
100   __abort_message_ptr = args.abort_message_ptr;
101 
102   // AT_RANDOM is a pointer to 16 bytes of randomness on the stack.
103   __stack_chk_guard = *reinterpret_cast<uintptr_t*>(getauxval(AT_RANDOM));
104 
105   // Get the main thread from TLS and add it to the thread list.
106   pthread_internal_t* main_thread = __get_thread();
107   main_thread->allocated_on_heap = false;
108   _pthread_internal_add(main_thread);
109 
110   __system_properties_init(); // Requires 'environ'.
111 }
112 
113 /* This function will be called during normal program termination
114  * to run the destructors that are listed in the .fini_array section
115  * of the executable, if any.
116  *
117  * 'fini_array' points to a list of function addresses. The first
118  * entry in the list has value -1, the last one has value 0.
119  */
__libc_fini(void * array)120 void __libc_fini(void* array) {
121   void** fini_array = reinterpret_cast<void**>(array);
122   const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */
123 
124   /* Sanity check - first entry must be -1 */
125   if (array == NULL || (size_t)fini_array[0] != minus1) {
126     return;
127   }
128 
129   /* skip over it */
130   fini_array += 1;
131 
132   /* Count the number of destructors. */
133   int count = 0;
134   while (fini_array[count] != NULL) {
135     ++count;
136   }
137 
138   /* Now call each destructor in reverse order. */
139   while (count > 0) {
140     void (*func)() = (void (*)()) fini_array[--count];
141 
142     /* Sanity check, any -1 in the list is ignored */
143     if ((size_t)func == minus1) {
144       continue;
145     }
146 
147     func();
148   }
149 
150 #ifndef LIBC_STATIC
151   {
152     extern void __libc_postfini(void) __attribute__((weak));
153     if (__libc_postfini) {
154       __libc_postfini();
155     }
156   }
157 #endif
158 }
159