1/* $OpenBSD: md_init.h,v 1.18 2023/11/18 16:26:16 deraadt Exp $ */
2
3/*-
4 * Copyright (c) 2001 Ross Harvey
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed by the NetBSD
18 *      Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 *    contributors may be used to endorse or promote products derived
21 *    from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/*
37 * arm overrides these because it uses %progbits instead of @progbits
38 */
39#define MD_DATA_SECTION_FLAGS_SYMBOL(section, flags, type, symbol)	\
40	extern __dso_hidden type symbol[];				\
41	__asm("	.section "section",\""flags"\",%progbits		\n" \
42	"	.balign 4						\n" \
43	#symbol":							\n" \
44	"	.previous")
45#define MD_DATA_SECTION_SYMBOL_VALUE(section, type, symbol, value)	\
46	extern __dso_hidden type symbol[];				\
47	__asm("	.section "section",\"aw\",%progbits			\n" \
48	"	.balign 4						\n" \
49	#symbol":							\n" \
50	"	.int "#value"						\n" \
51	"	.previous")
52#define MD_DATA_SECTION_FLAGS_VALUE(section, flags, value)		\
53	__asm("	.section "section",\""flags"\",%progbits		\n" \
54	"	.balign 4						\n" \
55	"	.int "#value"						\n" \
56	"	.previous")
57
58#define MD_SECT_CALL_FUNC(section, func) \
59	__asm (".section "#section", \"ax\"		\n" \
60	"	movw	r0, #:lower16:" #func "- 1f - 8	\n" \
61	"	movt	r0, #:upper16:" #func "- 1f - 8	\n" \
62	"1:	add	r0, r0, pc			\n" \
63	"	blx	r0				\n" \
64	"	.previous")
65
66#define MD_SECTION_PROLOGUE(sect, entry_pt)	\
67	__asm (					\
68	".section "#sect",\"ax\",%progbits	\n" \
69	"	.globl " #entry_pt "		\n" \
70	"	.type " #entry_pt ",%function	\n" \
71	"	.align 4			\n" \
72	#entry_pt":				\n" \
73	"	push	{r4, lr}		\n" \
74	"	/* fall thru */			\n" \
75	"	.previous")
76
77
78#define MD_SECTION_EPILOGUE(sect)		\
79	__asm (					\
80	".section "#sect",\"ax\",%progbits	\n" \
81	"	pop	{r4, pc}		\n" \
82	"	.previous")
83
84
85/*
86 * The definitions of environ and __progname prevent the creation
87 * of COPY relocations for WEAK symbols.
88 */
89#define	MD_CRT0_START				\
90	char **environ, *__progname;		\
91	__asm(					\
92	".text					\n" \
93	"	.align	0			\n" \
94	"	.globl	_start			\n" \
95	"	.globl	__start			\n" \
96	"_start:				\n" \
97	"__start:				\n" \
98	"	mov	r3, r0	/* cleanup */	\n" \
99	"/* Get argc/argv/envp from stack */	\n" \
100	"	ldr	r0, [sp, #0]		\n" \
101	"	add	r1, sp, #4		\n" \
102	"	add	r2, r1, r0, lsl #2	\n" \
103	"	add	r2, r2, #4		\n" \
104	"					\n" \
105	"/*					\n" \
106	" * Ensure the stack is properly	\n" \
107	" * aligned before calling C code.	\n" \
108	" */					\n" \
109	"	bic	sp, sp, #7" /*__STRING(STACKALIGNBYTES)*/ "	\n" \
110	"	b	___start		\n" \
111	".previous");
112
113#define	MD_RCRT0_START				\
114	char **environ, *__progname;		\
115	__asm(					\
116	".text					\n" \
117	"	.align	0			\n" \
118	"	.globl	_start			\n" \
119	"	.globl	__start			\n" \
120	"_start:				\n" \
121	"__start:				\n" \
122	"	mov	fp, sp			\n" \
123	"	mov	r0, fp			\n" \
124	"					\n" \
125	"	sub	sp, sp, #4+4+(16*4)	\n" \
126	"	add	r1, sp, #4		\n" \
127	"					\n" \
128	"	ldr	r8, .L_GOT		\n" \
129	"1:	add	r8, pc, r8		\n" \
130	"	ldr	r2, .L__DYNAMIC		\n" \
131	"	add	r2, r2, r8		\n" \
132	"					\n" \
133	"	bl	_dl_boot_bind		\n" \
134	"					\n" \
135	"	mov	sp, fp			\n" \
136	"	mov	fp, #0			\n" \
137	"					\n" \
138	"	mov	r3, #0	/* cleanup */	\n" \
139	"/* Get argc/argv/envp from stack */	\n" \
140	"	ldr	r0, [sp, #0]		\n" \
141	"	add	r1, sp, #4		\n" \
142	"	add	r2, r1, r0, lsl #2	\n" \
143	"	add	r2, r2, #4		\n" \
144	"					\n" \
145	"/*					\n" \
146	" * Ensure the stack is properly	\n" \
147	" * aligned before calling C code.	\n" \
148	" */					\n" \
149	"	bic	sp, sp, #7" /*__STRING(STACKALIGNBYTES)*/ "	\n" \
150	"	b	___start		\n" \
151	"					\n" \
152	".L_GOT:				\n" \
153	"	.long	_GLOBAL_OFFSET_TABLE_-(1b+8)	\n" \
154	".L__DYNAMIC:				\n" \
155	"	.long	_DYNAMIC(GOTOFF)	\n" \
156	"					\n" \
157	"_csu_abort:				\n" \
158	"	udf	#0			\n" \
159	".previous");
160