/* Startup code for CR16 */

#include <features.h>

#if !(defined L_Scrt1 && defined __UCLIBC_FORMAT_SHARED_FLAT__)
	.text
	.align	4
	.global	_start
	.type	_start,@function
	.weak	__init
	.weak	__fini
	.global	___uClibc_main
	.type	___uClibc_main,@function

/* Stick in a dummy reference to main(), so that if an application
 * is linking when the main() function is in a static library (.a)
 * we can be sure that main() actually gets linked in
 */

	.type	_main,@function

_start:
#if defined (__UCLIBC_FORMAT_SHARED_FLAT__)
	movd	$__current_shared_library_r12_offset_, (r1,r0)
	cmpd	$0, (r1,r0)
	beq	.Lgo_on
	loadd	[r12]0(r1,r0), (r12)

	loadd	___shared_flat_add_library@cGOT(r12), (r1,r0)
	jal	(r1,r0)
.Lgo_on:
#endif

	/*
	 * On the stack we have argc. We can calculate argv/envp
	 * from that and the succeeding stack location, but fix so
	 * we get the right calling convention regs in (r3,r2), (r5,r4).
	 *
	 * Please view linux/fs/binfmt_elf.c for a complete
	 * understanding of this.
	 */

	pop	$4, r2

	/* __uClibc_main( main, argc, argv, _init, _fini, 0, 0 ) ; */
	movd	$0, (r1,r0)
	push	$2, r0

/*
	push	$2, r0
*/
#if defined( __UCLIBC_FORMAT_SHARED_FLAT__ )
	loadd	___shared_flat_fini@cGOT(r12), (r1,r0)
#elif defined( __PIC__ )
	loadd	__fini@cGOT(r12), (r1,r0)
#else
	movd	$__fini@c, (r1,r0)
#endif
	push	$2, r0


#if defined( __UCLIBC_FORMAT_SHARED_FLAT__ )
	loadd   ___shared_flat_init@cGOT(r12), (r1,r0)
#elif defined( __PIC__ )
	loadd   __init@cGOT(r12), (r1,r0)
#else
	movd	$__init@c, (r1,r0)
#endif
	push	$2, r0

	push	$2, r4
	movd	(r3,r2), (r5,r4)

#if defined( __PIC__ )
	loadd	_main@cGOT(r12), (r3,r2)
	loadd	___uClibc_main@cGOT(r12), (r1,r0)
#else
	movd	$_main@c, (r3,r2)
	movd	$___uClibc_main@c, (r1,r0)
#endif
	jump	(r1,r0)

#if 0
	addd	$0x14, (sp)
	jump	(ra)
#endif

#else

	.text
	.global _lib_main
	.hidden _lib_main
	.type _lib_main,@function
_lib_main:
	/* We know we have a local copy, so we can avoid the GOT.  */
#if 0
	br	___shared_flat_add_library
#else
	bal	(ra), ___shared_flat_add_library

	/* The loader has pushed the return address */
	popret	ra
#endif

#if 1
	.hidden __current_shared_library_r12_offset_
#endif

#endif

