Home | History | Annotate | Download | only in bionic
      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 <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>
     41 
     42 extern unsigned __get_sp(void);
     43 extern pid_t    gettid(void);
     44 
     45 char*  __progname;
     46 char **environ;
     47 
     48 /* from asm/page.h */
     49 unsigned int __page_size = PAGE_SIZE;
     50 unsigned int __page_shift = PAGE_SHIFT;
     51 
     52 
     53 int __system_properties_init(void);
     54 
     55 /* Init TLS for the initial thread. Called by the linker _before_ libc is mapped
     56  * in memory. Beware: all writes to libc globals from this function will
     57  * apply to linker-private copies and will not be visible from libc later on.
     58  *
     59  * Note: this function creates a pthread_internal_t for the initial thread and
     60  * stores the pointer in TLS, but does not add it to pthread's gThreadList. This
     61  * has to be done later from libc itself (see __libc_init_common).
     62  *
     63  * This function also stores elfdata argument in a specific TLS slot to be later
     64  * picked up by the libc constructor.
     65  */
     66 void __libc_init_tls(unsigned** elfdata)
     67 {
     68     pthread_attr_t             thread_attr;
     69     static pthread_internal_t  thread;
     70     static void*               tls_area[BIONIC_TLS_SLOTS];
     71 
     72     /* setup pthread runtime and main thread descriptor */
     73     unsigned stacktop = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE;
     74     unsigned stacksize = 128 * 1024;
     75     unsigned stackbottom = stacktop - stacksize;
     76 
     77     pthread_attr_init(&thread_attr);
     78     pthread_attr_setstack(&thread_attr, (void*)stackbottom, stacksize);
     79     _init_thread(&thread, gettid(), &thread_attr, (void*)stackbottom);
     80     __init_tls(tls_area, &thread);
     81 
     82     tls_area[TLS_SLOT_BIONIC_PREINIT] = elfdata;
     83 }
     84 
     85 void __libc_init_common(uintptr_t *elfdata)
     86 {
     87     int     argc = *elfdata;
     88     char**  argv = (char**)(elfdata + 1);
     89     char**  envp = argv + argc + 1;
     90 
     91     /* get the initial thread from TLS and add it to gThreadList */
     92     _pthread_internal_add(__get_thread());
     93 
     94     /* clear errno */
     95     errno = 0;
     96 
     97     /* set program name */
     98     __progname = argv[0] ? argv[0] : "<unknown>";
     99 
    100     /* setup environment pointer */
    101     environ = envp;
    102 
    103     /* setup system properties - requires environment */
    104     __system_properties_init();
    105 }
    106 
    107 /* This function will be called during normal program termination
    108  * to run the destructors that are listed in the .fini_array section
    109  * of the executable, if any.
    110  *
    111  * 'fini_array' points to a list of function addresses. The first
    112  * entry in the list has value -1, the last one has value 0.
    113  */
    114 void __libc_fini(void* array)
    115 {
    116     int count;
    117     void** fini_array = array;
    118     const size_t  minus1 = ~(size_t)0; /* ensure proper sign extension */
    119 
    120     /* Sanity check - first entry must be -1 */
    121     if (array == NULL || (size_t)fini_array[0] != minus1) {
    122         return;
    123     }
    124 
    125     /* skip over it */
    126     fini_array += 1;
    127 
    128     /* Count the number of destructors. */
    129     for (count = 0; fini_array[count] != NULL; count++);
    130 
    131     /* Now call each destructor in reverse order. */
    132     while (count > 0) {
    133         void (*func)() = (void (*)) fini_array[--count];
    134 
    135         /* Sanity check, any -1 in the list is ignored */
    136         if ((size_t)func == minus1)
    137             continue;
    138 
    139         func();
    140     }
    141 
    142 #ifndef LIBC_STATIC
    143     {
    144         extern void __libc_postfini(void) __attribute__((weak));
    145         if (__libc_postfini)
    146             __libc_postfini();
    147     }
    148 #endif
    149 }
    150