-
-
Notifications
You must be signed in to change notification settings - Fork 621
/
init.S
158 lines (152 loc) · 5.36 KB
/
init.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
│ vi: set noet ft=asm ts=8 sw=8 fenc=utf-8 :vi │
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/macros.h"
#include "libc/sysv/consts/prot.h"
#include "libc/dce.h"
// Decentralized function for process initialization.
//
// Modules may inject cheap data structure initialization code into
// this function using the .init.start and .init.end macros. That
// code can use the LODS and STOS instructions to initialize memory
// that's restricted to read-only after initialization by PIRO.
//
// This is fast, since the linker is able to roll-up initialization
// for large codebases comprised of many modules, into a perfectly
// linear order. It also enables a common pattern we use, which we
// call “Referencing Is Initialization” (RII).
//
// C/C++ code should favor using ordinary constructors, since under
// normal circumstances the compiler will clobber RDI and RSI which
// are granted special meanings within this function.
//
// @param r12 is argc (still callee saved)
// @param r13 is argv (still callee saved)
// @param r14 is envp (still callee saved)
// @param r15 is envp (still callee saved)
// @note rdi is __init_bss_start (callee monotonic lockstep)
// @note rsi is __init_rodata_start (callee monotonic lockstep)
// @see .init.start & .init.end (libc/macros.h)
// @see ape/ape.lds
.section .initprologue,"ax",@progbits
.type _init,@function
.globl _init
_init:
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
ezlea __init_bss_start,di
ezlea __init_rodata_start,si
#elif defined(__aarch64__)
stp x29,x30,[sp,-16]!
mov x29,sp
#endif
.previous/*
...
decentralized content
...
*/.section .initepilogue,"ax",@progbits
#ifdef __x86_64__
#if IsModeDbg()
_init_check_rdi_rsi:
jmp 2f
1: call abort
2: ezlea __init_bss_end,ax
cmp %rax,%rdi
jne 1b
ezlea __init_rodata_end,ax
cmp %rax,%rsi
jne 1b
3: .endfn _init_check_rdi_rsi
#endif
leave
#elif defined(__aarch64__)
ldp x29,x30,[sp],#16
#endif
ret
.previous
#ifdef __x86_64__
// Decentralized section for packed data structures & initializers.
//
// @see .initro (libc/macros.h)
// @see ape/ape.lds
.section .initroprologue,"a",@progbits
.type __init_rodata_start,@object
.type __init_rodata_end,@object
.globl __init_rodata_start,__init_rodata_end
.hidden __init_rodata_start,__init_rodata_end
.balign __SIZEOF_POINTER__
.underrun
__init_rodata_start:
.previous/*
...
decentralized content
...
*/.section .initroepilogue,"a",@progbits
__init_rodata_end:
.byte 0x90
.overrun
.previous
// Decentralized section for unpacked data structures.
//
// Data in this section becomes read-only after initialization.
//
// @see .piro.bss.init (libc/macros.h)
// @see libc/runtime/piro.c
// @see ape/ape.lds
.section .piro.bss.init.1,"aw",@nobits
.type __init_bss_start,@object
.type __init_bss_end,@object
.globl __init_bss_start,__init_bss_end
.hidden __init_bss_start,__init_bss_end
.balign __SIZEOF_POINTER__
.underrun
__init_bss_start:
.previous/*
...
decentralized content
...
*/.section .piro.bss.init.3,"aw",@nobits
__init_bss_end:
.byte 0
.overrun
.previous
// Special area for Windows NT support code.
//
// Isolating this code adds value for Windows users by minimizing
// page faults through improved locality. On System Five the PIRO
// runtime can unmap these pages.
//
// @see libc/runtime/piro.c
// @see ape/ape.lds
.section .textwindowsprologue,"ax",@progbits
.type __text_windows_start,@object
.type __text_windows_end,@object
.globl __text_windows_start,__text_windows_end
.hidden __text_windows_start,__text_windows_end
int3
__text_windows_start:
.previous/*
...
decentralized content
...
*/.section .textwindowsepilogue,"ax",@progbits
__text_windows_end:
int3
.previous
#endif /* __x86_64__ */