move most of the _start operations to _start_c(), include the stackprotector initialization.
Showing the changes to tools/include (1 of 1 file). Show all
Unified · Split
| @@ -8,6 +8,7 @@ · 7 unmodified lines | ||
| 8 | 8 | #define _NOLIBC_ARCH_X86_64_H |
| 9 | 9 | |
| 10 | 10 | #include "compiler.h" |
| 11 | +#include "crt.h" | |
| 11 | 12 | |
| 12 | 13 | /* Syscalls for x86_64 : |
| 13 | 14 | * - registers are 64-bit |
| @@ -153,9 +154,6 @@ · 139 unmodified lines | ||
| 153 | 154 | _ret; \ |
| 154 | 155 | }) |
| 155 | 156 | |
| 156 | −char **environ __attribute__((weak)); | |
| 157 | −const unsigned long *_auxv __attribute__((weak)); | |
| 158 | − | |
| 159 | 157 | /* startup code */ |
| 160 | 158 | /* |
| 161 | 159 | * x86-64 System V ABI mandates: |
| @@ -166,26 +164,11 @@ const unsigned long *_auxv __attribute__((weak)); · 4 unmodified lines | ||
| 166 | 164 | void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void) |
| 167 | 165 | { |
| 168 | 166 | __asm__ volatile ( |
| 169 | −#ifdef _NOLIBC_STACKPROTECTOR | |
| 170 | − "call __stack_chk_init\n" /* initialize stack protector */ | |
| 171 | −#endif | |
| 172 | − "pop %rdi\n" /* argc (first arg, %rdi) */ | |
| 173 | − "mov %rsp, %rsi\n" /* argv[] (second arg, %rsi) */ | |
| 174 | − "lea 8(%rsi,%rdi,8),%rdx\n" /* then a NULL then envp (third arg, %rdx) */ | |
| 175 | − "mov %rdx, environ\n" /* save environ */ | |
| 176 | − "xor %ebp, %ebp\n" /* zero the stack frame */ | |
| 177 | − "mov %rdx, %rax\n" /* search for auxv (follows NULL after last env) */ | |
| 178 | − "0:\n" | |
| 179 | − "add $8, %rax\n" /* search for auxv using rax, it follows the */ | |
| 180 | − "cmp -8(%rax), %rbp\n" /* ... NULL after last env (rbp is zero here) */ | |
| 181 | − "jnz 0b\n" | |
| 182 | − "mov %rax, _auxv\n" /* save it into _auxv */ | |
| 183 | − "and $-16, %rsp\n" /* x86 ABI : esp must be 16-byte aligned before call */ | |
| 184 | − "call main\n" /* main() returns the status code, we'll exit with it. */ | |
| 185 | − "mov %eax, %edi\n" /* retrieve exit code (32 bit) */ | |
| 186 | − "mov $60, %eax\n" /* NR_exit == 60 */ | |
| 187 | − "syscall\n" /* really exit */ | |
| 188 | − "hlt\n" /* ensure it does not return */ | |
| 167 | + "xor %ebp, %ebp\n" /* zero the stack frame */ | |
| 168 | + "mov %rsp, %rdi\n" /* save stack pointer to %rdi, as arg1 of _start_c */ | |
| 169 | + "and $-16, %rsp\n" /* %rsp must be 16-byte aligned before call */ | |
| 170 | + "call _start_c\n" /* transfer to c runtime */ | |
| 171 | + "hlt\n" /* ensure it does not return */ | |
| 189 | 172 | ); |
| 190 | 173 | __builtin_unreachable(); |
| 191 | 174 | } |