11.4Suwe/*	$NetBSD: execsp.S,v 1.4 2025/05/07 16:26:47 uwe Exp $	*/
21.1Sriastrad
31.1Sriastrad/*-
41.1Sriastrad * Copyright (c) 2025 The NetBSD Foundation, Inc.
51.1Sriastrad * All rights reserved.
61.1Sriastrad *
71.1Sriastrad * Redistribution and use in source and binary forms, with or without
81.1Sriastrad * modification, are permitted provided that the following conditions
91.1Sriastrad * are met:
101.1Sriastrad * 1. Redistributions of source code must retain the above copyright
111.1Sriastrad *    notice, this list of conditions and the following disclaimer.
121.1Sriastrad * 2. Redistributions in binary form must reproduce the above copyright
131.1Sriastrad *    notice, this list of conditions and the following disclaimer in the
141.1Sriastrad *    documentation and/or other materials provided with the distribution.
151.1Sriastrad *
161.1Sriastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Sriastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Sriastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Sriastrad * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Sriastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Sriastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Sriastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Sriastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Sriastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Sriastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Sriastrad * POSSIBILITY OF SUCH DAMAGE.
271.1Sriastrad */
281.1Sriastrad
291.1Sriastrad#define	_LOCORE
301.1Sriastrad
311.1Sriastrad#include <machine/asm.h>
321.1Sriastrad
331.4SuweRCSID("$NetBSD: execsp.S,v 1.4 2025/05/07 16:26:47 uwe Exp $")
341.1Sriastrad
351.1Sriastrad/*
361.1Sriastrad * void execsp_start(struct ps_strings *ps_strings@r0, void *obj_main@r1,
371.1Sriastrad *     void (*cleanup@r2)(void))
381.1Sriastrad *
391.1Sriastrad *	ELF entry point.  Saves the stack pointer in startsp and defers
401.1Sriastrad *	to the usual csu __start routine.
411.1Sriastrad */
421.1SriastradENTRY(execsp_start)
431.4Suwe0:	GOT_INIT(r3, .Lgot.execsp_start)
441.2Sriastrad	mov	r4, sp
451.2Sriastrad	GOT_GET(r5, r3, .Lstartsp)
461.2Sriastrad	str	r4, [r5]
471.1Sriastrad	b	PLT_SYM(_C_LABEL(__start))
481.1Sriastrad
491.4Suwe	GOT_INITSYM(.Lgot.execsp_start, 0b)
501.1Sriastrad.Lstartsp:
511.1Sriastrad	.word	GOT_SYM(startsp)
521.1SriastradEND(execsp_start)
531.1Sriastrad
541.1Sriastrad/*
551.1Sriastrad * void execsp_ctor(void)
561.1Sriastrad *
571.1Sriastrad *	ELF constructor.  Saves the stack pointer in ctorsp and
581.1Sriastrad *	returns.
591.1Sriastrad */
601.1SriastradENTRY(execsp_ctor)
611.4Suwe0:	GOT_INIT(r0, .Lgot.execsp_ctor)
621.1Sriastrad	mov	r1, sp
631.1Sriastrad	GOT_GET(r2, r0, .Lctorsp)
641.1Sriastrad	str	r1, [r2]
651.1Sriastrad	RET
661.1Sriastrad
671.4Suwe	GOT_INITSYM(.Lgot.execsp_ctor, 0b)
681.1Sriastrad.Lctorsp:
691.1Sriastrad	.word	GOT_SYM(ctorsp)
701.1SriastradEND(execsp_ctor)
711.1Sriastrad
721.1Sriastrad	/* Make execsp_ctor a constructor. */
731.1Sriastrad	.section .init_array,"aw",%init_array
741.1Sriastrad	.p2align 2
751.1Sriastrad	.word	_C_LABEL(execsp_ctor)
761.1Sriastrad
771.1Sriastrad/*
781.1Sriastrad * int main(int argc@r0, char **argv@r1, ...)
791.1Sriastrad *
801.1Sriastrad *	Main function.  Saves the stack pointer in mainsp and returns
811.1Sriastrad *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
821.1Sriastrad *	been initialized.
831.1Sriastrad */
841.1SriastradENTRY(main)
851.4Suwe0:	GOT_INIT(r0, .Lgot.main)
861.1Sriastrad	mov	r1, sp
871.1Sriastrad	GOT_GET(r2, r0, .Lmainsp)
881.1Sriastrad	str	r1, [r2]
891.3Smartin	mov	r0, #0
901.1Sriastrad	RET
911.1Sriastrad
921.4Suwe	GOT_INITSYM(.Lgot.main, 0b)
931.1Sriastrad.Lmainsp:
941.1Sriastrad	.word	GOT_SYM(mainsp)
951.1SriastradEND(main)
961.1Sriastrad
971.1Sriastrad/*
981.1Sriastrad * void execsp_dtor(void)
991.1Sriastrad *
1001.1Sriastrad *	ELF destructor.  Saves the stack pointer in dtorsp and defers
1011.1Sriastrad *	to the C execsp_main in h_execsp.c to report the stack pointers
1021.1Sriastrad *	back to the t_signal_and_sp parent.
1031.1Sriastrad */
1041.1SriastradENTRY(execsp_dtor)
1051.4Suwe0:	GOT_INIT(r0, .Lgot.execsp_dtor)
1061.1Sriastrad	mov	r1, sp
1071.1Sriastrad	GOT_GET(r2, r0, .Ldtorsp)
1081.1Sriastrad	str	r1, [r2]
1091.1Sriastrad	b	PLT_SYM(_C_LABEL(execsp_main))
1101.1Sriastrad
1111.4Suwe	GOT_INITSYM(.Lgot.execsp_dtor, 0b)
1121.1Sriastrad.Ldtorsp:
1131.1Sriastrad	.word	GOT_SYM(dtorsp)
1141.1SriastradEND(execsp_dtor)
1151.1Sriastrad
1161.1Sriastrad	/* Make execsp_ctor a destructor. */
1171.1Sriastrad	.section .fini_array,"aw",%fini_array
1181.1Sriastrad	.p2align 2
1191.1Sriastrad	.word	_C_LABEL(execsp_dtor)
120