xref: /openbsd-src/lib/csu/i386/md_init.h (revision 1e43641e997f17b3e752e4bc940b17070ff580a3)
1*1e43641eSderaadt /* $OpenBSD: md_init.h,v 1.13 2023/11/18 16:26:16 deraadt Exp $ */
28d0ba7c9Sdrahn 
38d0ba7c9Sdrahn /*-
48d0ba7c9Sdrahn  * Copyright (c) 2001 Ross Harvey
58d0ba7c9Sdrahn  * All rights reserved.
68d0ba7c9Sdrahn  *
78d0ba7c9Sdrahn  * Redistribution and use in source and binary forms, with or without
88d0ba7c9Sdrahn  * modification, are permitted provided that the following conditions
98d0ba7c9Sdrahn  * are met:
108d0ba7c9Sdrahn  * 1. Redistributions of source code must retain the above copyright
118d0ba7c9Sdrahn  *    notice, this list of conditions and the following disclaimer.
128d0ba7c9Sdrahn  * 2. Redistributions in binary form must reproduce the above copyright
138d0ba7c9Sdrahn  *    notice, this list of conditions and the following disclaimer in the
148d0ba7c9Sdrahn  *    documentation and/or other materials provided with the distribution.
158d0ba7c9Sdrahn  * 3. All advertising materials mentioning features or use of this software
168d0ba7c9Sdrahn  *    must display the following acknowledgement:
178d0ba7c9Sdrahn  *      This product includes software developed by the NetBSD
188d0ba7c9Sdrahn  *      Foundation, Inc. and its contributors.
198d0ba7c9Sdrahn  * 4. Neither the name of The NetBSD Foundation nor the names of its
208d0ba7c9Sdrahn  *    contributors may be used to endorse or promote products derived
218d0ba7c9Sdrahn  *    from this software without specific prior written permission.
228d0ba7c9Sdrahn  *
238d0ba7c9Sdrahn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
248d0ba7c9Sdrahn  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
258d0ba7c9Sdrahn  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
268d0ba7c9Sdrahn  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
278d0ba7c9Sdrahn  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
288d0ba7c9Sdrahn  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
298d0ba7c9Sdrahn  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
308d0ba7c9Sdrahn  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
318d0ba7c9Sdrahn  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
328d0ba7c9Sdrahn  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
338d0ba7c9Sdrahn  * POSSIBILITY OF SUCH DAMAGE.
348d0ba7c9Sdrahn  */
358d0ba7c9Sdrahn 
368d0ba7c9Sdrahn #define MD_SECT_CALL_FUNC(section, func) \
378d0ba7c9Sdrahn 	__asm (".section "#section", \"ax\"\n"	\
388d0ba7c9Sdrahn 	"	call " #func "\n"		\
398d0ba7c9Sdrahn 	"	.previous")
408d0ba7c9Sdrahn 
418d0ba7c9Sdrahn #define MD_SECTION_PROLOGUE(sect, entry_pt)	\
428d0ba7c9Sdrahn 	__asm (					\
438d0ba7c9Sdrahn 	".section "#sect",\"ax\",@progbits	\n" \
448d0ba7c9Sdrahn 	"	.globl " #entry_pt "		\n" \
458d0ba7c9Sdrahn 	"	.type " #entry_pt ",@function	\n" \
468d0ba7c9Sdrahn 	"	.align 16			\n" \
47ba1a662dSderaadt 	#entry_pt":				\n" \
489c1165c0Skettenis 	"	pushl	%ebp			\n" \
499c1165c0Skettenis 	"	movl	%esp,%ebp		\n" \
509c1165c0Skettenis 	"	andl	$~15,%esp		\n" \
518d0ba7c9Sdrahn 	"	.previous")
528d0ba7c9Sdrahn 
538d0ba7c9Sdrahn 
548d0ba7c9Sdrahn #define MD_SECTION_EPILOGUE(sect)		\
558d0ba7c9Sdrahn 	__asm (					\
568d0ba7c9Sdrahn 	".section "#sect",\"ax\",@progbits	\n" \
579c1165c0Skettenis 	"	leave				\n" \
588d0ba7c9Sdrahn 	"	ret				\n" \
598d0ba7c9Sdrahn 	"	.previous")
60453b49cdSguenther 
61453b49cdSguenther 
62453b49cdSguenther #define	MD_CRT0_START				\
63453b49cdSguenther 	__asm(					\
64453b49cdSguenther 	".text					\n" \
65453b49cdSguenther 	"	.align	4			\n" \
66453b49cdSguenther 	"	.globl	__start			\n" \
67453b49cdSguenther 	"	.globl	_start			\n" \
68453b49cdSguenther 	"_start:				\n" \
69453b49cdSguenther 	"__start:				\n" \
70453b49cdSguenther 	"	movl	%esp,%ebp		\n" \
71453b49cdSguenther 	"	andl	$~15,%esp	# align stack\n" \
72453b49cdSguenther 	"	pushl	%edx		# cleanup\n" \
73453b49cdSguenther 	"	movl	0(%ebp),%eax		\n" \
74453b49cdSguenther 	"	leal	8(%ebp,%eax,4),%ecx	\n" \
75453b49cdSguenther 	"	leal	4(%ebp),%edx		\n" \
76453b49cdSguenther 	"	pushl	%ecx			\n" \
77453b49cdSguenther 	"	pushl	%edx			\n" \
78453b49cdSguenther 	"	pushl	%eax			\n" \
79453b49cdSguenther 	"	xorl	%ebp,%ebp	# mark deepest stack frame\n" \
80453b49cdSguenther 	"	call	___start		\n" \
81453b49cdSguenther 	"	.previous")
82f67a8cacSkurt 
83f67a8cacSkurt #define	MD_RCRT0_START				\
84f67a8cacSkurt 	__asm(					\
85f67a8cacSkurt 	".text					\n" \
86f67a8cacSkurt 	"	.align	4			\n" \
87f67a8cacSkurt 	"	.globl	__start			\n" \
88f67a8cacSkurt 	"	.globl	_start			\n" \
89f67a8cacSkurt 	"_start:				\n" \
90f67a8cacSkurt 	"__start:				\n" \
91f67a8cacSkurt 	"	movl	%esp,%eax	# save SP for dl_boot_bind \n" \
92f67a8cacSkurt 	"	subl	$16*4,%esp 	# allocate dl_data \n" \
93712997c7Sguenther 	"	call	1f		# push &_DYNAMIC... \n" \
94712997c7Sguenther 	"1:	addl	$(_DYNAMIC-1b),(%esp)	# ...for dl_boot_bind \n" \
95f67a8cacSkurt 	"	movl	%esp,%ebx		\n" \
96f67a8cacSkurt 	"	pushl	%ebx		# push dl_data for dl_boot_bind \n" \
97f67a8cacSkurt 	"					\n" \
98f67a8cacSkurt 	"	mov	%eax, %esi	# save stack for ___start \n" \
99f67a8cacSkurt 	"	pushl	%eax		# push saved SP for dl_boot_bind \n" \
100f67a8cacSkurt 	"					\n" \
101f67a8cacSkurt 	"	call	_dl_boot_bind@PLT # _dl_boot_bind(sp,dl_data,0) \n" \
102f67a8cacSkurt 	"	addl	$3*4,%esp	# pop args \n" \
103f67a8cacSkurt 	"					\n" \
104f67a8cacSkurt 	"	movl	%esi,%ebp		\n" \
105f67a8cacSkurt 	"	andl	$~15,%esp	# align stack\n" \
106f67a8cacSkurt 	"	pushl	$0		# cleanup\n" \
107f67a8cacSkurt 	"	movl	0(%ebp),%eax	# argc	\n" \
108f67a8cacSkurt 	"	leal	8(%ebp,%eax,4),%ecx #envp \n" \
109f67a8cacSkurt 	"	leal	4(%ebp),%edx	# argv	\n" \
110f67a8cacSkurt 	"	pushl	%ecx			\n" \
111f67a8cacSkurt 	"	pushl	%edx			\n" \
112f67a8cacSkurt 	"	pushl	%eax			\n" \
113f67a8cacSkurt 	"	xorl	%ebp,%ebp	# mark deepest stack frame\n" \
114f67a8cacSkurt 	"	call	___start	# ___start(argc,argv,envp,0) \n" \
115f67a8cacSkurt 	"					\n" \
116f67a8cacSkurt 	"	.align	4			\n" \
117*1e43641eSderaadt 	"	.globl	_csu_abort		\n" \
118*1e43641eSderaadt 	"	.type	_csu_abort,@function	\n" \
119*1e43641eSderaadt 	"_csu_abort:				\n" \
1202bc3a8c0Sderaadt 	"	int3				\n" \
121f67a8cacSkurt 	"	.previous")
122