/* $Id$ */
/* Copyright (c) 2007-2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS System libc */
/* All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */



_start:
	/* reset the stack */
	xor	%rbp, %rbp

	/* setup the environment */
	mov	(%rsp), %rdi	/* argc */
	mov	%rsp, %rsi	/* argv */
	add	$0x8, %rsi
	mov	%rdi, %rdx	/* envp */
	inc	%rdx
	shl	$3, %rdx
	add	%rsi, %rdx
#ifdef __PIC__			/* environ */
	mov	environ@GOTPCREL(%rip), %rcx
	mov	%rdx, (%rcx)
#else
	mov	%rdx, environ
#endif
	push	%rdi
	push	%rsi
	push	%rdx
#ifdef __ELF__
	mov	%rdx, %rcx	/* auxv */
auxv:
	cmpq	$0x0, (%rcx)
	jz	auxv_done
	add	$0x8, %rcx
	jmp	auxv
auxv_done:
	add	$0x8, %rcx
	push	%rcx
#endif

#if defined(__SSP__)
	/* initialize SSP */
# ifdef __PIC__
	call	__stack_chk_setup@PLT
# else
	call	__stack_chk_setup
# endif
#endif

#ifdef __ELF__
	/* initialize dlfcn */
	pop	%rdi
# ifdef __PIC__
	call	__start_dlfcn@PLT
# else
	call	__start_dlfcn
# endif
#endif

	/* initialize stdio */
# ifdef __PIC__
	call	__start_stdio@PLT
# else
	call	__start_stdio
# endif

	/* call the global constructors */
#ifdef __PIC__
	call	_init@PLT
#else
	call	_init
#endif

	/* run the program */
	pop	%rdx
	pop	%rsi
	pop	%rdi
#ifdef __PIC__
	call	main@PLT
#else
	call	main
#endif
	push	%rax

	/* call the global destructors */
#ifdef __PIC__
	call	_fini@PLT
#else
	call	_fini
#endif

	/* exit the program */
	pop	%rdi
#ifdef __PIC__
	call	exit@PLT
#else
	call	exit
#endif

	hlt
