History

tools/nolibc: x86_64: shrink _start with _start_c

move most of the _start operations to _start_c(), include the stackprotector initialization.

Zhangjin Wu committed 3 years ago

Commit 539287d1 parent 2ab44631 file · +6 −23Browse files

Showing the changes to tools/include (1 of 1 file). Show all

Unified · Split

tools/include/nolibc/arch-x86_64.h+6−23
@@ -8,6 +8,7 @@ · 7 unmodified lines
88 #define _NOLIBC_ARCH_X86_64_H
99
1010 #include "compiler.h"
11+#include "crt.h"
1112
1213 /* Syscalls for x86_64 :
1314 * - registers are 64-bit
@@ -153,9 +154,6 @@ · 139 unmodified lines
153154 _ret; \
154155 })
155156
156−char **environ __attribute__((weak));
157−const unsigned long *_auxv __attribute__((weak));
158−
159157 /* startup code */
160158 /*
161159 * x86-64 System V ABI mandates:
@@ -166,26 +164,11 @@ const unsigned long *_auxv __attribute__((weak)); · 4 unmodified lines
166164 void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void)
167165 {
168166 __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 */
189172 );
190173 __builtin_unreachable();
191174 }