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
8#define _NOLIBC_ARCH_X86_64_H8#define _NOLIBC_ARCH_X86_64_H
9 9
10#include "compiler.h"10#include "compiler.h"
11#include "crt.h"
11 12
12/* Syscalls for x86_64 :13/* Syscalls for x86_64 :
13 * - registers are 64-bit14 * - registers are 64-bit
@@ -153,9 +154,6 @@ · 139 unmodified lines
153 _ret; \154 _ret; \
154})155})
155 156
156char **environ __attribute__((weak));
157const unsigned long *_auxv __attribute__((weak));
158
159/* startup code */157/* startup code */
160/*158/*
161 * x86-64 System V ABI mandates:159 * x86-64 System V ABI mandates:
@@ -166,26 +164,11 @@ const unsigned long *_auxv __attribute__((weak)); · 4 unmodified lines
166void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void)164void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void)
167{165{
168 __asm__ volatile (166 __asm__ volatile (
169#ifdef _NOLIBC_STACKPROTECTOR167 "xor %ebp, %ebp\n" /* zero the stack frame */
170 "call __stack_chk_init\n" /* initialize stack protector */168 "mov %rsp, %rdi\n" /* save stack pointer to %rdi, as arg1 of _start_c */
171#endif169 "and $-16, %rsp\n" /* %rsp must be 16-byte aligned before call */
172 "pop %rdi\n" /* argc (first arg, %rdi) */170 "call _start_c\n" /* transfer to c runtime */
173 "mov %rsp, %rsi\n" /* argv[] (second arg, %rsi) */171 "hlt\n" /* ensure it does not return */
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 */
189 );172 );
190 __builtin_unreachable();173 __builtin_unreachable();
191}174}