Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -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 | /* |
| 29 | * libc_init_dynamic.c |
| 30 | * |
| 31 | * This source files provides two important functions for dynamic |
| 32 | * executables: |
| 33 | * |
| 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. |
| 44 | */ |
| 45 | |
| 46 | #include <stddef.h> |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <stdint.h> |
| 50 | #include <elf.h> |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 51 | #include "libc_init_common.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 52 | |
| 53 | #include "private/bionic_tls.h" |
| 54 | #include "private/KernelArgumentBlock.h" |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 55 | |
| 56 | extern "C" { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 57 | extern void malloc_debug_init(void); |
| 58 | extern void malloc_debug_fini(void); |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 59 | extern void netdClientInit(void); |
Dmitriy Ivanov | 53c3c27 | 2014-07-11 12:59:16 -0700 | [diff] [blame] | 60 | extern int __cxa_atexit(void (*)(void *), void *, void *); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | // We flag the __libc_preinit function as a constructor to ensure |
| 64 | // that its address is listed in libc.so's .init_array section. |
| 65 | // This ensures that the function is called by the dynamic linker |
| 66 | // as soon as the shared library is loaded. |
Elliott Hughes | ce53272 | 2013-03-15 16:31:09 -0700 | [diff] [blame] | 67 | __attribute__((constructor)) static void __libc_preinit() { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 68 | // Read the kernel argument block pointer from TLS. |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 69 | void** tls = __get_tls(); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 70 | KernelArgumentBlock** args_slot = &reinterpret_cast<KernelArgumentBlock**>(tls)[TLS_SLOT_BIONIC_PREINIT]; |
| 71 | KernelArgumentBlock* args = *args_slot; |
| 72 | |
| 73 | // Clear the slot so no other initializer sees its value. |
| 74 | // __libc_init_common() will change the TLS area so the old one won't be accessible anyway. |
| 75 | *args_slot = NULL; |
| 76 | |
| 77 | __libc_init_common(*args); |
| 78 | |
Elliott Hughes | 07f1ded | 2014-05-14 11:35:49 -0700 | [diff] [blame] | 79 | // Hooks for various libraries to let them know that we're starting up. |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 80 | malloc_debug_init(); |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 81 | netdClientInit(); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Elliott Hughes | ce53272 | 2013-03-15 16:31:09 -0700 | [diff] [blame] | 84 | __LIBC_HIDDEN__ void __libc_postfini() { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 85 | // A hook for the debug malloc library to let it know that we're shutting down. |
| 86 | malloc_debug_fini(); |
| 87 | } |
| 88 | |
| 89 | // This function is called from the executable's _start entry point |
| 90 | // (see arch-$ARCH/bionic/crtbegin_dynamic.S), which is itself |
| 91 | // called by the dynamic linker after it has loaded all shared |
| 92 | // libraries the executable depends on. |
| 93 | // |
| 94 | // Note that the dynamic linker has also run all constructors in the |
| 95 | // executable at this point. |
| 96 | __noreturn void __libc_init(void* raw_args, |
Elliott Hughes | d286796 | 2014-06-03 15:22:34 -0700 | [diff] [blame] | 97 | void (*onexit)(void) __unused, |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 98 | int (*slingshot)(int, char**, char**), |
Dmitriy Ivanov | 4b41555 | 2014-09-04 21:54:34 +0000 | [diff] [blame^] | 99 | structors_array_t const * const structors) { |
| 100 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 101 | KernelArgumentBlock args(raw_args); |
| 102 | |
| 103 | // Several Linux ABIs don't pass the onexit pointer, and the ones that |
| 104 | // do never use it. Therefore, we ignore it. |
| 105 | |
Dmitriy Ivanov | 4b41555 | 2014-09-04 21:54:34 +0000 | [diff] [blame^] | 106 | // The executable may have its own destructors listed in its .fini_array |
| 107 | // so we need to ensure that these are called when the program exits |
| 108 | // normally. |
| 109 | if (structors->fini_array) { |
| 110 | __cxa_atexit(__libc_fini,structors->fini_array,NULL); |
| 111 | } |
| 112 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 113 | exit(slingshot(args.argc, args.argv, args.envp)); |
| 114 | } |