xref: /freebsd/lib/csu/i386/crt1_s.S (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1302af795SEd Maste/*-
2302af795SEd Maste * Copyright 2009 Konstantin Belousov.
3302af795SEd Maste * All rights reserved.
4302af795SEd Maste *
5302af795SEd Maste * Redistribution and use in source and binary forms, with or without
6302af795SEd Maste * modification, are permitted provided that the following conditions
7302af795SEd Maste * are met:
8302af795SEd Maste * 1. Redistributions of source code must retain the above copyright
9302af795SEd Maste *    notice, this list of conditions and the following disclaimer.
10302af795SEd Maste *
11302af795SEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12302af795SEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
13302af795SEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
14302af795SEd Maste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
15302af795SEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16302af795SEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
17302af795SEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
18302af795SEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
19302af795SEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
20302af795SEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21302af795SEd Maste */
22302af795SEd Maste
23302af795SEd Maste#include <machine/asm.h>
24302af795SEd Maste	.text
25302af795SEd Maste	.align	4
26302af795SEd Maste	.globl	_start
27302af795SEd Maste	.type	_start, @function
28302af795SEd Maste_start:
29302af795SEd Maste	.cfi_startproc
30*d744a37aSDmitry Chagin	.cfi_undefined %eip
31*d744a37aSDmitry Chagin	popl	%esi		# Pop argc
32*d744a37aSDmitry Chagin	.cfi_def_cfa_offset -4
33*d744a37aSDmitry Chagin	movl	%esp,%edi	# argv starts at stack top
34302af795SEd Maste	xorl	%ebp,%ebp
35302af795SEd Maste	pushl	%ebp
36*d744a37aSDmitry Chagin	.cfi_def_cfa_offset 0
37302af795SEd Maste	movl	%esp,%ebp
38*d744a37aSDmitry Chagin	.cfi_offset %ebp,-4
39302af795SEd Maste	.cfi_def_cfa_register %ebp
40302af795SEd Maste	andl	$0xfffffff0,%esp # align stack
41*d744a37aSDmitry Chagin
42*d744a37aSDmitry Chagin#ifdef GCRT
43*d744a37aSDmitry Chagin	subl	$4,%esp		# Align stack for 7 arguments
44*d744a37aSDmitry Chagin	pushl	$etext
45*d744a37aSDmitry Chagin	pushl	$eprol
46*d744a37aSDmitry Chagineprol:
47*d744a37aSDmitry Chagin#else
48*d744a37aSDmitry Chagin	subl	$12,%esp	# Align stack for 5 arguments
49*d744a37aSDmitry Chagin#endif /* GCRT */
50*d744a37aSDmitry Chagin
51*d744a37aSDmitry Chagin#ifdef PIC
52*d744a37aSDmitry Chagin	calll	1f
53*d744a37aSDmitry Chagin1:	popl	%ebx
54*d744a37aSDmitry Chagin	addl	$_GLOBAL_OFFSET_TABLE_+[.-1b],%ebx
55*d744a37aSDmitry Chagin	leal	main@GOTOFF(%ebx),%eax
56*d744a37aSDmitry Chagin	pushl	%eax
57*d744a37aSDmitry Chagin#else
58*d744a37aSDmitry Chagin	pushl	$main
59*d744a37aSDmitry Chagin#endif /* PIC */
60*d744a37aSDmitry Chagin
61302af795SEd Maste	pushl	%edx		# rtld cleanup
62*d744a37aSDmitry Chagin	/* env = argv + argc + 1 */
63*d744a37aSDmitry Chagin	movl	%edi,%eax	# env = argv
64*d744a37aSDmitry Chagin	movl	%esi,%ecx
65*d744a37aSDmitry Chagin	shll	$2,%ecx		# argc * 4
66*d744a37aSDmitry Chagin	addl	%ecx,%eax	# env += argc
67*d744a37aSDmitry Chagin	addl	$4,%eax		# env += 1
68*d744a37aSDmitry Chagin	pushl	%eax		# env
69*d744a37aSDmitry Chagin	pushl	%edi		# argv
70*d744a37aSDmitry Chagin	pushl	%esi		# argc
71*d744a37aSDmitry Chagin
72*d744a37aSDmitry Chagin#ifdef GCRT
73*d744a37aSDmitry Chagin	/*
74*d744a37aSDmitry Chagin	 * __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext);
75*d744a37aSDmitry Chagin	 */
76*d744a37aSDmitry Chagin	calll	__libc_start1_gcrt
77*d744a37aSDmitry Chagin#else
78*d744a37aSDmitry Chagin	/*
79*d744a37aSDmitry Chagin	 * __libc_start1(argc, argv, env, cleanup, main);
80*d744a37aSDmitry Chagin	 */
81*d744a37aSDmitry Chagin#ifdef PIC
82*d744a37aSDmitry Chagin	calll	__libc_start1@PLT
83*d744a37aSDmitry Chagin#else
84*d744a37aSDmitry Chagin	calll	__libc_start1
85*d744a37aSDmitry Chagin#endif
86*d744a37aSDmitry Chagin#endif /* GCRT */
87302af795SEd Maste	int3
88302af795SEd Maste	.cfi_endproc
89302af795SEd Maste	.size	_start, . - _start
90302af795SEd Maste
91302af795SEd Maste	.section .note.GNU-stack,"",%progbits
92