| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | /* | 
| David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 29 | * libc_init_dynamic.c | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 30 | * | 
| David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 31 | * This source files provides two important functions for dynamic | 
|  | 32 | * executables: | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 33 | * | 
| David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 34 | * - a C runtime initializer (__libc_preinit), which is called by | 
|  | 35 | *   the dynamic linker when libc.so is loaded. This happens before | 
|  | 36 | *   any other initializer (e.g. static C++ constructors in other | 
|  | 37 | *   shared libraries the program depends on). | 
|  | 38 | * | 
|  | 39 | * - a program launch function (__libc_init), which is called after | 
|  | 40 | *   all dynamic linking has been performed. Technically, it is called | 
|  | 41 | *   from arch-$ARCH/bionic/crtbegin_dynamic.S which is itself called | 
|  | 42 | *   by the dynamic linker after all libraries have been loaded and | 
|  | 43 | *   initialized. | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 44 | */ | 
|  | 45 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 46 | #include <stddef.h> | 
|  | 47 | #include <stdio.h> | 
|  | 48 | #include <stdlib.h> | 
|  | 49 | #include <stdint.h> | 
|  | 50 | #include <elf.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 51 | #include "atexit.h" | 
|  | 52 | #include "libc_init_common.h" | 
| David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 53 | #include <bionic_tls.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 54 |  | 
|  | 55 | extern void malloc_debug_init(); | 
|  | 56 |  | 
| David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 57 | /* We flag the __libc_preinit function as a constructor to ensure | 
|  | 58 | * that its address is listed in libc.so's .init_array section. | 
|  | 59 | * This ensures that the function is called by the dynamic linker | 
|  | 60 | * as soon as the shared library is loaded. | 
|  | 61 | */ | 
|  | 62 | void __attribute__((constructor)) __libc_prenit(void); | 
|  | 63 |  | 
|  | 64 | void __libc_prenit(void) | 
|  | 65 | { | 
|  | 66 | /* Read the ELF data pointer form a special slot of the | 
|  | 67 | * TLS area, then call __libc_init_common with it. | 
|  | 68 | * | 
|  | 69 | * Note that: | 
|  | 70 | * - we clear the slot so no other initializer sees its value. | 
|  | 71 | * - __libc_init_common() will change the TLS area so the old one | 
|  | 72 | *   won't be accessible anyway. | 
|  | 73 | */ | 
|  | 74 | void**      tls_area = (void**)__get_tls(); | 
|  | 75 | unsigned*   elfdata   = tls_area[TLS_SLOT_BIONIC_PREINIT]; | 
|  | 76 |  | 
|  | 77 | tls_area[TLS_SLOT_BIONIC_PREINIT] = NULL; | 
|  | 78 |  | 
|  | 79 | __libc_init_common(elfdata); | 
| Andy McFadden | 39f3745 | 2009-07-21 15:25:23 -0700 | [diff] [blame] | 80 |  | 
|  | 81 | #ifdef MALLOC_LEAK_CHECK | 
|  | 82 | /* setup malloc leak checker, requires system properties */ | 
|  | 83 | extern void malloc_debug_init(void); | 
|  | 84 | malloc_debug_init(); | 
|  | 85 | #endif | 
|  | 86 |  | 
| David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 89 | __noreturn void __libc_init(uintptr_t *elfdata, | 
|  | 90 | void (*onexit)(void), | 
|  | 91 | int (*slingshot)(int, char**, char**), | 
|  | 92 | structors_array_t const * const structors) | 
|  | 93 | { | 
| David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 94 | /* When we reach this point, all initializers have been already | 
|  | 95 | * run by the dynamic linker, so ignore 'structors'. | 
|  | 96 | */ | 
|  | 97 | int     argc = (int)*elfdata; | 
|  | 98 | char**  argv = (char**)(elfdata + 1); | 
|  | 99 | char**  envp = argv + argc + 1; | 
|  | 100 |  | 
|  | 101 | /* Several Linux ABIs don't pass the onexit pointer, and the ones that | 
|  | 102 | * do never use it.  Therefore, we ignore it. | 
|  | 103 | */ | 
|  | 104 |  | 
|  | 105 | exit(slingshot(argc, argv, envp)); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 106 | } |