1 /* $NetBSD: execsp.S,v 1.1 2026/08/29 13:40:17 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2026 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 33 RCSID("$NetBSD: execsp.S,v 1.1 2026/08/29 13:40:17 riastradh Exp $") 34 35 /* 36 * void execsp_start(void *obj_main@a0, void (*cleanup@a1)(void), 37 * struct ps_strings *ps_strings@a2) 38 * 39 * ELF entry point. Saves the stack pointer in startsp and defers 40 * to the usual csu __start routine. 41 */ 42 ENTRY(execsp_start) 43 movl %sp,%d0 44 #ifdef __PIC__ 45 GOT_SETUP(%a3) 46 movl _C_LABEL(startsp)@GOT:w(%a3),%a3 47 movl %d0,(%a3) 48 #else 49 movl %d0,_C_LABEL(startsp) 50 #endif 51 jbsr PIC_PLT(_C_LABEL(__start)) 52 END(execsp_start) 53 54 /* 55 * void execsp_ctor(void) 56 * 57 * ELF constructor. Saves the stack pointer in ctorsp and 58 * returns. 59 */ 60 ENTRY(execsp_ctor) 61 movl %sp,%d0 62 #ifdef __PIC__ 63 GOT_SETUP(%a0) 64 movl _C_LABEL(ctorsp)@GOT:w(%a0),%a0 65 movl %d0,(%a0) 66 #else 67 movl %d0,_C_LABEL(ctorsp) 68 #endif 69 rts 70 END(execsp_ctor) 71 72 /* Make execsp_ctor a constructor. */ 73 .pushsection .ctors,"aw",@progbits 74 .p2align 2 75 .long execsp_ctor 76 .popsection 77 78 /* 79 * int main(int argc@sp[4,8), char **argv@sp[8,12), ...) 80 * 81 * Main function. Saves the stack pointer in mainsp and returns 82 * zero. We will call execsp_main in execsp_dtor once dtorsp has 83 * been initialized. 84 */ 85 ENTRY(main) 86 movl %sp,%d0 87 #ifdef __PIC__ 88 GOT_SETUP(%a0) 89 movl _C_LABEL(mainsp)@GOT:w(%a0),%a0 90 movl %d0,(%a0) 91 #else 92 movl %d0,_C_LABEL(mainsp) 93 #endif 94 clrl %d0 95 rts 96 END(main) 97 98 /* 99 * void execsp_dtor(void) 100 * 101 * ELF destructor. Saves the stack pointer in dtorsp and defers 102 * to the C execsp_main in h_execsp.c to report the stack pointers 103 * back to the t_signal_and_sp parent. 104 */ 105 ENTRY(execsp_dtor) 106 movl %sp,%d0 107 #ifdef __PIC__ 108 GOT_SETUP(%a0) 109 movl _C_LABEL(dtorsp)@GOT:w(%a0),%a0 110 movl %d0,(%a0) 111 #else 112 movl %d0,_C_LABEL(dtorsp) 113 #endif 114 jra PIC_PLT(_C_LABEL(execsp_main)) 115 END(execsp_dtor) 116 117 /* Make execsp_ctor a destructor. */ 118 .pushsection .dtors,"aw",@progbits 119 .p2align 2 120 .long execsp_dtor 121 .popsection 122