blob: 1cef6320324f516ee79785eca6ce6f2adbb88631 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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' Turnerb56b5652009-07-18 01:11:10 +020031 * 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 Project1dc9e472009-03-03 19:28:35 -080035 *
David 'Digit' Turnerb56b5652009-07-18 01:11:10 +020036 * 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 Project1dc9e472009-03-03 19:28:35 -080038 */
39
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040#include <stddef.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <stdint.h>
44#include <elf.h>
David 'Digit' Turner3a654b12009-06-03 19:32:37 +020045#include "pthread_internal.h"
46#include "atexit.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047#include "libc_init_common.h"
48
David 'Digit' Turner3a654b12009-06-03 19:32:37 +020049#include <bionic_tls.h>
50#include <errno.h>
Nick Kralevichac3de8d2012-11-09 16:51:36 -080051#include <sys/mman.h>
Nick Kralevich2c5153b2013-01-11 14:43:05 -080052#include <sys/auxv.h>
Nick Kralevichac3de8d2012-11-09 16:51:36 -080053
54// Returns the address of the page containing address 'x'.
55#define PAGE_START(x) ((x) & PAGE_MASK)
56
57// Returns the address of the next page after address 'x', unless 'x' is
58// itself at the start of a page.
59#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
David 'Digit' Turner3a654b12009-06-03 19:32:37 +020060
David 'Digit' Turnerb56b5652009-07-18 01:11:10 +020061static void call_array(void(**list)())
62{
63 // First element is -1, list is null-terminated
64 while (*++list) {
65 (*list)();
66 }
67}
68
Nick Kralevich2c5153b2013-01-11 14:43:05 -080069static void apply_gnu_relro() {
Nick Kralevichac3de8d2012-11-09 16:51:36 -080070 Elf32_Phdr *phdr_start;
Nick Kralevich2c5153b2013-01-11 14:43:05 -080071 unsigned long int phdr_ct;
Nick Kralevichac3de8d2012-11-09 16:51:36 -080072 Elf32_Phdr *phdr;
73
Nick Kralevich2c5153b2013-01-11 14:43:05 -080074 phdr_start = (Elf32_Phdr *) getauxval(AT_PHDR);
75 phdr_ct = getauxval(AT_PHNUM);
Nick Kralevichac3de8d2012-11-09 16:51:36 -080076
77 for (phdr = phdr_start; phdr < (phdr_start + phdr_ct); phdr++) {
78 if (phdr->p_type != PT_GNU_RELRO)
79 continue;
80
81 Elf32_Addr seg_page_start = PAGE_START(phdr->p_vaddr);
82 Elf32_Addr seg_page_end = PAGE_END(phdr->p_vaddr + phdr->p_memsz);
83
84 // Check return value here? What do we do if we fail?
85 mprotect((void *) seg_page_start,
86 seg_page_end - seg_page_start,
87 PROT_READ);
88 }
89}
90
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091__noreturn void __libc_init(uintptr_t *elfdata,
92 void (*onexit)(void),
93 int (*slingshot)(int, char**, char**),
94 structors_array_t const * const structors)
95{
David 'Digit' Turnerb56b5652009-07-18 01:11:10 +020096 int argc;
97 char **argv, **envp;
98
Nick Kraleviche3a49a82013-01-14 14:46:26 -080099 __libc_init_tls(elfdata);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +0400100
David 'Digit' Turnerb56b5652009-07-18 01:11:10 +0200101 /* Initialize the C runtime environment */
102 __libc_init_common(elfdata);
103
Nick Kralevich9fb48ac2013-01-11 18:38:26 -0800104 apply_gnu_relro();
105
David 'Digit' Turnerb56b5652009-07-18 01:11:10 +0200106 /* Several Linux ABIs don't pass the onexit pointer, and the ones that
107 * do never use it. Therefore, we ignore it.
108 */
109
110 /* pre-init array. */
111 call_array(structors->preinit_array);
112
David 'Digit' Turnerb56b5652009-07-18 01:11:10 +0200113 // call static constructors
114 call_array(structors->init_array);
115
116 argc = (int) *elfdata;
117 argv = (char**)(elfdata + 1);
118 envp = argv + argc + 1;
119
David 'Digit' Turner1df986c2010-10-21 04:16:50 +0200120 /* The executable may have its own destructors listed in its .fini_array
121 * so we need to ensure that these are called when the program exits
122 * normally.
123 */
124 if (structors->fini_array)
125 __cxa_atexit(__libc_fini,structors->fini_array,NULL);
126
David 'Digit' Turnerb56b5652009-07-18 01:11:10 +0200127 exit(slingshot(argc, argv, envp));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128}