-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
236 lines (197 loc) · 8.84 KB
/
main.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
The MIT License
Copyright (c) 2019 Geoffrey Daniels. http://gpdaniels.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE
*/
///////////////////////////////////////////////////////////////////////////////
// Functions required by compilers.
///////////////////////////////////////////////////////////////////////////////
#if defined(__clang__)
// The function called when a pure virtual function is called.
extern "C" void __cxa_pure_virtual();
extern "C" void __cxa_pure_virtual() {
}
// The guard variable type.
__extension__ typedef int __guard __attribute__((mode(__DI__)));
// Functions for guarding a function scope static variable construction.
extern "C" int __cxa_guard_acquire(__guard* guard);
extern "C" int __cxa_guard_acquire (__guard* guard) {
return !*reinterpret_cast<char*>(guard);
}
extern "C" void __cxa_guard_release(__guard* guard);
extern "C" void __cxa_guard_release(__guard* guard) {
*reinterpret_cast<char*>(guard) = 1;
}
extern "C" void __cxa_guard_abort (__guard* guard);
extern "C" void __cxa_guard_abort (__guard* guard) {
static_cast<void>(guard);
}
#elif (defined(__GNUC__) || defined(__GNUG__)) && !(defined(__clang__) || defined(__INTEL_COMPILER))
// The function called when a pure virtual function is called.
extern "C" void __cxa_pure_virtual();
extern "C" void __cxa_pure_virtual() {
}
// The guard variable type.
__extension__ typedef int __guard __attribute__((mode(__DI__)));
// Functions for guarding a function scope static variable construction.
extern "C" int __cxa_guard_acquire(__guard* guard);
extern "C" int __cxa_guard_acquire (__guard* guard) {
return !*reinterpret_cast<char*>(guard);
}
extern "C" void __cxa_guard_release(__guard* guard);
extern "C" void __cxa_guard_release(__guard* guard) {
*reinterpret_cast<char*>(guard) = 1;
}
extern "C" void __cxa_guard_abort (__guard* guard);
extern "C" void __cxa_guard_abort (__guard* guard) {
static_cast<void>(guard);
}
#else
#error "No support for this compiler."
#endif
///////////////////////////////////////////////////////////////////////////////
// Memory allocation function stubs.
///////////////////////////////////////////////////////////////////////////////
void* operator new(__SIZE_TYPE__ size);
void* operator new(__SIZE_TYPE__ size) {
static_cast<void>(size);
asm volatile("syscall" : : "a" (60), "D" (-1u));
return reinterpret_cast<void*>(-1ull);
}
void *operator new[](__SIZE_TYPE__ size);
void *operator new[](__SIZE_TYPE__ size) {
static_cast<void>(size);
asm volatile("syscall" : : "a" (60), "D" (-1u));
return reinterpret_cast<void*>(-1ull);
}
void operator delete(void* memory_pointer) noexcept;
void operator delete(void* memory_pointer) noexcept {
static_cast<void>(memory_pointer);
asm volatile("syscall" : : "a" (60), "D" (-1u));
}
void operator delete[](void* memory_pointer) noexcept;
void operator delete[](void* memory_pointer) noexcept {
static_cast<void>(memory_pointer);
asm volatile("syscall" : : "a" (60), "D" (-1u));
}
void operator delete(void* memory_pointer, __SIZE_TYPE__ size) noexcept;
void operator delete(void* memory_pointer, __SIZE_TYPE__ size) noexcept {
static_cast<void>(memory_pointer);
static_cast<void>(size);
asm volatile("syscall" : : "a" (60), "D" (-1u));
}
void operator delete[](void* memory_pointer, __SIZE_TYPE__ size) noexcept;
void operator delete[](void* memory_pointer, __SIZE_TYPE__ size) noexcept {
static_cast<void>(memory_pointer);
static_cast<void>(size);
asm volatile("syscall" : : "a" (60), "D" (-1u));
}
///////////////////////////////////////////////////////////////////////////////
// At exit function and associated variables.
///////////////////////////////////////////////////////////////////////////////
using function_pointer_type = void(*)();
constexpr static const __SIZE_TYPE__ atexit_max_functions = 128;
static function_pointer_type atexit_functions[atexit_max_functions];
static __SIZE_TYPE__ atexit_function_count = 0;
extern "C" int atexit(function_pointer_type function);
extern "C" int atexit(function_pointer_type function) {
if (atexit_function_count >= atexit_max_functions) {
return -1;
};
atexit_functions[atexit_function_count++] = function;
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// Entry point and setup functions.
///////////////////////////////////////////////////////////////////////////////
// Entry point.
extern "C" [[noreturn, gnu::naked]] void _start();
extern "C" [[noreturn, gnu::naked]] void _start() {
// This code assumes that there is no frame pointer.
// If compiled with frame pointers uncomment the below line.
// asm volatile("pop %rbp");
// Copy the stack pointer to the frame pointer.
asm volatile("mov %rsp, %rbp");
// Read x64 ABI variables for argc, argv and envp.
// argc = RDI = [ RSP ]
// argv = RSI = [ RSP+8 ]
// envp = RDX = [ argv + (8 * argc) + 8 ]
asm volatile("movq (0)(%rbp), %rdi");
asm volatile("leaq (8)(%rbp), %rsi");
asm volatile("leaq (8)(%rsi, %rdi, 8), %rdx");
// Ensure the stack is aligned.
asm volatile("andq $-15, %rsp");
// Call the setup function that calls static constructors, main, and static destructors.
asm volatile("call _setup");
}
// Forward declaration of the main function.
int main(int argument_count, char* arguments[], char* environment[]);
// Setup function.
extern "C" [[noreturn]] void _setup(int argument_count, char* arguments[], char* environment[]);
extern "C" [[noreturn]] void _setup(int argument_count, char* arguments[], char* environment[]) {
// Preinit functions defined by the linker script.
extern function_pointer_type __preinit_array_start;
extern function_pointer_type __preinit_array_end;
for (function_pointer_type* function = &__preinit_array_start; function < &__preinit_array_end; ++function) {
(*function)();
}
// Init functions defined by the linker script.
extern function_pointer_type __init_array_start;
extern function_pointer_type __init_array_end;
for (function_pointer_type* function = &__init_array_start; function < &__init_array_end; ++function) {
(*function)();
}
// Run main.
int return_code = main(argument_count, arguments, environment);
// Call atexit functions.
while (atexit_function_count--) {
atexit_functions[atexit_function_count]();
}
// Destructors defined by the linker script.
extern function_pointer_type __fini_array_start;
extern function_pointer_type __fini_array_end;
for (function_pointer_type* function = &__fini_array_start; function < &__fini_array_end; ++function) {
(*function)();
}
// Exit syscall.
asm volatile("syscall" : : "a" (60), "D" (return_code));
// Never return.
for(;;);
}
////////////////////////////////////////////////////////////////////////////////
// Syscall for writing output to a file handle.
////////////////////////////////////////////////////////////////////////////////
static inline int sys_write(int file_handle, const void* buffer, __SIZE_TYPE__ size) {
int characters_written;
asm volatile("syscall\n"
: "=a" (characters_written)
: "a" (1), "D" (file_handle), "S" (buffer), "d" (size)
: "rcx", "r11", "memory"
);
return characters_written;
}
////////////////////////////////////////////////////////////////////////////////
// Main function.
////////////////////////////////////////////////////////////////////////////////
int main(int argument_count, char* arguments[], char* environment[]) {
static_cast<void>(argument_count);
static_cast<void>(arguments);
static_cast<void>(environment);
constexpr static const char hello_world[] = "hello world!\n";
sys_write(1, hello_world, sizeof(hello_world));
return 0;
}