execsp.S revision 1.4
1/*	$NetBSD: execsp.S,v 1.4 2025/05/07 16:26:47 uwe Exp $	*/
2
3/*-
4 * Copyright (c) 2025 The NetBSD Foundation, Inc.
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#define	_LOCORE
30
31#include <machine/asm.h>
32
33RCSID("$NetBSD: execsp.S,v 1.4 2025/05/07 16:26:47 uwe Exp $")
34
35/*
36 * void execsp_start(struct ps_strings *ps_strings@r0, void *obj_main@r1,
37 *     void (*cleanup@r2)(void))
38 *
39 *	ELF entry point.  Saves the stack pointer in startsp and defers
40 *	to the usual csu __start routine.
41 */
42ENTRY(execsp_start)
430:	GOT_INIT(r3, .Lgot.execsp_start)
44	mov	r4, sp
45	GOT_GET(r5, r3, .Lstartsp)
46	str	r4, [r5]
47	b	PLT_SYM(_C_LABEL(__start))
48
49	GOT_INITSYM(.Lgot.execsp_start, 0b)
50.Lstartsp:
51	.word	GOT_SYM(startsp)
52END(execsp_start)
53
54/*
55 * void execsp_ctor(void)
56 *
57 *	ELF constructor.  Saves the stack pointer in ctorsp and
58 *	returns.
59 */
60ENTRY(execsp_ctor)
610:	GOT_INIT(r0, .Lgot.execsp_ctor)
62	mov	r1, sp
63	GOT_GET(r2, r0, .Lctorsp)
64	str	r1, [r2]
65	RET
66
67	GOT_INITSYM(.Lgot.execsp_ctor, 0b)
68.Lctorsp:
69	.word	GOT_SYM(ctorsp)
70END(execsp_ctor)
71
72	/* Make execsp_ctor a constructor. */
73	.section .init_array,"aw",%init_array
74	.p2align 2
75	.word	_C_LABEL(execsp_ctor)
76
77/*
78 * int main(int argc@r0, char **argv@r1, ...)
79 *
80 *	Main function.  Saves the stack pointer in mainsp and returns
81 *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
82 *	been initialized.
83 */
84ENTRY(main)
850:	GOT_INIT(r0, .Lgot.main)
86	mov	r1, sp
87	GOT_GET(r2, r0, .Lmainsp)
88	str	r1, [r2]
89	mov	r0, #0
90	RET
91
92	GOT_INITSYM(.Lgot.main, 0b)
93.Lmainsp:
94	.word	GOT_SYM(mainsp)
95END(main)
96
97/*
98 * void execsp_dtor(void)
99 *
100 *	ELF destructor.  Saves the stack pointer in dtorsp and defers
101 *	to the C execsp_main in h_execsp.c to report the stack pointers
102 *	back to the t_signal_and_sp parent.
103 */
104ENTRY(execsp_dtor)
1050:	GOT_INIT(r0, .Lgot.execsp_dtor)
106	mov	r1, sp
107	GOT_GET(r2, r0, .Ldtorsp)
108	str	r1, [r2]
109	b	PLT_SYM(_C_LABEL(execsp_main))
110
111	GOT_INITSYM(.Lgot.execsp_dtor, 0b)
112.Ldtorsp:
113	.word	GOT_SYM(dtorsp)
114END(execsp_dtor)
115
116	/* Make execsp_ctor a destructor. */
117	.section .fini_array,"aw",%fini_array
118	.p2align 2
119	.word	_C_LABEL(execsp_dtor)
120