|
| 1 | +/* $NetBSD: execsp.S,v 1.1 2025/04/25 02:24:01 riastradh 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 | + |
| 33 | +RCSID("$NetBSD: execsp.S,v 1.1 2025/04/25 02:24:01 riastradh 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 | + */ |
| 42 | +ENTRY(execsp_start) |
| 43 | + GOT_INIT(r0, .Lgot.execsp_start, .Lpc.execsp_start) |
| 44 | + mov r1, sp |
| 45 | + GOT_GET(r2, r0, .Lstartsp) |
| 46 | + str r1, [r2] |
| 47 | + b PLT_SYM(_C_LABEL(__start)) |
| 48 | + |
| 49 | + GOT_INITSYM(.Lgot.execsp_start, .Lpc.execsp_start) |
| 50 | +.Lstartsp: |
| 51 | + .word GOT_SYM(startsp) |
| 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 | + GOT_INIT(r0, .Lgot.execsp_ctor, .Lpc.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, .Lpc.execsp_ctor) |
| 68 | +.Lctorsp: |
| 69 | + .word GOT_SYM(ctorsp) |
| 70 | +END(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 | + */ |
| 84 | +ENTRY(main) |
| 85 | + GOT_INIT(r0, .Lgot.main, .Lpc.main) |
| 86 | + mov r1, sp |
| 87 | + GOT_GET(r2, r0, .Lmainsp) |
| 88 | + str r1, [r2] |
| 89 | + RET |
| 90 | + |
| 91 | + GOT_INITSYM(.Lgot.main, .Lpc.main) |
| 92 | +.Lmainsp: |
| 93 | + .word GOT_SYM(mainsp) |
| 94 | +END(main) |
| 95 | + |
| 96 | +/* |
| 97 | + * void execsp_dtor(void) |
| 98 | + * |
| 99 | + * ELF destructor. Saves the stack pointer in dtorsp and defers |
| 100 | + * to the C execsp_main in h_execsp.c to report the stack pointers |
| 101 | + * back to the t_signal_and_sp parent. |
| 102 | + */ |
| 103 | +ENTRY(execsp_dtor) |
| 104 | + GOT_INIT(r0, .Lgot.execsp_dtor, .Lpc.execsp_dtor) |
| 105 | + mov r1, sp |
| 106 | + GOT_GET(r2, r0, .Ldtorsp) |
| 107 | + str r1, [r2] |
| 108 | + b PLT_SYM(_C_LABEL(execsp_main)) |
| 109 | + |
| 110 | + GOT_INITSYM(.Lgot.execsp_dtor, .Lpc.execsp_dtor) |
| 111 | +.Ldtorsp: |
| 112 | + .word GOT_SYM(dtorsp) |
| 113 | +END(execsp_dtor) |
| 114 | + |
| 115 | + /* Make execsp_ctor a destructor. */ |
| 116 | + .section .fini_array,"aw",%fini_array |
| 117 | + .p2align 2 |
| 118 | + .word _C_LABEL(execsp_dtor) |
0 commit comments