blob: 127896a0550f5018af6c378d8fcd729a6272acd2 [file] [log] [blame]
Pavel Chupin20c4a3a2012-11-28 18:31:14 +04001/*
2 * Copyright (C) 2012 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
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080029#include "../../bionic/libc_init_common.h"
30#include <stddef.h>
Elliott Hughes646e0582013-02-07 12:16:10 -080031#include <stdint.h>
Pavel Chupin20c4a3a2012-11-28 18:31:14 +040032
Yabin Cui744cfd32023-08-24 13:20:23 -070033extern init_func_t* __preinit_array_start[];
34extern init_func_t* __preinit_array_end[];
35extern init_func_t* __init_array_start[];
36extern init_func_t* __init_array_end[];
37extern fini_func_t* __fini_array_start[];
38extern fini_func_t* __fini_array_end[];
39
40#if !defined(CRTBEGIN_STATIC)
41/* This function will be called during normal program termination
42 * to run the destructors that are listed in the .fini_array section
43 * of the executable, if any.
44 *
45 * 'fini_array' points to a list of function addresses.
46 */
47static void call_fini_array() {
48 fini_func_t** array = __fini_array_start;
49 size_t count = __fini_array_end - __fini_array_start;
50 // Call fini functions in reverse order.
51 while (count-- > 0) {
52 fini_func_t* function = array[count];
53 (*function)();
54 }
55}
56
57// libc.so needs fini_array with sentinels. So create a fake fini_array with sentinels.
58// It contains a function to call functions in real fini_array.
59static fini_func_t* fini_array_with_sentinels[] = {
60 (fini_func_t*)-1,
61 &call_fini_array,
62 (fini_func_t*)0,
63};
64#endif // !defined(CRTBEGIN_STATIC)
Pavel Chupin20c4a3a2012-11-28 18:31:14 +040065
Dan Albert2e2c72d2018-01-24 13:19:08 -080066__used static void _start_main(void* raw_args) {
Yabin Cui744cfd32023-08-24 13:20:23 -070067 structors_array_t array = {};
68#if defined(CRTBEGIN_STATIC)
69 array.preinit_array = __preinit_array_start;
70 array.preinit_array_count = __preinit_array_end - __preinit_array_start;
71 array.init_array = __init_array_start;
72 array.init_array_count = __init_array_end - __init_array_start;
73 array.fini_array = __fini_array_start;
74 array.fini_array_count = __fini_array_end - __fini_array_start;
75#else
76 if (__fini_array_end - __fini_array_start > 0) {
77 array.fini_array = fini_array_with_sentinels;
78 }
79#endif // !defined(CRTBEGIN_STATIC)
Pavel Chupin20c4a3a2012-11-28 18:31:14 +040080
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080081 __libc_init(raw_args, NULL, &main, &array);
Pavel Chupin20c4a3a2012-11-28 18:31:14 +040082}
83
Elliott Hughes94072fb2017-05-23 11:03:58 -070084#define PRE ".text; .global _start; .type _start,%function; _start:"
85#define POST "; .size _start, .-_start"
86
87#if defined(__aarch64__)
Chris Wailes559f2782021-05-03 14:27:12 -070088__asm__(PRE "bti j; mov x29,#0; mov x30,#0; mov x0,sp; b _start_main" POST);
Elliott Hughes94072fb2017-05-23 11:03:58 -070089#elif defined(__arm__)
Chris Wailes559f2782021-05-03 14:27:12 -070090__asm__(PRE "mov fp,#0; mov lr,#0; mov r0,sp; b _start_main" POST);
Elliott Hughes94072fb2017-05-23 11:03:58 -070091#elif defined(__i386__)
Chris Wailes559f2782021-05-03 14:27:12 -070092__asm__(PRE
93 "xorl %ebp,%ebp; movl %esp,%eax; andl $~0xf,%esp; subl $12,%esp; pushl %eax;"
Elliott Hughesef768602021-05-19 09:14:41 -070094 "call _start_main" POST);
Elliott Hughes7a196242022-10-17 20:06:39 +000095#elif defined(__riscv)
96__asm__(PRE "li fp,0; li ra,0; mv a0,sp; tail _start_main" POST);
Elliott Hughes94072fb2017-05-23 11:03:58 -070097#elif defined(__x86_64__)
Chris Wailes559f2782021-05-03 14:27:12 -070098__asm__(PRE "xorl %ebp, %ebp; movq %rsp,%rdi; andq $~0xf,%rsp; callq _start_main" POST);
Elliott Hughes94072fb2017-05-23 11:03:58 -070099#else
100#error unsupported architecture
101#endif
102
103#undef PRE
104#undef POST
105
Ryan Prichardcc9b1002019-05-21 15:38:20 -0700106// On arm32 and arm64, when targeting Q and up, overalign the TLS segment to
107// (8 * sizeof(void*)), which reserves enough space between the thread pointer
108// and the executable's TLS segment for Bionic's TLS slots. It has the side
109// effect of placing a 0-sized TLS segment into Android executables that don't
110// use TLS, but this should be harmless.
111//
Ryan Prichard85be9722019-05-29 17:09:47 -0700112// To ensure that the .tdata input section isn't deleted (e.g. by
113// --gc-sections), the .text input section (which contains _start) has a
114// relocation to the .tdata input section.
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800115#if __ANDROID_API__ >= 29
Ryan Prichardcc9b1002019-05-21 15:38:20 -0700116#if defined(__arm__)
117asm(" .section .tdata,\"awT\",%progbits\n"
118 " .p2align 5\n"
Ryan Prichardcc9b1002019-05-21 15:38:20 -0700119 " .text\n"
Ryan Prichard85be9722019-05-29 17:09:47 -0700120 " .reloc 0, R_ARM_NONE, .tdata\n");
Ryan Prichardcc9b1002019-05-21 15:38:20 -0700121#elif defined(__aarch64__)
122asm(" .section .tdata,\"awT\",@progbits\n"
123 " .p2align 6\n"
Ryan Prichardcc9b1002019-05-21 15:38:20 -0700124 " .text\n"
Ryan Prichard85be9722019-05-29 17:09:47 -0700125 " .reloc 0, R_AARCH64_NONE, .tdata\n");
Ryan Prichardcc9b1002019-05-21 15:38:20 -0700126#endif
127#endif
128
Pavel Chupin20c4a3a2012-11-28 18:31:14 +0400129#include "__dso_handle.h"
130#include "atexit.h"
Dmitriy Ivanovea295f62014-11-20 20:47:02 -0800131#include "pthread_atfork.h"