shithub: mc

ref: de496a920339b8285ca2e9225b1abff7af54698a
dir: /rt/start-freebsd.s/

View raw version
.data

.globl sys$__cenvp
sys$__cenvp:
.quad 0

.text
/*
 * The entry point for the whole program.
 * This is called by the OS. In order, it:
 *  - Sets up all argc entries as slices
 *  - Sets up all envp entries as slices
 *  - Converts argc/argv to a slice
 *  - Stashes envp in std._environment
 *  - Stashes a raw envp copy in __cenvp (for syscalls to use)
 *  - Calls main()
 */
.globl _start
_start:
	/* stack allocate sizeof(byte[:])*(argc + len(envp)) */
	movq	(%rdi),%rax
	leaq	16(%rdi,%rax,8), %rbx	/* argp = argv + 8*argc + 8 */
	call    count
	addq	%r9,%rax
	imulq	$16,%rax
	subq	%rax,%rsp
	movq	%rsp, %rdx	/* saved args[:] */

	/* convert envp to byte[:][:] for std._environment */
	movq	(%rdi),%rax
	leaq	16(%rdi,%rax,8), %rbx	/* envp = argv + 8*argc + 8 */
	/* store envp for some syscalls to use without spurious conversion. */
	movq    %rbx,sys$__cenvp(%rip)

	/* convert argc, argv to byte[:][:] for args. */
	movq	%rsp, %rcx
	movq	(%rdi), %rax	/* argc */
	leaq	8(%rdi), %rbx	/* argv */
	movq	(%rdi), %rsi	/* saved argc */
	call    cvt
	pushq   %rsi
	pushq   %rdx

	xorq %rbp,%rbp
	/* call pre-main initializers */
	call	__init__

	/* enter the main program */
	call	main
	/* exit(0) */
	xorq	%rdi,%rdi
	movq	$1,%rax
	syscall