feat(start): _start, __libc_start_main, exit/atexit/environ
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* vlibc — x86_64 _start (todo 3).
|
||||
*
|
||||
* The kernel enters the program here with the initial stack holding, in
|
||||
* order from %rsp: argc, argv[0..argc-1], a NULL argv terminator, the
|
||||
* environment strings, a NULL envp terminator, and the auxiliary vector.
|
||||
* The kernel leaves %rsp 16-byte aligned at entry.
|
||||
*
|
||||
* _start forwards (main, argc, argv, envp) to __libc_start_main, matching
|
||||
* vlibc's startup signature (src/start/start.h). The frame pointer is
|
||||
* zeroed first so the outermost frame terminates backtraces, and %rsp is
|
||||
* 16-byte aligned at the call site per the SysV AMD64 ABI. __libc_start_main
|
||||
* never returns (it ends in exit), so the hlt loop below is unreachable; it
|
||||
* exists only to satisfy the assembler and to fault loudly (hlt in user
|
||||
* mode raises #GP) if a broken build ever falls through.
|
||||
*/
|
||||
|
||||
.text
|
||||
.global _start
|
||||
.type _start, @function
|
||||
_start:
|
||||
xor %ebp, %ebp # outermost frame: zero frame pointer
|
||||
mov %rsp, %r8 # keep the raw initial stack for envp
|
||||
pop %rsi # rsi = argc
|
||||
mov %rsp, %rdx # rdx = argv
|
||||
lea 16(%r8, %rsi, 8), %rcx # rcx = envp = initial rsp + 8 + 8*(argc+1)
|
||||
lea main(%rip), %rdi # rdi = &main
|
||||
and $-16, %rsp # ABI: rsp % 16 == 0 at the call site
|
||||
call __libc_start_main # _Noreturn: routes main's result into exit
|
||||
1: hlt
|
||||
jmp 1b
|
||||
.size _start, . - _start
|
||||
|
||||
.section .note.GNU-stack, "", @progbits
|
||||
Reference in New Issue
Block a user