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 | * libc_init_static.c |
| 30 | * |
David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 31 | * The program startup function __libc_init() defined here is |
| 32 | * used for static executables only (i.e. those that don't depend |
| 33 | * on shared libraries). It is called from arch-$ARCH/bionic/crtbegin_static.S |
| 34 | * which is directly invoked by the kernel when the program is launched. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | * |
David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 36 | * The 'structors' parameter contains pointers to various initializer |
| 37 | * arrays that must be run before the program's 'main' routine is launched. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 38 | */ |
| 39 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 | #include <stddef.h> |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <stdint.h> |
| 44 | #include <elf.h> |
David 'Digit' Turner | 3a654b1 | 2009-06-03 19:32:37 +0200 | [diff] [blame] | 45 | #include "pthread_internal.h" |
| 46 | #include "atexit.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | #include "libc_init_common.h" |
| 48 | |
David 'Digit' Turner | 3a654b1 | 2009-06-03 19:32:37 +0200 | [diff] [blame] | 49 | #include <bionic_tls.h> |
| 50 | #include <errno.h> |
Nick Kralevich | ac3de8d | 2012-11-09 16:51:36 -0800 | [diff] [blame^] | 51 | #include <sys/mman.h> |
| 52 | |
| 53 | // Returns the address of the page containing address 'x'. |
| 54 | #define PAGE_START(x) ((x) & PAGE_MASK) |
| 55 | |
| 56 | // Returns the address of the next page after address 'x', unless 'x' is |
| 57 | // itself at the start of a page. |
| 58 | #define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1)) |
David 'Digit' Turner | 3a654b1 | 2009-06-03 19:32:37 +0200 | [diff] [blame] | 59 | |
David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 60 | static void call_array(void(**list)()) |
| 61 | { |
| 62 | // First element is -1, list is null-terminated |
| 63 | while (*++list) { |
| 64 | (*list)(); |
| 65 | } |
| 66 | } |
| 67 | |
Nick Kralevich | ac3de8d | 2012-11-09 16:51:36 -0800 | [diff] [blame^] | 68 | /* |
| 69 | * Find the value of the AT_* variable passed to us by the kernel. |
| 70 | */ |
| 71 | static unsigned find_aux(unsigned *vecs, unsigned type) { |
| 72 | while (vecs[0]) { |
| 73 | if (vecs[0] == type) { |
| 74 | return vecs[1]; |
| 75 | } |
| 76 | vecs += 2; |
| 77 | } |
| 78 | |
| 79 | return 0; // should never happen |
| 80 | } |
| 81 | |
| 82 | static void apply_gnu_relro(unsigned *vecs) { |
| 83 | Elf32_Phdr *phdr_start; |
| 84 | unsigned phdr_ct; |
| 85 | Elf32_Phdr *phdr; |
| 86 | |
| 87 | phdr_start = (Elf32_Phdr *) find_aux(vecs, AT_PHDR); |
| 88 | phdr_ct = find_aux(vecs, AT_PHNUM); |
| 89 | |
| 90 | for (phdr = phdr_start; phdr < (phdr_start + phdr_ct); phdr++) { |
| 91 | if (phdr->p_type != PT_GNU_RELRO) |
| 92 | continue; |
| 93 | |
| 94 | Elf32_Addr seg_page_start = PAGE_START(phdr->p_vaddr); |
| 95 | Elf32_Addr seg_page_end = PAGE_END(phdr->p_vaddr + phdr->p_memsz); |
| 96 | |
| 97 | // Check return value here? What do we do if we fail? |
| 98 | mprotect((void *) seg_page_start, |
| 99 | seg_page_end - seg_page_start, |
| 100 | PROT_READ); |
| 101 | } |
| 102 | } |
| 103 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 104 | __noreturn void __libc_init(uintptr_t *elfdata, |
| 105 | void (*onexit)(void), |
| 106 | int (*slingshot)(int, char**, char**), |
| 107 | structors_array_t const * const structors) |
| 108 | { |
David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 109 | int argc; |
| 110 | char **argv, **envp; |
Nick Kralevich | ac3de8d | 2012-11-09 16:51:36 -0800 | [diff] [blame^] | 111 | unsigned *vecs; |
David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 112 | |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 113 | __libc_init_tls(NULL); |
| 114 | |
David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 115 | /* Initialize the C runtime environment */ |
| 116 | __libc_init_common(elfdata); |
| 117 | |
| 118 | /* Several Linux ABIs don't pass the onexit pointer, and the ones that |
| 119 | * do never use it. Therefore, we ignore it. |
| 120 | */ |
| 121 | |
| 122 | /* pre-init array. */ |
| 123 | call_array(structors->preinit_array); |
| 124 | |
David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 125 | // call static constructors |
| 126 | call_array(structors->init_array); |
| 127 | |
| 128 | argc = (int) *elfdata; |
| 129 | argv = (char**)(elfdata + 1); |
| 130 | envp = argv + argc + 1; |
| 131 | |
Nick Kralevich | ac3de8d | 2012-11-09 16:51:36 -0800 | [diff] [blame^] | 132 | // The auxiliary vector is at the end of the environment block |
| 133 | vecs = (unsigned *) envp; |
| 134 | while (vecs[0] != 0) { |
| 135 | vecs++; |
| 136 | } |
| 137 | /* The end of the environment block is marked by two NULL pointers */ |
| 138 | vecs++; |
| 139 | |
David 'Digit' Turner | 1df986c | 2010-10-21 04:16:50 +0200 | [diff] [blame] | 140 | /* The executable may have its own destructors listed in its .fini_array |
| 141 | * so we need to ensure that these are called when the program exits |
| 142 | * normally. |
| 143 | */ |
| 144 | if (structors->fini_array) |
| 145 | __cxa_atexit(__libc_fini,structors->fini_array,NULL); |
| 146 | |
Nick Kralevich | ac3de8d | 2012-11-09 16:51:36 -0800 | [diff] [blame^] | 147 | apply_gnu_relro(vecs); |
David 'Digit' Turner | b56b565 | 2009-07-18 01:11:10 +0200 | [diff] [blame] | 148 | exit(slingshot(argc, argv, envp)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 149 | } |