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 | |
| 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 | |
David 'Digit' Turner | 03eabfe | 2009-05-28 15:54:03 +0200 | [diff] [blame^] | 42 | /* This contains the common C library initialization code. |
| 43 | * To understand what happens here, you should read the |
| 44 | * "Initialization and Finalization" section of the file |
| 45 | * named bionic/linker/README.TXT |
| 46 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | |
| 48 | static void call_array(void(**list)()) |
| 49 | { |
| 50 | // First element is -1, list is null-terminated |
| 51 | while (*++list) { |
| 52 | (*list)(); |
| 53 | } |
| 54 | } |
| 55 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 56 | char* __progname; |
| 57 | char **environ; |
| 58 | |
| 59 | /* from asm/page.h */ |
| 60 | unsigned int __page_size = PAGE_SIZE; |
| 61 | unsigned int __page_shift = PAGE_SHIFT; |
| 62 | |
| 63 | |
| 64 | int __system_properties_init(void); |
| 65 | |
David 'Digit' Turner | 03eabfe | 2009-05-28 15:54:03 +0200 | [diff] [blame^] | 66 | /* This function can be run under two different contexts: |
| 67 | * |
| 68 | * - for statically linked executables (i.e. those who do |
| 69 | * not depend on shared libraries at all), it will be |
| 70 | * called from the __libc_init() function defined in |
| 71 | * bionic/libc_init_static.c |
| 72 | * |
| 73 | * - for dynamic executables, it will be called from the |
| 74 | * __libc_init() function defined in bionic/libc_init_dynamic.c |
| 75 | * |
| 76 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 77 | void __libc_init_common(uintptr_t *elfdata, |
| 78 | void (*onexit)(void), |
| 79 | int (*slingshot)(int, char**, char**), |
| 80 | structors_array_t const * const structors, |
| 81 | void (*pre_ctor_hook)()) |
| 82 | { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 83 | int argc; |
| 84 | char **argv, **envp, **envend; |
| 85 | struct auxentry *auxentry; |
| 86 | unsigned int page_size = 0, page_shift = 0; |
| 87 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 88 | argc = (int) *elfdata++; |
| 89 | argv = (char**) elfdata; |
| 90 | envp = argv+(argc+1); |
| 91 | environ = envp; |
| 92 | |
| 93 | __progname = argv[0] ? argv[0] : "<unknown>"; |
| 94 | |
| 95 | errno = 0; |
| 96 | |
| 97 | __system_properties_init(); |
| 98 | |
| 99 | if (pre_ctor_hook) pre_ctor_hook(); |
| 100 | |
David 'Digit' Turner | 03eabfe | 2009-05-28 15:54:03 +0200 | [diff] [blame^] | 101 | if (structors != NULL) { |
| 102 | // pre-init array. |
| 103 | call_array(structors->preinit_array); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 104 | |
David 'Digit' Turner | 03eabfe | 2009-05-28 15:54:03 +0200 | [diff] [blame^] | 105 | // for compatibility with non-eabi binary, call the .ctors section |
| 106 | // this is only useful for static non-ARM (e.g. x86) executables. |
| 107 | call_array(structors->ctors_array); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 108 | |
David 'Digit' Turner | 03eabfe | 2009-05-28 15:54:03 +0200 | [diff] [blame^] | 109 | // call static constructors |
| 110 | call_array(structors->init_array); |
| 111 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 112 | |
| 113 | exit(slingshot(argc, argv, envp)); |
| 114 | } |